diff --git a/.changeset/mastra-logger-routing.md b/.changeset/mastra-logger-routing.md new file mode 100644 index 000000000..218b8f3b5 --- /dev/null +++ b/.changeset/mastra-logger-routing.md @@ -0,0 +1,5 @@ +--- +"braintrust": minor +--- + +feat(mastra): Route traces by logger diff --git a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.json b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.json index 04e2a6c83..03ae714fd 100644 --- a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.json +++ b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.json @@ -189,7 +189,15 @@ "model": "mock-model-id", "provider": "mock-provider" } - }, + } + ], + "metadata": { + "operation": "generate" + } + }, + { + "name": "mastra-agent-stream-operation", + "children": [ { "name": "agent run: 'weather-agent'", "type": "task", @@ -372,7 +380,22 @@ "model": "mock-model-id", "provider": "mock-provider" } - }, + } + ], + "metadata": { + "operation": "stream" + } + }, + { + "name": "mastra-tool-operation", + "children": [], + "metadata": { + "operation": "tool" + } + }, + { + "name": "mastra-workflow-operation", + "children": [ { "name": "workflow run: 'travel-flow'", "type": "task", @@ -407,27 +430,6 @@ } } ], - "metadata": { - "operation": "generate" - } - }, - { - "name": "mastra-agent-stream-operation", - "children": [], - "metadata": { - "operation": "stream" - } - }, - { - "name": "mastra-tool-operation", - "children": [], - "metadata": { - "operation": "tool" - } - }, - { - "name": "mastra-workflow-operation", - "children": [], "metadata": { "operation": "workflow" } diff --git a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.txt b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.txt index 9ba5dc824..693467aba 100644 --- a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.txt +++ b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1-latest.span-tree.txt @@ -7,346 +7,322 @@ span_tree: │ metadata: { │ "operation": "generate" │ } - │ ├── agent run: 'weather-agent' [task] - │ │ input: "What is the weather in Paris?" - │ │ output: { - │ │ "files": [], - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent", - │ │ "model": "mock-model-id", - │ │ "provider": "mock-provider" - │ │ } - │ │ └── llm: 'mock-model-id' [llm] - │ │ input: { - │ │ "messages": [ - │ │ { - │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ "role": "system" - │ │ }, - │ │ { - │ │ "content": [ - │ │ { - │ │ "providerOptions": { - │ │ "mastra": { - │ │ "createdAt": 0 - │ │ } - │ │ }, - │ │ "text": "What is the weather in Paris?", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ } - │ │ output: { - │ │ "files": [], - │ │ "reasoning": [], - │ │ "sources": [], - │ │ "text": "The forecast is sunny.", - │ │ "warnings": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent", - │ │ "model": "mock-model-id", - │ │ "provider": "mock-provider" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "time_to_first_token": 0, - │ │ "tokens": 30 - │ │ } - │ │ └── step: 0 [llm] - │ │ input: [ - │ │ { - │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ "role": "system" - │ │ }, - │ │ { - │ │ "content": "What is the weather in Paris?", - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: { - │ │ "text": "The forecast is sunny.", - │ │ "toolCalls": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "tokens": 30 - │ │ } - │ │ ├── llm request processor: provider-history-compat [function] - │ │ │ input: { - │ │ │ "prompt": [ - │ │ │ { - │ │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ │ "role": "system" - │ │ │ }, - │ │ │ { - │ │ │ "content": [ - │ │ │ { - │ │ │ "providerOptions": { - │ │ │ "mastra": { - │ │ │ "createdAt": 0 - │ │ │ } - │ │ │ }, - │ │ │ "text": "What is the weather in Paris?", - │ │ │ "type": "text" - │ │ │ } - │ │ │ ], - │ │ │ "role": "user" - │ │ │ } - │ │ │ ], - │ │ │ "retryCount": 0, - │ │ │ "stepNumber": 0 - │ │ │ } - │ │ │ output: { - │ │ │ "shortCircuited": false - │ │ │ } - │ │ │ metadata: { - │ │ │ "entity_id": "provider-history-compat", - │ │ │ "entity_name": "Provider History Compat", - │ │ │ "entity_type": "input_processor" - │ │ │ } - │ │ ├── llm request processor: unsupported-file-handler [function] - │ │ │ input: { - │ │ │ "prompt": [ - │ │ │ { - │ │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ │ "role": "system" - │ │ │ }, - │ │ │ { - │ │ │ "content": [ - │ │ │ { - │ │ │ "providerOptions": { - │ │ │ "mastra": { - │ │ │ "createdAt": 0 - │ │ │ } - │ │ │ }, - │ │ │ "text": "What is the weather in Paris?", - │ │ │ "type": "text" - │ │ │ } - │ │ │ ], - │ │ │ "role": "user" - │ │ │ } - │ │ │ ], - │ │ │ "retryCount": 0, - │ │ │ "stepNumber": 0 - │ │ │ } - │ │ │ output: { - │ │ │ "shortCircuited": false - │ │ │ } - │ │ │ metadata: { - │ │ │ "entity_id": "unsupported-file-handler", - │ │ │ "entity_name": "Unsupported File Handler", - │ │ │ "entity_type": "input_processor" - │ │ │ } - │ │ └── chunk: 'text' [llm] - │ │ output: { - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ ├── agent run: 'weather-agent' [task] - │ │ input: "Stream the Paris forecast." - │ │ output: { - │ │ "files": [], - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent", - │ │ "model": "mock-model-id", - │ │ "provider": "mock-provider" - │ │ } - │ │ └── llm: 'mock-model-id' [llm] - │ │ input: { - │ │ "messages": [ - │ │ { - │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ "role": "system" - │ │ }, - │ │ { - │ │ "content": [ - │ │ { - │ │ "providerOptions": { - │ │ "mastra": { - │ │ "createdAt": 0 - │ │ } - │ │ }, - │ │ "text": "Stream the Paris forecast.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ } - │ │ output: { - │ │ "files": [], - │ │ "reasoning": [], - │ │ "sources": [], - │ │ "text": "The forecast is sunny.", - │ │ "warnings": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent", - │ │ "model": "mock-model-id", - │ │ "provider": "mock-provider" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "time_to_first_token": 0, - │ │ "tokens": 30 - │ │ } - │ │ └── step: 0 [llm] - │ │ input: [ - │ │ { - │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ "role": "system" - │ │ }, - │ │ { - │ │ "content": "Stream the Paris forecast.", - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: { - │ │ "text": "The forecast is sunny.", - │ │ "toolCalls": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "tokens": 30 - │ │ } - │ │ ├── llm request processor: provider-history-compat [function] - │ │ │ input: { - │ │ │ "prompt": [ - │ │ │ { - │ │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ │ "role": "system" - │ │ │ }, - │ │ │ { - │ │ │ "content": [ - │ │ │ { - │ │ │ "providerOptions": { - │ │ │ "mastra": { - │ │ │ "createdAt": 0 - │ │ │ } - │ │ │ }, - │ │ │ "text": "Stream the Paris forecast.", - │ │ │ "type": "text" - │ │ │ } - │ │ │ ], - │ │ │ "role": "user" - │ │ │ } - │ │ │ ], - │ │ │ "retryCount": 0, - │ │ │ "stepNumber": 0 - │ │ │ } - │ │ │ output: { - │ │ │ "shortCircuited": false - │ │ │ } - │ │ │ metadata: { - │ │ │ "entity_id": "provider-history-compat", - │ │ │ "entity_name": "Provider History Compat", - │ │ │ "entity_type": "input_processor" - │ │ │ } - │ │ ├── llm request processor: unsupported-file-handler [function] - │ │ │ input: { - │ │ │ "prompt": [ - │ │ │ { - │ │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ │ "role": "system" - │ │ │ }, - │ │ │ { - │ │ │ "content": [ - │ │ │ { - │ │ │ "providerOptions": { - │ │ │ "mastra": { - │ │ │ "createdAt": 0 - │ │ │ } - │ │ │ }, - │ │ │ "text": "Stream the Paris forecast.", - │ │ │ "type": "text" - │ │ │ } - │ │ │ ], - │ │ │ "role": "user" - │ │ │ } - │ │ │ ], - │ │ │ "retryCount": 0, - │ │ │ "stepNumber": 0 - │ │ │ } - │ │ │ output: { - │ │ │ "shortCircuited": false - │ │ │ } - │ │ │ metadata: { - │ │ │ "entity_id": "unsupported-file-handler", - │ │ │ "entity_name": "Unsupported File Handler", - │ │ │ "entity_type": "input_processor" - │ │ │ } - │ │ └── chunk: 'text' [llm] - │ │ output: { - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ └── workflow run: 'travel-flow' [task] - │ input: { - │ "city": "Berlin" - │ } + │ └── agent run: 'weather-agent' [task] + │ input: "What is the weather in Paris?" │ output: { - │ "forecast": "Sunny in Berlin" + │ "files": [], + │ "text": "The forecast is sunny." │ } │ metadata: { - │ "entity_id": "travel-flow", - │ "entity_name": "travel-flow", - │ "entity_type": "workflow_run" + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent", + │ "model": "mock-model-id", + │ "provider": "mock-provider" │ } - │ └── workflow step: 'lookup-step' [function] + │ └── llm: 'mock-model-id' [llm] │ input: { - │ "city": "Berlin" + │ "messages": [ + │ { + │ "content": "Answer weather questions with the provided mock forecast.", + │ "role": "system" + │ }, + │ { + │ "content": [ + │ { + │ "providerOptions": { + │ "mastra": { + │ "createdAt": 0 + │ } + │ }, + │ "text": "What is the weather in Paris?", + │ "type": "text" + │ } + │ ], + │ "role": "user" + │ } + │ ] │ } │ output: { - │ "forecast": "Sunny in Berlin" + │ "files": [], + │ "reasoning": [], + │ "sources": [], + │ "text": "The forecast is sunny.", + │ "warnings": [] │ } │ metadata: { - │ "entity_id": "lookup-step", - │ "entity_name": "travel-flow", - │ "entity_type": "workflow_step" + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent", + │ "model": "mock-model-id", + │ "provider": "mock-provider" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "time_to_first_token": 0, + │ "tokens": 30 │ } + │ └── step: 0 [llm] + │ input: [ + │ { + │ "content": "Answer weather questions with the provided mock forecast.", + │ "role": "system" + │ }, + │ { + │ "content": "What is the weather in Paris?", + │ "role": "user" + │ } + │ ] + │ output: { + │ "text": "The forecast is sunny.", + │ "toolCalls": [] + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "tokens": 30 + │ } + │ ├── llm request processor: provider-history-compat [function] + │ │ input: { + │ │ "prompt": [ + │ │ { + │ │ "content": "Answer weather questions with the provided mock forecast.", + │ │ "role": "system" + │ │ }, + │ │ { + │ │ "content": [ + │ │ { + │ │ "providerOptions": { + │ │ "mastra": { + │ │ "createdAt": 0 + │ │ } + │ │ }, + │ │ "text": "What is the weather in Paris?", + │ │ "type": "text" + │ │ } + │ │ ], + │ │ "role": "user" + │ │ } + │ │ ], + │ │ "retryCount": 0, + │ │ "stepNumber": 0 + │ │ } + │ │ output: { + │ │ "shortCircuited": false + │ │ } + │ │ metadata: { + │ │ "entity_id": "provider-history-compat", + │ │ "entity_name": "Provider History Compat", + │ │ "entity_type": "input_processor" + │ │ } + │ ├── llm request processor: unsupported-file-handler [function] + │ │ input: { + │ │ "prompt": [ + │ │ { + │ │ "content": "Answer weather questions with the provided mock forecast.", + │ │ "role": "system" + │ │ }, + │ │ { + │ │ "content": [ + │ │ { + │ │ "providerOptions": { + │ │ "mastra": { + │ │ "createdAt": 0 + │ │ } + │ │ }, + │ │ "text": "What is the weather in Paris?", + │ │ "type": "text" + │ │ } + │ │ ], + │ │ "role": "user" + │ │ } + │ │ ], + │ │ "retryCount": 0, + │ │ "stepNumber": 0 + │ │ } + │ │ output: { + │ │ "shortCircuited": false + │ │ } + │ │ metadata: { + │ │ "entity_id": "unsupported-file-handler", + │ │ "entity_name": "Unsupported File Handler", + │ │ "entity_type": "input_processor" + │ │ } + │ └── chunk: 'text' [llm] + │ output: { + │ "text": "The forecast is sunny." + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } ├── mastra-agent-stream-operation │ metadata: { │ "operation": "stream" │ } + │ └── agent run: 'weather-agent' [task] + │ input: "Stream the Paris forecast." + │ output: { + │ "files": [], + │ "text": "The forecast is sunny." + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent", + │ "model": "mock-model-id", + │ "provider": "mock-provider" + │ } + │ └── llm: 'mock-model-id' [llm] + │ input: { + │ "messages": [ + │ { + │ "content": "Answer weather questions with the provided mock forecast.", + │ "role": "system" + │ }, + │ { + │ "content": [ + │ { + │ "providerOptions": { + │ "mastra": { + │ "createdAt": 0 + │ } + │ }, + │ "text": "Stream the Paris forecast.", + │ "type": "text" + │ } + │ ], + │ "role": "user" + │ } + │ ] + │ } + │ output: { + │ "files": [], + │ "reasoning": [], + │ "sources": [], + │ "text": "The forecast is sunny.", + │ "warnings": [] + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent", + │ "model": "mock-model-id", + │ "provider": "mock-provider" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "time_to_first_token": 0, + │ "tokens": 30 + │ } + │ └── step: 0 [llm] + │ input: [ + │ { + │ "content": "Answer weather questions with the provided mock forecast.", + │ "role": "system" + │ }, + │ { + │ "content": "Stream the Paris forecast.", + │ "role": "user" + │ } + │ ] + │ output: { + │ "text": "The forecast is sunny.", + │ "toolCalls": [] + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "tokens": 30 + │ } + │ ├── llm request processor: provider-history-compat [function] + │ │ input: { + │ │ "prompt": [ + │ │ { + │ │ "content": "Answer weather questions with the provided mock forecast.", + │ │ "role": "system" + │ │ }, + │ │ { + │ │ "content": [ + │ │ { + │ │ "providerOptions": { + │ │ "mastra": { + │ │ "createdAt": 0 + │ │ } + │ │ }, + │ │ "text": "Stream the Paris forecast.", + │ │ "type": "text" + │ │ } + │ │ ], + │ │ "role": "user" + │ │ } + │ │ ], + │ │ "retryCount": 0, + │ │ "stepNumber": 0 + │ │ } + │ │ output: { + │ │ "shortCircuited": false + │ │ } + │ │ metadata: { + │ │ "entity_id": "provider-history-compat", + │ │ "entity_name": "Provider History Compat", + │ │ "entity_type": "input_processor" + │ │ } + │ ├── llm request processor: unsupported-file-handler [function] + │ │ input: { + │ │ "prompt": [ + │ │ { + │ │ "content": "Answer weather questions with the provided mock forecast.", + │ │ "role": "system" + │ │ }, + │ │ { + │ │ "content": [ + │ │ { + │ │ "providerOptions": { + │ │ "mastra": { + │ │ "createdAt": 0 + │ │ } + │ │ }, + │ │ "text": "Stream the Paris forecast.", + │ │ "type": "text" + │ │ } + │ │ ], + │ │ "role": "user" + │ │ } + │ │ ], + │ │ "retryCount": 0, + │ │ "stepNumber": 0 + │ │ } + │ │ output: { + │ │ "shortCircuited": false + │ │ } + │ │ metadata: { + │ │ "entity_id": "unsupported-file-handler", + │ │ "entity_name": "Unsupported File Handler", + │ │ "entity_type": "input_processor" + │ │ } + │ └── chunk: 'text' [llm] + │ output: { + │ "text": "The forecast is sunny." + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } ├── mastra-tool-operation │ metadata: { │ "operation": "tool" @@ -355,3 +331,27 @@ span_tree: metadata: { "operation": "workflow" } + └── workflow run: 'travel-flow' [task] + input: { + "city": "Berlin" + } + output: { + "forecast": "Sunny in Berlin" + } + metadata: { + "entity_id": "travel-flow", + "entity_name": "travel-flow", + "entity_type": "workflow_run" + } + └── workflow step: 'lookup-step' [function] + input: { + "city": "Berlin" + } + output: { + "forecast": "Sunny in Berlin" + } + metadata: { + "entity_id": "lookup-step", + "entity_name": "travel-flow", + "entity_type": "workflow_step" + } diff --git a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.json b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.json index 01bfe9400..5c3483698 100644 --- a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.json +++ b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.json @@ -103,7 +103,15 @@ "entity_name": "Weather Agent", "entity_type": "agent" } - }, + } + ], + "metadata": { + "operation": "generate" + } + }, + { + "name": "mastra-agent-stream-operation", + "children": [ { "name": "agent run: 'weather-agent'", "type": "task", @@ -200,7 +208,22 @@ "entity_name": "Weather Agent", "entity_type": "agent" } - }, + } + ], + "metadata": { + "operation": "stream" + } + }, + { + "name": "mastra-tool-operation", + "children": [], + "metadata": { + "operation": "tool" + } + }, + { + "name": "mastra-workflow-operation", + "children": [ { "name": "workflow run: 'travel-flow'", "type": "task", @@ -235,27 +258,6 @@ } } ], - "metadata": { - "operation": "generate" - } - }, - { - "name": "mastra-agent-stream-operation", - "children": [], - "metadata": { - "operation": "stream" - } - }, - { - "name": "mastra-tool-operation", - "children": [], - "metadata": { - "operation": "tool" - } - }, - { - "name": "mastra-workflow-operation", - "children": [], "metadata": { "operation": "workflow" } diff --git a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.txt b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.txt index cdf5c3c3a..3a7bed95d 100644 --- a/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.txt +++ b/e2e/scenarios/mastra-instrumentation/__snapshots__/mastra-v1.span-tree.txt @@ -7,190 +7,166 @@ span_tree: │ metadata: { │ "operation": "generate" │ } - │ ├── agent run: 'weather-agent' [task] - │ │ input: "What is the weather in Paris?" - │ │ output: { - │ │ "files": [], - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ │ └── llm: 'mock-model-id' [llm] - │ │ input: { - │ │ "messages": [ - │ │ { - │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ "role": "system" - │ │ }, - │ │ { - │ │ "content": [ - │ │ { - │ │ "providerOptions": { - │ │ "mastra": { - │ │ "createdAt": 0 - │ │ } - │ │ }, - │ │ "text": "What is the weather in Paris?", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ } - │ │ output: { - │ │ "files": [], - │ │ "reasoning": [], - │ │ "sources": [], - │ │ "text": "The forecast is sunny.", - │ │ "warnings": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent", - │ │ "model": "mock-model-id", - │ │ "provider": "mock-provider" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "time_to_first_token": 0, - │ │ "tokens": 30 - │ │ } - │ │ └── step: 0 [llm] - │ │ output: { - │ │ "text": "The forecast is sunny.", - │ │ "toolCalls": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "tokens": 30 - │ │ } - │ │ └── chunk: 'text' [llm] - │ │ output: { - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ ├── agent run: 'weather-agent' [task] - │ │ input: "Stream the Paris forecast." - │ │ output: { - │ │ "files": [], - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ │ └── llm: 'mock-model-id' [llm] - │ │ input: { - │ │ "messages": [ - │ │ { - │ │ "content": "Answer weather questions with the provided mock forecast.", - │ │ "role": "system" - │ │ }, - │ │ { - │ │ "content": [ - │ │ { - │ │ "providerOptions": { - │ │ "mastra": { - │ │ "createdAt": 0 - │ │ } - │ │ }, - │ │ "text": "Stream the Paris forecast.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ } - │ │ output: { - │ │ "files": [], - │ │ "reasoning": [], - │ │ "sources": [], - │ │ "text": "The forecast is sunny.", - │ │ "warnings": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent", - │ │ "model": "mock-model-id", - │ │ "provider": "mock-provider" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "time_to_first_token": 0, - │ │ "tokens": 30 - │ │ } - │ │ └── step: 0 [llm] - │ │ output: { - │ │ "text": "The forecast is sunny.", - │ │ "toolCalls": [] - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 20, - │ │ "prompt_tokens": 10, - │ │ "tokens": 30 - │ │ } - │ │ └── chunk: 'text' [llm] - │ │ output: { - │ │ "text": "The forecast is sunny." - │ │ } - │ │ metadata: { - │ │ "entity_id": "weather-agent", - │ │ "entity_name": "Weather Agent", - │ │ "entity_type": "agent" - │ │ } - │ └── workflow run: 'travel-flow' [task] - │ input: { - │ "city": "Berlin" - │ } + │ └── agent run: 'weather-agent' [task] + │ input: "What is the weather in Paris?" │ output: { - │ "forecast": "Sunny in Berlin" + │ "files": [], + │ "text": "The forecast is sunny." │ } │ metadata: { - │ "entity_id": "travel-flow", - │ "entity_name": "travel-flow", - │ "entity_type": "workflow_run" + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" │ } - │ └── workflow step: 'lookup-step' [function] + │ └── llm: 'mock-model-id' [llm] │ input: { - │ "city": "Berlin" + │ "messages": [ + │ { + │ "content": "Answer weather questions with the provided mock forecast.", + │ "role": "system" + │ }, + │ { + │ "content": [ + │ { + │ "providerOptions": { + │ "mastra": { + │ "createdAt": 0 + │ } + │ }, + │ "text": "What is the weather in Paris?", + │ "type": "text" + │ } + │ ], + │ "role": "user" + │ } + │ ] │ } │ output: { - │ "forecast": "Sunny in Berlin" + │ "files": [], + │ "reasoning": [], + │ "sources": [], + │ "text": "The forecast is sunny.", + │ "warnings": [] │ } │ metadata: { - │ "entity_id": "lookup-step", - │ "entity_name": "travel-flow", - │ "entity_type": "workflow_step" + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent", + │ "model": "mock-model-id", + │ "provider": "mock-provider" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "time_to_first_token": 0, + │ "tokens": 30 │ } + │ └── step: 0 [llm] + │ output: { + │ "text": "The forecast is sunny.", + │ "toolCalls": [] + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "tokens": 30 + │ } + │ └── chunk: 'text' [llm] + │ output: { + │ "text": "The forecast is sunny." + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } ├── mastra-agent-stream-operation │ metadata: { │ "operation": "stream" │ } + │ └── agent run: 'weather-agent' [task] + │ input: "Stream the Paris forecast." + │ output: { + │ "files": [], + │ "text": "The forecast is sunny." + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } + │ └── llm: 'mock-model-id' [llm] + │ input: { + │ "messages": [ + │ { + │ "content": "Answer weather questions with the provided mock forecast.", + │ "role": "system" + │ }, + │ { + │ "content": [ + │ { + │ "providerOptions": { + │ "mastra": { + │ "createdAt": 0 + │ } + │ }, + │ "text": "Stream the Paris forecast.", + │ "type": "text" + │ } + │ ], + │ "role": "user" + │ } + │ ] + │ } + │ output: { + │ "files": [], + │ "reasoning": [], + │ "sources": [], + │ "text": "The forecast is sunny.", + │ "warnings": [] + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent", + │ "model": "mock-model-id", + │ "provider": "mock-provider" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "time_to_first_token": 0, + │ "tokens": 30 + │ } + │ └── step: 0 [llm] + │ output: { + │ "text": "The forecast is sunny.", + │ "toolCalls": [] + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } + │ metrics: { + │ "completion_tokens": 20, + │ "prompt_tokens": 10, + │ "tokens": 30 + │ } + │ └── chunk: 'text' [llm] + │ output: { + │ "text": "The forecast is sunny." + │ } + │ metadata: { + │ "entity_id": "weather-agent", + │ "entity_name": "Weather Agent", + │ "entity_type": "agent" + │ } ├── mastra-tool-operation │ metadata: { │ "operation": "tool" @@ -199,3 +175,27 @@ span_tree: metadata: { "operation": "workflow" } + └── workflow run: 'travel-flow' [task] + input: { + "city": "Berlin" + } + output: { + "forecast": "Sunny in Berlin" + } + metadata: { + "entity_id": "travel-flow", + "entity_name": "travel-flow", + "entity_type": "workflow_run" + } + └── workflow step: 'lookup-step' [function] + input: { + "city": "Berlin" + } + output: { + "forecast": "Sunny in Berlin" + } + metadata: { + "entity_id": "lookup-step", + "entity_name": "travel-flow", + "entity_type": "workflow_step" + } diff --git a/js/src/wrappers/mastra.test.ts b/js/src/wrappers/mastra.test.ts index f62eddf8a..b41aad6ac 100644 --- a/js/src/wrappers/mastra.test.ts +++ b/js/src/wrappers/mastra.test.ts @@ -6,6 +6,7 @@ import { expect, test, } from "vite-plus/test"; +import type { LoggingEvent } from "../../util/object"; import { configureNode } from "../node/config"; import { _exportsForTestingOnly } from "../logger/testing"; import { initLogger } from "../logger/core"; @@ -83,6 +84,89 @@ describe("BraintrustObservabilityExporter", () => { }); }); + test.each(["fixed", "per agent"] as const)( + "routes overlapping traces with a %s logger", + async (mode) => { + const first = initLogger({ + projectId: "first", + projectName: "first", + setCurrent: false, + }); + const second = initLogger({ + projectId: "second", + projectName: "second", + setCurrent: false, + }); + const selected: string[] = []; + const exporter = new BraintrustObservabilityExporter({ + logger: + mode === "fixed" + ? first + : (root) => { + selected.push(root.id); + return root.entityId === "a" ? first : second; + }, + }); + + await logger.traced(async () => { + for (const id of ["a", "b"]) { + await exporter.exportTracingEvent({ + type: "span_started", + exportedSpan: span({ id, traceId: id, entityId: id }), + }); + } + }); + for (const id of ["b", "a"]) { + await exporter.exportTracingEvent({ + type: "span_started", + exportedSpan: span({ + id: id + "-tool", + traceId: id, + parentSpanId: id, + isEvent: true, + }), + }); + await exporter.exportTracingEvent({ + type: "span_ended", + exportedSpan: span({ id, traceId: id }), + }); + } + await exporter.shutdown(); + const rows = (await backgroundLogger.drain()) as LoggingEvent[]; + expect(selected).toEqual(mode === "fixed" ? [] : ["a", "b"]); + for (const id of ["a", "b"]) { + const project = id === "a" || mode === "fixed" ? "first" : "second"; + const root = rows.find((row) => row.id === id); + expect(root?.project_id).toBe(project); + expect(root?.span_parents ?? []).toEqual([]); + expect(rows.find((row) => row.id === id + "-tool")).toMatchObject({ + project_id: project, + root_span_id: root?.root_span_id, + span_parents: [root?.span_id], + }); + } + }, + ); + + test("captures the current parent separately for each Mastra run", async () => { + const exporter = new BraintrustObservabilityExporter(); + for (const id of ["first", "second"]) { + await logger.traced(async (parent) => { + const exportedSpan = span({ id, traceId: id }); + await exporter.exportTracingEvent({ + type: "span_started", + exportedSpan, + }); + await exporter.exportTracingEvent({ type: "span_ended", exportedSpan }); + await exporter.flush(); + const rows = (await backgroundLogger.drain()) as LoggingEvent[]; + expect(rows.find((row) => row.id === id)?.span_parents).toEqual([ + parent.spanId, + ]); + }); + } + }); + // Run a model span's full lifecycle through the exporter and return the logged // row. Both events are required: onEnd is a no-op without a prior onStart. async function logModelSpan( diff --git a/js/src/wrappers/mastra.ts b/js/src/wrappers/mastra.ts index b77283d2d..b1c6ff4a3 100644 --- a/js/src/wrappers/mastra.ts +++ b/js/src/wrappers/mastra.ts @@ -24,7 +24,8 @@ import { debugLogger } from "../debug-logger"; import { _internalGetGlobalState, _internalSetInitialState, - currentSpan, + type BraintrustState, + type Logger, startSpan, type StartSpanArgs, type Span, @@ -243,15 +244,15 @@ export class BraintrustObservabilityExporter implements MastraObservabilityExpor public readonly name: "braintrust" = MASTRA_BRAINTRUST_EXPORTER_NAME; private readonly spans = new Map(); - // Captured at the first SPAN_STARTED event. Mastra's observability bus may - // dispatch later events outside the user's AsyncLocalStorage context, where - // `currentSpan()` returns NOOP_SPAN — which would make our `startSpan()` - // calls go to a no-op logger and silently drop. Anchoring on the parent - // we observe while still in-context keeps the whole Mastra subtree under - // the user's traced scenario. - private capturedParent: Span | undefined; - - constructor() { + private readonly roots = new Map(); + private readonly loggerStates = new Set(); + + constructor( + private readonly options: { + /** Start roots on this logger, or choose a logger from each Mastra root span. */ + logger?: Logger | ((root: Readonly) => Logger); + } = {}, + ) { // The auto-instrumentations bundle and the main braintrust bundle each // have their own module-scoped `_globalState`. The main bundle initializes // state via `configureNode`; this call rehydrates our bundle's copy from @@ -286,15 +287,20 @@ export class BraintrustObservabilityExporter implements MastraObservabilityExpor } async flush(): Promise { - const state = _internalGetGlobalState(); - if (state) { - await state.bgLogger().flush(); + if (this.options.logger) { + await Promise.all( + Array.from(this.loggerStates, (state) => state.bgLogger().flush()), + ); + } else { + await _internalGetGlobalState()?.bgLogger().flush(); } } async shutdown(): Promise { await this.flush(); this.spans.clear(); + this.roots.clear(); + this.loggerStates.clear(); } private onStart(exported: MastraExportedSpan): void { @@ -315,27 +321,29 @@ export class BraintrustObservabilityExporter implements MastraObservabilityExpor INSTRUMENTATION_NAMES.MASTRA, ); - const parentRecord = exported.parentSpanId - ? this.spans.get(exported.parentSpanId) + // A skipped internal span can leave a child without a direct parent. + const isRoot = exported.isRootSpan || !exported.parentSpanId; + const parent = exported.parentSpanId + ? (this.spans.get(exported.parentSpanId) ?? + this.roots.get(exported.traceId)) : undefined; - - // Capture the user's current span on the very first event we see; later - // events from Mastra's bus may run outside that AsyncLocalStorage scope. - if (!this.capturedParent) { - const probe = currentSpan(); - if (probe && (probe as Span).spanId) { - this.capturedParent = probe; - } + let owner: Span | Logger | undefined = parent?.span; + if (!owner && this.options.logger) { + const configured = this.options.logger; + // A child with no known root cannot identify its agent's logger. + if (typeof configured === "function" && !isRoot) return; + const logger = + typeof configured === "function" ? configured(exported) : configured; + // Logger.startSpan otherwise inherits the active trace, even across projects. + args.parentSpanIds = { rootSpanId: exported.traceId, parentSpanIds: [] }; + this.loggerStates.add(logger.loggingState); + owner = logger; } const { parent: _ignoredParent, ...publicArgs } = args; - const span = parentRecord - ? parentRecord.span.startSpan(publicArgs) - : this.capturedParent - ? this.capturedParent.startSpan(publicArgs) - : startSpan(publicArgs); - + const span = owner ? owner.startSpan(publicArgs) : startSpan(publicArgs); const record: SpanRecord = { span, hasLoggedInput: false }; + if (isRoot && !exported.isEvent) this.roots.set(exported.traceId, record); this.logPayload(record, exported); this.spans.set(exported.id, record); @@ -369,6 +377,9 @@ export class BraintrustObservabilityExporter implements MastraObservabilityExpor } record.span.end({ endTime: epochSeconds(exported.endTime) }); + if (this.roots.get(exported.traceId) === record) { + this.roots.delete(exported.traceId); + } this.spans.delete(exported.id); }