@@ -15,6 +15,9 @@ import {
15
15
} from "./tracing" ;
16
16
import { SpanAttributes } from "@traceloop/ai-semantic-conventions" ;
17
17
18
+ export const ALL_INSTRUMENTATION_LIBRARIES = "all" as const ;
19
+ type AllInstrumentationLibraries = typeof ALL_INSTRUMENTATION_LIBRARIES ;
20
+
18
21
export interface SpanProcessorOptions {
19
22
/**
20
23
* The API Key for sending traces data. Optional.
@@ -44,6 +47,14 @@ export interface SpanProcessorOptions {
44
47
* The headers to be sent with the traces data. Optional.
45
48
*/
46
49
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 ;
47
58
}
48
59
49
60
/**
@@ -78,11 +89,40 @@ export const createSpanProcessor = (
78
89
const originalOnEnd = spanProcessor . onEnd . bind ( spanProcessor ) ;
79
90
80
91
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
+ }
82
106
83
107
return spanProcessor ;
84
108
} ;
85
109
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
+
86
126
/**
87
127
* Handles span start event, enriching it with workflow and entity information
88
128
*/
@@ -119,8 +159,18 @@ const onSpanStart = (span: Span): void => {
119
159
/**
120
160
* Handles span end event, adapting attributes for Vercel AI compatibility
121
161
*/
122
- const onSpanEnd = ( originalOnEnd : ( span : ReadableSpan ) => void ) => {
162
+ const onSpanEnd = (
163
+ originalOnEnd : ( span : ReadableSpan ) => void ,
164
+ instrumentationLibraries ?: string [ ] ,
165
+ ) => {
123
166
return ( span : ReadableSpan ) : void => {
167
+ if (
168
+ instrumentationLibraries &&
169
+ ! instrumentationLibraries . includes ( span . instrumentationLibrary . name )
170
+ ) {
171
+ return ;
172
+ }
173
+
124
174
// Vercel AI Adapters
125
175
const attributes = span . attributes ;
126
176
0 commit comments