Skip to content

Commit e46116e

Browse files
committed
fix: proper tracer fetcing in llamaindex and langchain
1 parent 795710d commit e46116e

File tree

6 files changed

+27
-22
lines changed

6 files changed

+27
-22
lines changed

packages/instrumentation-langchain/src/instrumentation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
8585
moduleExports.RetrievalQAChain.prototype,
8686
"_call",
8787
workflowWrapper(
88-
this.tracer,
88+
() => this.tracer,
8989
this._shouldSendPrompts(),
9090
"retrieval_qa.workflow",
9191
),
9292
);
9393
this._wrap(
9494
moduleExports.BaseChain.prototype,
9595
"call",
96-
taskWrapper(this.tracer, this._shouldSendPrompts()),
96+
taskWrapper(() => this.tracer, this._shouldSendPrompts()),
9797
);
9898
return moduleExports;
9999
}
@@ -109,7 +109,7 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
109109
moduleExports.AgentExecutor.prototype,
110110
"_call",
111111
workflowWrapper(
112-
this.tracer,
112+
() => this.tracer,
113113
this._shouldSendPrompts(),
114114
"langchain.agent",
115115
),
@@ -127,7 +127,7 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
127127
this._wrap(
128128
moduleExports.Tool.prototype,
129129
"call",
130-
taskWrapper(this.tracer, this._shouldSendPrompts()),
130+
taskWrapper(() => this.tracer, this._shouldSendPrompts()),
131131
);
132132
return moduleExports;
133133
}

packages/instrumentation-langchain/src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import {
66
} from "@traceloop/ai-semantic-conventions";
77

88
export function genericWrapper(
9-
tracer: Tracer,
9+
tracer: () => Tracer,
1010
shouldSendPrompts: boolean,
1111
spanKind: TraceloopSpanKindValues,
1212
spanName?: string,
1313
) {
1414
// eslint-disable-next-line @typescript-eslint/ban-types
1515
return (original: Function) => {
1616
return function method(this: any, ...args: unknown[]) {
17-
const span = tracer.startSpan(
17+
const span = tracer().startSpan(
1818
spanName || `langchain.${spanKind}.${this.constructor.name}`,
1919
);
2020
span.setAttribute(SpanAttributes.TRACELOOP_SPAN_KIND, spanKind);
@@ -97,7 +97,7 @@ export function genericWrapper(
9797
}
9898

9999
export function taskWrapper(
100-
tracer: Tracer,
100+
tracer: () => Tracer,
101101
shouldSendPrompts: boolean,
102102
spanName?: string,
103103
) {
@@ -110,7 +110,7 @@ export function taskWrapper(
110110
}
111111

112112
export function workflowWrapper(
113-
tracer: Tracer,
113+
tracer: () => Tracer,
114114
shouldSendPrompts: boolean,
115115
spanName: string,
116116
) {

packages/instrumentation-llamaindex/src/custom-llm-instrumentation.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ type AsyncResponseType =
2525

2626
export class CustomLLMInstrumentation {
2727
private config: LlamaIndexInstrumentationConfig;
28-
private tracer: Tracer;
28+
private tracer: () => Tracer;
2929

30-
constructor(config: LlamaIndexInstrumentationConfig, tracer: Tracer) {
30+
constructor(config: LlamaIndexInstrumentationConfig, tracer: () => Tracer) {
3131
this.config = config;
3232
this.tracer = tracer;
3333
}
@@ -42,10 +42,11 @@ export class CustomLLMInstrumentation {
4242
const messages = params?.messages;
4343
const streaming = params?.stream;
4444

45-
const span = plugin.tracer.startSpan(
46-
`llamaindex.${lodash.snakeCase(className)}.chat`,
47-
{ kind: SpanKind.CLIENT },
48-
);
45+
const span = plugin
46+
.tracer()
47+
.startSpan(`llamaindex.${lodash.snakeCase(className)}.chat`, {
48+
kind: SpanKind.CLIENT,
49+
});
4950

5051
span.setAttribute(SpanAttributes.LLM_VENDOR, className);
5152
span.setAttribute(

packages/instrumentation-llamaindex/src/instrumentation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
9191

9292
const customLLMInstrumentation = new CustomLLMInstrumentation(
9393
this._config,
94-
this.tracer,
94+
() => this.tracer, // this is on purpose. Tracer may change
9595
);
9696

9797
this._wrap(
@@ -101,7 +101,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
101101
moduleExports.RetrieverQueryEngine.name,
102102
"query",
103103
TraceloopSpanKindValues.WORKFLOW,
104-
this.tracer,
104+
() => this.tracer,
105105
shouldSendPrompts(this._config),
106106
),
107107
);
@@ -113,7 +113,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
113113
moduleExports.ContextChatEngine.name,
114114
"chat",
115115
TraceloopSpanKindValues.WORKFLOW,
116-
this.tracer,
116+
() => this.tracer,
117117
shouldSendPrompts(this._config),
118118
),
119119
);
@@ -134,7 +134,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
134134
cls.name,
135135
"getQueryEmbedding",
136136
TraceloopSpanKindValues.TASK,
137-
this.tracer,
137+
() => this.tracer,
138138
shouldSendPrompts(this._config),
139139
),
140140
);
@@ -146,7 +146,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
146146
cls.name,
147147
"synthesize",
148148
TraceloopSpanKindValues.TASK,
149-
this.tracer,
149+
() => this.tracer,
150150
shouldSendPrompts(this._config),
151151
),
152152
);
@@ -158,7 +158,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
158158
cls.name,
159159
"retrieve",
160160
TraceloopSpanKindValues.TASK,
161-
this.tracer,
161+
() => this.tracer,
162162
shouldSendPrompts(this._config),
163163
),
164164
);

packages/instrumentation-llamaindex/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ export function genericWrapper(
4444
className: string,
4545
methodName: string,
4646
kind: TraceloopSpanKindValues,
47-
tracer: Tracer,
47+
tracer: () => Tracer,
4848
shouldSendPrompts: boolean,
4949
) {
5050
// eslint-disable-next-line @typescript-eslint/ban-types
5151
return (original: Function) => {
5252
return function method(this: any, ...args: unknown[]) {
5353
const name = `${lodash.snakeCase(className)}.${lodash.snakeCase(methodName)}`;
54-
const span = tracer.startSpan(`${name}`);
54+
const span = tracer().startSpan(`${name}`, {}, context.active());
5555
span.setAttribute(SpanAttributes.TRACELOOP_SPAN_KIND, kind);
5656

5757
if (kind === TraceloopSpanKindValues.WORKFLOW) {

packages/traceloop-sdk/src/lib/node-server-sdk.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { initInstrumentations } from "./tracing";
2+
13
export * from "./errors";
24
export { InitializeOptions } from "./interfaces";
35
export { initialize } from "./configuration";
@@ -6,3 +8,5 @@ export * from "./tracing/decorators";
68
export * from "./tracing/association";
79
export * from "./tracing/score";
810
export * from "./prompts";
11+
12+
initInstrumentations();

0 commit comments

Comments
 (0)