Files
comet-ml--opik/apps/opik-documentation/documentation/fern/docs/tracing/integrations/mastra.mdx
T
wehub-resource-sync 5a558eb09e
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / render-equality (push) Has been cancelled
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Has been cancelled
Python BE E2E Tests / Python BE E2E (push) Has been cancelled
Python Backend Tests / run-python-backend-tests (push) Has been cancelled
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
SDK E2E Libraries Integration Tests / Check Secrets (push) Has been cancelled
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Has been cancelled
SDK E2E Libraries Integration Tests / build-opik (push) Has been cancelled
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Has been cancelled
TypeScript SDK Build & Publish / build-and-publish (push) Has been cancelled
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Has been cancelled
Backend Tests / discover-tests (push) Has been cancelled
Backend Tests / ${{ matrix.name }} (push) Has been cancelled
Build and Publish SDK / build-and-publish (push) Has been cancelled
Build Opik Docker Images / set-version (push) Has been cancelled
Build Opik Docker Images / build-backend (push) Has been cancelled
Build Opik Docker Images / build-sandbox-executor-python (push) Has been cancelled
Build Opik Docker Images / build-python-backend (push) Has been cancelled
Build Opik Docker Images / build-frontend (push) Has been cancelled
Build Opik Docker Images / create-git-tag (push) Has been cancelled
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Has been cancelled
Docs - Publish / run (push) Has been cancelled
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Has been cancelled
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Has been cancelled
Frontend Unit Tests / Test on Node 20 (push) Has been cancelled
Guardrails E2E Tests / Select Python version matrix (push) Has been cancelled
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Guardrails E2E Tests / 📢 Slack Notification (push) Has been cancelled
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Has been cancelled
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Has been cancelled
Lint Opik Helm Chart / unittest-helm-chart (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:25:44 +08:00

284 lines
9.5 KiB
Plaintext

---
description: Start here to integrate Opik into your Mastra-based genai application
for end-to-end LLM observability, unit testing, and optimization.
headline: Mastra | Opik Documentation
og:description: Build AI applications with Mastra's TypeScript framework. Capture
telemetry, implement workflows, and enhance memory integration seamlessly.
og:site_name: Opik Documentation
og:title: 'Mastra: AI Agent Framework - Opik'
title: Observability for Mastra with Opik
canonical-url: https://www.comet.com/docs/opik/integrations/mastra
---
Mastra is the TypeScript agent framework designed to provide the essential primitives for building AI applications. It enables developers to create AI agents with memory and tool-calling capabilities, implement deterministic LLM workflows, and leverage RAG for knowledge integration.
Mastra's primary advantage is its built-in telemetry support that automatically captures agent interactions, LLM calls, and workflow executions, making it easy to monitor and debug AI applications.
<Frame>
<img src="/img/tracing/mastra_integration.png" alt="Mastra tracing" />
</Frame>
## Getting started
### Create a Mastra project
If you don't have a Mastra project yet, you can create one using the Mastra CLI:
```bash
npx create-mastra
cd your-mastra-project
```
### Install required packages
Install the necessary dependencies for Mastra observability:
```bash
npm install @mastra/observability @mastra/otel
```
### Add environment variables
Create or update your `.env` file with the following variables:
<Tabs>
<Tab value="Opik Cloud" title="Opik Cloud">
```bash wordWrap
# Your LLM API key
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
# Opik configuration
OPIK_API_KEY=<your-opik-api-key>
OPIK_WORKSPACE_NAME=<your-workspace>
OPIK_PROJECT_NAME=<your-project-name>
```
</Tab>
<Tab value="Enterprise deployment" title="Enterprise deployment">
```bash wordWrap
# Your LLM API key
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
# Opik configuration
OPIK_API_KEY=<your-opik-api-key>
OPIK_WORKSPACE_NAME=<your-workspace>
OPIK_PROJECT_NAME=<your-project-name>
```
</Tab>
<Tab value="Self-hosted instance" title="Self-hosted instance">
```bash
# Your LLM API key
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
# Opik configuration
OPIK_PROJECT_NAME=<your-project-name>
```
</Tab>
</Tabs>
## Set up an agent
Create an agent in your project. For example, create a file `src/mastra/index.ts`:
<Tabs>
<Tab value="Opik Cloud" title="Opik Cloud">
```typescript
import { Mastra } from "@mastra/core/mastra";
import { Observability } from "@mastra/observability";
import { OtelExporter } from "@mastra/otel";
import { PinoLogger } from "@mastra/loggers";
import { LibSQLStore } from "@mastra/libsql";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
const OPIK_API_KEY = process.env.OPIK_API_KEY!;
const OPIK_WORKSPACE_NAME = process.env.OPIK_WORKSPACE_NAME!;
const OPIK_PROJECT_NAME = process.env.OPIK_PROJECT_NAME!;
export const chefAgent = new Agent({
name: "chef-agent",
instructions:
"You are Michel, a practical and experienced home chef " +
"You help people cook with whatever ingredients they have available.",
model: openai("gpt-4o-mini"),
});
export const mastra = new Mastra({
agents: { chefAgent },
storage: new LibSQLStore({
url: ":memory:",
}),
logger: new PinoLogger({
name: "Mastra",
level: "info",
}),
observability: new Observability({
configs: {
default: {
serviceName: "chef-agent",
exporters: [
new OtelExporter({
provider: {
custom: {
endpoint: "https://www.comet.com/opik/api/v1/private/otel/v1/traces",
protocol: "http/json",
headers: {
Authorization: OPIK_API_KEY,
"Comet-Workspace": OPIK_WORKSPACE_NAME,
projectName: OPIK_PROJECT_NAME,
},
},
},
}),
],
},
},
}),
});
```
</Tab>
<Tab value="Enterprise deployment" title="Enterprise deployment">
```typescript
import { Mastra } from "@mastra/core/mastra";
import { Observability } from "@mastra/observability";
import { OtelExporter } from "@mastra/otel";
import { PinoLogger } from "@mastra/loggers";
import { LibSQLStore } from "@mastra/libsql";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
const OPIK_API_KEY = process.env.OPIK_API_KEY!;
const OPIK_WORKSPACE_NAME = process.env.OPIK_WORKSPACE_NAME!;
const OPIK_PROJECT_NAME = process.env.OPIK_PROJECT_NAME!;
export const chefAgent = new Agent({
name: "chef-agent",
instructions:
"You are Michel, a practical and experienced home chef " +
"You help people cook with whatever ingredients they have available.",
model: openai("gpt-4o-mini"),
});
export const mastra = new Mastra({
agents: { chefAgent },
storage: new LibSQLStore({
url: ":memory:",
}),
logger: new PinoLogger({
name: "Mastra",
level: "info",
}),
observability: new Observability({
configs: {
default: {
serviceName: "chef-agent",
exporters: [
new OtelExporter({
provider: {
custom: {
endpoint: "https://<comet-deployment-url>/opik/api/v1/private/otel/v1/traces",
protocol: "http/json",
headers: {
Authorization: OPIK_API_KEY,
"Comet-Workspace": OPIK_WORKSPACE_NAME,
projectName: OPIK_PROJECT_NAME,
},
},
},
}),
],
},
},
}),
});
```
</Tab>
<Tab value="Self-hosted instance" title="Self-hosted instance">
```typescript
import { Mastra } from "@mastra/core/mastra";
import { Observability } from "@mastra/observability";
import { OtelExporter } from "@mastra/otel";
import { PinoLogger } from "@mastra/loggers";
import { LibSQLStore } from "@mastra/libsql";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
const OPIK_PROJECT_NAME = process.env.OPIK_PROJECT_NAME!;
export const chefAgent = new Agent({
name: "chef-agent",
instructions:
"You are Michel, a practical and experienced home chef " +
"You help people cook with whatever ingredients they have available.",
model: openai("gpt-4o-mini"),
});
export const mastra = new Mastra({
agents: { chefAgent },
storage: new LibSQLStore({
url: ":memory:",
}),
logger: new PinoLogger({
name: "Mastra",
level: "info",
}),
observability: new Observability({
configs: {
default: {
serviceName: "chef-agent",
exporters: [
new OtelExporter({
provider: {
custom: {
endpoint: "http://localhost:5173/api/v1/private/otel/v1/traces",
protocol: "http/json",
headers: {
projectName: OPIK_PROJECT_NAME,
},
},
},
}),
],
},
},
}),
});
```
</Tab>
</Tabs>
## Run Mastra development server
Start the Mastra development server:
```bash
npm run dev
```
Head over to the developer playground with the provided URL and start chatting with your agent.
## What gets traced
With this setup, your Mastra application will automatically trace:
- **Agent interactions**: Complete conversation flows with agents
- **LLM calls**: Model requests, responses, and token usage
- **Tool executions**: Function calls and their results
- **Workflow steps**: Individual steps in complex workflows
- **Memory operations**: Context and memory updates
## Validation
1. Run the Mastra dev server and execute one agent chat.
2. Confirm OTLP export requests are sent to your configured endpoint.
3. Verify the trace in Opik under the expected workspace/project.
## Source references
- [Mastra docs](https://mastra.ai/)
- [Mastra telemetry docs](https://mastra.ai/en/docs/observability/overview)
- [Opik OpenTelemetry overview](/v1/integrations/opentelemetry)
## Further improvements
If you have any questions or suggestions for improving the Mastra integration, please [open an issue](https://github.com/comet-ml/opik/issues/new/choose) on our GitHub repository.