netra-sdk 1.2.0 → 1.3.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.
package/dist/index.d.cts CHANGED
@@ -1214,6 +1214,28 @@ declare function span(target: AnyClass): void;
1214
1214
  declare function span<T extends AnyFunction>(target: T): T;
1215
1215
  declare function span(options?: DecoratorOptions): UnifiedDecorator;
1216
1216
 
1217
+ /**
1218
+ * Shared utility functions for instrumentation.
1219
+ * Handles setting OTel span attributes for LLM request/response tracing.
1220
+ */
1221
+
1222
+ /**
1223
+ * Controls where native traces are sent.
1224
+ *
1225
+ * - `"netra"` -- Replace the SDK's default processors so traces go
1226
+ * only to Netra. If the SDK does not expose the required APIs, falls back
1227
+ * to additive mode (both Netra and native) and logs a warning.
1228
+ * - `"netra-strict"` (default) -- Same replacement as `"netra"`, but if the SDK APIs
1229
+ * are unavailable the processor is **not** registered at all, ensuring
1230
+ * traces never reach native even at the cost of no tracing.
1231
+ * - `"both"` -- The Netra processor is added alongside the native defaults;
1232
+ * traces go to both Netra and native.
1233
+ *
1234
+ * Can also be set via the `NATIVE_TRACING_MODE` environment
1235
+ * variable using the same string values.
1236
+ */
1237
+ type NativeTracingMode = "both" | "netra" | "netra-strict";
1238
+
1217
1239
  /**
1218
1240
  * Custom MistralAI instrumentor for Netra SDK
1219
1241
  *
@@ -1328,12 +1350,15 @@ interface TracingProcessor {
1328
1350
  forceFlush(): Promise<void> | void;
1329
1351
  shutdown(timeout?: number): Promise<void> | void;
1330
1352
  }
1353
+
1331
1354
  interface InstrumentorOptions {
1332
1355
  tracerProvider?: {
1333
1356
  getTracer(name: string, version?: string): any;
1334
1357
  };
1335
1358
  /** Override the `llm.system` attribute value (default: `"openai"`). */
1336
1359
  systemName?: string;
1360
+ /** Controls where traces are sent. See {@link NativeTracingMode}. */
1361
+ nativeTracing?: NativeTracingMode;
1337
1362
  }
1338
1363
 
1339
1364
  declare class NetraAgentsTracingProcessor implements TracingProcessor {
package/dist/index.d.ts CHANGED
@@ -1214,6 +1214,28 @@ declare function span(target: AnyClass): void;
1214
1214
  declare function span<T extends AnyFunction>(target: T): T;
1215
1215
  declare function span(options?: DecoratorOptions): UnifiedDecorator;
1216
1216
 
1217
+ /**
1218
+ * Shared utility functions for instrumentation.
1219
+ * Handles setting OTel span attributes for LLM request/response tracing.
1220
+ */
1221
+
1222
+ /**
1223
+ * Controls where native traces are sent.
1224
+ *
1225
+ * - `"netra"` -- Replace the SDK's default processors so traces go
1226
+ * only to Netra. If the SDK does not expose the required APIs, falls back
1227
+ * to additive mode (both Netra and native) and logs a warning.
1228
+ * - `"netra-strict"` (default) -- Same replacement as `"netra"`, but if the SDK APIs
1229
+ * are unavailable the processor is **not** registered at all, ensuring
1230
+ * traces never reach native even at the cost of no tracing.
1231
+ * - `"both"` -- The Netra processor is added alongside the native defaults;
1232
+ * traces go to both Netra and native.
1233
+ *
1234
+ * Can also be set via the `NATIVE_TRACING_MODE` environment
1235
+ * variable using the same string values.
1236
+ */
1237
+ type NativeTracingMode = "both" | "netra" | "netra-strict";
1238
+
1217
1239
  /**
1218
1240
  * Custom MistralAI instrumentor for Netra SDK
1219
1241
  *
@@ -1328,12 +1350,15 @@ interface TracingProcessor {
1328
1350
  forceFlush(): Promise<void> | void;
1329
1351
  shutdown(timeout?: number): Promise<void> | void;
1330
1352
  }
1353
+
1331
1354
  interface InstrumentorOptions {
1332
1355
  tracerProvider?: {
1333
1356
  getTracer(name: string, version?: string): any;
1334
1357
  };
1335
1358
  /** Override the `llm.system` attribute value (default: `"openai"`). */
1336
1359
  systemName?: string;
1360
+ /** Controls where traces are sent. See {@link NativeTracingMode}. */
1361
+ nativeTracing?: NativeTracingMode;
1337
1362
  }
1338
1363
 
1339
1364
  declare class NetraAgentsTracingProcessor implements TracingProcessor {
package/dist/index.js CHANGED
@@ -610,7 +610,8 @@ var DEFAULT_INSTRUMENTS_FOR_ROOT = /* @__PURE__ */ new Set([
610
610
  // AI frameworks
611
611
  "langchain" /* LANGCHAIN */,
612
612
  "langgraph" /* LANGGRAPH */,
613
- "llama_index" /* LLAMA_INDEX */
613
+ "llama_index" /* LLAMA_INDEX */,
614
+ "openai_agents" /* OPENAI_AGENTS */
614
615
  ]);
615
616
  var DEFAULT_INSTRUMENTS = /* @__PURE__ */ new Set([
616
617
  ...DEFAULT_INSTRUMENTS_FOR_ROOT,
@@ -3626,6 +3627,13 @@ var SpanAttributes2 = {
3626
3627
  };
3627
3628
 
3628
3629
  // src/instrumentation/utils.ts
3630
+ var VALID_NATIVE_TRACING_MODES = /* @__PURE__ */ new Set(["both", "netra", "netra-strict"]);
3631
+ function parseNativeTracingEnv(name) {
3632
+ const val = process.env[name];
3633
+ if (val === void 0 || val === "") return void 0;
3634
+ if (VALID_NATIVE_TRACING_MODES.has(val)) return val;
3635
+ return void 0;
3636
+ }
3629
3637
  var SUPPRESS_INSTRUMENTATION_KEY = /* @__PURE__ */ Symbol("netra.suppress_instrumentation");
3630
3638
  function shouldSuppressInstrumentation() {
3631
3639
  const ctx = context.active();
@@ -9132,6 +9140,8 @@ var cachedAgentsModule = null;
9132
9140
  var isInstrumented6 = false;
9133
9141
  var activeTracer = null;
9134
9142
  var activeProcessor = null;
9143
+ var originalProcessors = null;
9144
+ var didReplaceProcessors = false;
9135
9145
  async function resolveAgentsModule() {
9136
9146
  if (cachedAgentsModule) return cachedAgentsModule;
9137
9147
  try {
@@ -9184,20 +9194,51 @@ var NetraOpenAIAgentsInstrumentor = class {
9184
9194
  }
9185
9195
  const systemName = options.systemName ?? DEFAULT_LLM_SYSTEM;
9186
9196
  activeProcessor = new NetraAgentsTracingProcessor(activeTracer, systemName);
9197
+ const mode = options.nativeTracing ?? parseNativeTracingEnv("NATIVE_TRACING_MODE") ?? "netra-strict";
9198
+ const canSet = typeof agentsModule.setTraceProcessors === "function";
9199
+ const canAdd = typeof agentsModule.addTraceProcessor === "function";
9200
+ let strategy;
9201
+ if (mode === "both" || mode === "netra" && !canSet) {
9202
+ strategy = "append";
9203
+ } else if (canSet) {
9204
+ strategy = "replace";
9205
+ } else {
9206
+ strategy = "skip";
9207
+ }
9187
9208
  try {
9188
- if (typeof agentsModule.addTraceProcessor === "function") {
9189
- agentsModule.addTraceProcessor(activeProcessor);
9190
- } else if (typeof agentsModule.setTraceProcessors === "function") {
9191
- const existing = typeof agentsModule.getTraceProcessors === "function" ? agentsModule.getTraceProcessors() : [];
9192
- agentsModule.setTraceProcessors([...existing, activeProcessor]);
9193
- } else {
9194
- Logger.warn(
9195
- "OpenAI Agents SDK does not expose addTraceProcessor or setTraceProcessors.",
9196
- "Tracing integration may not work."
9197
- );
9198
- activeProcessor = null;
9199
- activeTracer = null;
9200
- return this;
9209
+ switch (strategy) {
9210
+ case "replace":
9211
+ if (typeof agentsModule.getTraceProcessors === "function") {
9212
+ originalProcessors = agentsModule.getTraceProcessors();
9213
+ }
9214
+ agentsModule.setTraceProcessors([activeProcessor]);
9215
+ didReplaceProcessors = true;
9216
+ Logger.debug("OpenAI Agents native tracing disabled \u2014 traces will only be sent to Netra.");
9217
+ break;
9218
+ case "append":
9219
+ if (canAdd) {
9220
+ agentsModule.addTraceProcessor(activeProcessor);
9221
+ } else {
9222
+ Logger.warn("OpenAI Agents SDK does not expose addTraceProcessor or setTraceProcessors.");
9223
+ activeProcessor = null;
9224
+ activeTracer = null;
9225
+ return this;
9226
+ }
9227
+ if (mode === "netra") {
9228
+ Logger.warn(
9229
+ "Cannot exclusively replace native trace processors in this @openai/agents version.",
9230
+ "Traces may still be sent to OpenAI."
9231
+ );
9232
+ }
9233
+ break;
9234
+ case "skip":
9235
+ Logger.warn(
9236
+ 'nativeTracing is "netra-strict" but the installed @openai/agents version',
9237
+ "does not support processor replacement. Skipping instrumentation."
9238
+ );
9239
+ activeProcessor = null;
9240
+ activeTracer = null;
9241
+ return this;
9201
9242
  }
9202
9243
  } catch (error) {
9203
9244
  Logger.warn("Failed to register trace processor with @openai/agents:", error);
@@ -9217,6 +9258,21 @@ var NetraOpenAIAgentsInstrumentor = class {
9217
9258
  activeProcessor.shutdown();
9218
9259
  activeProcessor = null;
9219
9260
  }
9261
+ if (didReplaceProcessors && cachedAgentsModule) {
9262
+ try {
9263
+ if (originalProcessors && typeof cachedAgentsModule.setTraceProcessors === "function") {
9264
+ cachedAgentsModule.setTraceProcessors(originalProcessors);
9265
+ Logger.debug("Restored original OpenAI Agents trace processors");
9266
+ } else if (typeof cachedAgentsModule.setDefaultOpenAITracingExporter === "function") {
9267
+ cachedAgentsModule.setDefaultOpenAITracingExporter();
9268
+ Logger.debug("Restored default OpenAI Agents tracing exporter");
9269
+ }
9270
+ } catch (error) {
9271
+ Logger.debug("Failed to restore original trace processors:", error);
9272
+ }
9273
+ }
9274
+ originalProcessors = null;
9275
+ didReplaceProcessors = false;
9220
9276
  activeTracer = null;
9221
9277
  cachedAgentsModule = null;
9222
9278
  isInstrumented6 = false;
@@ -10099,7 +10155,9 @@ function initInstrumentations(config2, instruments, blockInstruments, rootInstru
10099
10155
  mistral: false,
10100
10156
  langgraph: false,
10101
10157
  googleGenAI: false,
10102
- anthropic: false};
10158
+ anthropic: false,
10159
+ openAiAgents: false
10160
+ };
10103
10161
  const resolved = enableAll ? new Set(Object.values(NetraInstruments).filter((v) => v !== "__all__" /* ALL */)) : instruments && instruments.size > 0 ? instruments : DEFAULT_INSTRUMENTS;
10104
10162
  instrumentModules.google_vertexai = false;
10105
10163
  instrumentModules.langchain = false;
@@ -10147,6 +10205,9 @@ function initInstrumentations(config2, instruments, blockInstruments, rootInstru
10147
10205
  if (resolved.has("anthropic" /* ANTHROPIC */)) {
10148
10206
  customInstrumentModules.anthropic = true;
10149
10207
  }
10208
+ if (resolved.has("openai_agents" /* OPENAI_AGENTS */)) {
10209
+ customInstrumentModules.openAiAgents = true;
10210
+ }
10150
10211
  if (blockInstruments && blockInstruments.size > 0) {
10151
10212
  const blockAll = blockInstruments.has("__all__" /* ALL */);
10152
10213
  if (blockAll || blockInstruments.has("openai" /* OPENAI */)) customInstrumentModules.openai = false;
@@ -10155,6 +10216,7 @@ function initInstrumentations(config2, instruments, blockInstruments, rootInstru
10155
10216
  if (blockAll || blockInstruments.has("langgraph" /* LANGGRAPH */)) customInstrumentModules.langgraph = false;
10156
10217
  if (blockAll || blockInstruments.has("google_genai" /* GOOGLE_GENERATIVE_AI */)) customInstrumentModules.googleGenAI = false;
10157
10218
  if (blockAll || blockInstruments.has("anthropic" /* ANTHROPIC */)) customInstrumentModules.anthropic = false;
10219
+ if (blockAll || blockInstruments.has("openai_agents" /* OPENAI_AGENTS */)) customInstrumentModules.openAiAgents = false;
10158
10220
  if (blockAll || blockInstruments.has("vertexai" /* VERTEX_AI */)) instrumentModules.google_vertexai = false;
10159
10221
  if (blockAll || blockInstruments.has("langchain" /* LANGCHAIN */)) instrumentModules.langchain = false;
10160
10222
  if (blockAll || blockInstruments.has("llama_index" /* LLAMA_INDEX */)) instrumentModules.llamaIndex = false;
@@ -10277,7 +10339,7 @@ async function initCustomInstrumentationsAsync(config2, tracerProvider, customIn
10277
10339
  Logger.debug("Failed to initialize custom Anthropic instrumentation:", e);
10278
10340
  }
10279
10341
  }
10280
- if (customInstrumentModules.openaiAgents && !blockInstruments?.has("openai_agents" /* OPENAI_AGENTS */)) {
10342
+ if (customInstrumentModules.openAiAgents && !blockInstruments?.has("openai_agents" /* OPENAI_AGENTS */)) {
10281
10343
  try {
10282
10344
  await openaiAgentsInstrumentor.instrument({ tracerProvider });
10283
10345
  Logger.debug("Custom OpenAI Agents SDK instrumentation enabled");