Skip to content

Commit 82d82bf

Browse files
authored
fix: log instrumentation patching (#148)
1 parent 8287a02 commit 82d82bf

File tree

14 files changed

+198
-62
lines changed

14 files changed

+198
-62
lines changed

packages/instrumentation-azure/src/instrumentation.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export class AzureOpenAIInstrumentation extends InstrumentationBase<any> {
5151
}
5252

5353
public manuallyInstrument(module: typeof azure) {
54+
this._diag.debug(`Patching @azure/openai manually`);
55+
5456
this._wrap(
5557
module.OpenAIClient.prototype,
5658
"getChatCompletions",
@@ -74,7 +76,9 @@ export class AzureOpenAIInstrumentation extends InstrumentationBase<any> {
7476
return module;
7577
}
7678

77-
private patch(moduleExports: typeof azure) {
79+
private patch(moduleExports: typeof azure, moduleVersion?: string) {
80+
this._diag.debug(`Patching @azure/openai@${moduleVersion}`);
81+
7882
this._wrap(
7983
moduleExports.OpenAIClient.prototype,
8084
"getChatCompletions",
@@ -88,7 +92,9 @@ export class AzureOpenAIInstrumentation extends InstrumentationBase<any> {
8892
return moduleExports;
8993
}
9094

91-
private unpatch(moduleExports: typeof azure): void {
95+
private unpatch(moduleExports: typeof azure, moduleVersion?: string): void {
96+
this._diag.debug(`Unpatching @azure/openai@${moduleVersion}`);
97+
9298
this._unwrap(moduleExports.OpenAIClient.prototype, "getChatCompletions");
9399
this._unwrap(moduleExports.OpenAIClient.prototype, "getCompletions");
94100
}
@@ -127,8 +133,11 @@ export class AzureOpenAIInstrumentation extends InstrumentationBase<any> {
127133
return original.apply(this, args);
128134
});
129135
},
130-
// eslint-disable-next-line @typescript-eslint/no-empty-function
131-
() => {},
136+
(e) => {
137+
if (e) {
138+
plugin._diag.error("Error in Azure OpenAI instrumentation", e);
139+
}
140+
},
132141
);
133142

134143
const wrappedPromise = plugin._wrapPromise(

packages/instrumentation-bedrock/src/instrumentation.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@ export class BedrockInstrumentation extends InstrumentationBase<any> {
5858
}
5959

6060
public manuallyInstrument(module: typeof bedrock) {
61+
this._diag.debug(`Patching @aws-sdk/client-bedrock-runtime manually`);
62+
6163
this._wrap(
6264
module.BedrockRuntimeClient.prototype,
6365
"send",
6466
this.wrapperMethod(),
6567
);
6668
}
6769

68-
private wrap(module: typeof bedrock) {
70+
private wrap(module: typeof bedrock, moduleVersion?: string) {
71+
this._diag.debug(
72+
`Patching @aws-sdk/client-bedrock-runtime@${moduleVersion}`,
73+
);
74+
6975
this._wrap(
7076
module.BedrockRuntimeClient.prototype,
7177
"send",
@@ -75,7 +81,11 @@ export class BedrockInstrumentation extends InstrumentationBase<any> {
7581
return module;
7682
}
7783

78-
private unwrap(module: typeof bedrock) {
84+
private unwrap(module: typeof bedrock, moduleVersion?: string) {
85+
this._diag.debug(
86+
`Unpatching @aws-sdk/client-bedrock-runtime@${moduleVersion}`,
87+
);
88+
7989
this._unwrap(module.BedrockRuntimeClient.prototype, "send");
8090
}
8191

@@ -95,8 +105,11 @@ export class BedrockInstrumentation extends InstrumentationBase<any> {
95105
return original.apply(this, args);
96106
});
97107
},
98-
// eslint-disable-next-line @typescript-eslint/no-empty-function
99-
() => {},
108+
(e) => {
109+
if (e) {
110+
plugin._diag.error(`Error in bedrock instrumentation`, e);
111+
}
112+
},
100113
);
101114
const wrappedPromise = plugin._wrapPromise(span, execPromise);
102115
return context.bind(execContext, wrappedPromise);

packages/instrumentation-cohere/src/instrumentation.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ export class CohereInstrumentation extends InstrumentationBase<any> {
6060
}
6161

6262
public manuallyInstrument(module: typeof cohere) {
63+
this._diag.debug(`Manually patching cohere-ai`);
6364
this.wrap(module);
6465
}
6566

66-
private wrap(module: typeof cohere) {
67+
private wrap(module: typeof cohere, moduleVersion?: string) {
68+
this._diag.debug(`Patching cohere-ai@${moduleVersion}`);
69+
6770
this._wrap(
6871
module.CohereClient.prototype,
6972
"generate",
@@ -93,7 +96,9 @@ export class CohereInstrumentation extends InstrumentationBase<any> {
9396
return module;
9497
}
9598

96-
private unwrap(module: typeof cohere) {
99+
private unwrap(module: typeof cohere, moduleVersion?: string) {
100+
this._diag.debug(`Unpatching @cohere-ai@${moduleVersion}`);
101+
97102
this._unwrap(module.CohereClient.prototype, "generateStream");
98103
this._unwrap(module.CohereClient.prototype, "chat");
99104
this._unwrap(module.CohereClient.prototype, "chatStream");
@@ -117,8 +122,11 @@ export class CohereInstrumentation extends InstrumentationBase<any> {
117122
return original.apply(this, args);
118123
});
119124
},
120-
// eslint-disable-next-line @typescript-eslint/no-empty-function
121-
() => {},
125+
(e) => {
126+
if (e) {
127+
plugin._diag.error("Error in cohere instrumentation", e);
128+
}
129+
},
122130
);
123131
const wrappedPromise = plugin._wrapPromise(type, span, execPromise);
124132
return context.bind(execContext, wrappedPromise as any);

packages/instrumentation-langchain/src/instrumentation.ts

+37-7
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
4242
toolsModule?: any;
4343
}) {
4444
if (chainsModule) {
45+
this._diag.debug("Manually instrumenting langchain chains");
4546
this.patchChainModule(chainsModule);
4647
}
4748
if (agentsModule) {
49+
this._diag.debug("Manually instrumenting langchain agents");
4850
this.patchAgentModule(agentsModule);
4951
}
5052
if (toolsModule) {
53+
this._diag.debug("Manually instrumenting langchain tools");
5154
this.patchToolsModule(toolsModule);
5255
}
5356
}
@@ -74,7 +77,12 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
7477
return [chainModule, agentModule, toolsModule];
7578
}
7679

77-
private patchChainModule(moduleExports: typeof ChainsModule) {
80+
private patchChainModule(
81+
moduleExports: typeof ChainsModule,
82+
moduleVersion?: string,
83+
) {
84+
this._diag.debug(`Patching langchain/chains.cjs@${moduleVersion}`);
85+
7886
this._wrap(
7987
moduleExports.RetrievalQAChain.prototype,
8088
"_call",
@@ -92,7 +100,12 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
92100
return moduleExports;
93101
}
94102

95-
private patchAgentModule(moduleExports: typeof AgentsModule) {
103+
private patchAgentModule(
104+
moduleExports: typeof AgentsModule,
105+
moduleVersion?: string,
106+
) {
107+
this._diag.debug(`Patching langchain/agents.cjs@${moduleVersion}`);
108+
96109
this._wrap(
97110
moduleExports.AgentExecutor.prototype,
98111
"_call",
@@ -105,7 +118,12 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
105118
return moduleExports;
106119
}
107120

108-
private patchToolsModule(moduleExports: typeof ToolsModule) {
121+
private patchToolsModule(
122+
moduleExports: typeof ToolsModule,
123+
moduleVersion?: string,
124+
) {
125+
this._diag.debug(`Patching langchain/tools.cjs@${moduleVersion}`);
126+
109127
this._wrap(
110128
moduleExports.Tool.prototype,
111129
"call",
@@ -114,19 +132,31 @@ export class LangChainInstrumentation extends InstrumentationBase<any> {
114132
return moduleExports;
115133
}
116134

117-
private unpatchChainModule(moduleExports: any) {
135+
private unpatchChainModule(
136+
moduleExports: typeof ChainsModule,
137+
moduleVersion?: string,
138+
) {
139+
this._diag.debug(`Unpatching langchain/chains.cjs@${moduleVersion}`);
140+
118141
this._unwrap(moduleExports.RetrievalQAChain.prototype, "_call");
119142
this._unwrap(moduleExports.BaseChain.prototype, "call");
120143
return moduleExports;
121144
}
122145

123-
private unpatchAgentModule(moduleExports: any) {
146+
private unpatchAgentModule(
147+
moduleExports: typeof AgentsModule,
148+
moduleVersion?: string,
149+
) {
150+
this._diag.debug(`Unpatching langchain/agents.cjs@${moduleVersion}`);
151+
124152
this._unwrap(moduleExports.AgentExecutor.prototype, "_call");
125153
return moduleExports;
126154
}
127155

128-
private unpatchToolsModule(moduleExports: any) {
129-
this._unwrap(moduleExports.AgentExecutor.prototype, "_call");
156+
private unpatchToolsModule(moduleExports: typeof ToolsModule) {
157+
this._diag.debug(`Unpatching langchain/tools.cjs`);
158+
159+
this._unwrap(moduleExports.Tool.prototype, "call");
130160
return moduleExports;
131161
}
132162

packages/instrumentation-llamaindex/src/instrumentation.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
4040
}
4141

4242
public manuallyInstrument(module: typeof llamaindex) {
43+
this._diag.debug("Manually instrumenting llamaindex");
44+
4345
this.patch(module);
4446
}
4547

@@ -78,7 +80,9 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
7880
return retriever && (retriever as BaseRetriever).retrieve !== undefined;
7981
}
8082

81-
private patch(moduleExports: typeof llamaindex) {
83+
private patch(moduleExports: typeof llamaindex, moduleVersion?: string) {
84+
this._diag.debug(`Patching llamaindex@${moduleVersion}`);
85+
8286
const customLLMInstrumentation = new CustomLLMInstrumentation(
8387
this._config,
8488
() => this.tracer, // this is on purpose. Tracer may change
@@ -158,7 +162,9 @@ export class LlamaIndexInstrumentation extends InstrumentationBase<any> {
158162
return moduleExports;
159163
}
160164

161-
private unpatch(moduleExports: typeof llamaindex) {
165+
private unpatch(moduleExports: typeof llamaindex, moduleVersion?: string) {
166+
this._diag.debug(`Unpatching llamaindex@${moduleVersion}`);
167+
162168
this._unwrap(moduleExports.RetrieverQueryEngine.prototype, "query");
163169

164170
for (const key in moduleExports) {

packages/instrumentation-openai/src/instrumentation.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class OpenAIInstrumentation extends InstrumentationBase<any> {
5656
}
5757

5858
public manuallyInstrument(module: typeof openai.OpenAI) {
59+
this._diag.debug(`Manually instrumenting openai`);
60+
5961
// Old version of OpenAI API (v3.1.0)
6062
if ((module as any).OpenAIApi) {
6163
this._wrap(
@@ -92,7 +94,9 @@ export class OpenAIInstrumentation extends InstrumentationBase<any> {
9294
return module;
9395
}
9496

95-
private patch(moduleExports: typeof openai) {
97+
private patch(moduleExports: typeof openai, moduleVersion?: string) {
98+
this._diag.debug(`Patching openai@${moduleVersion}`);
99+
96100
// Old version of OpenAI API (v3.1.0)
97101
if ((moduleExports as any).OpenAIApi) {
98102
this._wrap(
@@ -120,7 +124,9 @@ export class OpenAIInstrumentation extends InstrumentationBase<any> {
120124
return moduleExports;
121125
}
122126

123-
private unpatch(moduleExports: typeof openai): void {
127+
private unpatch(moduleExports: typeof openai, moduleVersion?: string): void {
128+
this._diag.debug(`Unpatching openai@${moduleVersion}`);
129+
124130
// Old version of OpenAI API (v3.1.0)
125131
if ((moduleExports as any).OpenAIApi) {
126132
this._unwrap(
@@ -171,8 +177,11 @@ export class OpenAIInstrumentation extends InstrumentationBase<any> {
171177
return original.apply(this, args);
172178
});
173179
},
174-
// eslint-disable-next-line @typescript-eslint/no-empty-function
175-
() => {},
180+
(e) => {
181+
if (e) {
182+
plugin._diag.error("OpenAI instrumentation: error", e);
183+
}
184+
},
176185
);
177186

178187
if (

packages/instrumentation-pinecone/src/instrumentation.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export class PineconeInstrumentation extends InstrumentationBase<any> {
4747
return module;
4848
}
4949

50-
private patch(moduleExports: typeof pinecone) {
50+
private patch(moduleExports: typeof pinecone, moduleVersion?: string) {
51+
this._diag.debug(`Patching @pinecone-database/pinecone@${moduleVersion}`);
52+
5153
this._wrap(
5254
moduleExports.Index.prototype,
5355
"query",
@@ -77,7 +79,12 @@ export class PineconeInstrumentation extends InstrumentationBase<any> {
7779
return moduleExports;
7880
}
7981

80-
private unpatch(moduleExports: typeof pinecone): void {
82+
private unpatch(
83+
moduleExports: typeof pinecone,
84+
moduleVersion?: string,
85+
): void {
86+
this._diag.debug(`Unpatching @pinecone-database/pinecone@${moduleVersion}`);
87+
8188
this._unwrap(moduleExports.Index.prototype, "query");
8289
this._unwrap(moduleExports.Index.prototype, "upsert");
8390
this._unwrap(moduleExports.Index.prototype, "deleteAll");
@@ -86,6 +93,9 @@ export class PineconeInstrumentation extends InstrumentationBase<any> {
8693
}
8794

8895
private genericWrapper(methodName: string, tracer: Tracer) {
96+
// eslint-disable-next-line @typescript-eslint/no-this-alias
97+
const plugin = this;
98+
8999
// eslint-disable-next-line @typescript-eslint/ban-types
90100
return (original: Function) => {
91101
return function method(this: any, ...args: unknown[]) {
@@ -98,8 +108,9 @@ export class PineconeInstrumentation extends InstrumentationBase<any> {
98108
return original.apply(this, args);
99109
});
100110
},
101-
// eslint-disable-next-line @typescript-eslint/no-empty-function
102-
() => {},
111+
(e) => {
112+
plugin._diag.error(`Error in Pinecone instrumentation`, e);
113+
},
103114
);
104115
const wrappedPromise = execPromise
105116
.then((result: any) => {
@@ -125,6 +136,9 @@ export class PineconeInstrumentation extends InstrumentationBase<any> {
125136
}
126137

127138
private queryWrapper(tracer: Tracer) {
139+
// eslint-disable-next-line @typescript-eslint/no-this-alias
140+
const plugin = this;
141+
128142
// eslint-disable-next-line @typescript-eslint/ban-types
129143
return (original: Function) => {
130144
return function method(this: any, ...args: unknown[]) {
@@ -164,8 +178,11 @@ export class PineconeInstrumentation extends InstrumentationBase<any> {
164178
return original.apply(this, args);
165179
});
166180
},
167-
// eslint-disable-next-line @typescript-eslint/no-empty-function
168-
() => {},
181+
(e) => {
182+
if (e) {
183+
plugin._diag.error(`Error in Pinecone instrumentation`, e);
184+
}
185+
},
169186
);
170187
const wrappedPromise = execPromise
171188
.then((result: any) => {

0 commit comments

Comments
 (0)