@atrim/instrument-node 0.5.1-dev.14fdea7.20260108232041 → 0.5.1-dev.14fdea7.20260109021657

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.
@@ -166,17 +166,18 @@ declare class AutoTracingSupervisor extends Supervisor.AbstractSupervisor<void>
166
166
  private readonly fiberSpans;
167
167
  private readonly fiberStartTimes;
168
168
  private _tracer;
169
+ private readonly tracerProvider;
169
170
  private readonly includePatterns;
170
171
  private readonly excludePatterns;
171
172
  private activeFiberCount;
172
173
  private _rootSpan;
173
- constructor(config: AutoInstrumentationConfig);
174
+ constructor(config: AutoInstrumentationConfig, tracerProvider?: OtelApi.TracerProvider);
174
175
  /**
175
176
  * Set the root span for parent context propagation
176
177
  */
177
178
  setRootSpan(span: OtelApi.Span): void;
178
179
  /**
179
- * Get the tracer lazily - this allows time for the NodeSdk layer to register the global provider
180
+ * Get the tracer lazily - uses provided TracerProvider if available, otherwise uses global
180
181
  */
181
182
  private get tracer();
182
183
  /**
@@ -211,7 +212,7 @@ declare class AutoTracingSupervisor extends Supervisor.AbstractSupervisor<void>
211
212
  /**
212
213
  * Create a custom AutoTracingSupervisor with the given config
213
214
  */
214
- declare const createAutoTracingSupervisor: (config: AutoInstrumentationConfig) => AutoTracingSupervisor;
215
+ declare const createAutoTracingSupervisor: (config: AutoInstrumentationConfig, tracerProvider?: OtelApi.TracerProvider) => AutoTracingSupervisor;
215
216
  /**
216
217
  * Layer that provides auto-tracing with custom configuration
217
218
  *
@@ -166,17 +166,18 @@ declare class AutoTracingSupervisor extends Supervisor.AbstractSupervisor<void>
166
166
  private readonly fiberSpans;
167
167
  private readonly fiberStartTimes;
168
168
  private _tracer;
169
+ private readonly tracerProvider;
169
170
  private readonly includePatterns;
170
171
  private readonly excludePatterns;
171
172
  private activeFiberCount;
172
173
  private _rootSpan;
173
- constructor(config: AutoInstrumentationConfig);
174
+ constructor(config: AutoInstrumentationConfig, tracerProvider?: OtelApi.TracerProvider);
174
175
  /**
175
176
  * Set the root span for parent context propagation
176
177
  */
177
178
  setRootSpan(span: OtelApi.Span): void;
178
179
  /**
179
- * Get the tracer lazily - this allows time for the NodeSdk layer to register the global provider
180
+ * Get the tracer lazily - uses provided TracerProvider if available, otherwise uses global
180
181
  */
181
182
  private get tracer();
182
183
  /**
@@ -211,7 +212,7 @@ declare class AutoTracingSupervisor extends Supervisor.AbstractSupervisor<void>
211
212
  /**
212
213
  * Create a custom AutoTracingSupervisor with the given config
213
214
  */
214
- declare const createAutoTracingSupervisor: (config: AutoInstrumentationConfig) => AutoTracingSupervisor;
215
+ declare const createAutoTracingSupervisor: (config: AutoInstrumentationConfig, tracerProvider?: OtelApi.TracerProvider) => AutoTracingSupervisor;
215
216
  /**
216
217
  * Layer that provides auto-tracing with custom configuration
217
218
  *
@@ -1,5 +1,5 @@
1
- import { Data, Context, Effect, Layer, FiberRef, Option, Supervisor, FiberRefs, Exit } from 'effect';
2
- import { NodeSdk } from '@effect/opentelemetry';
1
+ import { Data, Context, Effect, Layer, FiberRef, Option, Supervisor, FiberRefs, Tracer, Exit } from 'effect';
2
+ import { NodeSdk, Tracer as Tracer$1 } from '@effect/opentelemetry';
3
3
  import { BasicTracerProvider, SimpleSpanProcessor, ConsoleSpanExporter, BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
4
4
  import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
5
5
  import { FileSystem } from '@effect/platform/FileSystem';
@@ -706,7 +706,7 @@ function sanitizeSpanName(name) {
706
706
  var AutoTracingEnabled = FiberRef.unsafeMake(true);
707
707
  var AutoTracingSpanName = FiberRef.unsafeMake(Option.none());
708
708
  var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
709
- constructor(config) {
709
+ constructor(config, tracerProvider) {
710
710
  super();
711
711
  this.config = config;
712
712
  // WeakMap to associate fibers with their OTel spans
@@ -715,6 +715,8 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
715
715
  __publicField(this, "fiberStartTimes", /* @__PURE__ */ new WeakMap());
716
716
  // OpenTelemetry tracer - lazily initialized
717
717
  __publicField(this, "_tracer", null);
718
+ // Optional TracerProvider (if provided, use this instead of global)
719
+ __publicField(this, "tracerProvider", null);
718
720
  // Compiled filter patterns
719
721
  __publicField(this, "includePatterns");
720
722
  __publicField(this, "excludePatterns");
@@ -722,6 +724,10 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
722
724
  __publicField(this, "activeFiberCount", 0);
723
725
  // Root span for parent context (set by withAutoTracing)
724
726
  __publicField(this, "_rootSpan", null);
727
+ if (tracerProvider) {
728
+ this.tracerProvider = tracerProvider;
729
+ logger.log("@atrim/auto-trace: Using provided TracerProvider");
730
+ }
725
731
  this.includePatterns = (config.filter?.include || []).map((p) => new RegExp(p));
726
732
  this.excludePatterns = (config.filter?.exclude || []).map((p) => new RegExp(p));
727
733
  logger.log("@atrim/auto-trace: Supervisor initialized");
@@ -736,11 +742,17 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
736
742
  this._rootSpan = span;
737
743
  }
738
744
  /**
739
- * Get the tracer lazily - this allows time for the NodeSdk layer to register the global provider
745
+ * Get the tracer lazily - uses provided TracerProvider if available, otherwise uses global
740
746
  */
741
747
  get tracer() {
742
748
  if (!this._tracer) {
743
- this._tracer = OtelApi.trace.getTracer("@atrim/auto-trace", "1.0.0");
749
+ if (this.tracerProvider) {
750
+ logger.log("@atrim/auto-trace: Getting tracer from provided TracerProvider");
751
+ this._tracer = this.tracerProvider.getTracer("@atrim/auto-trace", "1.0.0");
752
+ } else {
753
+ logger.log("@atrim/auto-trace: Getting tracer from global API");
754
+ this._tracer = OtelApi.trace.getTracer("@atrim/auto-trace", "1.0.0");
755
+ }
744
756
  }
745
757
  return this._tracer;
746
758
  }
@@ -754,9 +766,11 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
754
766
  * Called when a fiber starts executing
755
767
  */
756
768
  onStart(_context, _effect, parent, fiber) {
769
+ logger.log(`@atrim/auto-trace: onStart called for fiber ${fiber.id().id}`);
757
770
  const fiberRefsValue = fiber.getFiberRefs();
758
771
  const enabled = FiberRefs.getOrDefault(fiberRefsValue, AutoTracingEnabled);
759
772
  if (!enabled) {
773
+ logger.log(`@atrim/auto-trace: Auto-tracing disabled for fiber ${fiber.id().id}`);
760
774
  return;
761
775
  }
762
776
  const samplingRate = this.config.performance?.sampling_rate ?? 1;
@@ -780,7 +794,19 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
780
794
  }
781
795
  let parentContext = OtelApi.ROOT_CONTEXT;
782
796
  let parentFiberId;
783
- if (Option.isSome(parent)) {
797
+ const maybeEffectParentSpan = Context.getOption(_context, Tracer.ParentSpan);
798
+ if (Option.isSome(maybeEffectParentSpan)) {
799
+ const effectSpan = maybeEffectParentSpan.value;
800
+ logger.log(`@atrim/auto-trace: Found ParentSpan - traceId=${effectSpan.traceId.slice(0, 8)}..., spanId=${effectSpan.spanId.slice(0, 8)}...`);
801
+ const otelSpanContext = {
802
+ traceId: effectSpan.traceId,
803
+ spanId: effectSpan.spanId,
804
+ traceFlags: effectSpan.sampled ? OtelApi.TraceFlags.SAMPLED : OtelApi.TraceFlags.NONE,
805
+ isRemote: false
806
+ };
807
+ const wrappedSpan = OtelApi.trace.wrapSpanContext(otelSpanContext);
808
+ parentContext = OtelApi.trace.setSpan(OtelApi.ROOT_CONTEXT, wrappedSpan);
809
+ } else if (Option.isSome(parent)) {
784
810
  parentFiberId = parent.value.id().id;
785
811
  const parentSpan = this.fiberSpans.get(parent.value);
786
812
  if (parentSpan) {
@@ -791,6 +817,9 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
791
817
  } else if (this._rootSpan) {
792
818
  parentContext = OtelApi.trace.setSpan(OtelApi.ROOT_CONTEXT, this._rootSpan);
793
819
  }
820
+ if (Option.isSome(parent)) {
821
+ parentFiberId = parent.value.id().id;
822
+ }
794
823
  const span = this.tracer.startSpan(
795
824
  spanName,
796
825
  {
@@ -799,6 +828,7 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
799
828
  },
800
829
  parentContext
801
830
  );
831
+ logger.log(`@atrim/auto-trace: Created span "${spanName}" for fiber ${fiber.id().id}`);
802
832
  this.fiberSpans.set(fiber, span);
803
833
  this.fiberStartTimes.set(fiber, process.hrtime.bigint());
804
834
  this.activeFiberCount++;
@@ -926,8 +956,8 @@ var AutoTracingSupervisor = class extends Supervisor.AbstractSupervisor {
926
956
  }
927
957
  }
928
958
  };
929
- var createAutoTracingSupervisor = (config) => {
930
- return new AutoTracingSupervisor(config);
959
+ var createAutoTracingSupervisor = (config, tracerProvider) => {
960
+ return new AutoTracingSupervisor(config, tracerProvider);
931
961
  };
932
962
  var createAutoTracingLayer = (options) => {
933
963
  return Layer.unwrapEffect(
@@ -1195,12 +1225,27 @@ var createCombinedTracingLayer = () => {
1195
1225
  },
1196
1226
  spanProcessor: createSpanProcessor()
1197
1227
  }));
1198
- const supervisor = createAutoTracingSupervisor(autoConfig);
1199
- const supervisorLayer = Supervisor.addSupervisor(supervisor);
1200
- logger.log("@atrim/auto-trace: Combined layer created");
1201
- logger.log(" - HTTP requests: auto-traced via Effect platform");
1202
- logger.log(" - Forked fibers: auto-traced via Supervisor");
1203
- return Layer.mergeAll(Layer.discard(sdkLayer), supervisorLayer);
1228
+ const supervisorLayer = Layer.unwrapScoped(
1229
+ Effect.gen(function* () {
1230
+ const tracerProvider = yield* Effect.serviceOption(Tracer$1.OtelTracerProvider);
1231
+ if (Option.isSome(tracerProvider)) {
1232
+ logger.log("@atrim/auto-trace: Got TracerProvider from NodeSdk layer");
1233
+ const supervisor = createAutoTracingSupervisor(autoConfig, tracerProvider.value);
1234
+ logger.log("@atrim/auto-trace: Combined layer created");
1235
+ logger.log(" - HTTP requests: auto-traced via Effect platform");
1236
+ logger.log(" - Forked fibers: auto-traced via Supervisor");
1237
+ return Supervisor.addSupervisor(supervisor);
1238
+ } else {
1239
+ logger.log("@atrim/auto-trace: WARNING: No TracerProvider found, creating supervisor without it");
1240
+ const supervisor = createAutoTracingSupervisor(autoConfig);
1241
+ logger.log("@atrim/auto-trace: Combined layer created (fallback)");
1242
+ logger.log(" - HTTP requests: auto-traced via Effect platform");
1243
+ logger.log(" - Forked fibers: auto-traced via Supervisor");
1244
+ return Supervisor.addSupervisor(supervisor);
1245
+ }
1246
+ })
1247
+ );
1248
+ return supervisorLayer.pipe(Layer.provide(Layer.discard(sdkLayer)));
1204
1249
  })
1205
1250
  );
1206
1251
  };