diff --git a/packages/sample-app/src/sample_otel_sdk.ts b/packages/sample-app/src/sample_otel_sdk.ts index 29232a95..37a0da1b 100644 --- a/packages/sample-app/src/sample_otel_sdk.ts +++ b/packages/sample-app/src/sample_otel_sdk.ts @@ -1,6 +1,6 @@ import { NodeSDK } from "@opentelemetry/sdk-node"; import { Resource } from "@opentelemetry/resources"; -import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions"; +import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; import { createSpanProcessor, withTask, @@ -13,12 +13,13 @@ const traceloopSpanProcessor = createSpanProcessor({ apiKey: process.env.TRACELOOP_API_KEY, baseUrl: process.env.TRACELOOP_BASE_URL, disableBatch: true, + allowedInstrumentationLibraries: ["my-sample-app"], }); // Initialize the OpenTelemetry SDK with Traceloop's span processor const sdk = new NodeSDK({ resource: new Resource({ - [SemanticResourceAttributes.SERVICE_NAME]: "my-sample-app", + [ATTR_SERVICE_NAME]: "my-sample-app", }), spanProcessors: [traceloopSpanProcessor], }); diff --git a/packages/traceloop-sdk/src/lib/tracing/index.ts b/packages/traceloop-sdk/src/lib/tracing/index.ts index 3d425f0d..f66e6a09 100644 --- a/packages/traceloop-sdk/src/lib/tracing/index.ts +++ b/packages/traceloop-sdk/src/lib/tracing/index.ts @@ -4,7 +4,7 @@ import { baggageUtils } from "@opentelemetry/core"; import { context, diag } from "@opentelemetry/api"; import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto"; import { Resource } from "@opentelemetry/resources"; -import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; +import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; import { Instrumentation } from "@opentelemetry/instrumentation"; import { InitializeOptions } from "../interfaces"; import { Telemetry } from "../telemetry/telemetry"; @@ -25,7 +25,10 @@ import { LangChainInstrumentation } from "@traceloop/instrumentation-langchain"; import { ChromaDBInstrumentation } from "@traceloop/instrumentation-chromadb"; import { QdrantInstrumentation } from "@traceloop/instrumentation-qdrant"; import { TogetherInstrumentation } from "@traceloop/instrumentation-together"; -import { createSpanProcessor } from "./span-processor"; +import { + ALL_INSTRUMENTATION_LIBRARIES, + createSpanProcessor, +} from "./span-processor"; let _sdk: NodeSDK; let _spanProcessor: SpanProcessor; @@ -268,6 +271,7 @@ export const startTracing = (options: InitializeOptions) => { disableBatch: options.disableBatch, exporter: traceExporter, headers, + allowedInstrumentationLibraries: ALL_INSTRUMENTATION_LIBRARIES, }); const spanProcessors: SpanProcessor[] = [_spanProcessor]; @@ -277,8 +281,7 @@ export const startTracing = (options: InitializeOptions) => { _sdk = new NodeSDK({ resource: new Resource({ - [SEMRESATTRS_SERVICE_NAME]: - options.appName || process.env.npm_package_name, + [ATTR_SERVICE_NAME]: options.appName || process.env.npm_package_name, }), spanProcessors, contextManager: options.contextManager, diff --git a/packages/traceloop-sdk/src/lib/tracing/span-processor.ts b/packages/traceloop-sdk/src/lib/tracing/span-processor.ts index 7dc69cd3..02c483de 100644 --- a/packages/traceloop-sdk/src/lib/tracing/span-processor.ts +++ b/packages/traceloop-sdk/src/lib/tracing/span-processor.ts @@ -15,6 +15,9 @@ import { } from "./tracing"; import { SpanAttributes } from "@traceloop/ai-semantic-conventions"; +export const ALL_INSTRUMENTATION_LIBRARIES = "all" as const; +type AllInstrumentationLibraries = typeof ALL_INSTRUMENTATION_LIBRARIES; + export interface SpanProcessorOptions { /** * The API Key for sending traces data. Optional. @@ -44,6 +47,14 @@ export interface SpanProcessorOptions { * The headers to be sent with the traces data. Optional. */ headers?: Record; + + /** + * The instrumentation libraries to be allowed in the traces. Optional. + * Defaults to Traceloop instrumentation libraries. + * If set to ALL_INSTRUMENTATION_LIBRARIES, all instrumentation libraries will be allowed. + * If set to an array of instrumentation libraries, only traceloop instrumentation libraries and the specified instrumentation libraries will be allowed. + */ + allowedInstrumentationLibraries?: string[] | AllInstrumentationLibraries; } /** @@ -78,11 +89,40 @@ export const createSpanProcessor = ( const originalOnEnd = spanProcessor.onEnd.bind(spanProcessor); spanProcessor.onStart = onSpanStart; - spanProcessor.onEnd = onSpanEnd(originalOnEnd); + + if ( + options.allowedInstrumentationLibraries === ALL_INSTRUMENTATION_LIBRARIES + ) { + spanProcessor.onEnd = onSpanEnd(originalOnEnd); + } else { + const instrumentationLibraries = [...traceloopInstrumentationLibraries]; + + if (options.allowedInstrumentationLibraries) { + instrumentationLibraries.push(...options.allowedInstrumentationLibraries); + } + + spanProcessor.onEnd = onSpanEnd(originalOnEnd, instrumentationLibraries); + } return spanProcessor; }; +export const traceloopInstrumentationLibraries = [ + "@traceloop/node-server-sdk", + "@traceloop/instrumentation-openai", + "@traceloop/instrumentation-langchain", + "@traceloop/instrumentation-chroma", + "@traceloop/instrumentation-anthropic", + "@traceloop/instrumentation-azure", + "@traceloop/instrumentation-llamaindex", + "@traceloop/instrumentation-vertexai", + "@traceloop/instrumentation-bedrock", + "@traceloop/instrumentation-cohere", + "@traceloop/instrumentation-pinecone", + "@traceloop/instrumentation-qdrant", + "@traceloop/instrumentation-together", +]; + /** * Handles span start event, enriching it with workflow and entity information */ @@ -119,8 +159,18 @@ const onSpanStart = (span: Span): void => { /** * Handles span end event, adapting attributes for Vercel AI compatibility */ -const onSpanEnd = (originalOnEnd: (span: ReadableSpan) => void) => { +const onSpanEnd = ( + originalOnEnd: (span: ReadableSpan) => void, + instrumentationLibraries?: string[], +) => { return (span: ReadableSpan): void => { + if ( + instrumentationLibraries && + !instrumentationLibraries.includes(span.instrumentationLibrary.name) + ) { + return; + } + // Vercel AI Adapters const attributes = span.attributes; diff --git a/packages/traceloop-sdk/src/lib/tracing/tracing.ts b/packages/traceloop-sdk/src/lib/tracing/tracing.ts index 97813f05..ef91f010 100644 --- a/packages/traceloop-sdk/src/lib/tracing/tracing.ts +++ b/packages/traceloop-sdk/src/lib/tracing/tracing.ts @@ -1,7 +1,7 @@ import { trace, createContextKey } from "@opentelemetry/api"; import { Context } from "@opentelemetry/api/build/src/context/types"; -const TRACER_NAME = "traceloop.tracer"; +const TRACER_NAME = "@traceloop/node-server-sdk"; export const WORKFLOW_NAME_KEY = createContextKey("workflow_name"); export const ENTITY_NAME_KEY = createContextKey("entity_name"); export const ASSOCATION_PROPERTIES_KEY = createContextKey(