|
| 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 | +}; |
0 commit comments