docs/docs/genai/tracing/integrations/listing/mastra.mdx
import ImageBox from "@site/src/components/ImageBox";
MLflow Tracing provides automatic tracing capability for Mastra, a flexible and modular AI agents framework developed by Mastra. MLflow supports tracing for Mastra through the OpenTelemetry integration.
npm create mastra@latest
npm install @mastra/otel-exporter
This will create a new TypeScript project with a simple tool calling agent implementation.
Start the MLflow Tracking Server with a SQL-based backend store:
mlflow server --backend-store-uri sqlite:///mlflow.db --port 5000
This example uses SQLite as the backend store. To use other types of SQL databases such as PostgreSQL, MySQL, and MSSQL, change the store URI as described in the backend store documentation. OpenTelemetry ingestion is not supported with file-based backend stores.
Configure the OpenTelemetry tracer in your Mastra agent to export traces to the MLflow Tracking Server endpoint.
Open the src/mastra/index.ts file and add the observability configuration to the Mastra agent instantiation.
import { OtelExporter } from "@mastra/otel-exporter";
export const mastra = new Mastra({
workflows: { weatherWorkflow },
...
// Add the following observability configuration to enable OpenTelemetry tracing.
observability: {
configs: {
otel: {
serviceName: "maestra-app",
exporters: [new OtelExporter({
provider: {
custom: {
// Specify tracking server URI with the `/v1/traces` path.
endpoint: "http://localhost:5000/v1/traces",
// Set the MLflow experiment ID in the header.
headers: { "x-mlflow-experiment-id": "<your-experiment-id>"},
// MLflow support HTTP/Protobuf protocol.
protocol: "http/protobuf"
}
}
})]
}
}
},
});
Start the Mastra agent with the npm run dev command. The playground URL will be displayed in the console.
After chatting with the agent, open the MLflow UI at http://localhost:5000 and navigate to the experiment to see the traces like the screenshot at the top of this page.
Since this integration is built on OpenTelemetry, you can combine the automatically generated traces with the MLflow Tracing SDK (@mlflow/core) to add custom spans, set tags, and update trace metadata within the same trace.
import { init, withSpan } from "@mlflow/core";
// Initialize MLflow SDK - sets up the OTel provider to capture all spans
init({
trackingUri: "http://localhost:5000",
experimentId: "<your-experiment-id>",
});
// Add custom MLflow spans alongside the auto-generated Mastra traces
const result = await withSpan(
{ name: "custom_step", inputs: { query: "test" } },
async (span) => {
// your application logic here
return { result: "success" };
}
);
For detailed instructions and examples, see Combining the OpenTelemetry SDK and the MLflow Tracing SDK.