@sentry/opentelemetry 10.70.0 → 11.0.0-alpha.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.
Files changed (133) hide show
  1. package/README.md +2 -15
  2. package/build/cjs/index.js +614 -101
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.js +611 -65
  5. package/build/esm/index.js.map +1 -1
  6. package/build/esm/package.json +1 -1
  7. package/build/types/asyncContextStrategy.d.ts +2 -4
  8. package/build/types/asyncContextStrategy.d.ts.map +1 -1
  9. package/build/types/asyncLocalStorageContextManager.d.ts +16 -4
  10. package/build/types/asyncLocalStorageContextManager.d.ts.map +1 -1
  11. package/build/types/constants.d.ts +0 -15
  12. package/build/types/constants.d.ts.map +1 -1
  13. package/build/types/index.d.ts +5 -4
  14. package/build/types/index.d.ts.map +1 -1
  15. package/build/types/propagator.d.ts +11 -38
  16. package/build/types/propagator.d.ts.map +1 -1
  17. package/build/types/trace.d.ts +4 -31
  18. package/build/types/trace.d.ts.map +1 -1
  19. package/build/types/tracer.d.ts.map +1 -1
  20. package/build/types/tracerProvider.d.ts +0 -9
  21. package/build/types/tracerProvider.d.ts.map +1 -1
  22. package/build/types/types.d.ts +1 -24
  23. package/build/types/types.d.ts.map +1 -1
  24. package/build/types/utils/TraceState.d.ts.map +1 -1
  25. package/build/types/utils/buildContextWithSentryScopes.d.ts +1 -1
  26. package/build/types/utils/buildContextWithSentryScopes.d.ts.map +1 -1
  27. package/build/types/utils/contextData.d.ts.map +1 -1
  28. package/build/types/utils/getActiveSpan.d.ts +2 -1
  29. package/build/types/utils/getActiveSpan.d.ts.map +1 -1
  30. package/build/types/utils/getSamplingDecision.d.ts +0 -17
  31. package/build/types/utils/getSamplingDecision.d.ts.map +1 -1
  32. package/build/types/utils/reconcileDscSampled.d.ts +14 -0
  33. package/build/types/utils/reconcileDscSampled.d.ts.map +1 -0
  34. package/build/types/utils/suppressTracing.d.ts +5 -4
  35. package/build/types/utils/suppressTracing.d.ts.map +1 -1
  36. package/package.json +11 -38
  37. package/build/cjs/asyncContextStrategy-volGaYqZ.js +0 -1892
  38. package/build/cjs/asyncContextStrategy-volGaYqZ.js.map +0 -1
  39. package/build/cjs/index.browser.js +0 -53
  40. package/build/cjs/index.browser.js.map +0 -1
  41. package/build/esm/asyncContextStrategy-jD8YWNDU.js +0 -1857
  42. package/build/esm/asyncContextStrategy-jD8YWNDU.js.map +0 -1
  43. package/build/esm/index.browser.js +0 -14
  44. package/build/esm/index.browser.js.map +0 -1
  45. package/build/types/applyOtelSpanData.d.ts +0 -18
  46. package/build/types/applyOtelSpanData.d.ts.map +0 -1
  47. package/build/types/contextManager.d.ts +0 -22
  48. package/build/types/contextManager.d.ts.map +0 -1
  49. package/build/types/custom/client.d.ts +0 -12
  50. package/build/types/custom/client.d.ts.map +0 -1
  51. package/build/types/exports.d.ts +0 -27
  52. package/build/types/exports.d.ts.map +0 -1
  53. package/build/types/index.browser.d.ts +0 -10
  54. package/build/types/index.browser.d.ts.map +0 -1
  55. package/build/types/nodeAsyncContextStrategy.d.ts +0 -4
  56. package/build/types/nodeAsyncContextStrategy.d.ts.map +0 -1
  57. package/build/types/resource.d.ts +0 -26
  58. package/build/types/resource.d.ts.map +0 -1
  59. package/build/types/sampler.d.ts +0 -32
  60. package/build/types/sampler.d.ts.map +0 -1
  61. package/build/types/semanticAttributes.d.ts +0 -4
  62. package/build/types/semanticAttributes.d.ts.map +0 -1
  63. package/build/types/setupEventContextTrace.d.ts +0 -4
  64. package/build/types/setupEventContextTrace.d.ts.map +0 -1
  65. package/build/types/spanExporter.d.ts +0 -50
  66. package/build/types/spanExporter.d.ts.map +0 -1
  67. package/build/types/spanProcessor.d.ts +0 -31
  68. package/build/types/spanProcessor.d.ts.map +0 -1
  69. package/build/types/utils/backfillStreamedSpanData.d.ts +0 -16
  70. package/build/types/utils/backfillStreamedSpanData.d.ts.map +0 -1
  71. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +0 -7
  72. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts.map +0 -1
  73. package/build/types/utils/getParentSpanId.d.ts +0 -8
  74. package/build/types/utils/getParentSpanId.d.ts.map +0 -1
  75. package/build/types/utils/getRequestSpanData.d.ts +0 -8
  76. package/build/types/utils/getRequestSpanData.d.ts.map +0 -1
  77. package/build/types/utils/getSpanKind.d.ts +0 -10
  78. package/build/types/utils/getSpanKind.d.ts.map +0 -1
  79. package/build/types/utils/getTraceData.d.ts +0 -12
  80. package/build/types/utils/getTraceData.d.ts.map +0 -1
  81. package/build/types/utils/groupSpansWithParents.d.ts +0 -17
  82. package/build/types/utils/groupSpansWithParents.d.ts.map +0 -1
  83. package/build/types/utils/isSentryRequest.d.ts +0 -8
  84. package/build/types/utils/isSentryRequest.d.ts.map +0 -1
  85. package/build/types/utils/mapStatus.d.ts +0 -9
  86. package/build/types/utils/mapStatus.d.ts.map +0 -1
  87. package/build/types/utils/parseSpanDescription.d.ts +0 -56
  88. package/build/types/utils/parseSpanDescription.d.ts.map +0 -1
  89. package/build/types/utils/setupCheck.d.ts +0 -9
  90. package/build/types/utils/setupCheck.d.ts.map +0 -1
  91. package/build/types/utils/spanTypes.d.ts +0 -52
  92. package/build/types/utils/spanTypes.d.ts.map +0 -1
  93. package/build/types-ts3.8/applyOtelSpanData.d.ts +0 -18
  94. package/build/types-ts3.8/asyncContextStrategy.d.ts +0 -9
  95. package/build/types-ts3.8/asyncLocalStorageContextManager.d.ts +0 -31
  96. package/build/types-ts3.8/constants.d.ts +0 -24
  97. package/build/types-ts3.8/contextManager.d.ts +0 -22
  98. package/build/types-ts3.8/custom/client.d.ts +0 -12
  99. package/build/types-ts3.8/debug-build.d.ts +0 -7
  100. package/build/types-ts3.8/exports.d.ts +0 -27
  101. package/build/types-ts3.8/index.browser.d.ts +0 -10
  102. package/build/types-ts3.8/index.d.ts +0 -5
  103. package/build/types-ts3.8/nodeAsyncContextStrategy.d.ts +0 -4
  104. package/build/types-ts3.8/propagator.d.ts +0 -43
  105. package/build/types-ts3.8/resource.d.ts +0 -29
  106. package/build/types-ts3.8/sampler.d.ts +0 -32
  107. package/build/types-ts3.8/semanticAttributes.d.ts +0 -4
  108. package/build/types-ts3.8/setupEventContextTrace.d.ts +0 -4
  109. package/build/types-ts3.8/spanExporter.d.ts +0 -50
  110. package/build/types-ts3.8/spanProcessor.d.ts +0 -31
  111. package/build/types-ts3.8/trace.d.ts +0 -75
  112. package/build/types-ts3.8/tracer.d.ts +0 -13
  113. package/build/types-ts3.8/tracerProvider.d.ts +0 -23
  114. package/build/types-ts3.8/types.d.ts +0 -29
  115. package/build/types-ts3.8/utils/TraceState.d.ts +0 -21
  116. package/build/types-ts3.8/utils/backfillStreamedSpanData.d.ts +0 -16
  117. package/build/types-ts3.8/utils/buildContextWithSentryScopes.d.ts +0 -12
  118. package/build/types-ts3.8/utils/contextData.d.ts +0 -30
  119. package/build/types-ts3.8/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +0 -7
  120. package/build/types-ts3.8/utils/getActiveSpan.d.ts +0 -6
  121. package/build/types-ts3.8/utils/getParentSpanId.d.ts +0 -8
  122. package/build/types-ts3.8/utils/getRequestSpanData.d.ts +0 -8
  123. package/build/types-ts3.8/utils/getSamplingDecision.d.ts +0 -27
  124. package/build/types-ts3.8/utils/getSpanKind.d.ts +0 -10
  125. package/build/types-ts3.8/utils/getTraceData.d.ts +0 -12
  126. package/build/types-ts3.8/utils/groupSpansWithParents.d.ts +0 -17
  127. package/build/types-ts3.8/utils/isSentryRequest.d.ts +0 -8
  128. package/build/types-ts3.8/utils/makeTraceState.d.ts +0 -10
  129. package/build/types-ts3.8/utils/mapStatus.d.ts +0 -9
  130. package/build/types-ts3.8/utils/parseSpanDescription.d.ts +0 -56
  131. package/build/types-ts3.8/utils/setupCheck.d.ts +0 -9
  132. package/build/types-ts3.8/utils/spanTypes.d.ts +0 -52
  133. package/build/types-ts3.8/utils/suppressTracing.d.ts +0 -5
@@ -1,24 +1,505 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
- const asyncContextStrategy = require('./asyncContextStrategy-volGaYqZ.js');
4
3
  const core = require('@sentry/core');
5
4
  const api = require('@opentelemetry/api');
5
+ const attributes = require('@sentry/conventions/attributes');
6
6
  const node_async_hooks = require('node:async_hooks');
7
- const node_events = require('node:events');
7
+
8
+ const SENTRY_TRACE_STATE_DSC = "sentry.dsc";
9
+ const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = "sentry.sampled_not_recording";
10
+ const SENTRY_SCOPES_CONTEXT_KEY = api.createContextKey("sentry_scopes");
11
+ const SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY = api.createContextKey("sentry_fork_isolation_scope");
12
+ const SENTRY_FORK_SET_SCOPE_CONTEXT_KEY = api.createContextKey("sentry_fork_set_scope");
13
+ const SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY = api.createContextKey("sentry_fork_set_isolation_scope");
14
+
15
+ const SCOPE_CONTEXT_FIELD = "context";
16
+ function getScopesFromContext(context) {
17
+ return context.getValue(SENTRY_SCOPES_CONTEXT_KEY);
18
+ }
19
+ function setScopesOnContext(context, scopes) {
20
+ return context.setValue(SENTRY_SCOPES_CONTEXT_KEY, scopes);
21
+ }
22
+ function setContextOnScope(scope, context) {
23
+ scope.refs[SCOPE_CONTEXT_FIELD] = core.makeWeakRef(context);
24
+ }
25
+ function getContextFromScope(scope) {
26
+ return core.derefWeakRef(scope.refs[SCOPE_CONTEXT_FIELD]);
27
+ }
28
+
29
+ class TraceState {
30
+ constructor() {
31
+ this._internalState = /* @__PURE__ */ new Map();
32
+ }
33
+ /** @inheritDoc */
34
+ set(key, value) {
35
+ const next = this._clone();
36
+ if (next._internalState.has(key)) {
37
+ next._internalState.delete(key);
38
+ }
39
+ next._internalState.set(key, value);
40
+ return next;
41
+ }
42
+ /** @inheritDoc */
43
+ unset(key) {
44
+ const next = this._clone();
45
+ next._internalState.delete(key);
46
+ return next;
47
+ }
48
+ /** @inheritDoc */
49
+ get(key) {
50
+ return this._internalState.get(key);
51
+ }
52
+ /** @inheritDoc */
53
+ serialize() {
54
+ return Array.from(this._internalState.keys()).reverse().map((key) => `${key}=${this._internalState.get(key)}`).join(",");
55
+ }
56
+ _clone() {
57
+ const next = new TraceState();
58
+ next._internalState = new Map(this._internalState);
59
+ return next;
60
+ }
61
+ }
62
+
63
+ function makeTraceState({
64
+ dsc,
65
+ sampled
66
+ }) {
67
+ const dscString = dsc ? core.dynamicSamplingContextToSentryBaggageHeader(dsc) : void 0;
68
+ const traceStateBase = new TraceState();
69
+ const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
70
+ return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, "1") : traceStateWithDsc;
71
+ }
72
+
73
+ const SENTRY_TRACE_HEADER = "sentry-trace";
74
+ const SENTRY_BAGGAGE_HEADER = "baggage";
75
+ const W3C_TRACEPARENT_HEADER = "traceparent";
76
+ class SentryPropagator {
77
+ /** @inheritDoc */
78
+ inject(ctx, carrier, setter) {
79
+ if (ctx !== api.context.active()) {
80
+ core.consoleSandbox(() => {
81
+ console.warn(
82
+ "SentryPropagator: Injecting trace data of a different context than the active one is not supported. Skipping injection."
83
+ );
84
+ });
85
+ return;
86
+ }
87
+ if (core.isTracingSuppressed()) {
88
+ return;
89
+ }
90
+ const { propagateTraceparent } = core.getClient()?.getOptions() ?? {};
91
+ const { "sentry-trace": sentryTrace, baggage, traceparent } = core.getTraceData({ propagateTraceparent });
92
+ if (sentryTrace) {
93
+ setter.set(carrier, SENTRY_TRACE_HEADER, sentryTrace);
94
+ }
95
+ if (baggage) {
96
+ setter.set(carrier, SENTRY_BAGGAGE_HEADER, baggage);
97
+ }
98
+ if (traceparent) {
99
+ setter.set(carrier, W3C_TRACEPARENT_HEADER, traceparent);
100
+ }
101
+ }
102
+ /** @inheritDoc */
103
+ extract(ctx, carrier, getter) {
104
+ const maybeSentryTraceHeader = getter.get(carrier, SENTRY_TRACE_HEADER);
105
+ const baggage = getter.get(carrier, SENTRY_BAGGAGE_HEADER);
106
+ const sentryTrace = Array.isArray(maybeSentryTraceHeader) ? maybeSentryTraceHeader[0] : maybeSentryTraceHeader;
107
+ return getContextWithRemoteActiveSpanAndScopes(ctx, { sentryTrace, baggage });
108
+ }
109
+ /** @inheritDoc */
110
+ fields() {
111
+ return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER, W3C_TRACEPARENT_HEADER];
112
+ }
113
+ }
114
+ function getContextWithRemoteActiveSpan(ctx, { sentryTrace, baggage }) {
115
+ const propagationContext = core.propagationContextFromHeaders(sentryTrace, baggage);
116
+ const { traceId, parentSpanId, sampled, dsc } = propagationContext;
117
+ const client = core.getClient();
118
+ const incomingDsc = core.baggageHeaderToDynamicSamplingContext(baggage);
119
+ if (!parentSpanId || client && !core.shouldContinueTrace(client, incomingDsc?.org_id)) {
120
+ return ctx;
121
+ }
122
+ const spanContext = generateRemoteSpanContext({
123
+ traceId,
124
+ spanId: parentSpanId,
125
+ sampled,
126
+ dsc
127
+ });
128
+ return api.trace.setSpanContext(ctx, spanContext);
129
+ }
130
+ function getContextWithRemoteActiveSpanAndScopes(ctx, options) {
131
+ const ctxWithRemoteSpan = getContextWithRemoteActiveSpan(ctx, options);
132
+ const isContinuingTrace = api.trace.getSpanContext(ctxWithRemoteSpan) !== void 0;
133
+ return ensureScopesOnContext(ctxWithRemoteSpan, isContinuingTrace);
134
+ }
135
+ function ensureScopesOnContext(ctx, isContinuingTrace) {
136
+ const scopes = getScopesFromContext(ctx);
137
+ const scope = scopes ? scopes.scope : core.getCurrentScope().clone();
138
+ if (!scopes && !isContinuingTrace) {
139
+ const propagationContext = scope.getPropagationContext();
140
+ scope.setPropagationContext({
141
+ ...propagationContext,
142
+ traceId: core.generateTraceId(),
143
+ sampleRand: core._INTERNAL_safeMathRandom()
144
+ });
145
+ }
146
+ const newScopes = {
147
+ scope,
148
+ isolationScope: scopes ? scopes.isolationScope : core.getIsolationScope()
149
+ };
150
+ return setScopesOnContext(ctx, newScopes);
151
+ }
152
+ function generateRemoteSpanContext({
153
+ spanId,
154
+ traceId,
155
+ sampled,
156
+ dsc
157
+ }) {
158
+ const traceState = makeTraceState({
159
+ dsc,
160
+ sampled
161
+ });
162
+ const spanContext = {
163
+ traceId,
164
+ spanId,
165
+ isRemote: true,
166
+ traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
167
+ traceState
168
+ };
169
+ return spanContext;
170
+ }
171
+
172
+ const SUPPRESS_TRACING_KEY = api.createContextKey("OpenTelemetry SDK Context Key SUPPRESS_TRACING");
173
+ function suppressTracing(context) {
174
+ return context.setValue(SUPPRESS_TRACING_KEY, true);
175
+ }
176
+ function isTracingSuppressed(context) {
177
+ return context.getValue(SUPPRESS_TRACING_KEY) === true;
178
+ }
179
+
180
+ function getSamplingDecision(spanContext) {
181
+ const { traceFlags, traceState } = spanContext;
182
+ const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === "1" : false;
183
+ if (traceFlags === api.TraceFlags.SAMPLED) {
184
+ return true;
185
+ }
186
+ if (sampledNotRecording) {
187
+ return false;
188
+ }
189
+ const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : void 0;
190
+ const dsc = dscString ? core.baggageHeaderToDynamicSamplingContext(dscString) : void 0;
191
+ if (dsc?.sampled === "true") {
192
+ return true;
193
+ }
194
+ if (dsc?.sampled === "false") {
195
+ return false;
196
+ }
197
+ return void 0;
198
+ }
199
+
200
+ class SentryTracer {
201
+ /** @inheritdoc */
202
+ startSpan(name, options = {}, ctx) {
203
+ const parentContext = ctx || api.context.active();
204
+ const parentSpanCandidate = options.root ? void 0 : api.trace.getSpan(parentContext);
205
+ const parentSpan = parentSpanCandidate && api.isSpanContextValid(parentSpanCandidate.spanContext()) ? parentSpanCandidate : void 0;
206
+ if (isTracingSuppressed(parentContext)) {
207
+ return this._createNonRecordingSpan(parentSpan);
208
+ }
209
+ const span = this._startSentrySpan(name, options, parentSpan, ctx !== void 0);
210
+ core.markSpanAsTracerProviderSpan(span);
211
+ return span;
212
+ }
213
+ startActiveSpan(name, optionsOrFn, contextOrFn, fn) {
214
+ const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
215
+ const ctx = typeof contextOrFn === "function" || contextOrFn === void 0 ? api.context.active() : contextOrFn;
216
+ const callback = typeof optionsOrFn === "function" ? optionsOrFn : typeof contextOrFn === "function" ? contextOrFn : fn;
217
+ const span = this.startSpan(name, options, ctx);
218
+ const capturedIsolationScope = core.getCapturedScopesOnSpan(span).isolationScope;
219
+ const withCapturedIsolationScope = (contextToFork) => capturedIsolationScope ? contextToFork.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, capturedIsolationScope) : contextToFork;
220
+ if (core.spanIsIgnored(span) && api.trace.getSpan(ctx)) {
221
+ return api.context.with(withCapturedIsolationScope(ctx), () => callback(span));
222
+ }
223
+ return api.context.with(withCapturedIsolationScope(api.trace.setSpan(ctx, span)), () => {
224
+ core._INTERNAL_setSpanForScope(core.getCurrentScope(), span);
225
+ return callback(span);
226
+ });
227
+ }
228
+ _startSentrySpan(name, options, parentSpan, hasExplicitContext) {
229
+ const sentryOptions = {
230
+ name,
231
+ attributes: options.attributes || {},
232
+ links: options.links,
233
+ startTime: options.startTime
234
+ };
235
+ if (options.kind) {
236
+ sentryOptions.attributes[attributes.SENTRY_KIND] = core.spanKindToName(options.kind);
237
+ }
238
+ if (options.root) {
239
+ return core.startNewTrace(() => core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan: null }));
240
+ }
241
+ if (parentSpan?.spanContext().isRemote) {
242
+ return this._startRootSpanWithRemoteParent(sentryOptions, parentSpan);
243
+ }
244
+ if (parentSpan) {
245
+ return core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan });
246
+ }
247
+ return core._INTERNAL_startInactiveSpan({
248
+ ...sentryOptions,
249
+ parentSpan: hasExplicitContext ? null : void 0
250
+ });
251
+ }
252
+ _startRootSpanWithRemoteParent(options, parentSpan) {
253
+ const { spanId, traceId, traceState } = parentSpan.spanContext();
254
+ const dsc = core.getDynamicSamplingContextFromSpan(parentSpan);
255
+ const sampleRand = typeof dsc.sample_rand === "string" ? Number(dsc.sample_rand) : void 0;
256
+ const hasIncomingDsc = !!traceState?.get(SENTRY_TRACE_STATE_DSC);
257
+ return core.withScope((scope) => {
258
+ scope.setPropagationContext({
259
+ traceId,
260
+ parentSpanId: spanId,
261
+ sampled: getSamplingDecision(parentSpan.spanContext()),
262
+ dsc: hasIncomingDsc ? dsc : void 0,
263
+ sampleRand: typeof sampleRand === "number" && !Number.isNaN(sampleRand) ? sampleRand : core._INTERNAL_safeMathRandom()
264
+ });
265
+ core._INTERNAL_setSpanForScope(scope, void 0);
266
+ return core._INTERNAL_startInactiveSpan({ ...options, parentSpan: null });
267
+ });
268
+ }
269
+ _createNonRecordingSpan(parentSpan) {
270
+ const traceId = parentSpan?.spanContext().traceId ?? core.getCurrentScope().getPropagationContext().traceId;
271
+ const span = new core.SentryNonRecordingSpan({ traceId });
272
+ if (parentSpan) {
273
+ core.addChildSpanToSpan(parentSpan, span);
274
+ }
275
+ core.setCapturedScopesOnSpan(span, core.getCurrentScope(), core.getIsolationScope());
276
+ return span;
277
+ }
278
+ }
279
+
280
+ class SentryTracerProvider {
281
+ constructor() {
282
+ this._tracers = /* @__PURE__ */ new Map();
283
+ }
284
+ /** @inheritdoc */
285
+ getTracer(name, version, options) {
286
+ const key = JSON.stringify([name, version, options]);
287
+ const cachedTracer = this._tracers.get(key);
288
+ if (cachedTracer) {
289
+ return cachedTracer;
290
+ }
291
+ const tracer = new SentryTracer();
292
+ this._tracers.set(key, tracer);
293
+ return tracer;
294
+ }
295
+ /** Compatibility with SDK tracer providers. */
296
+ forceFlush() {
297
+ return Promise.resolve();
298
+ }
299
+ /** Compatibility with SDK tracer providers. */
300
+ shutdown() {
301
+ return Promise.resolve();
302
+ }
303
+ }
304
+
305
+ function reconcileDscSampled(dsc, sampled) {
306
+ const reconciled = { ...dsc };
307
+ if (sampled === void 0) {
308
+ delete reconciled.sampled;
309
+ } else {
310
+ reconciled.sampled = String(sampled);
311
+ if (sampled === false) {
312
+ delete reconciled.transaction;
313
+ }
314
+ }
315
+ return reconciled;
316
+ }
317
+
318
+ function _startSpan(options, callback, autoEnd) {
319
+ const tracer = getTracer();
320
+ const { name, parentSpan: customParentSpan } = options;
321
+ const wrapper = getActiveSpanWrapper(customParentSpan);
322
+ return wrapper(() => {
323
+ const activeCtx = getContext(options.scope, options.forceTransaction);
324
+ const missingRequiredParent = options.onlyIfParent && !api.trace.getSpan(activeCtx);
325
+ const ctx = missingRequiredParent ? suppressTracing(activeCtx) : activeCtx;
326
+ if (missingRequiredParent) {
327
+ core.getClient()?.recordDroppedEvent("no_parent_span", "span");
328
+ }
329
+ const spanOptions = getSpanOptions(options);
330
+ if (!core.hasSpansEnabled()) {
331
+ const suppressedCtx = isTracingSuppressed(ctx) ? ctx : suppressTracing(ctx);
332
+ return api.context.with(suppressedCtx, () => {
333
+ return tracer.startActiveSpan(name, spanOptions, suppressedCtx, (span) => {
334
+ patchSpanEnd(span);
335
+ return api.context.with(api.trace.setSpan(activeCtx, span), () => {
336
+ return core.handleCallbackErrors(
337
+ () => callback(span),
338
+ () => {
339
+ if (core.spanToStaticSpanJSON(span).status === "ok") {
340
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
341
+ }
342
+ },
343
+ autoEnd ? () => span.end() : void 0
344
+ );
345
+ });
346
+ });
347
+ });
348
+ }
349
+ return tracer.startActiveSpan(name, spanOptions, ctx, (span) => {
350
+ patchSpanEnd(span);
351
+ return core.handleCallbackErrors(
352
+ () => callback(span),
353
+ () => {
354
+ if (core.spanToStaticSpanJSON(span).status === "ok") {
355
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
356
+ }
357
+ },
358
+ autoEnd ? () => span.end() : void 0
359
+ );
360
+ });
361
+ });
362
+ }
363
+ function startSpan(options, callback) {
364
+ return _startSpan(options, callback, true);
365
+ }
366
+ function startSpanManual(options, callback) {
367
+ return _startSpan(options, (span) => callback(span, () => span.end()), false);
368
+ }
369
+ function startInactiveSpan(options) {
370
+ const tracer = getTracer();
371
+ const { name, parentSpan: customParentSpan } = options;
372
+ const wrapper = getActiveSpanWrapper(customParentSpan);
373
+ return wrapper(() => {
374
+ const activeCtx = getContext(options.scope, options.forceTransaction);
375
+ const missingRequiredParent = options.onlyIfParent && !api.trace.getSpan(activeCtx);
376
+ let ctx = missingRequiredParent ? suppressTracing(activeCtx) : activeCtx;
377
+ if (missingRequiredParent) {
378
+ core.getClient()?.recordDroppedEvent("no_parent_span", "span");
379
+ }
380
+ const spanOptions = getSpanOptions(options);
381
+ if (!core.hasSpansEnabled()) {
382
+ ctx = isTracingSuppressed(ctx) ? ctx : suppressTracing(ctx);
383
+ }
384
+ const span = tracer.startSpan(name, spanOptions, ctx);
385
+ patchSpanEnd(span);
386
+ return span;
387
+ });
388
+ }
389
+ function withActiveSpan(span, callback) {
390
+ const newContextWithActiveSpan = span ? api.trace.setSpan(api.context.active(), span) : api.trace.deleteSpan(api.context.active());
391
+ return api.context.with(newContextWithActiveSpan, () => callback(core.getCurrentScope()));
392
+ }
393
+ function getTracer() {
394
+ const client = core.getClient();
395
+ return client?.tracer || api.trace.getTracer("@sentry/opentelemetry", core.SDK_VERSION);
396
+ }
397
+ function getSpanOptions(options) {
398
+ const { startTime, attributes, op, links } = options;
399
+ const fixedStartTime = typeof startTime === "number" ? ensureTimestampInMilliseconds(startTime) : startTime;
400
+ return {
401
+ attributes: op ? {
402
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
403
+ ...attributes
404
+ } : attributes,
405
+ links,
406
+ startTime: fixedStartTime
407
+ };
408
+ }
409
+ function ensureTimestampInMilliseconds(timestamp) {
410
+ const isMs = timestamp < 9999999999;
411
+ return isMs ? timestamp * 1e3 : timestamp;
412
+ }
413
+ function patchSpanEnd(span) {
414
+ const originalEnd = span.end.bind(span);
415
+ span.end = (endTime) => {
416
+ return originalEnd(typeof endTime === "number" ? ensureTimestampInMilliseconds(endTime) : endTime);
417
+ };
418
+ }
419
+ function getContext(scope, forceTransaction) {
420
+ const ctx = getContextForScope(scope);
421
+ const parentSpan = api.trace.getSpan(ctx);
422
+ if (!parentSpan) {
423
+ return ctx;
424
+ }
425
+ if (!forceTransaction) {
426
+ return ctx;
427
+ }
428
+ const ctxWithoutSpan = api.trace.deleteSpan(ctx);
429
+ const { spanId, traceId } = parentSpan.spanContext();
430
+ const sampled = getSamplingDecision(parentSpan.spanContext());
431
+ const rootSpan = core.getRootSpan(parentSpan);
432
+ const rawDsc = core.getDynamicSamplingContextFromSpan(rootSpan);
433
+ const hasIncomingFrozenDsc = !!rootSpan.spanContext().traceState?.get(SENTRY_TRACE_STATE_DSC);
434
+ const dsc = hasIncomingFrozenDsc ? rawDsc : reconcileDscSampled(rawDsc, sampled);
435
+ const traceState = makeTraceState({
436
+ dsc,
437
+ sampled
438
+ });
439
+ const spanOptions = {
440
+ traceId,
441
+ spanId,
442
+ isRemote: true,
443
+ traceFlags: sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
444
+ traceState
445
+ };
446
+ const ctxWithSpanContext = api.trace.setSpanContext(ctxWithoutSpan, spanOptions);
447
+ return ctxWithSpanContext;
448
+ }
449
+ function getContextForScope(scope) {
450
+ if (scope) {
451
+ const ctx = getContextFromScope(scope);
452
+ if (ctx) {
453
+ return ctx;
454
+ }
455
+ }
456
+ return api.context.active();
457
+ }
458
+ function getActiveSpanWrapper(parentSpan) {
459
+ return parentSpan !== void 0 ? (callback) => {
460
+ return withActiveSpan(parentSpan, callback);
461
+ } : (callback) => callback();
462
+ }
463
+
464
+ function getActiveSpan(scope) {
465
+ if (!scope) {
466
+ return api.trace.getActiveSpan();
467
+ }
468
+ const ctx = getContextFromScope(scope);
469
+ if (ctx) {
470
+ return api.trace.getSpan(ctx);
471
+ }
472
+ return void 0;
473
+ }
474
+
475
+ function buildContextWithSentryScopes(context) {
476
+ const currentScopes = getScopesFromContext(context);
477
+ const currentScope = currentScopes?.scope || core.getCurrentScope();
478
+ const currentIsolationScope = currentScopes?.isolationScope || core.getIsolationScope();
479
+ const shouldForkIsolationScope = context.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
480
+ const scope = context.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY);
481
+ const isolationScope = context.getValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY);
482
+ const newCurrentScope = scope || currentScope.clone();
483
+ const newIsolationScope = isolationScope || (shouldForkIsolationScope ? currentIsolationScope.clone() : currentIsolationScope);
484
+ const scopes = { scope: newCurrentScope, isolationScope: newIsolationScope };
485
+ const ctx1 = setScopesOnContext(context, scopes);
486
+ const ctx2 = ctx1.deleteValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY).deleteValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY).deleteValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY);
487
+ setContextOnScope(newCurrentScope, ctx2);
488
+ return ctx2;
489
+ }
8
490
 
9
491
  const ADD_LISTENER_METHODS = ["addListener", "on", "once", "prependListener", "prependOnceListener"];
10
492
  class SentryAsyncLocalStorageContextManager {
11
- constructor() {
493
+ constructor(asyncLocalStorage) {
12
494
  this._kOtListeners = /* @__PURE__ */ Symbol("OtListeners");
13
495
  this._wrapped = false;
14
- asyncContextStrategy.setIsSetup("SentryContextManager");
15
- this._asyncLocalStorage = core.getAsyncContextStrategy(core.getMainCarrier()).getTracingChannelBinding?.()?.asyncLocalStorage ?? new node_async_hooks.AsyncLocalStorage();
496
+ this._asyncLocalStorage = asyncLocalStorage;
16
497
  }
17
498
  active() {
18
499
  return this._asyncLocalStorage.getStore() ?? api.ROOT_CONTEXT;
19
500
  }
20
501
  with(context, fn, thisArg, ...args) {
21
- const ctx2 = asyncContextStrategy.buildContextWithSentryScopes(context, this.active());
502
+ const ctx2 = buildContextWithSentryScopes(context);
22
503
  const cb = thisArg == null ? fn : fn.bind(thisArg);
23
504
  return this._asyncLocalStorage.run(ctx2, cb, ...args);
24
505
  }
@@ -26,11 +507,14 @@ class SentryAsyncLocalStorageContextManager {
26
507
  return this;
27
508
  }
28
509
  disable() {
29
- this._asyncLocalStorage.disable();
510
+ try {
511
+ this._asyncLocalStorage.disable();
512
+ } catch {
513
+ }
30
514
  return this;
31
515
  }
32
516
  bind(context, target) {
33
- if (target instanceof node_events.EventEmitter) {
517
+ if (isEventEmitter(target)) {
34
518
  return this._bindEventEmitter(context, target);
35
519
  }
36
520
  if (typeof target === "function") {
@@ -45,7 +529,7 @@ class SentryAsyncLocalStorageContextManager {
45
529
  getAsyncLocalStorageLookup() {
46
530
  return {
47
531
  asyncLocalStorage: this._asyncLocalStorage,
48
- contextSymbol: asyncContextStrategy.SENTRY_SCOPES_CONTEXT_KEY
532
+ contextSymbol: SENTRY_SCOPES_CONTEXT_KEY
49
533
  };
50
534
  }
51
535
  _bindFunction(context, target) {
@@ -65,27 +549,22 @@ class SentryAsyncLocalStorageContextManager {
65
549
  if (this._getPatchMap(ee) !== void 0) {
66
550
  return ee;
67
551
  }
68
- this._createPatchMap(ee);
69
- for (const methodName of ADD_LISTENER_METHODS) {
70
- if (ee[methodName] === void 0) continue;
71
- ee[methodName] = this._patchAddListener(
72
- ee,
73
- ee[methodName],
74
- context
75
- );
552
+ if (this._createPatchMap(ee) === void 0) {
553
+ return ee;
76
554
  }
77
- if (typeof ee.removeListener === "function") {
78
- ee.removeListener = this._patchRemoveListener(ee, ee.removeListener);
555
+ for (const methodName of ADD_LISTENER_METHODS) {
556
+ const original = getMethod(ee, methodName);
557
+ if (!original) continue;
558
+ trySetMethod(ee, methodName, this._patchAddListener(ee, original, context));
79
559
  }
80
- if (typeof ee.off === "function") {
81
- ee.off = this._patchRemoveListener(ee, ee.off);
560
+ for (const methodName of ["removeListener", "off"]) {
561
+ const original = getMethod(ee, methodName);
562
+ if (!original) continue;
563
+ trySetMethod(ee, methodName, this._patchRemoveListener(ee, original));
82
564
  }
83
- if (typeof ee.removeAllListeners === "function") {
84
- ee.removeAllListeners = this._patchRemoveAllListeners(
85
- ee,
86
- // oxlint-disable-next-line @typescript-eslint/unbound-method
87
- ee.removeAllListeners
88
- );
565
+ const removeAllListeners = getMethod(ee, "removeAllListeners");
566
+ if (removeAllListeners) {
567
+ trySetMethod(ee, "removeAllListeners", this._patchRemoveAllListeners(ee, removeAllListeners));
89
568
  }
90
569
  return ee;
91
570
  }
@@ -120,9 +599,9 @@ class SentryAsyncLocalStorageContextManager {
120
599
  if (contextManager._wrapped) {
121
600
  return original.call(this, event, listener);
122
601
  }
123
- let map = contextManager._getPatchMap(ee);
602
+ const map = contextManager._getPatchMap(ee) ?? contextManager._createPatchMap(ee);
124
603
  if (map === void 0) {
125
- map = contextManager._createPatchMap(ee);
604
+ return original.call(this, event, listener);
126
605
  }
127
606
  let listeners = map[event];
128
607
  if (listeners === void 0) {
@@ -139,87 +618,121 @@ class SentryAsyncLocalStorageContextManager {
139
618
  }
140
619
  };
141
620
  }
621
+ /**
622
+ * Attach a fresh patch map to the emitter. Returns `undefined` if the emitter does not accept the
623
+ * property (e.g. it is frozen or sealed), in which case the emitter must not be patched at all —
624
+ * without a patch map the remove-listener patches could not resolve their wrapped listeners.
625
+ */
142
626
  _createPatchMap(ee) {
143
627
  const map = /* @__PURE__ */ Object.create(null);
144
- ee[this._kOtListeners] = map;
145
- return map;
628
+ try {
629
+ ee[this._kOtListeners] = map;
630
+ } catch {
631
+ return void 0;
632
+ }
633
+ return this._getPatchMap(ee) === map ? map : void 0;
146
634
  }
147
635
  _getPatchMap(ee) {
148
636
  return ee[this._kOtListeners];
149
637
  }
150
638
  }
151
-
152
- function setNodeOpenTelemetryContextAsyncContextStrategy(options) {
153
- asyncContextStrategy.setOpenTelemetryContextAsyncContextStrategy({
154
- getTracingChannelBinding: !options?.skipOpenTelemetrySetup ? getDefaultAsyncLocalStorageFactory() : getCustomAsyncLocalStorageFactory()
155
- });
639
+ function isEventEmitter(target) {
640
+ if (typeof target !== "object" || !target) {
641
+ return false;
642
+ }
643
+ const candidate = target;
644
+ return typeof candidate.on === "function" && typeof candidate.emit === "function";
156
645
  }
157
- function getDefaultAsyncLocalStorageFactory() {
158
- const defaultAsyncLocalStorage = new node_async_hooks.AsyncLocalStorage();
159
- return () => {
160
- return {
161
- asyncLocalStorage: defaultAsyncLocalStorage,
162
- getStoreWithActiveSpan
163
- };
164
- };
646
+ function getMethod(ee, methodName) {
647
+ const value = ee[methodName];
648
+ return typeof value === "function" ? value : void 0;
165
649
  }
166
- function getCustomAsyncLocalStorageFactory() {
167
- return () => {
168
- try {
169
- const contextManager = api.context._getContextManager();
170
- const asyncLocalStorage = contextManager?.getAsyncLocalStorageLookup().asyncLocalStorage;
171
- return asyncLocalStorage ? {
172
- asyncLocalStorage,
173
- getStoreWithActiveSpan
174
- } : void 0;
175
- } catch {
176
- return void 0;
650
+ function trySetMethod(ee, methodName, patched) {
651
+ try {
652
+ ee[methodName] = patched;
653
+ } catch {
654
+ }
655
+ }
656
+
657
+ function setOpenTelemetryContextAsyncContextStrategy() {
658
+ const existingAsyncLocalStorage = core.getAsyncContextStrategy(core.getMainCarrier()).getTracingChannelBinding?.()?.asyncLocalStorage;
659
+ const asyncLocalStorage = existingAsyncLocalStorage ?? new node_async_hooks.AsyncLocalStorage();
660
+ function getScopes() {
661
+ const ctx = api.context.active();
662
+ const scopes = getScopesFromContext(ctx);
663
+ if (scopes) {
664
+ return scopes;
177
665
  }
178
- };
666
+ return {
667
+ scope: core.getDefaultCurrentScope(),
668
+ isolationScope: core.getDefaultIsolationScope()
669
+ };
670
+ }
671
+ function withScope(callback) {
672
+ const ctx = api.context.active();
673
+ return api.context.with(ctx, () => {
674
+ return callback(getCurrentScope());
675
+ });
676
+ }
677
+ function withSetScope(scope, callback) {
678
+ const ctx = getContextFromScope(scope) || api.context.active();
679
+ return api.context.with(ctx.setValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY, scope), () => {
680
+ return callback(scope);
681
+ });
682
+ }
683
+ function withIsolationScope(callback) {
684
+ const ctx = api.context.active();
685
+ return api.context.with(ctx.setValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY, true), () => {
686
+ const scope = getCurrentScope();
687
+ if (!core.isContinuingTrace(scope.getPropagationContext())) {
688
+ scope.setPropagationContext({
689
+ traceId: core.generateTraceId(),
690
+ sampleRand: core._INTERNAL_safeMathRandom()
691
+ });
692
+ }
693
+ return callback(getIsolationScope());
694
+ });
695
+ }
696
+ function withSetIsolationScope(isolationScope, callback) {
697
+ const ctx = api.context.active();
698
+ return api.context.with(ctx.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, isolationScope), () => {
699
+ return callback(getIsolationScope());
700
+ });
701
+ }
702
+ function getCurrentScope() {
703
+ return getScopes().scope;
704
+ }
705
+ function getIsolationScope() {
706
+ return getScopes().isolationScope;
707
+ }
708
+ function getTracingChannelBinding() {
709
+ return {
710
+ asyncLocalStorage
711
+ };
712
+ }
713
+ core.setAsyncContextStrategy({
714
+ withScope,
715
+ withSetScope,
716
+ withSetIsolationScope,
717
+ withIsolationScope,
718
+ getCurrentScope,
719
+ getIsolationScope,
720
+ startSpan,
721
+ startSpanManual,
722
+ startInactiveSpan,
723
+ getActiveSpan,
724
+ // The types here don't fully align, because our own `Span` type is narrower
725
+ // than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
726
+ withActiveSpan,
727
+ getTracingChannelBinding
728
+ });
729
+ const ctxManager = new SentryAsyncLocalStorageContextManager(asyncLocalStorage);
730
+ api.context.setGlobalContextManager(ctxManager);
731
+ return ctxManager.getAsyncLocalStorageLookup();
179
732
  }
180
- function getStoreWithActiveSpan(span) {
181
- const activeContext = api.context.active();
182
- const isIgnoredChild = core.spanIsIgnored(span) && core.getRootSpan(span) !== span || span.spanContext().traceState?.get(asyncContextStrategy.SENTRY_TRACE_STATE_CHILD_IGNORED) === "1";
183
- return isIgnoredChild ? activeContext : api.trace.setSpan(activeContext, span);
184
- }
185
-
186
- exports.SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION = asyncContextStrategy.SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION;
187
- exports.SentryPropagator = asyncContextStrategy.SentryPropagator;
188
- exports.SentrySampler = asyncContextStrategy.SentrySampler;
189
- exports.SentrySpanProcessor = asyncContextStrategy.SentrySpanProcessor;
190
- exports.SentryTracerProvider = asyncContextStrategy.SentryTracerProvider;
191
- exports.applyOtelSpanData = asyncContextStrategy.applyOtelSpanData;
192
- exports.backfillStreamedSpanDataFromOtel = asyncContextStrategy.backfillStreamedSpanDataFromOtel;
193
- exports.continueTrace = asyncContextStrategy.continueTrace;
194
- exports.enhanceDscWithOpenTelemetryRootSpanName = asyncContextStrategy.enhanceDscWithOpenTelemetryRootSpanName;
195
- exports.getActiveSpan = asyncContextStrategy.getActiveSpan;
196
- exports.getRequestSpanData = asyncContextStrategy.getRequestSpanData;
197
- exports.getScopesFromContext = asyncContextStrategy.getScopesFromContext;
198
- exports.getSentryResource = asyncContextStrategy.getSentryResource;
199
- exports.getSpanKind = asyncContextStrategy.getSpanKind;
200
- exports.getTraceContextForScope = asyncContextStrategy.getTraceContextForScope;
201
- exports.isSentryRequestSpan = asyncContextStrategy.isSentryRequestSpan;
202
- exports.openTelemetrySetupCheck = asyncContextStrategy.openTelemetrySetupCheck;
203
- exports.setIsSetup = asyncContextStrategy.setIsSetup;
204
- exports.setupEventContextTrace = asyncContextStrategy.setupEventContextTrace;
205
- exports.spanHasAttributes = asyncContextStrategy.spanHasAttributes;
206
- exports.spanHasEvents = asyncContextStrategy.spanHasEvents;
207
- exports.spanHasKind = asyncContextStrategy.spanHasKind;
208
- exports.spanHasName = asyncContextStrategy.spanHasName;
209
- exports.spanHasParentId = asyncContextStrategy.spanHasParentId;
210
- exports.spanHasStatus = asyncContextStrategy.spanHasStatus;
211
- exports.startInactiveSpan = asyncContextStrategy.startInactiveSpan;
212
- exports.startSpan = asyncContextStrategy.startSpan;
213
- exports.startSpanManual = asyncContextStrategy.startSpanManual;
214
- exports.suppressTracing = asyncContextStrategy.suppressTracing;
215
- exports.withActiveSpan = asyncContextStrategy.withActiveSpan;
216
- exports.wrapClientClass = asyncContextStrategy.wrapClientClass;
217
- exports.wrapContextManagerClass = asyncContextStrategy.wrapContextManagerClass;
218
- exports.wrapSamplingDecision = asyncContextStrategy.wrapSamplingDecision;
219
- exports.getClient = core.getClient;
220
- exports.getDynamicSamplingContextFromSpan = core.getDynamicSamplingContextFromSpan;
221
- exports.shouldPropagateTraceForUrl = core.shouldPropagateTraceForUrl;
222
- exports.withStreamedSpan = core.withStreamedSpan;
223
- exports.SentryAsyncLocalStorageContextManager = SentryAsyncLocalStorageContextManager;
224
- exports.setOpenTelemetryContextAsyncContextStrategy = setNodeOpenTelemetryContextAsyncContextStrategy;
733
+
734
+ exports.SentryPropagator = SentryPropagator;
735
+ exports.SentryTracerProvider = SentryTracerProvider;
736
+ exports.getScopesFromContext = getScopesFromContext;
737
+ exports.setOpenTelemetryContextAsyncContextStrategy = setOpenTelemetryContextAsyncContextStrategy;
225
738
  //# sourceMappingURL=index.js.map