@sentry/opentelemetry 11.0.0-alpha.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 (35) hide show
  1. package/build/cjs/index.js +24 -260
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.js +28 -261
  4. package/build/esm/index.js.map +1 -1
  5. package/build/esm/package.json +1 -1
  6. package/build/types/asyncContextStrategy.d.ts.map +1 -1
  7. package/build/types/constants.d.ts +0 -5
  8. package/build/types/constants.d.ts.map +1 -1
  9. package/build/types/index.d.ts +0 -3
  10. package/build/types/index.d.ts.map +1 -1
  11. package/build/types/propagator.d.ts +0 -6
  12. package/build/types/propagator.d.ts.map +1 -1
  13. package/build/types/trace.d.ts +4 -5
  14. package/build/types/trace.d.ts.map +1 -1
  15. package/build/types/tracer.d.ts.map +1 -1
  16. package/build/types/types.d.ts +1 -5
  17. package/build/types/types.d.ts.map +1 -1
  18. package/build/types/utils/buildContextWithSentryScopes.d.ts +1 -1
  19. package/build/types/utils/buildContextWithSentryScopes.d.ts.map +1 -1
  20. package/build/types/utils/contextData.d.ts.map +1 -1
  21. package/build/types/utils/getActiveSpan.d.ts +2 -1
  22. package/build/types/utils/getActiveSpan.d.ts.map +1 -1
  23. package/build/types/utils/getSamplingDecision.d.ts +0 -17
  24. package/build/types/utils/getSamplingDecision.d.ts.map +1 -1
  25. package/package.json +3 -3
  26. package/build/types/applyOtelSpanData.d.ts +0 -15
  27. package/build/types/applyOtelSpanData.d.ts.map +0 -1
  28. package/build/types/setupEventContextTrace.d.ts +0 -4
  29. package/build/types/setupEventContextTrace.d.ts.map +0 -1
  30. package/build/types/utils/backfillStreamedSpanData.d.ts +0 -14
  31. package/build/types/utils/backfillStreamedSpanData.d.ts.map +0 -1
  32. package/build/types/utils/mapStatus.d.ts +0 -3
  33. package/build/types/utils/mapStatus.d.ts.map +0 -1
  34. package/build/types/utils/parseSpanDescription.d.ts +0 -32
  35. package/build/types/utils/parseSpanDescription.d.ts.map +0 -1
@@ -3,18 +3,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3
3
  const core = require('@sentry/core');
4
4
  const api = require('@opentelemetry/api');
5
5
  const attributes = require('@sentry/conventions/attributes');
6
- const op = require('@sentry/conventions/op');
7
6
  const node_async_hooks = require('node:async_hooks');
8
7
 
9
8
  const SENTRY_TRACE_STATE_DSC = "sentry.dsc";
10
9
  const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = "sentry.sampled_not_recording";
11
- const SENTRY_TRACE_STATE_CHILD_IGNORED = "sentry.ignored";
12
10
  const SENTRY_SCOPES_CONTEXT_KEY = api.createContextKey("sentry_scopes");
13
11
  const SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY = api.createContextKey("sentry_fork_isolation_scope");
14
12
  const SENTRY_FORK_SET_SCOPE_CONTEXT_KEY = api.createContextKey("sentry_fork_set_scope");
15
13
  const SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY = api.createContextKey("sentry_fork_set_isolation_scope");
16
14
 
17
- const SCOPE_CONTEXT_FIELD = "_scopeContext";
15
+ const SCOPE_CONTEXT_FIELD = "context";
18
16
  function getScopesFromContext(context) {
19
17
  return context.getValue(SENTRY_SCOPES_CONTEXT_KEY);
20
18
  }
@@ -22,33 +20,10 @@ function setScopesOnContext(context, scopes) {
22
20
  return context.setValue(SENTRY_SCOPES_CONTEXT_KEY, scopes);
23
21
  }
24
22
  function setContextOnScope(scope, context) {
25
- core.addNonEnumerableProperty(scope, SCOPE_CONTEXT_FIELD, core.makeWeakRef(context));
23
+ scope.refs[SCOPE_CONTEXT_FIELD] = core.makeWeakRef(context);
26
24
  }
27
25
  function getContextFromScope(scope) {
28
- return core.derefWeakRef(scope[SCOPE_CONTEXT_FIELD]);
29
- }
30
-
31
- function getActiveSpan() {
32
- return api.trace.getActiveSpan();
33
- }
34
-
35
- function setupEventContextTrace(client) {
36
- client.on("preprocessEvent", (event) => {
37
- const span = getActiveSpan();
38
- if (!span || event.type === "transaction") {
39
- return;
40
- }
41
- event.contexts = {
42
- trace: core.spanToTraceContext(span),
43
- ...event.contexts
44
- };
45
- const rootSpan = core.getRootSpan(span);
46
- event.sdkProcessingMetadata = {
47
- dynamicSamplingContext: core.getDynamicSamplingContextFromSpan(rootSpan),
48
- ...event.sdkProcessingMetadata
49
- };
50
- return event;
51
- });
26
+ return core.derefWeakRef(scope.refs[SCOPE_CONTEXT_FIELD]);
52
27
  }
53
28
 
54
29
  class TraceState {
@@ -194,205 +169,6 @@ function generateRemoteSpanContext({
194
169
  return spanContext;
195
170
  }
196
171
 
197
- const canonicalGrpcErrorCodesMap = {
198
- "1": "cancelled",
199
- "2": "unknown_error",
200
- "3": "invalid_argument",
201
- "4": "deadline_exceeded",
202
- "5": "not_found",
203
- "6": "already_exists",
204
- "7": "permission_denied",
205
- "8": "resource_exhausted",
206
- "9": "failed_precondition",
207
- "10": "aborted",
208
- "11": "out_of_range",
209
- "12": "unimplemented",
210
- "13": "internal_error",
211
- "14": "unavailable",
212
- "15": "data_loss",
213
- "16": "unauthenticated"
214
- };
215
- function inferStatusFromAttributes(attributes$1) {
216
- const httpCodeAttribute = attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] || attributes$1[attributes.HTTP_STATUS_CODE];
217
- const grpcCodeAttribute = attributes$1[attributes.RPC_GRPC_STATUS_CODE];
218
- const numberHttpCode = typeof httpCodeAttribute === "number" ? httpCodeAttribute : typeof httpCodeAttribute === "string" ? parseInt(httpCodeAttribute) : void 0;
219
- if (typeof numberHttpCode === "number") {
220
- return core.getSpanStatusFromHttpCode(numberHttpCode);
221
- }
222
- if (typeof grpcCodeAttribute === "string") {
223
- return { code: core.SPAN_STATUS_ERROR, message: canonicalGrpcErrorCodesMap[grpcCodeAttribute] || "unknown_error" };
224
- }
225
- return void 0;
226
- }
227
-
228
- function inferSpanData(attributes$1) {
229
- const httpMethod = attributes$1[attributes.HTTP_REQUEST_METHOD] || attributes$1[attributes.HTTP_METHOD];
230
- if (httpMethod) {
231
- return descriptionForHttpMethod(attributes$1);
232
- }
233
- const dbSystem = attributes$1[attributes.DB_SYSTEM_NAME] || attributes$1[attributes.DB_SYSTEM];
234
- const opIsCache = typeof attributes$1[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] === "string" && attributes$1[core.SEMANTIC_ATTRIBUTE_SENTRY_OP].startsWith("cache.");
235
- if (dbSystem && !opIsCache) {
236
- return descriptionForDbSystem(attributes$1);
237
- }
238
- const rpcService = attributes$1[attributes.RPC_SERVICE];
239
- if (rpcService) {
240
- return {
241
- op: "rpc"
242
- };
243
- }
244
- const messagingSystem = attributes$1[attributes.MESSAGING_SYSTEM];
245
- if (messagingSystem) {
246
- return {
247
- op: getMessagingOp(attributes$1[attributes.MESSAGING_OPERATION_TYPE])
248
- };
249
- }
250
- const faasTrigger = attributes$1[attributes.FAAS_TRIGGER];
251
- if (faasTrigger) {
252
- return {
253
- op: getFaasOp(faasTrigger)
254
- };
255
- }
256
- return { op: void 0 };
257
- }
258
- function getFaasOp(trigger) {
259
- switch (trigger) {
260
- case "http":
261
- return op.WEB_SERVER_HTTP_SERVER_SPAN_OP;
262
- case "pubsub":
263
- return op.MESSAGING_QUEUE_PROCESS_SPAN_OP;
264
- default:
265
- return op.GENERAL_FUNCTION_SPAN_OP;
266
- }
267
- }
268
- function getMessagingOp(operationType) {
269
- switch (operationType) {
270
- case "publish":
271
- case "send":
272
- return op.MESSAGING_QUEUE_PUBLISH_SPAN_OP;
273
- case "receive":
274
- return op.MESSAGING_QUEUE_RECEIVE_SPAN_OP;
275
- case "process":
276
- return op.MESSAGING_QUEUE_PROCESS_SPAN_OP;
277
- default:
278
- return op.MESSAGING_QUEUE_SPAN_OP;
279
- }
280
- }
281
- function descriptionForDbSystem(attributes) {
282
- const userDefinedName = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
283
- if (typeof userDefinedName === "string") {
284
- return {
285
- op: "db"
286
- };
287
- }
288
- if (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === "custom") {
289
- return { op: "db" };
290
- }
291
- return { op: "db" };
292
- }
293
- function descriptionForHttpMethod(attributes$1) {
294
- const kind = attributes$1[attributes.SENTRY_KIND];
295
- const op$1 = kind === "client" ? op.WEB_SERVER_HTTP_CLIENT_SPAN_OP : kind === "server" ? op.WEB_SERVER_HTTP_SERVER_SPAN_OP : op.WEB_SERVER_HTTP_SPAN_OP;
296
- const { urlPath, url, query, fragment } = getSanitizedUrl(attributes$1);
297
- if (!urlPath) {
298
- return { op: op$1 };
299
- }
300
- const data = {};
301
- if (url) {
302
- data[attributes.URL_FULL] = core.filterCollectedUrl(url);
303
- }
304
- const urlQuery = core.filterCollectedUrlQuery(core.getUrlQuery(query));
305
- if (urlQuery) {
306
- data[attributes.URL_QUERY] = urlQuery;
307
- }
308
- const urlFragment = core.getUrlFragment(fragment);
309
- if (urlFragment) {
310
- data[attributes.URL_FRAGMENT] = urlFragment;
311
- }
312
- return {
313
- op: op$1,
314
- data
315
- };
316
- }
317
- function getSanitizedUrl(attributes$1) {
318
- const kind = attributes$1[attributes.SENTRY_KIND];
319
- const httpTarget = attributes$1[attributes.HTTP_TARGET];
320
- const httpUrl = attributes$1[attributes.URL_FULL];
321
- const httpRoute = attributes$1[attributes.HTTP_ROUTE];
322
- const parsedUrl = typeof httpUrl === "string" ? core.parseUrl(httpUrl) : void 0;
323
- const url = parsedUrl ? core.getSanitizedUrlString(parsedUrl) : void 0;
324
- const query = parsedUrl?.search || void 0;
325
- const fragment = parsedUrl?.hash || void 0;
326
- if (typeof httpRoute === "string") {
327
- return { urlPath: httpRoute, url, query, fragment, hasRoute: true };
328
- }
329
- if (kind === "server" && typeof httpTarget === "string") {
330
- return { urlPath: core.stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
331
- }
332
- if (parsedUrl) {
333
- return { urlPath: url, url, query, fragment, hasRoute: false };
334
- }
335
- if (typeof httpTarget === "string") {
336
- return { urlPath: core.stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
337
- }
338
- return { urlPath: void 0, url, query, fragment, hasRoute: false };
339
- }
340
-
341
- function applyOtelSpanData(span, options = {}) {
342
- const spanJSON = core.spanToJSON(span);
343
- const attributes = spanJSON.data;
344
- const mayInferSource = core.spanShouldInferOtelSource(span);
345
- const hasCustomSpanName = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME] !== void 0;
346
- const canInferSource = mayInferSource && !hasCustomSpanName && !core.spanSourceWasExplicitlySet(span);
347
- const attributesForInference = canInferSource && attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === "custom" ? { ...attributes, [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: void 0 } : attributes;
348
- const inferred = inferSpanData(attributesForInference);
349
- if (inferred.op && attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] === void 0) {
350
- span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_OP, inferred.op);
351
- }
352
- if (inferred.data) {
353
- Object.entries(inferred.data).forEach(([key, value]) => {
354
- if (value !== void 0 && attributes[key] === void 0) {
355
- span.setAttribute(key, value);
356
- }
357
- });
358
- }
359
- if (options.finalizeStatus) {
360
- applyOtelCompatibilityAttributes(span, attributes);
361
- const client = core.getClient();
362
- applyOtelSpanStatus(span, attributes, spanJSON.status, !!client && core.hasSpanStreamingEnabled(client));
363
- }
364
- }
365
- function applyOtelSpanStatus(span, attributes, status, spanStreamingEnabled) {
366
- if (status === "ok") {
367
- span.setStatus(inferStatusFromAttributes(attributes) || { code: core.SPAN_STATUS_OK });
368
- return;
369
- }
370
- if (!spanStreamingEnabled && status !== "ok" && !core.isStatusErrorMessageValid(status)) {
371
- span.setStatus({ code: core.SPAN_STATUS_ERROR, message: "internal_error" });
372
- }
373
- }
374
- function applyOtelCompatibilityAttributes(span, attributes$1) {
375
- const legacyHttpStatusCode = attributes$1[attributes.HTTP_STATUS_CODE];
376
- if (attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] === void 0 && legacyHttpStatusCode !== void 0) {
377
- span.setAttribute(attributes.HTTP_RESPONSE_STATUS_CODE, legacyHttpStatusCode);
378
- attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] = legacyHttpStatusCode;
379
- }
380
- }
381
-
382
- function backfillStreamedSpanDataFromOtel(spanJSON) {
383
- const attributes$1 = spanJSON.attributes ?? {};
384
- const { op, data } = inferSpanData(attributes$1);
385
- core.safeSetSpanJSONAttributes(spanJSON, {
386
- [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
387
- // If nothing in the chain previously set an origin, we now default it to 'manual'
388
- // For transactions, this is done in the SpanExporter.
389
- // TODO (v11): Remove this again once we fully moved away from OTel's TracerProvider.
390
- // at this point, we use `SentrySpan` everywhere, which defaults its origin to 'manual'.
391
- [attributes.SENTRY_ORIGIN]: "manual",
392
- ...data
393
- });
394
- }
395
-
396
172
  const SUPPRESS_TRACING_KEY = api.createContextKey("OpenTelemetry SDK Context Key SUPPRESS_TRACING");
397
173
  function suppressTracing(context) {
398
174
  return context.setValue(SUPPRESS_TRACING_KEY, true);
@@ -432,10 +208,6 @@ class SentryTracer {
432
208
  }
433
209
  const span = this._startSentrySpan(name, options, parentSpan, ctx !== void 0);
434
210
  core.markSpanAsTracerProviderSpan(span);
435
- if (options.attributes?.[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === void 0) {
436
- core.markSpanForOtelSourceInference(span);
437
- }
438
- applyOtelSpanData(span);
439
211
  return span;
440
212
  }
441
213
  startActiveSpan(name, optionsOrFn, contextOrFn, fn) {
@@ -564,7 +336,7 @@ function _startSpan(options, callback, autoEnd) {
564
336
  return core.handleCallbackErrors(
565
337
  () => callback(span),
566
338
  () => {
567
- if (core.spanToJSON(span).status === "ok") {
339
+ if (core.spanToStaticSpanJSON(span).status === "ok") {
568
340
  span.setStatus({ code: api.SpanStatusCode.ERROR });
569
341
  }
570
342
  },
@@ -579,7 +351,7 @@ function _startSpan(options, callback, autoEnd) {
579
351
  return core.handleCallbackErrors(
580
352
  () => callback(span),
581
353
  () => {
582
- if (core.spanToJSON(span).status === "ok") {
354
+ if (core.spanToStaticSpanJSON(span).status === "ok") {
583
355
  span.setStatus({ code: api.SpanStatusCode.ERROR });
584
356
  }
585
357
  },
@@ -623,14 +395,13 @@ function getTracer() {
623
395
  return client?.tracer || api.trace.getTracer("@sentry/opentelemetry", core.SDK_VERSION);
624
396
  }
625
397
  function getSpanOptions(options) {
626
- const { startTime, attributes, kind, op, links } = options;
398
+ const { startTime, attributes, op, links } = options;
627
399
  const fixedStartTime = typeof startTime === "number" ? ensureTimestampInMilliseconds(startTime) : startTime;
628
400
  return {
629
401
  attributes: op ? {
630
402
  [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
631
403
  ...attributes
632
404
  } : attributes,
633
- kind,
634
405
  links,
635
406
  startTime: fixedStartTime
636
407
  };
@@ -690,26 +461,28 @@ function getActiveSpanWrapper(parentSpan) {
690
461
  } : (callback) => callback();
691
462
  }
692
463
 
693
- function buildContextWithSentryScopes(context, activeContext) {
694
- const span = api.trace.getSpan(context);
695
- let effectiveContext;
696
- if (span?.spanContext().traceState?.get(SENTRY_TRACE_STATE_CHILD_IGNORED) === "1") {
697
- const contextWithoutSpan = api.trace.deleteSpan(context);
698
- const parentSpan = api.trace.getSpan(activeContext);
699
- effectiveContext = parentSpan ? api.trace.setSpan(contextWithoutSpan, parentSpan) : contextWithoutSpan;
700
- } else {
701
- effectiveContext = context;
464
+ function getActiveSpan(scope) {
465
+ if (!scope) {
466
+ return api.trace.getActiveSpan();
702
467
  }
703
- const currentScopes = getScopesFromContext(effectiveContext);
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);
704
477
  const currentScope = currentScopes?.scope || core.getCurrentScope();
705
478
  const currentIsolationScope = currentScopes?.isolationScope || core.getIsolationScope();
706
- const shouldForkIsolationScope = effectiveContext.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
707
- const scope = effectiveContext.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY);
708
- const isolationScope = effectiveContext.getValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY);
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);
709
482
  const newCurrentScope = scope || currentScope.clone();
710
483
  const newIsolationScope = isolationScope || (shouldForkIsolationScope ? currentIsolationScope.clone() : currentIsolationScope);
711
484
  const scopes = { scope: newCurrentScope, isolationScope: newIsolationScope };
712
- const ctx1 = setScopesOnContext(effectiveContext, scopes);
485
+ const ctx1 = setScopesOnContext(context, scopes);
713
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);
714
487
  setContextOnScope(newCurrentScope, ctx2);
715
488
  return ctx2;
@@ -726,7 +499,7 @@ class SentryAsyncLocalStorageContextManager {
726
499
  return this._asyncLocalStorage.getStore() ?? api.ROOT_CONTEXT;
727
500
  }
728
501
  with(context, fn, thisArg, ...args) {
729
- const ctx2 = buildContextWithSentryScopes(context, this.active());
502
+ const ctx2 = buildContextWithSentryScopes(context);
730
503
  const cb = thisArg == null ? fn : fn.bind(thisArg);
731
504
  return this._asyncLocalStorage.run(ctx2, cb, ...args);
732
505
  }
@@ -934,8 +707,7 @@ function setOpenTelemetryContextAsyncContextStrategy() {
934
707
  }
935
708
  function getTracingChannelBinding() {
936
709
  return {
937
- asyncLocalStorage,
938
- getStoreWithActiveSpan
710
+ asyncLocalStorage
939
711
  };
940
712
  }
941
713
  core.setAsyncContextStrategy({
@@ -958,17 +730,9 @@ function setOpenTelemetryContextAsyncContextStrategy() {
958
730
  api.context.setGlobalContextManager(ctxManager);
959
731
  return ctxManager.getAsyncLocalStorageLookup();
960
732
  }
961
- function getStoreWithActiveSpan(span) {
962
- const activeContext = api.context.active();
963
- const isIgnoredChild = core.spanIsIgnored(span) && core.getRootSpan(span) !== span || span.spanContext().traceState?.get(SENTRY_TRACE_STATE_CHILD_IGNORED) === "1";
964
- return isIgnoredChild ? activeContext : api.trace.setSpan(activeContext, span);
965
- }
966
733
 
967
734
  exports.SentryPropagator = SentryPropagator;
968
735
  exports.SentryTracerProvider = SentryTracerProvider;
969
- exports.applyOtelSpanData = applyOtelSpanData;
970
- exports.backfillStreamedSpanDataFromOtel = backfillStreamedSpanDataFromOtel;
971
736
  exports.getScopesFromContext = getScopesFromContext;
972
737
  exports.setOpenTelemetryContextAsyncContextStrategy = setOpenTelemetryContextAsyncContextStrategy;
973
- exports.setupEventContextTrace = setupEventContextTrace;
974
738
  //# sourceMappingURL=index.js.map