Skip to content

Commit c6b40a0

Browse files
fix(traceloop-sdk): omit spans of non-traceloop instrumentations by default when using standalone processor (#598)
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
1 parent 1665667 commit c6b40a0

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

packages/sample-app/src/sample_otel_sdk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NodeSDK } from "@opentelemetry/sdk-node";
22
import { Resource } from "@opentelemetry/resources";
3-
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
3+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
44
import {
55
createSpanProcessor,
66
withTask,
@@ -13,12 +13,13 @@ const traceloopSpanProcessor = createSpanProcessor({
1313
apiKey: process.env.TRACELOOP_API_KEY,
1414
baseUrl: process.env.TRACELOOP_BASE_URL,
1515
disableBatch: true,
16+
allowedInstrumentationLibraries: ["my-sample-app"],
1617
});
1718

1819
// Initialize the OpenTelemetry SDK with Traceloop's span processor
1920
const sdk = new NodeSDK({
2021
resource: new Resource({
21-
[SemanticResourceAttributes.SERVICE_NAME]: "my-sample-app",
22+
[ATTR_SERVICE_NAME]: "my-sample-app",
2223
}),
2324
spanProcessors: [traceloopSpanProcessor],
2425
});

packages/traceloop-sdk/src/lib/tracing/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { baggageUtils } from "@opentelemetry/core";
44
import { context, diag } from "@opentelemetry/api";
55
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
66
import { Resource } from "@opentelemetry/resources";
7-
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
7+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
88
import { Instrumentation } from "@opentelemetry/instrumentation";
99
import { InitializeOptions } from "../interfaces";
1010
import { Telemetry } from "../telemetry/telemetry";
@@ -25,7 +25,10 @@ import { LangChainInstrumentation } from "@traceloop/instrumentation-langchain";
2525
import { ChromaDBInstrumentation } from "@traceloop/instrumentation-chromadb";
2626
import { QdrantInstrumentation } from "@traceloop/instrumentation-qdrant";
2727
import { TogetherInstrumentation } from "@traceloop/instrumentation-together";
28-
import { createSpanProcessor } from "./span-processor";
28+
import {
29+
ALL_INSTRUMENTATION_LIBRARIES,
30+
createSpanProcessor,
31+
} from "./span-processor";
2932

3033
let _sdk: NodeSDK;
3134
let _spanProcessor: SpanProcessor;
@@ -268,6 +271,7 @@ export const startTracing = (options: InitializeOptions) => {
268271
disableBatch: options.disableBatch,
269272
exporter: traceExporter,
270273
headers,
274+
allowedInstrumentationLibraries: ALL_INSTRUMENTATION_LIBRARIES,
271275
});
272276

273277
const spanProcessors: SpanProcessor[] = [_spanProcessor];
@@ -277,8 +281,7 @@ export const startTracing = (options: InitializeOptions) => {
277281

278282
_sdk = new NodeSDK({
279283
resource: new Resource({
280-
[SEMRESATTRS_SERVICE_NAME]:
281-
options.appName || process.env.npm_package_name,
284+
[ATTR_SERVICE_NAME]: options.appName || process.env.npm_package_name,
282285
}),
283286
spanProcessors,
284287
contextManager: options.contextManager,

packages/traceloop-sdk/src/lib/tracing/span-processor.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
} from "./tracing";
1616
import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
1717

18+
export const ALL_INSTRUMENTATION_LIBRARIES = "all" as const;
19+
type AllInstrumentationLibraries = typeof ALL_INSTRUMENTATION_LIBRARIES;
20+
1821
export interface SpanProcessorOptions {
1922
/**
2023
* The API Key for sending traces data. Optional.
@@ -44,6 +47,14 @@ export interface SpanProcessorOptions {
4447
* The headers to be sent with the traces data. Optional.
4548
*/
4649
headers?: Record<string, string>;
50+
51+
/**
52+
* The instrumentation libraries to be allowed in the traces. Optional.
53+
* Defaults to Traceloop instrumentation libraries.
54+
* If set to ALL_INSTRUMENTATION_LIBRARIES, all instrumentation libraries will be allowed.
55+
* If set to an array of instrumentation libraries, only traceloop instrumentation libraries and the specified instrumentation libraries will be allowed.
56+
*/
57+
allowedInstrumentationLibraries?: string[] | AllInstrumentationLibraries;
4758
}
4859

4960
/**
@@ -78,11 +89,40 @@ export const createSpanProcessor = (
7889
const originalOnEnd = spanProcessor.onEnd.bind(spanProcessor);
7990

8091
spanProcessor.onStart = onSpanStart;
81-
spanProcessor.onEnd = onSpanEnd(originalOnEnd);
92+
93+
if (
94+
options.allowedInstrumentationLibraries === ALL_INSTRUMENTATION_LIBRARIES
95+
) {
96+
spanProcessor.onEnd = onSpanEnd(originalOnEnd);
97+
} else {
98+
const instrumentationLibraries = [...traceloopInstrumentationLibraries];
99+
100+
if (options.allowedInstrumentationLibraries) {
101+
instrumentationLibraries.push(...options.allowedInstrumentationLibraries);
102+
}
103+
104+
spanProcessor.onEnd = onSpanEnd(originalOnEnd, instrumentationLibraries);
105+
}
82106

83107
return spanProcessor;
84108
};
85109

110+
export const traceloopInstrumentationLibraries = [
111+
"@traceloop/node-server-sdk",
112+
"@traceloop/instrumentation-openai",
113+
"@traceloop/instrumentation-langchain",
114+
"@traceloop/instrumentation-chroma",
115+
"@traceloop/instrumentation-anthropic",
116+
"@traceloop/instrumentation-azure",
117+
"@traceloop/instrumentation-llamaindex",
118+
"@traceloop/instrumentation-vertexai",
119+
"@traceloop/instrumentation-bedrock",
120+
"@traceloop/instrumentation-cohere",
121+
"@traceloop/instrumentation-pinecone",
122+
"@traceloop/instrumentation-qdrant",
123+
"@traceloop/instrumentation-together",
124+
];
125+
86126
/**
87127
* Handles span start event, enriching it with workflow and entity information
88128
*/
@@ -119,8 +159,18 @@ const onSpanStart = (span: Span): void => {
119159
/**
120160
* Handles span end event, adapting attributes for Vercel AI compatibility
121161
*/
122-
const onSpanEnd = (originalOnEnd: (span: ReadableSpan) => void) => {
162+
const onSpanEnd = (
163+
originalOnEnd: (span: ReadableSpan) => void,
164+
instrumentationLibraries?: string[],
165+
) => {
123166
return (span: ReadableSpan): void => {
167+
if (
168+
instrumentationLibraries &&
169+
!instrumentationLibraries.includes(span.instrumentationLibrary.name)
170+
) {
171+
return;
172+
}
173+
124174
// Vercel AI Adapters
125175
const attributes = span.attributes;
126176

packages/traceloop-sdk/src/lib/tracing/tracing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { trace, createContextKey } from "@opentelemetry/api";
22
import { Context } from "@opentelemetry/api/build/src/context/types";
33

4-
const TRACER_NAME = "traceloop.tracer";
4+
const TRACER_NAME = "@traceloop/node-server-sdk";
55
export const WORKFLOW_NAME_KEY = createContextKey("workflow_name");
66
export const ENTITY_NAME_KEY = createContextKey("entity_name");
77
export const ASSOCATION_PROPERTIES_KEY = createContextKey(

0 commit comments

Comments
 (0)