MLflow 生成的跟踪与 OpenTelemetry 跟踪规范兼容。 因此,可以将 MLflow 跟踪导出到支持 OpenTelemetry 的各种可观测性解决方案。
默认情况下,MLflow 将跟踪导出到 MLflow 跟踪服务器。 若要启用将跟踪导出到 OpenTelemetry 收集器,请在OTEL_EXPORTER_OTLP_ENDPOINT
,将OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
环境变量(或)设置为 OpenTelemetry 收集器的目标 URL。
import mlflow
import os
# Set the endpoint of the OpenTelemetry Collector
os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = "http://localhost:4317/v1/traces"
# Optionally, set the service name to group traces
os.environ["OTEL_SERVICE_NAME"] = "<your-service-name>"
# Trace will be exported to the OTel collector at http://localhost:4317/v1/traces
with mlflow.start_span(name="foo") as span:
span.set_inputs({"a": 1})
span.set_outputs({"b": 2})
警告
MLflow 仅将跟踪导出到单个目标。
OTEL_EXPORTER_OTLP_ENDPOINT
配置环境变量后,MLflow 不会将追踪导出到 Databricks MLflow Tracking,因此您不会在 MLflow UI 中看到追踪记录。
同样,如果将模型部署到 启用了跟踪的 Databricks 模型服务,使用 OpenTelemetry 收集器会导致推理表中不记录这些跟踪信息。
单击以下图标,了解有关如何为特定可观测性平台设置 OpenTelemetry Collector 的详细信息。
平台 | OpenTelemetry 文档 |
---|---|
数据狗 | OpenTelemetry 指南 |
New Relic | OpenTelemetry APM 应用性能监控 |
SigNoz | OpenTelemetry Python Instrumentation |
Splunk(斯普朗克) | 获取数据 |
格拉法纳 | 通过 OTLP 发送数据 |
ServiceNow (Lightstep) | 收集器文档 |
配置
MLflow 使用标准 OTLP 导出程序将跟踪导出到 OpenTelemetry 收集器实例。 因此,可以使用 OpenTelemetry 支持 的所有配置 。 以下示例将 OTLP 导出程序配置为使用 HTTP 协议而不是默认 gRPC 并设置自定义标头:
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://localhost:4317/v1/traces"
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf"
export OTEL_EXPORTER_OTLP_TRACES_HEADERS="api_key=12345"