Skip to content

Commit 274ffe1

Browse files
committed
Upgrade JS telemetry instrumentation
1 parent 6eca1e7 commit 274ffe1

File tree

4 files changed

+72
-191
lines changed

4 files changed

+72
-191
lines changed

packages/constants/src/tracing/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export enum InstrumentationScope {
1212
Latitude = 'latitude',
1313
OpenAI = 'openai',
1414
Anthropic = 'anthropic',
15-
AzureOpenAI = 'azure',
15+
AzureOpenAI = 'azure', // Only python — js uses OpenAI instrumentation
1616
VercelAI = 'vercelai',
1717
VertexAI = 'vertexai',
1818
AIPlatform = 'aiplatform',

packages/telemetry/typescript/package.json

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,44 +52,34 @@
5252
"@opentelemetry/resources": "catalog:",
5353
"@opentelemetry/sdk-trace-node": "catalog:",
5454
"@opentelemetry/semantic-conventions": "catalog:",
55-
"@traceloop/instrumentation-anthropic": "0.13.0",
56-
"@traceloop/instrumentation-azure": "0.13.0",
57-
"@traceloop/instrumentation-bedrock": "0.13.0",
58-
"@traceloop/instrumentation-cohere": "0.13.0",
59-
"@traceloop/instrumentation-langchain": "0.13.0",
60-
"@traceloop/instrumentation-llamaindex": "0.13.0",
61-
"@traceloop/instrumentation-openai": "0.13.0",
62-
"@traceloop/instrumentation-together": "0.13.0",
63-
"@traceloop/instrumentation-vertexai": "0.13.0",
55+
"@traceloop/instrumentation-anthropic": "0.22.2",
56+
"@traceloop/instrumentation-bedrock": "0.22.2",
57+
"@traceloop/instrumentation-cohere": "0.22.2",
58+
"@traceloop/instrumentation-langchain": "0.22.2",
59+
"@traceloop/instrumentation-llamaindex": "0.22.2",
60+
"@traceloop/instrumentation-openai": "0.22.2",
61+
"@traceloop/instrumentation-together": "0.22.2",
62+
"@traceloop/instrumentation-vertexai": "0.22.2",
6463
"date-fns": "^4.1.0",
65-
"uuid": "11.1.0",
64+
"uuid": "13.0.0",
6665
"zod": "catalog:"
6766
},
6867
"devDependencies": {
69-
"@anthropic-ai/sdk": "0.62.0",
70-
"@aws-sdk/client-bedrock-runtime": "^3.830.0",
71-
"@azure/openai": "^2.0.0",
72-
"@google-cloud/vertexai": "^1.10.0",
73-
"@langchain/core": "^0.3.57",
7468
"@latitude-data/constants": "workspace:*",
7569
"@latitude-data/eslint-config": "workspace:*",
7670
"@latitude-data/sdk": "workspace:*",
7771
"@latitude-data/typescript-config": "workspace:*",
78-
"@rollup/plugin-alias": "^5.1.1",
79-
"@rollup/plugin-replace": "^6.0.2",
80-
"@rollup/plugin-typescript": "^11.1.6",
81-
"@types/node": "^22.15.21",
82-
"@types/uuid": "^10.0.0",
72+
"@rollup/plugin-alias": "^6.0.0",
73+
"@rollup/plugin-replace": "^6.0.3",
74+
"@rollup/plugin-typescript": "^12.3.0",
75+
"@types/node": "^24.10.1",
76+
"@types/uuid": "^11.0.0",
8377
"ai": "catalog:",
84-
"cohere-ai": "^7.17.1",
85-
"langchain": "^0.3.27",
86-
"msw": "^2.3.5",
87-
"openai": "^5.22.0",
78+
"msw": "^2.12.3",
8879
"promptl-ai": "catalog:",
89-
"rollup": "^4.41.0",
90-
"rollup-plugin-dts": "^6.2.1",
91-
"together-ai": "^0.16.0",
92-
"vitest": "^3.1.4"
80+
"rollup": "^4.53.3",
81+
"rollup-plugin-dts": "^6.3.0",
82+
"vitest": "^4.0.15"
9383
},
9484
"peerDependencies": {
9585
"typescript": "^5.5.4"

packages/telemetry/typescript/src/sdk/redact.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export const DEFAULT_REDACT_SPAN_PROCESSOR = () =>
7070
attributes: [
7171
/^.*auth.*$/i,
7272
/^.*authorization.*$/i,
73+
/^(?!ai\.).*usage.*$/i,
7374
/^(?!gen_ai\.).*token.*$/i,
7475
/^.*secret.*$/i,
7576
/^.*key.*$/i,

packages/telemetry/typescript/src/sdk/sdk.ts

Lines changed: 52 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import {
4040
} from '@opentelemetry/sdk-trace-node'
4141
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
4242
import { AnthropicInstrumentation } from '@traceloop/instrumentation-anthropic'
43-
import { AzureOpenAIInstrumentation } from '@traceloop/instrumentation-azure'
4443
import { BedrockInstrumentation } from '@traceloop/instrumentation-bedrock'
4544
import { CohereInstrumentation } from '@traceloop/instrumentation-cohere'
4645
import { 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'
6153
import 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

6955
const TRACES_URL = `${env.GATEWAY_BASE_URL}/api/v3/traces`
7056
const 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
10389
export 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

118102
export 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

147129
export 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

Comments
 (0)