@@ -15,6 +15,9 @@ import {
1515} from "./tracing" ;
1616import { SpanAttributes } from "@traceloop/ai-semantic-conventions" ;
1717
18+ export const ALL_INSTRUMENTATION_LIBRARIES = "all" as const ;
19+ type AllInstrumentationLibraries = typeof ALL_INSTRUMENTATION_LIBRARIES ;
20+
1821export 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
0 commit comments