From d98792089184c773cac3993e94c74963581ea2a8 Mon Sep 17 00:00:00 2001 From: Xiaopeng Wang Date: Wed, 8 May 2024 18:36:44 +0800 Subject: [PATCH] pfserving tracing improvemnt (#3142) # Description - support disable built-in trace exporters - force disable local setup exporter logic for serving mode with "OTEL_EXPORTER_OTLP_ENDPOINT" # All Promptflow Contribution checklist: - [x] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **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 - [ ] Title of the pull request is clear and informative. - [ ] 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 - [ ] Pull request includes test coverage for the included changes. --------- Co-authored-by: xiaopwan --- src/promptflow-core/promptflow/core/_serving/app_base.py | 6 +++--- src/promptflow-core/promptflow/core/_serving/constants.py | 2 ++ .../core/_serving/extension/default_extension.py | 2 +- .../promptflow/core/_serving/flow_invoker.py | 1 + .../promptflow/core/_serving/monitor/flow_monitor.py | 7 ++++++- src/promptflow-core/promptflow/executor/flow_executor.py | 5 ++++- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/promptflow-core/promptflow/core/_serving/app_base.py b/src/promptflow-core/promptflow/core/_serving/app_base.py index a3dcc386c4a..c7d22c36c1b 100644 --- a/src/promptflow-core/promptflow/core/_serving/app_base.py +++ b/src/promptflow-core/promptflow/core/_serving/app_base.py @@ -28,8 +28,10 @@ def init_app(self, **kwargs): self.logger = logger # default to local, can be override when creating the app self.extension = ExtensionFactory.create_extension(logger, **kwargs) - + # make sure pfserving exporters initiated before any customer code loading + self.flow_monitor = self.extension.get_flow_monitor(self.get_context_data_provider()) self.flow_invoker: AsyncFlowInvoker = None + # parse promptflow project path self.project_path = self.extension.get_flow_project_path() logger.info(f"Project path: {self.project_path}") @@ -48,8 +50,6 @@ def init_app(self, **kwargs): self.connections_override = conn_data_override self.connections_name_override = conn_name_override - self.flow_monitor = self.extension.get_flow_monitor(self.get_context_data_provider()) - self.connection_provider = self.extension.get_connection_provider() self.credential = self.extension.get_credential() self.sample = get_sample_json(self.project_path, logger) diff --git a/src/promptflow-core/promptflow/core/_serving/constants.py b/src/promptflow-core/promptflow/core/_serving/constants.py index 296239f0e77..dc8772424ca 100644 --- a/src/promptflow-core/promptflow/core/_serving/constants.py +++ b/src/promptflow-core/promptflow/core/_serving/constants.py @@ -4,3 +4,5 @@ FEEDBACK_TRACE_FIELD_NAME = "feedback" FEEDBACK_TRACE_SPAN_NAME = "promptflow-feedback" + +PF_BUILTIN_TRACE_EXPORTERS_DISABLE = "PF_BUILTIN_TRACE_EXPORTERS_DISABLE" diff --git a/src/promptflow-core/promptflow/core/_serving/extension/default_extension.py b/src/promptflow-core/promptflow/core/_serving/extension/default_extension.py index f4b66f6f4ec..4d20932d216 100644 --- a/src/promptflow-core/promptflow/core/_serving/extension/default_extension.py +++ b/src/promptflow-core/promptflow/core/_serving/extension/default_extension.py @@ -120,7 +120,7 @@ def _get_common_dimensions_from_env(self): common_dimensions = json.loads(common_dimensions_str) return common_dimensions except Exception as ex: - self.logger.warn(f"Failed to parse common dimensions with value={common_dimensions_str}: {ex}") + self.logger.warning(f"Failed to parse common dimensions with value={common_dimensions_str}: {ex}") return {} def _get_default_blueprints(self, flow_monitor, static_folder=None): diff --git a/src/promptflow-core/promptflow/core/_serving/flow_invoker.py b/src/promptflow-core/promptflow/core/_serving/flow_invoker.py index 727c3ef4cd0..4f24da934f0 100644 --- a/src/promptflow-core/promptflow/core/_serving/flow_invoker.py +++ b/src/promptflow-core/promptflow/core/_serving/flow_invoker.py @@ -181,6 +181,7 @@ def _init_executor(self, flow_path, working_dir): raise_ex=self.raise_ex, storage=storage, init_kwargs=self._init_kwargs, + env_exporter_setup=False, ) self.executor.enable_streaming_for_llm_flow(self.streaming) self.logger.info("Promptflow executor initiated successfully.") diff --git a/src/promptflow-core/promptflow/core/_serving/monitor/flow_monitor.py b/src/promptflow-core/promptflow/core/_serving/monitor/flow_monitor.py index 2817f90033a..a8fb0d5a1f2 100644 --- a/src/promptflow-core/promptflow/core/_serving/monitor/flow_monitor.py +++ b/src/promptflow-core/promptflow/core/_serving/monitor/flow_monitor.py @@ -2,9 +2,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # --------------------------------------------------------- +import os from typing import Dict from promptflow._utils.exception_utils import ErrorResponse +from promptflow.core._serving.constants import PF_BUILTIN_TRACE_EXPORTERS_DISABLE from promptflow.core._serving.monitor.context_data_provider import ContextDataProvider from promptflow.core._serving.monitor.data_collector import FlowDataCollector from promptflow.core._serving.monitor.metrics import MetricsRecorder, ResponseType @@ -48,7 +50,10 @@ def setup_metrics_recorder(self, custom_dimensions, metric_exporters): return None def setup_trace_exporters(self, trace_exporters): - if not trace_exporters: + # This is to support customer customize their own spanprocessor, in that case customer can disable the built-in + # trace exporters by setting the environment variable PF_BUILTIN_TRACE_EXPORTERS_DISABLE to true. + disable_builtin_trace_exporters = os.environ.get(PF_BUILTIN_TRACE_EXPORTERS_DISABLE, "false").lower() == "true" + if not trace_exporters or disable_builtin_trace_exporters: self.logger.warning("No trace exporter enabled.") return try: diff --git a/src/promptflow-core/promptflow/executor/flow_executor.py b/src/promptflow-core/promptflow/executor/flow_executor.py index d5c7fd1d23c..fc969d8c99d 100644 --- a/src/promptflow-core/promptflow/executor/flow_executor.py +++ b/src/promptflow-core/promptflow/executor/flow_executor.py @@ -209,7 +209,10 @@ def create( :return: A new instance of FlowExecutor. :rtype: ~promptflow.executor.flow_executor.FlowExecutor """ - setup_exporter_from_environ() + env_exporter_setup = kwargs.get("env_exporter_setup", True) + if env_exporter_setup: + setup_exporter_from_environ() + if hasattr(flow_file, "__call__") or inspect.isfunction(flow_file): from ._script_executor import ScriptExecutor