@@ -40,7 +40,6 @@ import {
4040} from '@opentelemetry/sdk-trace-node'
4141import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
4242import { AnthropicInstrumentation } from '@traceloop/instrumentation-anthropic'
43- import { AzureOpenAIInstrumentation } from '@traceloop/instrumentation-azure'
4443import { BedrockInstrumentation } from '@traceloop/instrumentation-bedrock'
4544import { CohereInstrumentation } from '@traceloop/instrumentation-cohere'
4645import { LangChainInstrumentation } from '@traceloop/instrumentation-langchain'
@@ -51,20 +50,7 @@ import {
5150 AIPlatformInstrumentation ,
5251 VertexAIInstrumentation ,
5352} from '@traceloop/instrumentation-vertexai'
54-
55- import type * as anthropic from '@anthropic-ai/sdk'
56- import type * as bedrock from '@aws-sdk/client-bedrock-runtime'
57- import type * as azure from '@azure/openai'
58- import type * as vertexai from '@google-cloud/vertexai'
59- import type * as langchain_runnables from '@langchain/core/runnables'
60- import type * as langchain_vectorstores from '@langchain/core/vectorstores'
6153import type * as latitude from '@latitude-data/sdk'
62- import type * as cohere from 'cohere-ai'
63- import type * as langchain_agents from 'langchain/agents'
64- import type * as langchain_chains from 'langchain/chains'
65- import type * as langchain_tools from 'langchain/tools'
66- import type * as openai from 'openai'
67- import type * as togetherai from 'together-ai'
6854
6955const TRACES_URL = `${ env . GATEWAY_BASE_URL } /api/v3/traces`
7056const SERVICE_NAME = process . env . npm_package_name || 'unknown'
@@ -101,47 +87,43 @@ export const DEFAULT_SPAN_EXPORTER = (apiKey: string) =>
10187
10288// Note: Only exporting typescript instrumentations
10389export enum Instrumentation {
104- Latitude = InstrumentationScope . Latitude ,
105- OpenAI = InstrumentationScope . OpenAI ,
10690 Anthropic = InstrumentationScope . Anthropic ,
107- AzureOpenAI = InstrumentationScope . AzureOpenAI ,
108- VercelAI = InstrumentationScope . VercelAI ,
109- VertexAI = InstrumentationScope . VertexAI ,
11091 AIPlatform = InstrumentationScope . AIPlatform ,
11192 Bedrock = InstrumentationScope . Bedrock ,
112- TogetherAI = InstrumentationScope . TogetherAI ,
11393 Cohere = InstrumentationScope . Cohere ,
11494 Langchain = InstrumentationScope . Langchain ,
95+ Latitude = InstrumentationScope . Latitude ,
11596 LlamaIndex = InstrumentationScope . LlamaIndex ,
97+ OpenAI = InstrumentationScope . OpenAI ,
98+ TogetherAI = InstrumentationScope . TogetherAI ,
99+ VertexAI = InstrumentationScope . VertexAI ,
116100}
117101
118102export type TelemetryOptions = {
103+ disableBatch ?: boolean
104+ exporter ?: SpanExporter
105+ processors ?: SpanProcessor [ ]
106+ propagators ?: TextMapPropagator [ ]
119107 instrumentations ?: {
120108 [ Instrumentation . Latitude ] ?:
121109 | typeof latitude . Latitude
122110 | LatitudeInstrumentationOptions
123- [ Instrumentation . OpenAI ] ?: typeof openai . OpenAI
124- [ Instrumentation . Anthropic ] ?: typeof anthropic
125- [ Instrumentation . AzureOpenAI ] ?: typeof azure
126- [ Instrumentation . VercelAI ] ?: 'manual'
127- [ Instrumentation . VertexAI ] ?: typeof vertexai
128- [ Instrumentation . AIPlatform ] ?: any // Note: Any because this type is huge
129- [ Instrumentation . Bedrock ] ?: typeof bedrock
130- [ Instrumentation . TogetherAI ] ?: typeof togetherai . Together
131- [ Instrumentation . Cohere ] ?: typeof cohere
111+
112+ // Note: These are all typed as 'any' because using the actual expected types will cause
113+ // type errors when the version installed in the package is even slightly different than
114+ // the version used in the project.
115+ [ Instrumentation . AIPlatform ] ?: any
116+ [ Instrumentation . Anthropic ] ?: any
117+ [ Instrumentation . Bedrock ] ?: any
118+ [ Instrumentation . Cohere ] ?: any
119+ [ Instrumentation . OpenAI ] ?: any
120+ [ Instrumentation . LlamaIndex ] ?: any
121+ [ Instrumentation . TogetherAI ] ?: any
122+ [ Instrumentation . VertexAI ] ?: any
132123 [ Instrumentation . Langchain ] ?: {
133- chainsModule : typeof langchain_chains
134- agentsModule : typeof langchain_agents
135- toolsModule : typeof langchain_tools
136- vectorStoreModule : typeof langchain_vectorstores
137- runnablesModule : typeof langchain_runnables
124+ callbackManagerModule ?: any
138125 }
139- [ Instrumentation . LlamaIndex ] ?: any // Note: Any because this type is huge
140126 }
141- disableBatch ?: boolean
142- exporter ?: SpanExporter
143- processors ?: SpanProcessor [ ]
144- propagators ?: TextMapPropagator [ ]
145127}
146128
147129export class LatitudeTelemetry {
@@ -250,137 +232,45 @@ export class LatitudeTelemetry {
250232 this . instrumentations . push ( instrumentation )
251233 }
252234
253- const openai = this . options . instrumentations ?. openai
254- if ( openai ) {
255- const provider = this . tracerProvider ( Instrumentation . OpenAI )
256- const instrumentation = new OpenAIInstrumentation ( { enrichTokens : true } )
257- instrumentation . setTracerProvider ( provider )
258- instrumentation . manuallyInstrument ( openai )
259- registerInstrumentations ( {
260- instrumentations : [ instrumentation ] ,
261- tracerProvider : provider ,
262- } )
263- this . instrumentations . push ( instrumentation )
264- }
265-
266- const anthropic = this . options . instrumentations ?. anthropic
267- if ( anthropic ) {
268- const provider = this . tracerProvider ( Instrumentation . Anthropic )
269- const instrumentation = new AnthropicInstrumentation ( )
270- instrumentation . setTracerProvider ( provider )
271- instrumentation . manuallyInstrument ( anthropic )
272- registerInstrumentations ( {
273- instrumentations : [ instrumentation ] ,
274- tracerProvider : provider ,
275- } )
276- this . instrumentations . push ( instrumentation )
277- }
278-
279- const azure = this . options . instrumentations ?. azure
280- if ( azure ) {
281- const provider = this . tracerProvider ( Instrumentation . AzureOpenAI )
282- const instrumentation = new AzureOpenAIInstrumentation ( )
283- instrumentation . setTracerProvider ( provider )
284- instrumentation . manuallyInstrument ( azure )
285- registerInstrumentations ( {
286- instrumentations : [ instrumentation ] ,
287- tracerProvider : provider ,
288- } )
289- this . instrumentations . push ( instrumentation )
290- }
291-
292- const vertexai = this . options . instrumentations ?. vertexai
293- if ( vertexai ) {
294- const provider = this . tracerProvider ( Instrumentation . VertexAI )
295- const instrumentation = new VertexAIInstrumentation ( )
296- instrumentation . setTracerProvider ( provider )
297- instrumentation . manuallyInstrument ( vertexai )
298- registerInstrumentations ( {
299- instrumentations : [ instrumentation ] ,
300- tracerProvider : provider ,
301- } )
302- this . instrumentations . push ( instrumentation )
303- }
304-
305- const aiplatform = this . options . instrumentations ?. aiplatform
306- if ( aiplatform ) {
307- const provider = this . tracerProvider ( Instrumentation . AIPlatform )
308- const instrumentation = new AIPlatformInstrumentation ( )
235+ type InstrumentationClass =
236+ | typeof AnthropicInstrumentation
237+ | typeof AIPlatformInstrumentation
238+ | typeof BedrockInstrumentation
239+ | typeof CohereInstrumentation
240+ | typeof LangChainInstrumentation
241+ | typeof LlamaIndexInstrumentation
242+ | typeof OpenAIInstrumentation
243+ | typeof TogetherInstrumentation
244+ | typeof VertexAIInstrumentation
245+
246+ const configureInstrumentation = (
247+ instrumentationType : Instrumentation ,
248+ InstrumentationConstructor : InstrumentationClass ,
249+ instrumentationOptions ?: { enrichTokens ?: boolean } ,
250+ ) => {
251+ const providerPkg = this . options . instrumentations ?. [ instrumentationType ]
252+ const provider = this . tracerProvider ( instrumentationType )
253+ const instrumentation = new InstrumentationConstructor ( instrumentationOptions ) // prettier-ignore
309254 instrumentation . setTracerProvider ( provider )
310- instrumentation . manuallyInstrument ( aiplatform )
255+ if ( providerPkg ) {
256+ instrumentation . manuallyInstrument ( providerPkg )
257+ }
311258 registerInstrumentations ( {
312259 instrumentations : [ instrumentation ] ,
313260 tracerProvider : provider ,
314261 } )
315262 this . instrumentations . push ( instrumentation )
316263 }
317264
318- const bedrock = this . options . instrumentations ?. bedrock
319- if ( bedrock ) {
320- const provider = this . tracerProvider ( Instrumentation . Bedrock )
321- const instrumentation = new BedrockInstrumentation ( )
322- instrumentation . setTracerProvider ( provider )
323- instrumentation . manuallyInstrument ( bedrock )
324- registerInstrumentations ( {
325- instrumentations : [ instrumentation ] ,
326- tracerProvider : provider ,
327- } )
328- this . instrumentations . push ( instrumentation )
329- }
330-
331- const togetherai = this . options . instrumentations ?. togetherai
332- if ( togetherai ) {
333- const provider = this . tracerProvider ( Instrumentation . TogetherAI )
334- const instrumentation = new TogetherInstrumentation ( {
335- enrichTokens : true ,
336- } )
337- instrumentation . setTracerProvider ( provider )
338- instrumentation . manuallyInstrument ( togetherai )
339- registerInstrumentations ( {
340- instrumentations : [ instrumentation ] ,
341- tracerProvider : provider ,
342- } )
343- this . instrumentations . push ( instrumentation )
344- }
345-
346- const cohere = this . options . instrumentations ?. cohere
347- if ( cohere ) {
348- const provider = this . tracerProvider ( Instrumentation . Cohere )
349- const instrumentation = new CohereInstrumentation ( )
350- instrumentation . setTracerProvider ( provider )
351- instrumentation . manuallyInstrument ( cohere )
352- registerInstrumentations ( {
353- instrumentations : [ instrumentation ] ,
354- tracerProvider : provider ,
355- } )
356- this . instrumentations . push ( instrumentation )
357- }
358-
359- const langchain = this . options . instrumentations ?. langchain
360- if ( langchain ) {
361- const provider = this . tracerProvider ( Instrumentation . Langchain )
362- const instrumentation = new LangChainInstrumentation ( )
363- instrumentation . setTracerProvider ( provider )
364- instrumentation . manuallyInstrument ( langchain )
365- registerInstrumentations ( {
366- instrumentations : [ instrumentation ] ,
367- tracerProvider : provider ,
368- } )
369- this . instrumentations . push ( instrumentation )
370- }
371-
372- const llamaindex = this . options . instrumentations ?. llamaindex
373- if ( llamaindex ) {
374- const provider = this . tracerProvider ( Instrumentation . LlamaIndex )
375- const instrumentation = new LlamaIndexInstrumentation ( )
376- instrumentation . setTracerProvider ( provider )
377- instrumentation . manuallyInstrument ( llamaindex )
378- registerInstrumentations ( {
379- instrumentations : [ instrumentation ] ,
380- tracerProvider : provider ,
381- } )
382- this . instrumentations . push ( instrumentation )
383- }
265+ configureInstrumentation ( Instrumentation . Anthropic , AnthropicInstrumentation ) // prettier-ignore
266+ configureInstrumentation ( Instrumentation . AIPlatform , AIPlatformInstrumentation ) // prettier-ignore
267+ configureInstrumentation ( Instrumentation . Bedrock , BedrockInstrumentation ) // prettier-ignore
268+ configureInstrumentation ( Instrumentation . Cohere , CohereInstrumentation ) // prettier-ignore
269+ configureInstrumentation ( Instrumentation . Langchain , LangChainInstrumentation ) // prettier-ignore
270+ configureInstrumentation ( Instrumentation . LlamaIndex , LlamaIndexInstrumentation ) // prettier-ignore
271+ configureInstrumentation ( Instrumentation . OpenAI , OpenAIInstrumentation , { enrichTokens : true } ) // prettier-ignore
272+ configureInstrumentation ( Instrumentation . TogetherAI , TogetherInstrumentation , { enrichTokens : true } ) // prettier-ignore
273+ configureInstrumentation ( Instrumentation . VertexAI , VertexAIInstrumentation ) // prettier-ignore
384274 }
385275
386276 instrument ( ) {
0 commit comments