Skip to content

Commit 832b4f5

Browse files
committed
Set span kind for mastra spans to agent
1 parent 73aeda6 commit 832b4f5

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import {
2+
OpenInferenceSpanKind,
3+
SemanticConventions,
4+
} from "@arizeai/openinference-semantic-conventions";
5+
import { ReadableSpan } from "@opentelemetry/sdk-trace-base";
6+
7+
const MASTRA_ROOT_SPAN_NAME_PREFIXES = ["post /api/agents"];
8+
9+
const MASTRA_AGENT_SPAN_NAME_PREFIXES = [
10+
"agent",
11+
"mastra.getAgent",
12+
"post /api/agents",
13+
];
14+
15+
/**
16+
* Add the OpenInference span kind to the given Mastra span.
17+
*
18+
* This function will add the OpenInference span kind to the given Mastra span.
19+
*/
20+
const addOpenInferenceSpanKind = (
21+
span: ReadableSpan,
22+
kind: OpenInferenceSpanKind,
23+
) => {
24+
span.attributes[SemanticConventions.OPENINFERENCE_SPAN_KIND] = kind;
25+
};
26+
27+
/**
28+
* Get the OpenInference span kind for the given Mastra span.
29+
*
30+
* This function will return the OpenInference span kind for the given Mastra span, if it has already been set.
31+
*/
32+
const getOpenInferenceSpanKind = (span: ReadableSpan) => {
33+
return span.attributes[SemanticConventions.OPENINFERENCE_SPAN_KIND] as
34+
| OpenInferenceSpanKind
35+
| undefined;
36+
};
37+
38+
/**
39+
* Detect the closest OpenInference span kind for the given Mastra span.
40+
*
41+
* This function will attempt to detect the closest OpenInference span kind for the given Mastra span,
42+
* based on the span's name and parent span ID.
43+
*/
44+
const detectOpenInferenceSpanKindFromMastraSpan = (
45+
span: ReadableSpan,
46+
): OpenInferenceSpanKind | null => {
47+
const oiKind = getOpenInferenceSpanKind(span);
48+
if (oiKind) {
49+
return oiKind;
50+
}
51+
const spanName = span.name.toLowerCase();
52+
const hasParent = span.parentSpanId != null;
53+
if (
54+
// child spans with an agent prefix in their name are agent spans
55+
(hasParent &&
56+
MASTRA_AGENT_SPAN_NAME_PREFIXES.some((prefix) =>
57+
spanName.startsWith(prefix),
58+
)) ||
59+
// root spans with a root span prefix in their name are agent spans
60+
(!hasParent &&
61+
MASTRA_ROOT_SPAN_NAME_PREFIXES.some((prefix) =>
62+
spanName.startsWith(prefix),
63+
))
64+
) {
65+
return OpenInferenceSpanKind.AGENT;
66+
}
67+
return null;
68+
};
69+
70+
/**
71+
* Enrich a Mastra span with OpenInference attributes.
72+
*
73+
* This function will add additional attributes to the span, based on the Mastra span's attributes.
74+
*
75+
* It will attempt to detect the closest OpenInference span kind for the given Mastra span, and then
76+
* enrich the span with the appropriate attributes based on the span kind and current attributes.
77+
*
78+
* @param span - The Mastra span to enrich.
79+
*/
80+
export const enrichBySpanKind = (span: ReadableSpan) => {
81+
if (getOpenInferenceSpanKind(span)) {
82+
// span has been processed already, skip
83+
return;
84+
}
85+
const kind = detectOpenInferenceSpanKindFromMastraSpan(span);
86+
if (kind) {
87+
addOpenInferenceSpanKind(span, kind);
88+
}
89+
switch (kind) {
90+
case OpenInferenceSpanKind.AGENT: {
91+
if (span.parentSpanId == null) {
92+
// add input and output attributes to the span
93+
// TODO: We need to collect the input and output from the children spans as we process them
94+
// and add them to the span attributes here
95+
}
96+
break;
97+
}
98+
default:
99+
break;
100+
}
101+
};

js/packages/openinference-mastra/src/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ReadableSpan } from "@opentelemetry/sdk-trace-base";
22
import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions";
33
import { isOpenInferenceSpan as isOpenInferenceSpanVercel } from "@arizeai/openinference-vercel/utils";
44
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
5+
import { enrichBySpanKind } from "./attributes.js";
56

67
/**
78
* Augments a Mastra span with OpenInference resource attributes.
@@ -29,7 +30,9 @@ export const addOpenInferenceResourceAttributesToMastraSpan = (
2930
export const addOpenInferenceAttributesToMastraSpan = (
3031
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3132
span: ReadableSpan,
32-
) => {};
33+
) => {
34+
enrichBySpanKind(span);
35+
};
3336

3437
/**
3538
* Checks if a span is an OpenInference span.

0 commit comments

Comments
 (0)