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.cjs CHANGED
@@ -15081,7 +15081,8 @@ var DEFAULT_INSTRUMENTS_FOR_ROOT = /* @__PURE__ */ new Set([
15081
15081
  // AI frameworks
15082
15082
  "langchain" /* LANGCHAIN */,
15083
15083
  "langgraph" /* LANGGRAPH */,
15084
- "llama_index" /* LLAMA_INDEX */
15084
+ "llama_index" /* LLAMA_INDEX */,
15085
+ "openai_agents" /* OPENAI_AGENTS */
15085
15086
  ]);
15086
15087
  var DEFAULT_INSTRUMENTS = /* @__PURE__ */ new Set([
15087
15088
  ...DEFAULT_INSTRUMENTS_FOR_ROOT,
@@ -18097,6 +18098,13 @@ var SpanAttributes2 = {
18097
18098
  };
18098
18099
 
18099
18100
  // src/instrumentation/utils.ts
18101
+ var VALID_NATIVE_TRACING_MODES = /* @__PURE__ */ new Set(["both", "netra", "netra-strict"]);
18102
+ function parseNativeTracingEnv(name) {
18103
+ const val = process.env[name];
18104
+ if (val === void 0 || val === "") return void 0;
18105
+ if (VALID_NATIVE_TRACING_MODES.has(val)) return val;
18106
+ return void 0;
18107
+ }
18100
18108
  var SUPPRESS_INSTRUMENTATION_KEY = /* @__PURE__ */ Symbol("netra.suppress_instrumentation");
18101
18109
  function shouldSuppressInstrumentation() {
18102
18110
  const ctx = api.context.active();
@@ -23603,6 +23611,8 @@ var cachedAgentsModule = null;
23603
23611
  var isInstrumented6 = false;
23604
23612
  var activeTracer = null;
23605
23613
  var activeProcessor = null;
23614
+ var originalProcessors = null;
23615
+ var didReplaceProcessors = false;
23606
23616
  async function resolveAgentsModule() {
23607
23617
  if (cachedAgentsModule) return cachedAgentsModule;
23608
23618
  try {
@@ -23655,20 +23665,51 @@ var NetraOpenAIAgentsInstrumentor = class {
23655
23665
  }
23656
23666
  const systemName = options.systemName ?? DEFAULT_LLM_SYSTEM;
23657
23667
  activeProcessor = new NetraAgentsTracingProcessor(activeTracer, systemName);
23668
+ const mode = options.nativeTracing ?? parseNativeTracingEnv("NATIVE_TRACING_MODE") ?? "netra-strict";
23669
+ const canSet = typeof agentsModule.setTraceProcessors === "function";
23670
+ const canAdd = typeof agentsModule.addTraceProcessor === "function";
23671
+ let strategy;
23672
+ if (mode === "both" || mode === "netra" && !canSet) {
23673
+ strategy = "append";
23674
+ } else if (canSet) {
23675
+ strategy = "replace";
23676
+ } else {
23677
+ strategy = "skip";
23678
+ }
23658
23679
  try {
23659
- if (typeof agentsModule.addTraceProcessor === "function") {
23660
- agentsModule.addTraceProcessor(activeProcessor);
23661
- } else if (typeof agentsModule.setTraceProcessors === "function") {
23662
- const existing = typeof agentsModule.getTraceProcessors === "function" ? agentsModule.getTraceProcessors() : [];
23663
- agentsModule.setTraceProcessors([...existing, activeProcessor]);
23664
- } else {
23665
- Logger.warn(
23666
- "OpenAI Agents SDK does not expose addTraceProcessor or setTraceProcessors.",
23667
- "Tracing integration may not work."
23668
- );
23669
- activeProcessor = null;
23670
- activeTracer = null;
23671
- return this;
23680
+ switch (strategy) {
23681
+ case "replace":
23682
+ if (typeof agentsModule.getTraceProcessors === "function") {
23683
+ originalProcessors = agentsModule.getTraceProcessors();
23684
+ }
23685
+ agentsModule.setTraceProcessors([activeProcessor]);
23686
+ didReplaceProcessors = true;
23687
+ Logger.debug("OpenAI Agents native tracing disabled \u2014 traces will only be sent to Netra.");
23688
+ break;
23689
+ case "append":
23690
+ if (canAdd) {
23691
+ agentsModule.addTraceProcessor(activeProcessor);
23692
+ } else {
23693
+ Logger.warn("OpenAI Agents SDK does not expose addTraceProcessor or setTraceProcessors.");
23694
+ activeProcessor = null;
23695
+ activeTracer = null;
23696
+ return this;
23697
+ }
23698
+ if (mode === "netra") {
23699
+ Logger.warn(
23700
+ "Cannot exclusively replace native trace processors in this @openai/agents version.",
23701
+ "Traces may still be sent to OpenAI."
23702
+ );
23703
+ }
23704
+ break;
23705
+ case "skip":
23706
+ Logger.warn(
23707
+ 'nativeTracing is "netra-strict" but the installed @openai/agents version',
23708
+ "does not support processor replacement. Skipping instrumentation."
23709
+ );
23710
+ activeProcessor = null;
23711
+ activeTracer = null;
23712
+ return this;
23672
23713
  }
23673
23714
  } catch (error) {
23674
23715
  Logger.warn("Failed to register trace processor with @openai/agents:", error);
@@ -23688,6 +23729,21 @@ var NetraOpenAIAgentsInstrumentor = class {
23688
23729
  activeProcessor.shutdown();
23689
23730
  activeProcessor = null;
23690
23731
  }
23732
+ if (didReplaceProcessors && cachedAgentsModule) {
23733
+ try {
23734
+ if (originalProcessors && typeof cachedAgentsModule.setTraceProcessors === "function") {
23735
+ cachedAgentsModule.setTraceProcessors(originalProcessors);
23736
+ Logger.debug("Restored original OpenAI Agents trace processors");
23737
+ } else if (typeof cachedAgentsModule.setDefaultOpenAITracingExporter === "function") {
23738
+ cachedAgentsModule.setDefaultOpenAITracingExporter();
23739
+ Logger.debug("Restored default OpenAI Agents tracing exporter");
23740
+ }
23741
+ } catch (error) {
23742
+ Logger.debug("Failed to restore original trace processors:", error);
23743
+ }
23744
+ }
23745
+ originalProcessors = null;
23746
+ didReplaceProcessors = false;
23691
23747
  activeTracer = null;
23692
23748
  cachedAgentsModule = null;
23693
23749
  isInstrumented6 = false;
@@ -24570,7 +24626,9 @@ function initInstrumentations(config2, instruments, blockInstruments, rootInstru
24570
24626
  mistral: false,
24571
24627
  langgraph: false,
24572
24628
  googleGenAI: false,
24573
- anthropic: false};
24629
+ anthropic: false,
24630
+ openAiAgents: false
24631
+ };
24574
24632
  const resolved = enableAll ? new Set(Object.values(NetraInstruments).filter((v) => v !== "__all__" /* ALL */)) : instruments && instruments.size > 0 ? instruments : DEFAULT_INSTRUMENTS;
24575
24633
  instrumentModules.google_vertexai = false;
24576
24634
  instrumentModules.langchain = false;
@@ -24618,6 +24676,9 @@ function initInstrumentations(config2, instruments, blockInstruments, rootInstru
24618
24676
  if (resolved.has("anthropic" /* ANTHROPIC */)) {
24619
24677
  customInstrumentModules.anthropic = true;
24620
24678
  }
24679
+ if (resolved.has("openai_agents" /* OPENAI_AGENTS */)) {
24680
+ customInstrumentModules.openAiAgents = true;
24681
+ }
24621
24682
  if (blockInstruments && blockInstruments.size > 0) {
24622
24683
  const blockAll = blockInstruments.has("__all__" /* ALL */);
24623
24684
  if (blockAll || blockInstruments.has("openai" /* OPENAI */)) customInstrumentModules.openai = false;
@@ -24626,6 +24687,7 @@ function initInstrumentations(config2, instruments, blockInstruments, rootInstru
24626
24687
  if (blockAll || blockInstruments.has("langgraph" /* LANGGRAPH */)) customInstrumentModules.langgraph = false;
24627
24688
  if (blockAll || blockInstruments.has("google_genai" /* GOOGLE_GENERATIVE_AI */)) customInstrumentModules.googleGenAI = false;
24628
24689
  if (blockAll || blockInstruments.has("anthropic" /* ANTHROPIC */)) customInstrumentModules.anthropic = false;
24690
+ if (blockAll || blockInstruments.has("openai_agents" /* OPENAI_AGENTS */)) customInstrumentModules.openAiAgents = false;
24629
24691
  if (blockAll || blockInstruments.has("vertexai" /* VERTEX_AI */)) instrumentModules.google_vertexai = false;
24630
24692
  if (blockAll || blockInstruments.has("langchain" /* LANGCHAIN */)) instrumentModules.langchain = false;
24631
24693
  if (blockAll || blockInstruments.has("llama_index" /* LLAMA_INDEX */)) instrumentModules.llamaIndex = false;
@@ -24748,7 +24810,7 @@ async function initCustomInstrumentationsAsync(config2, tracerProvider, customIn
24748
24810
  Logger.debug("Failed to initialize custom Anthropic instrumentation:", e);
24749
24811
  }
24750
24812
  }
24751
- if (customInstrumentModules.openaiAgents && !blockInstruments?.has("openai_agents" /* OPENAI_AGENTS */)) {
24813
+ if (customInstrumentModules.openAiAgents && !blockInstruments?.has("openai_agents" /* OPENAI_AGENTS */)) {
24752
24814
  try {
24753
24815
  await openaiAgentsInstrumentor.instrument({ tracerProvider });
24754
24816
  Logger.debug("Custom OpenAI Agents SDK instrumentation enabled");