@paid-ai/paid-node 1.0.1-alpha8 → 1.0.1

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.
@@ -48,8 +48,8 @@ class PaidClient {
48
48
  this._options = Object.assign(Object.assign({}, _options), { headers: (0, headers_js_1.mergeHeaders)({
49
49
  "X-Fern-Language": "JavaScript",
50
50
  "X-Fern-SDK-Name": "@paid-ai/paid-node",
51
- "X-Fern-SDK-Version": "1.0.1-alpha8",
52
- "User-Agent": "@paid-ai/paid-node/1.0.1-alpha8",
51
+ "X-Fern-SDK-Version": "1.0.1",
52
+ "User-Agent": "@paid-ai/paid-node/1.0.1",
53
53
  "X-Fern-Runtime": core.RUNTIME.type,
54
54
  "X-Fern-Runtime-Version": core.RUNTIME.version,
55
55
  }, _options === null || _options === void 0 ? void 0 : _options.headers) });
@@ -86,9 +86,6 @@ class AISDKSpanProcessor {
86
86
  if (!finalName.startsWith("paid.trace.")) {
87
87
  finalName = `paid.trace.${finalName}`;
88
88
  }
89
- if (!finalName.endsWith(".signal")) {
90
- finalName = `${finalName}.signal`;
91
- }
92
89
  span.name = finalName;
93
90
  }
94
91
  shutdown() {
@@ -1,8 +1,25 @@
1
- import type * as openai from "openai";
2
- import type * as anthropic from "@anthropic-ai/sdk";
3
- interface SupportedLibraries {
4
- openai?: typeof openai.OpenAI;
5
- anthropic?: typeof anthropic.Anthropic;
1
+ export type SupportedLibrary = "openai" | "anthropic";
2
+ export interface InstrumentModules {
3
+ openai?: any;
4
+ anthropic?: any;
6
5
  }
7
- export declare function paidAutoInstrument(libraries?: SupportedLibraries): Promise<void>;
8
- export {};
6
+ /**
7
+ * Auto-instrument supported libraries using require-in-the-middle hooks.
8
+ * Works in plain Node.js (CJS). Does NOT work in bundled environments (Next.js/webpack).
9
+ * For bundled environments, use {@link paidAutoInstrumentModules} instead.
10
+ *
11
+ * @param libraries - Optional list of libraries to instrument. If omitted, all available libraries are instrumented.
12
+ */
13
+ export declare function paidAutoInstrument(libraries?: SupportedLibrary[]): Promise<void>;
14
+ /**
15
+ * Instrument supported libraries by directly patching the provided module references.
16
+ * Required for bundled environments (Next.js/webpack) where require-in-the-middle hooks don't work.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import OpenAI from "openai";
21
+ * import Anthropic from "@anthropic-ai/sdk";
22
+ * await paidAutoInstrumentModules({ openAI: OpenAI, anthropic: Anthropic });
23
+ * ```
24
+ */
25
+ export declare function paidAutoInstrumentModules(modules: InstrumentModules): Promise<void>;
@@ -43,73 +43,103 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
43
43
  };
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.paidAutoInstrument = paidAutoInstrument;
46
- const instrumentation_1 = require("@opentelemetry/instrumentation");
46
+ exports.paidAutoInstrumentModules = paidAutoInstrumentModules;
47
47
  const tracing_js_1 = require("./tracing.js");
48
48
  let IS_INITIALIZED = false;
49
- const getInstrumentations = (tracerProvider) => __awaiter(void 0, void 0, void 0, function* () {
49
+ const getInstrumentations = (tracerProvider, libraries) => __awaiter(void 0, void 0, void 0, function* () {
50
50
  const instrumentations = [];
51
- try {
52
- const { OpenAIInstrumentation } = yield Promise.resolve().then(() => __importStar(require("@arizeai/openinference-instrumentation-openai")));
53
- instrumentations.push(new OpenAIInstrumentation({ tracerProvider }));
54
- }
55
- catch (_a) {
56
- tracing_js_1.logger.debug("OpenAI instrumentation not available - openai package not installed");
57
- }
58
- try {
59
- const { AnthropicInstrumentation } = yield Promise.resolve().then(() => __importStar(require("@arizeai/openinference-instrumentation-anthropic")));
60
- instrumentations.push(new AnthropicInstrumentation({ tracerProvider }));
61
- }
62
- catch (_b) {
63
- tracing_js_1.logger.debug("Anthropic instrumentation not available - @anthropic-ai/sdk package not installed");
64
- }
65
- return instrumentations;
66
- });
67
- const getManualInstrumentations = (tracerProvider, libraries) => __awaiter(void 0, void 0, void 0, function* () {
68
- const instrumentations = [];
69
- if (libraries.openai) {
51
+ const shouldInstrument = (lib) => !libraries || libraries.includes(lib);
52
+ if (shouldInstrument("openai")) {
70
53
  try {
71
54
  const { OpenAIInstrumentation } = yield Promise.resolve().then(() => __importStar(require("@arizeai/openinference-instrumentation-openai")));
72
- const openaiInstrumentation = new OpenAIInstrumentation({ tracerProvider });
73
- instrumentations.push(openaiInstrumentation);
74
- openaiInstrumentation.manuallyInstrument(libraries.openai);
55
+ instrumentations.push(new OpenAIInstrumentation({ tracerProvider }));
75
56
  }
76
57
  catch (_a) {
77
- tracing_js_1.logger.warn("Failed to load OpenAI instrumentation");
58
+ console.debug("OpenAI instrumentation not available - openai package not installed");
78
59
  }
79
60
  }
80
- if (libraries.anthropic) {
61
+ if (shouldInstrument("anthropic")) {
81
62
  try {
82
63
  const { AnthropicInstrumentation } = yield Promise.resolve().then(() => __importStar(require("@arizeai/openinference-instrumentation-anthropic")));
83
- const anthropicInstrumentation = new AnthropicInstrumentation({ tracerProvider });
84
- instrumentations.push(anthropicInstrumentation);
85
- anthropicInstrumentation.manuallyInstrument(libraries.anthropic);
64
+ instrumentations.push(new AnthropicInstrumentation({ tracerProvider }));
86
65
  }
87
66
  catch (_b) {
88
- tracing_js_1.logger.warn("Failed to load Anthropic instrumentation");
67
+ console.debug("Anthropic instrumentation not available - @anthropic-ai/sdk package not installed");
89
68
  }
90
69
  }
91
70
  return instrumentations;
92
71
  });
72
+ /**
73
+ * Auto-instrument supported libraries using require-in-the-middle hooks.
74
+ * Works in plain Node.js (CJS). Does NOT work in bundled environments (Next.js/webpack).
75
+ * For bundled environments, use {@link paidAutoInstrumentModules} instead.
76
+ *
77
+ * @param libraries - Optional list of libraries to instrument. If omitted, all available libraries are instrumented.
78
+ */
93
79
  function paidAutoInstrument(libraries) {
94
80
  return __awaiter(this, void 0, void 0, function* () {
95
81
  if (IS_INITIALIZED) {
96
- tracing_js_1.logger.info("Auto instrumentation is already initialized");
82
+ console.info("Auto instrumentation is already initialized");
97
83
  return;
98
84
  }
99
- (0, tracing_js_1.initializeTracing)(); // try initializing tracing
85
+ (0, tracing_js_1.initializeTracing)();
100
86
  const tracerProvider = (0, tracing_js_1.getPaidTracerProvider)();
101
87
  if (!tracerProvider) {
102
- tracing_js_1.logger.error("Could not get tracer provider, make sure you ran 'initializeTracing()' or check your environment variables");
88
+ console.error("Could not get tracer provider, make sure you ran 'initializeTracing()' or check your environment variables");
103
89
  return;
104
90
  }
105
- const isManualInstrumentation = libraries && Object.keys(libraries).length > 0;
106
- const instrumentations = isManualInstrumentation
107
- ? yield getManualInstrumentations(tracerProvider, libraries)
108
- : yield getInstrumentations(tracerProvider);
109
- (0, instrumentation_1.registerInstrumentations)({
91
+ const instrumentations = yield getInstrumentations(tracerProvider, libraries);
92
+ const { registerInstrumentations } = yield Promise.resolve().then(() => __importStar(require("@opentelemetry/instrumentation")));
93
+ registerInstrumentations({
110
94
  instrumentations,
111
95
  tracerProvider,
112
96
  });
113
97
  IS_INITIALIZED = true;
114
98
  });
115
99
  }
100
+ /**
101
+ * Instrument supported libraries by directly patching the provided module references.
102
+ * Required for bundled environments (Next.js/webpack) where require-in-the-middle hooks don't work.
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * import OpenAI from "openai";
107
+ * import Anthropic from "@anthropic-ai/sdk";
108
+ * await paidAutoInstrumentModules({ openAI: OpenAI, anthropic: Anthropic });
109
+ * ```
110
+ */
111
+ function paidAutoInstrumentModules(modules) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ if (IS_INITIALIZED) {
114
+ console.info("Auto instrumentation is already initialized");
115
+ return;
116
+ }
117
+ (0, tracing_js_1.initializeTracing)();
118
+ const tracerProvider = (0, tracing_js_1.getPaidTracerProvider)();
119
+ if (!tracerProvider) {
120
+ console.error("Could not get tracer provider, make sure you ran 'initializeTracing()' or check your environment variables");
121
+ return;
122
+ }
123
+ if (modules.openai) {
124
+ try {
125
+ const { OpenAIInstrumentation } = yield Promise.resolve().then(() => __importStar(require("@arizeai/openinference-instrumentation-openai")));
126
+ const inst = new OpenAIInstrumentation({ tracerProvider });
127
+ inst.manuallyInstrument(modules.openai);
128
+ }
129
+ catch (_a) {
130
+ console.debug("OpenAI instrumentation not available");
131
+ }
132
+ }
133
+ if (modules.anthropic) {
134
+ try {
135
+ const { AnthropicInstrumentation } = yield Promise.resolve().then(() => __importStar(require("@arizeai/openinference-instrumentation-anthropic")));
136
+ const inst = new AnthropicInstrumentation({ tracerProvider });
137
+ inst.manuallyInstrument(modules.anthropic);
138
+ }
139
+ catch (_b) {
140
+ console.debug("Anthropic instrumentation not available");
141
+ }
142
+ }
143
+ IS_INITIALIZED = true;
144
+ });
145
+ }
@@ -1,5 +1,5 @@
1
- import { getPaidTracer, getPaidTracerProvider, initializeTracing, trace } from "./tracing.js";
2
- import { paidAutoInstrument } from "./autoInstrumentation.js";
1
+ import { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, createPaidSpanProcessors } from "./tracing.js";
3
2
  import { signal } from "./signal.js";
4
3
  import { AISDKSpanProcessor } from "./aiSdkSpanProcessor.js";
5
- export { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, signal, paidAutoInstrument, AISDKSpanProcessor };
4
+ export type { InitializeTracingOptions } from "./tracing.js";
5
+ export { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, signal, AISDKSpanProcessor, createPaidSpanProcessors };
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AISDKSpanProcessor = exports.paidAutoInstrument = exports.signal = exports.trace = exports.initializeTracing = exports.getPaidTracerProvider = exports.getPaidTracer = void 0;
3
+ exports.createPaidSpanProcessors = exports.AISDKSpanProcessor = exports.signal = exports.trace = exports.initializeTracing = exports.getPaidTracerProvider = exports.getPaidTracer = void 0;
4
4
  const tracing_js_1 = require("./tracing.js");
5
5
  Object.defineProperty(exports, "getPaidTracer", { enumerable: true, get: function () { return tracing_js_1.getPaidTracer; } });
6
6
  Object.defineProperty(exports, "getPaidTracerProvider", { enumerable: true, get: function () { return tracing_js_1.getPaidTracerProvider; } });
7
7
  Object.defineProperty(exports, "initializeTracing", { enumerable: true, get: function () { return tracing_js_1.initializeTracing; } });
8
8
  Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return tracing_js_1.trace; } });
9
- const autoInstrumentation_js_1 = require("./autoInstrumentation.js");
10
- Object.defineProperty(exports, "paidAutoInstrument", { enumerable: true, get: function () { return autoInstrumentation_js_1.paidAutoInstrument; } });
9
+ Object.defineProperty(exports, "createPaidSpanProcessors", { enumerable: true, get: function () { return tracing_js_1.createPaidSpanProcessors; } });
11
10
  const signal_js_1 = require("./signal.js");
12
11
  Object.defineProperty(exports, "signal", { enumerable: true, get: function () { return signal_js_1.signal; } });
13
12
  const aiSdkSpanProcessor_js_1 = require("./aiSdkSpanProcessor.js");
@@ -1,11 +1,15 @@
1
1
  import type { Tracer } from "@opentelemetry/api";
2
+ import type { SpanProcessor } from "@opentelemetry/sdk-trace-base";
2
3
  import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
3
- import winston from "winston";
4
- export declare const logger: winston.Logger;
4
+ export interface InitializeTracingOptions {
5
+ collectorEndpoint?: string;
6
+ registerGlobal?: boolean;
7
+ }
5
8
  export declare function getToken(): string | undefined;
6
9
  export declare function getPaidTracerProvider(): NodeTracerProvider | undefined;
7
10
  export declare function getPaidTracer(): Tracer | undefined;
8
- export declare function initializeTracing(apiKey?: string, collectorEndpoint?: string): void;
11
+ export declare function createPaidSpanProcessors(apiKey: string, collectorEndpoint?: string): SpanProcessor[];
12
+ export declare function initializeTracing(apiKey?: string, options?: InitializeTracingOptions): void;
9
13
  export declare function trace<F extends (...args: any[]) => any>(options: {
10
14
  externalCustomerId: string;
11
15
  externalProductId?: string;
@@ -8,32 +8,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.logger = void 0;
16
12
  exports.getToken = getToken;
17
13
  exports.getPaidTracerProvider = getPaidTracerProvider;
18
14
  exports.getPaidTracer = getPaidTracer;
15
+ exports.createPaidSpanProcessors = createPaidSpanProcessors;
19
16
  exports.initializeTracing = initializeTracing;
20
17
  exports.trace = trace;
21
18
  const api_1 = require("@opentelemetry/api");
19
+ const context_async_hooks_1 = require("@opentelemetry/context-async-hooks");
22
20
  const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
23
21
  const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
24
- const winston_1 = __importDefault(require("winston"));
25
22
  const tracingContext_js_1 = require("./tracingContext.js");
26
23
  const spanProcessor_js_1 = require("./spanProcessor.js");
27
24
  const aiSdkSpanProcessor_js_1 = require("./aiSdkSpanProcessor.js");
28
- exports.logger = winston_1.default.createLogger({
29
- level: "silent", // Default to 'silent' to avoid logging unless set via environment variable
30
- format: winston_1.default.format.simple(),
31
- transports: [new winston_1.default.transports.Console()],
32
- });
33
- const logLevel = process.env.PAID_LOG_LEVEL;
34
- if (logLevel) {
35
- exports.logger.level = logLevel;
36
- }
37
25
  const DEFAULT_COLLECTOR_ENDPOINT = process.env["PAID_OTEL_COLLECTOR_ENDPOINT"] || "https://collector.agentpaid.io:4318/v1/traces";
38
26
  let paidApiToken = undefined;
39
27
  function getToken() {
@@ -57,26 +45,31 @@ const setupGracefulShutdown = (shuttable) => {
57
45
  _isShuttingDown = true;
58
46
  shuttable
59
47
  .shutdown()
60
- .then(() => exports.logger.info(`Paid tracing SDK shut down from signal: ${signal}`))
61
- .catch((error) => exports.logger.error(`Error shutting down Paid tracing SDK ${error}`));
48
+ .then(() => console.info(`Paid tracing SDK shut down from signal: ${signal}`))
49
+ .catch((error) => console.error(`Error shutting down Paid tracing SDK ${error}`));
62
50
  });
63
51
  });
64
52
  };
65
- function initializeTracing(apiKey, collectorEndpoint) {
53
+ function createPaidSpanProcessors(apiKey, collectorEndpoint) {
54
+ const url = collectorEndpoint || DEFAULT_COLLECTOR_ENDPOINT;
55
+ const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url, headers: { Authorization: `Bearer ${apiKey}` } });
56
+ return [new spanProcessor_js_1.PaidSpanProcessor(), new aiSdkSpanProcessor_js_1.AISDKSpanProcessor(), new sdk_trace_node_1.SimpleSpanProcessor(exporter)];
57
+ }
58
+ function initializeTracing(apiKey, options) {
66
59
  const paidEnabled = (process.env.PAID_ENABLED || "true") !== "false";
67
60
  if (!paidEnabled) {
68
- exports.logger.info("Paid tracing is disabled via PAID_ENABLED environment variable");
61
+ console.info("Paid tracing is disabled via PAID_ENABLED environment variable");
69
62
  return;
70
63
  }
71
64
  const token = getToken();
72
65
  if (!!token) {
73
- exports.logger.info("Tracing is already initialized - skipping re-intialization");
66
+ console.info("Tracing is already initialized - skipping re-intialization");
74
67
  return;
75
68
  }
76
69
  if (!apiKey) {
77
70
  const envKey = process.env.PAID_API_KEY;
78
71
  if (!envKey) {
79
- exports.logger.error("API key must be provided via PAID_API_KEY environment variable");
72
+ console.error("API key must be provided via PAID_API_KEY environment variable");
80
73
  return;
81
74
  }
82
75
  paidApiToken = envKey;
@@ -84,6 +77,7 @@ function initializeTracing(apiKey, collectorEndpoint) {
84
77
  else {
85
78
  paidApiToken = apiKey;
86
79
  }
80
+ const { collectorEndpoint, registerGlobal = false } = options || {};
87
81
  const url = collectorEndpoint || DEFAULT_COLLECTOR_ENDPOINT;
88
82
  const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url, headers: { Authorization: `Bearer ${paidApiToken}` } });
89
83
  const spanProcessor = new sdk_trace_node_1.SimpleSpanProcessor(exporter);
@@ -92,11 +86,25 @@ function initializeTracing(apiKey, collectorEndpoint) {
92
86
  paidTracerProvider = new sdk_trace_node_1.NodeTracerProvider({
93
87
  spanProcessors: [new spanProcessor_js_1.PaidSpanProcessor(), new aiSdkSpanProcessor_js_1.AISDKSpanProcessor(), spanProcessor],
94
88
  });
95
- // Don't call .register() - we pass the tracerProvider explicitly to instrumentations
96
- // This avoids polluting the global trace provider
89
+ if (registerGlobal) {
90
+ // Register the provider globally so that any tracer (including the AI SDK's
91
+ // `trace.getTracer("ai")`) routes spans through our span processors.
92
+ // This also sets up an AsyncLocalStorageContextManager for context propagation.
93
+ paidTracerProvider.register();
94
+ }
95
+ else {
96
+ // Set up context propagation without registering the provider globally.
97
+ // This gives us async parent-child linking via AsyncLocalStorage
98
+ // without polluting the global trace provider.
99
+ const contextManager = new context_async_hooks_1.AsyncLocalStorageContextManager();
100
+ contextManager.enable();
101
+ if (!api_1.context.setGlobalContextManager(contextManager)) {
102
+ contextManager.disable();
103
+ }
104
+ }
97
105
  paidTracer = paidTracerProvider.getTracer("paid.node");
98
106
  setupGracefulShutdown(spanProcessor);
99
- exports.logger.info(`Paid tracing SDK initialized with collector endpoint: ${url}`);
107
+ console.info(`Paid tracing SDK initialized with collector endpoint: ${url}`);
100
108
  }
101
109
  function trace(options, fn, ...args) {
102
110
  return __awaiter(this, void 0, void 0, function* () {
@@ -104,32 +112,34 @@ function trace(options, fn, ...args) {
104
112
  const tracer = getPaidTracer();
105
113
  if (!token || !tracer) {
106
114
  // don't throw, tracing should not crash user app.
107
- exports.logger.error("Paid tracing is not initialized. Make sure to call initializeTracing() first.");
115
+ console.error("Paid tracing is not initialized. Make sure to call initializeTracing() first.");
108
116
  return yield fn(...args);
109
117
  }
110
118
  const { externalCustomerId, externalProductId: externalAgentId, storePrompt, metadata } = options;
111
- return yield tracer.startActiveSpan("parent_span", (span) => __awaiter(this, void 0, void 0, function* () {
112
- try {
113
- const res = yield (0, tracingContext_js_1.runWithTracingContext)({
114
- externalCustomerId,
115
- externalProductId: externalAgentId,
116
- storePrompt,
117
- metadata,
118
- }, () => __awaiter(this, void 0, void 0, function* () { return yield fn(...args); }));
119
- span.setStatus({ code: api_1.SpanStatusCode.OK });
120
- return res;
121
- }
122
- catch (error) {
123
- span.setStatus({
124
- code: api_1.SpanStatusCode.ERROR,
125
- message: error.message,
126
- });
127
- span.recordException(error);
128
- throw error;
129
- }
130
- finally {
131
- span.end();
132
- }
119
+ return yield (0, tracingContext_js_1.runWithTracingContext)({
120
+ externalCustomerId,
121
+ externalProductId: externalAgentId,
122
+ storePrompt,
123
+ metadata,
124
+ }, () => __awaiter(this, void 0, void 0, function* () {
125
+ return yield tracer.startActiveSpan("parent_span", (span) => __awaiter(this, void 0, void 0, function* () {
126
+ try {
127
+ const res = yield fn(...args);
128
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
129
+ return res;
130
+ }
131
+ catch (error) {
132
+ span.setStatus({
133
+ code: api_1.SpanStatusCode.ERROR,
134
+ message: error.message,
135
+ });
136
+ span.recordException(error);
137
+ throw error;
138
+ }
139
+ finally {
140
+ span.end();
141
+ }
142
+ }));
133
143
  }));
134
144
  });
135
145
  }
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "1.0.1-alpha8";
1
+ export declare const SDK_VERSION = "1.0.1";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "1.0.1-alpha8";
4
+ exports.SDK_VERSION = "1.0.1";
@@ -12,8 +12,8 @@ export class PaidClient {
12
12
  this._options = Object.assign(Object.assign({}, _options), { headers: mergeHeaders({
13
13
  "X-Fern-Language": "JavaScript",
14
14
  "X-Fern-SDK-Name": "@paid-ai/paid-node",
15
- "X-Fern-SDK-Version": "1.0.1-alpha8",
16
- "User-Agent": "@paid-ai/paid-node/1.0.1-alpha8",
15
+ "X-Fern-SDK-Version": "1.0.1",
16
+ "User-Agent": "@paid-ai/paid-node/1.0.1",
17
17
  "X-Fern-Runtime": core.RUNTIME.type,
18
18
  "X-Fern-Runtime-Version": core.RUNTIME.version,
19
19
  }, _options === null || _options === void 0 ? void 0 : _options.headers) });
@@ -83,9 +83,6 @@ export class AISDKSpanProcessor {
83
83
  if (!finalName.startsWith("paid.trace.")) {
84
84
  finalName = `paid.trace.${finalName}`;
85
85
  }
86
- if (!finalName.endsWith(".signal")) {
87
- finalName = `${finalName}.signal`;
88
- }
89
86
  span.name = finalName;
90
87
  }
91
88
  shutdown() {
@@ -1,8 +1,25 @@
1
- import type * as openai from "openai";
2
- import type * as anthropic from "@anthropic-ai/sdk";
3
- interface SupportedLibraries {
4
- openai?: typeof openai.OpenAI;
5
- anthropic?: typeof anthropic.Anthropic;
1
+ export type SupportedLibrary = "openai" | "anthropic";
2
+ export interface InstrumentModules {
3
+ openai?: any;
4
+ anthropic?: any;
6
5
  }
7
- export declare function paidAutoInstrument(libraries?: SupportedLibraries): Promise<void>;
8
- export {};
6
+ /**
7
+ * Auto-instrument supported libraries using require-in-the-middle hooks.
8
+ * Works in plain Node.js (CJS). Does NOT work in bundled environments (Next.js/webpack).
9
+ * For bundled environments, use {@link paidAutoInstrumentModules} instead.
10
+ *
11
+ * @param libraries - Optional list of libraries to instrument. If omitted, all available libraries are instrumented.
12
+ */
13
+ export declare function paidAutoInstrument(libraries?: SupportedLibrary[]): Promise<void>;
14
+ /**
15
+ * Instrument supported libraries by directly patching the provided module references.
16
+ * Required for bundled environments (Next.js/webpack) where require-in-the-middle hooks don't work.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import OpenAI from "openai";
21
+ * import Anthropic from "@anthropic-ai/sdk";
22
+ * await paidAutoInstrumentModules({ openAI: OpenAI, anthropic: Anthropic });
23
+ * ```
24
+ */
25
+ export declare function paidAutoInstrumentModules(modules: InstrumentModules): Promise<void>;
@@ -7,69 +7,52 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { registerInstrumentations } from "@opentelemetry/instrumentation";
11
- import { getPaidTracerProvider, initializeTracing, logger } from "./tracing.mjs";
10
+ import { getPaidTracerProvider, initializeTracing } from "./tracing.mjs";
12
11
  let IS_INITIALIZED = false;
13
- const getInstrumentations = (tracerProvider) => __awaiter(void 0, void 0, void 0, function* () {
12
+ const getInstrumentations = (tracerProvider, libraries) => __awaiter(void 0, void 0, void 0, function* () {
14
13
  const instrumentations = [];
15
- try {
16
- const { OpenAIInstrumentation } = yield import("@arizeai/openinference-instrumentation-openai");
17
- instrumentations.push(new OpenAIInstrumentation({ tracerProvider }));
18
- }
19
- catch (_a) {
20
- logger.debug("OpenAI instrumentation not available - openai package not installed");
21
- }
22
- try {
23
- const { AnthropicInstrumentation } = yield import("@arizeai/openinference-instrumentation-anthropic");
24
- instrumentations.push(new AnthropicInstrumentation({ tracerProvider }));
25
- }
26
- catch (_b) {
27
- logger.debug("Anthropic instrumentation not available - @anthropic-ai/sdk package not installed");
28
- }
29
- return instrumentations;
30
- });
31
- const getManualInstrumentations = (tracerProvider, libraries) => __awaiter(void 0, void 0, void 0, function* () {
32
- const instrumentations = [];
33
- if (libraries.openai) {
14
+ const shouldInstrument = (lib) => !libraries || libraries.includes(lib);
15
+ if (shouldInstrument("openai")) {
34
16
  try {
35
17
  const { OpenAIInstrumentation } = yield import("@arizeai/openinference-instrumentation-openai");
36
- const openaiInstrumentation = new OpenAIInstrumentation({ tracerProvider });
37
- instrumentations.push(openaiInstrumentation);
38
- openaiInstrumentation.manuallyInstrument(libraries.openai);
18
+ instrumentations.push(new OpenAIInstrumentation({ tracerProvider }));
39
19
  }
40
20
  catch (_a) {
41
- logger.warn("Failed to load OpenAI instrumentation");
21
+ console.debug("OpenAI instrumentation not available - openai package not installed");
42
22
  }
43
23
  }
44
- if (libraries.anthropic) {
24
+ if (shouldInstrument("anthropic")) {
45
25
  try {
46
26
  const { AnthropicInstrumentation } = yield import("@arizeai/openinference-instrumentation-anthropic");
47
- const anthropicInstrumentation = new AnthropicInstrumentation({ tracerProvider });
48
- instrumentations.push(anthropicInstrumentation);
49
- anthropicInstrumentation.manuallyInstrument(libraries.anthropic);
27
+ instrumentations.push(new AnthropicInstrumentation({ tracerProvider }));
50
28
  }
51
29
  catch (_b) {
52
- logger.warn("Failed to load Anthropic instrumentation");
30
+ console.debug("Anthropic instrumentation not available - @anthropic-ai/sdk package not installed");
53
31
  }
54
32
  }
55
33
  return instrumentations;
56
34
  });
35
+ /**
36
+ * Auto-instrument supported libraries using require-in-the-middle hooks.
37
+ * Works in plain Node.js (CJS). Does NOT work in bundled environments (Next.js/webpack).
38
+ * For bundled environments, use {@link paidAutoInstrumentModules} instead.
39
+ *
40
+ * @param libraries - Optional list of libraries to instrument. If omitted, all available libraries are instrumented.
41
+ */
57
42
  export function paidAutoInstrument(libraries) {
58
43
  return __awaiter(this, void 0, void 0, function* () {
59
44
  if (IS_INITIALIZED) {
60
- logger.info("Auto instrumentation is already initialized");
45
+ console.info("Auto instrumentation is already initialized");
61
46
  return;
62
47
  }
63
- initializeTracing(); // try initializing tracing
48
+ initializeTracing();
64
49
  const tracerProvider = getPaidTracerProvider();
65
50
  if (!tracerProvider) {
66
- logger.error("Could not get tracer provider, make sure you ran 'initializeTracing()' or check your environment variables");
51
+ console.error("Could not get tracer provider, make sure you ran 'initializeTracing()' or check your environment variables");
67
52
  return;
68
53
  }
69
- const isManualInstrumentation = libraries && Object.keys(libraries).length > 0;
70
- const instrumentations = isManualInstrumentation
71
- ? yield getManualInstrumentations(tracerProvider, libraries)
72
- : yield getInstrumentations(tracerProvider);
54
+ const instrumentations = yield getInstrumentations(tracerProvider, libraries);
55
+ const { registerInstrumentations } = yield import("@opentelemetry/instrumentation");
73
56
  registerInstrumentations({
74
57
  instrumentations,
75
58
  tracerProvider,
@@ -77,3 +60,49 @@ export function paidAutoInstrument(libraries) {
77
60
  IS_INITIALIZED = true;
78
61
  });
79
62
  }
63
+ /**
64
+ * Instrument supported libraries by directly patching the provided module references.
65
+ * Required for bundled environments (Next.js/webpack) where require-in-the-middle hooks don't work.
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * import OpenAI from "openai";
70
+ * import Anthropic from "@anthropic-ai/sdk";
71
+ * await paidAutoInstrumentModules({ openAI: OpenAI, anthropic: Anthropic });
72
+ * ```
73
+ */
74
+ export function paidAutoInstrumentModules(modules) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ if (IS_INITIALIZED) {
77
+ console.info("Auto instrumentation is already initialized");
78
+ return;
79
+ }
80
+ initializeTracing();
81
+ const tracerProvider = getPaidTracerProvider();
82
+ if (!tracerProvider) {
83
+ console.error("Could not get tracer provider, make sure you ran 'initializeTracing()' or check your environment variables");
84
+ return;
85
+ }
86
+ if (modules.openai) {
87
+ try {
88
+ const { OpenAIInstrumentation } = yield import("@arizeai/openinference-instrumentation-openai");
89
+ const inst = new OpenAIInstrumentation({ tracerProvider });
90
+ inst.manuallyInstrument(modules.openai);
91
+ }
92
+ catch (_a) {
93
+ console.debug("OpenAI instrumentation not available");
94
+ }
95
+ }
96
+ if (modules.anthropic) {
97
+ try {
98
+ const { AnthropicInstrumentation } = yield import("@arizeai/openinference-instrumentation-anthropic");
99
+ const inst = new AnthropicInstrumentation({ tracerProvider });
100
+ inst.manuallyInstrument(modules.anthropic);
101
+ }
102
+ catch (_b) {
103
+ console.debug("Anthropic instrumentation not available");
104
+ }
105
+ }
106
+ IS_INITIALIZED = true;
107
+ });
108
+ }
@@ -1,5 +1,5 @@
1
- import { getPaidTracer, getPaidTracerProvider, initializeTracing, trace } from "./tracing.mjs";
2
- import { paidAutoInstrument } from "./autoInstrumentation.mjs";
1
+ import { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, createPaidSpanProcessors } from "./tracing.mjs";
3
2
  import { signal } from "./signal.mjs";
4
3
  import { AISDKSpanProcessor } from "./aiSdkSpanProcessor.mjs";
5
- export { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, signal, paidAutoInstrument, AISDKSpanProcessor };
4
+ export type { InitializeTracingOptions } from "./tracing.mjs";
5
+ export { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, signal, AISDKSpanProcessor, createPaidSpanProcessors };
@@ -1,5 +1,4 @@
1
- import { getPaidTracer, getPaidTracerProvider, initializeTracing, trace } from "./tracing.mjs";
2
- import { paidAutoInstrument } from "./autoInstrumentation.mjs";
1
+ import { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, createPaidSpanProcessors } from "./tracing.mjs";
3
2
  import { signal } from "./signal.mjs";
4
3
  import { AISDKSpanProcessor } from "./aiSdkSpanProcessor.mjs";
5
- export { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, signal, paidAutoInstrument, AISDKSpanProcessor };
4
+ export { getPaidTracer, getPaidTracerProvider, initializeTracing, trace, signal, AISDKSpanProcessor, createPaidSpanProcessors };