@arizeai/openinference-instrumentation-openai 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,8 @@ import {
14
14
  Attributes,
15
15
  SpanStatusCode,
16
16
  Span,
17
+ TracerProvider,
18
+ Tracer,
17
19
  } from "@opentelemetry/api";
18
20
  import { VERSION } from "./version";
19
21
  import {
@@ -60,6 +62,8 @@ import {
60
62
 
61
63
  const MODULE_NAME = "openai";
62
64
 
65
+ const INSTRUMENTATION_NAME = "@arizeai/openinference-instrumentation-openai";
66
+
63
67
  /**
64
68
  * Flag to check if the openai module has been patched
65
69
  * Note: This is a fallback in case the module is made immutable (e.x. Deno, webpack, etc.)
@@ -97,9 +101,12 @@ function getExecContext(span: Span) {
97
101
  */
98
102
  export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
99
103
  private oiTracer: OITracer;
104
+ private tracerProvider?: TracerProvider;
105
+ private traceConfig?: TraceConfigOptions;
100
106
  constructor({
101
107
  instrumentationConfig,
102
108
  traceConfig,
109
+ tracerProvider,
103
110
  }: {
104
111
  /**
105
112
  * The config for the instrumentation
@@ -111,13 +118,27 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
111
118
  * @see {@link TraceConfigOptions}
112
119
  */
113
120
  traceConfig?: TraceConfigOptions;
121
+ /**
122
+ * An optional custom trace provider to be used for tracing. If not provided, a tracer will be created using the global tracer provider.
123
+ * This is useful if you want to use a non-global tracer provider.
124
+ *
125
+ * @see {@link TracerProvider}
126
+ */
127
+ tracerProvider?: TracerProvider;
114
128
  } = {}) {
115
129
  super(
116
- "@arizeai/openinference-instrumentation-openai",
130
+ INSTRUMENTATION_NAME,
117
131
  VERSION,
118
132
  Object.assign({}, instrumentationConfig),
119
133
  );
120
- this.oiTracer = new OITracer({ tracer: this.tracer, traceConfig });
134
+ this.tracerProvider = tracerProvider;
135
+ this.traceConfig = traceConfig;
136
+ this.oiTracer = new OITracer({
137
+ tracer:
138
+ this.tracerProvider?.getTracer(INSTRUMENTATION_NAME, VERSION) ??
139
+ this.tracer,
140
+ traceConfig,
141
+ });
121
142
  }
122
143
 
123
144
  protected init(): InstrumentationModuleDefinition<typeof openai> {
@@ -139,6 +160,25 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
139
160
  this.patch(module);
140
161
  }
141
162
 
163
+ get tracer(): Tracer {
164
+ if (this.tracerProvider) {
165
+ return this.tracerProvider.getTracer(
166
+ this.instrumentationName,
167
+ this.instrumentationVersion,
168
+ );
169
+ }
170
+ return super.tracer;
171
+ }
172
+
173
+ setTracerProvider(tracerProvider: TracerProvider): void {
174
+ super.setTracerProvider(tracerProvider);
175
+ this.tracerProvider = tracerProvider;
176
+ this.oiTracer = new OITracer({
177
+ tracer: this.tracer,
178
+ traceConfig: this.traceConfig,
179
+ });
180
+ }
181
+
142
182
  /**
143
183
  * Patches the OpenAI module
144
184
  */
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // this is an auto-generated file. see js/scripts/version-update.js
2
- export const VERSION = "3.1.0";
2
+ export const VERSION = "3.2.0";