From 745704a5b7f868c61c71f7a12eb13ef695ab4333 Mon Sep 17 00:00:00 2001 From: Zhengfei Wang <38847871+zhengfeiwang@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:15:11 +0800 Subject: [PATCH] [devkit][bugfix] Add check for `trace.NoOpTracerProvider` to avoid crash (#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. --- src/promptflow-devkit/CHANGELOG.md | 5 +++++ .../promptflow/_sdk/_tracing.py | 4 ++++ .../tests/sdk_cli_test/unittests/test_trace.py | 16 +++++++++++++++- src/promptflow/CHANGELOG.md | 6 ++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/promptflow-devkit/CHANGELOG.md b/src/promptflow-devkit/CHANGELOG.md index a1983e82aef..b23171a1da8 100644 --- a/src/promptflow-devkit/CHANGELOG.md +++ b/src/promptflow-devkit/CHANGELOG.md @@ -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 diff --git a/src/promptflow-devkit/promptflow/_sdk/_tracing.py b/src/promptflow-devkit/promptflow/_sdk/_tracing.py index 5eeff36df49..6c1bb9b8ff3 100644 --- a/src/promptflow-devkit/promptflow/_sdk/_tracing.py +++ b/src/promptflow-devkit/promptflow/_sdk/_tracing.py @@ -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...") diff --git a/src/promptflow-devkit/tests/sdk_cli_test/unittests/test_trace.py b/src/promptflow-devkit/tests/sdk_cli_test/unittests/test_trace.py index a1ac05f487e..d24e8a4c25e 100644 --- a/src/promptflow-devkit/tests/sdk_cli_test/unittests/test_trace.py +++ b/src/promptflow-devkit/tests/sdk_cli_test/unittests/test_trace.py @@ -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 @@ -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 diff --git a/src/promptflow/CHANGELOG.md b/src/promptflow/CHANGELOG.md index ea4ab49ff8e..09a5620d4d7 100644 --- a/src/promptflow/CHANGELOG.md +++ b/src/promptflow/CHANGELOG.md @@ -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