将 OpenTelemetry Traces 收集到 MLflow 中
MLflow 3.6.0 及更高版本支持 OpenTelemetry trace 摄取。
OpenTelemetry 端点 (OTLP)
MLflow Server 在 /v1/traces 处公开了一个 OTLP 端点 (OTLP)。此端点接受来自任何原生 OpenTelemetry instrumentations 的 traces,允许您跟踪用 Java、Go、Rust 等其他语言编写的应用程序。
要使用此端点,请使用基于 SQL 的后端存储启动 MLflow Server。以下命令使用 SQLite 后端存储启动 MLflow Server:
mlflow server --backend-store-uri sqlite:///mlflow.db
要使用 PostgreSQL、MySQL 和 MSSQL 等其他类型的 SQL 数据库,请按后端存储文档中的说明更改存储 URI。
在您的应用程序中,配置服务器端点并在 OTLP 标头 x-mlflow-experiment-id 中设置 MLflow 实验 ID。
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://:5000/v1/traces
export OTEL_EXPORTER_OTLP_TRACES_HEADERS=x-mlflow-experiment-id=123
截至 MLflow 3.6.0,MLflow Server 仅支持 OTLP/HTTP 端点。OTLP/gRPC 端点尚不支持。
基本示例
以下示例展示了如何使用 OpenTelemetry FastAPI instrumentations 从 FastAPI 应用程序收集 traces。
import os
import uvicorn
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
# Set the endpoint and header
MLFLOW_TRACKING_URI = "https://:5000"
MLFLOW_EXPERIMENT_ID = "123"
os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = f"{MLFLOW_TRACKING_URI}/v1/traces"
os.environ[
"OTEL_EXPORTER_OTLP_TRACES_HEADERS"
] = f"x-mlflow-experiment-id={MLFLOW_EXPERIMENT_ID}"
app = FastAPI()
FastAPIInstrumentor.instrument_app(app)
@app.get("/")
async def root():
return {"message": "Hello, World!"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
使用 OpenTelemetry Collector
OpenTelemetry Collector 是一个与供应商无关的代理,可用于收集、处理和导出 traces 到各种可观察性平台。要配置 OpenTelemetry Collector 将 traces 摄取到 MLflow,请使用以下配置:
export MLFLOW_TRACKING_URI=https://:5000
export MLFLOW_EXPERIMENT_ID=123
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
exporters:
otlphttp:
endpoint: ${MLFLOW_TRACKING_URI}
headers:
x-mlflow-experiment-id: ${MLFLOW_EXPERIMENT_ID}
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlphttp]
docker run -d --name opentelemetry-collector \
-p 4317:4317 \
-p 4318:4318 \
-v $(pwd)/opentelemetry-collector.yaml:/etc/otel/collector/config.yaml \
otel/opentelemetry-collector
压缩
自 MLflow 3.7.0 起,MLflow 的 OTLP/HTTP 端点接受压缩的 trace payload。要启用压缩,请将 OTEL_EXPORTER_OTLP_TRACES_COMPRESSION 环境变量设置为 gzip 或 deflate。不支持其他编码。
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://:5000/v1/traces
export OTEL_EXPORTER_OTLP_TRACES_HEADERS=x-mlflow-experiment-id=123
export OTEL_EXPORTER_OTLP_TRACES_COMPRESSION=gzip
当通过 OpenTelemetry Collector 路由时,在 OTLP HTTP exporter 上设置压缩,以便它将压缩请求转发到 MLflow。
exporters:
otlphttp:
endpoint: ${MLFLOW_TRACKING_URI}
headers:
x-mlflow-experiment-id: ${MLFLOW_EXPERIMENT_ID}
compression: gzip