UNPKG

@aws/aws-distro-opentelemetry-node-autoinstrumentation

Version:

This package provides Amazon Web Services distribution of the OpenTelemetry Node Instrumentation, which allows for auto-instrumentation of NodeJS applications.

45 lines 2.16 kB
"use strict"; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.GenAiNestedClientSpanProcessor = void 0; const api_1 = require("@opentelemetry/api"); const semconv_1 = require("./instrumentation/common/semconv"); class GenAiNestedClientSpanProcessor { constructor() { // OTel GenAI semantic conventions require outgoing LLM calls to be CLIENT spans. // However, the same call can be instrumented by both the agentic framework // and the underlying LLM client SDK, producing nested CLIENT spans for a single request. // This processor converts the outer span to INTERNAL so only the innermost // SDK span remains CLIENT, avoiding the nested CLIENT anti-pattern. this._hasGenAiClientChild = new Map(); } onStart(_span, _parentContext) { } onEnd(span) { var _a; if (span.kind !== api_1.SpanKind.CLIENT) { return; } const parentSpanId = (_a = span.parentSpanContext) === null || _a === void 0 ? void 0 : _a.spanId; if (parentSpanId) { this._hasGenAiClientChild.set(parentSpanId, true); } const operationName = (span.attributes || {})[semconv_1.ATTR_GEN_AI_OPERATION_NAME]; const isLlmSpan = operationName === semconv_1.GEN_AI_OPERATION_NAME_VALUE_CHAT || operationName === semconv_1.GEN_AI_OPERATION_NAME_VALUE_TEXT_COMPLETION || operationName === semconv_1.GEN_AI_OPERATION_NAME_VALUE_GENERATE_CONTENT || operationName === semconv_1.GEN_AI_OPERATION_NAME_VALUE_EMBEDDINGS; if (isLlmSpan && span.spanContext() && this._hasGenAiClientChild.delete(span.spanContext().spanId)) { span.kind = api_1.SpanKind.INTERNAL; } } shutdown() { this._hasGenAiClientChild.clear(); return Promise.resolve(); } forceFlush() { return Promise.resolve(); } } exports.GenAiNestedClientSpanProcessor = GenAiNestedClientSpanProcessor; //# sourceMappingURL=gen-ai-nested-client-span-processor.js.map