Skip to content

Commit

Permalink
[devkit][bugfix] Add check for trace.NoOpTracerProvider to avoid cr…
Browse files Browse the repository at this point in the history
…ash (#3407)

# Description

In some cases (we have observed in runtime scenario, related to mlflow),
tracer provider can be set as `trace.NoOpTracerProvider`, which does not
have method `add_span_processor` and lead to error. This PR adds a check
on that to avoid such crash.

# All Promptflow Contribution checklist:
- [x] **The pull request does not introduce [breaking changes].**
- [x] **CHANGELOG is updated for new features, bug fixes or other
significant changes.**
- [x] **I have read the [contribution guidelines](../CONTRIBUTING.md).**
- [ ] **Create an issue and link to the pull request to get dedicated
review from promptflow team. Learn more: [suggested
workflow](../CONTRIBUTING.md#suggested-workflow).**

## General Guidelines and Best Practices
- [x] Title of the pull request is clear and informative.
- [x] There are a small number of commits, each of which have an
informative message. This means that previously merged commits do not
appear in the history of the PR. For more information on cleaning up the
commits in your PR, [see this
page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).

### Testing Guidelines
- [x] Pull request includes test coverage for the included changes.
  • Loading branch information
zhengfeiwang authored Jun 12, 2024
1 parent 47a5b29 commit 745704a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/promptflow-devkit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# promptflow-devkit package

## v1.13.0 (Upcoming)

### Bugs Fixed
- Fix incompatibility with `trace.NoOpTracerProvider` when set exporter to prompt flow service.

## v1.12.0 (2024.06.11)

### Improvements
Expand Down
4 changes: 4 additions & 0 deletions src/promptflow-devkit/promptflow/_sdk/_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ def setup_exporter_to_pfs() -> None:
tracer_provider: TracerProvider = trace.get_tracer_provider()
if is_exporter_setup_skipped():
_logger.debug("exporter setup is skipped according to environment variable.")
elif isinstance(tracer_provider, trace.NoOpTracerProvider):
_logger.warning(
"tracer provider is set to NoOpTracerProvider, skip setting exporter to prompt flow service."
)
else:
if not getattr(tracer_provider, TRACER_PROVIDER_PFS_EXPORTER_SET_ATTR, False):
_logger.info("have not set exporter to prompt flow service, will set it...")
Expand Down
16 changes: 15 additions & 1 deletion src/promptflow-devkit/tests/sdk_cli_test/unittests/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
TRACE_LIST_DEFAULT_LIMIT,
ContextAttributeKey,
)
from promptflow._sdk._tracing import start_trace_with_devkit
from promptflow._sdk._tracing import setup_exporter_to_pfs, start_trace_with_devkit
from promptflow._sdk._utilities.tracing_utils import WorkspaceKindLocalCache, append_conditions, parse_protobuf_span
from promptflow.client import PFClient
from promptflow.exceptions import UserErrorException
Expand Down Expand Up @@ -196,6 +196,20 @@ def test_pfs_invocation_failed_in_start_trace(self):
start_trace_with_devkit(collection=str(uuid.uuid4()))
assert monitor_func.call_count == 0

@pytest.mark.usefixtures("reset_tracer_provider")
def test_no_op_tracer_provider(self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture):
# logger with "promptflow." prefix cannot be captured by caplog, so patch the logger for this test
with patch("promptflow._sdk._tracing._logger", logging.getLogger(__name__)):
with caplog.at_level(level=logging.WARNING):
monkeypatch.setenv(OTEL_EXPORTER_OTLP_ENDPOINT, "http://dummy-endpoint")
trace.set_tracer_provider(trace.NoOpTracerProvider())
setup_exporter_to_pfs()
monkeypatch.delenv(OTEL_EXPORTER_OTLP_ENDPOINT)
assert (
"tracer provider is set to NoOpTracerProvider, skip setting exporter to prompt flow service."
in caplog.text
)


@pytest.mark.unittest
@pytest.mark.sdk_test
Expand Down
6 changes: 6 additions & 0 deletions src/promptflow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Release History

## v1.13.0 (Upcoming)

### Bugs Fixed
- Fix incompatibility with `trace.NoOpTracerProvider` when set exporter to prompt flow service.

## v1.12.0 (2024.06.11)

### Bugs fixed
Expand Down

0 comments on commit 745704a

Please sign in to comment.