@@ -36,19 +36,29 @@ import { version } from "../package.json";
3636
3737export class LlamaIndexInstrumentation extends InstrumentationBase {
3838 declare protected _config : LlamaIndexInstrumentationConfig ;
39+ private customLLMInstrumentation ! : CustomLLMInstrumentation ;
3940
4041 constructor ( config : LlamaIndexInstrumentationConfig = { } ) {
4142 super ( "@traceloop/instrumentation-llamaindex" , version , config ) ;
43+ this . customLLMInstrumentation = new CustomLLMInstrumentation (
44+ this . _config ,
45+ this . _diag ,
46+ ( ) => this . tracer ,
47+ ) ;
4248 }
4349
4450 public override setConfig ( config : LlamaIndexInstrumentationConfig = { } ) {
4551 super . setConfig ( config ) ;
4652 }
4753
48- public manuallyInstrument ( module : typeof llamaindex ) {
54+ public manuallyInstrument ( module : typeof llamaindex , openaiModule ?: any ) {
4955 this . _diag . debug ( "Manually instrumenting llamaindex" ) ;
5056
5157 this . patch ( module ) ;
58+
59+ if ( openaiModule ) {
60+ this . patchOpenAI ( openaiModule ) ;
61+ }
5262 }
5363
5464 protected init ( ) : InstrumentationModuleDefinition [ ] {
@@ -94,12 +104,6 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
94104 private patch ( moduleExports : typeof llamaindex , moduleVersion ?: string ) {
95105 this . _diag . debug ( `Patching llamaindex@${ moduleVersion } ` ) ;
96106
97- const customLLMInstrumentation = new CustomLLMInstrumentation (
98- this . _config ,
99- this . _diag ,
100- ( ) => this . tracer , // this is on purpose. Tracer may change
101- ) ;
102-
103107 this . _wrap (
104108 moduleExports . RetrieverQueryEngine . prototype ,
105109 "query" ,
@@ -133,7 +137,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
133137 this . _wrap (
134138 cls . prototype ,
135139 "chat" ,
136- customLLMInstrumentation . chatWrapper ( { className : cls . name } ) ,
140+ this . customLLMInstrumentation . chatWrapper ( { className : cls . name } ) ,
137141 ) ;
138142 } else if ( this . isEmbedding ( cls . prototype ) ) {
139143 this . _wrap (
@@ -202,7 +206,16 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
202206 private patchOpenAI ( moduleExports : any , moduleVersion ?: string ) {
203207 this . _diag . debug ( `Patching @llamaindex/openai@${ moduleVersion } ` ) ;
204208
205- // Instrument OpenAIAgent if it exists
209+ if ( moduleExports . OpenAI && this . isLLM ( moduleExports . OpenAI . prototype ) ) {
210+ this . _wrap (
211+ moduleExports . OpenAI . prototype ,
212+ "chat" ,
213+ this . customLLMInstrumentation . chatWrapper ( {
214+ className : moduleExports . OpenAI . name ,
215+ } ) ,
216+ ) ;
217+ }
218+
206219 if ( moduleExports . OpenAIAgent && moduleExports . OpenAIAgent . prototype ) {
207220 this . _wrap (
208221 moduleExports . OpenAIAgent . prototype ,
@@ -223,7 +236,10 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
223236 private unpatchOpenAI ( moduleExports : any , moduleVersion ?: string ) {
224237 this . _diag . debug ( `Unpatching @llamaindex/openai@${ moduleVersion } ` ) ;
225238
226- // Unwrap OpenAIAgent if it exists
239+ if ( moduleExports . OpenAI && moduleExports . OpenAI . prototype ) {
240+ this . _unwrap ( moduleExports . OpenAI . prototype , "chat" ) ;
241+ }
242+
227243 if ( moduleExports . OpenAIAgent && moduleExports . OpenAIAgent . prototype ) {
228244 this . _unwrap ( moduleExports . OpenAIAgent . prototype , "chat" ) ;
229245 }
0 commit comments