@sentry/opentelemetry 10.63.0 → 10.65.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.
Files changed (50) hide show
  1. package/README.md +29 -0
  2. package/build/cjs/{asyncContextStrategy-CvLbRw2N.js → asyncContextStrategy-DP1H8zYV.js} +220 -29
  3. package/build/cjs/asyncContextStrategy-DP1H8zYV.js.map +1 -0
  4. package/build/cjs/index.browser.js +5 -1
  5. package/build/cjs/index.browser.js.map +1 -1
  6. package/build/cjs/index.js +5 -1
  7. package/build/cjs/index.js.map +1 -1
  8. package/build/esm/{asyncContextStrategy-DfM95hx5.js → asyncContextStrategy-CDufUmpa.js} +220 -32
  9. package/build/esm/asyncContextStrategy-CDufUmpa.js.map +1 -0
  10. package/build/esm/index.browser.js +1 -1
  11. package/build/esm/index.js +3 -3
  12. package/build/esm/package.json +1 -1
  13. package/build/types/applyOtelSpanData.d.ts +18 -0
  14. package/build/types/applyOtelSpanData.d.ts.map +1 -0
  15. package/build/types/custom/client.d.ts.map +1 -1
  16. package/build/types/exports.d.ts +5 -1
  17. package/build/types/exports.d.ts.map +1 -1
  18. package/build/types/spanProcessor.d.ts.map +1 -1
  19. package/build/types/tracer.d.ts +13 -0
  20. package/build/types/tracer.d.ts.map +1 -0
  21. package/build/types/tracerProvider.d.ts +23 -0
  22. package/build/types/tracerProvider.d.ts.map +1 -0
  23. package/build/types/types.d.ts +6 -2
  24. package/build/types/types.d.ts.map +1 -1
  25. package/build/types/utils/backfillStreamedSpanData.d.ts +16 -0
  26. package/build/types/utils/backfillStreamedSpanData.d.ts.map +1 -0
  27. package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts.map +1 -1
  28. package/build/types/utils/getSamplingDecision.d.ts +17 -0
  29. package/build/types/utils/getSamplingDecision.d.ts.map +1 -1
  30. package/build/types/utils/mapStatus.d.ts +3 -1
  31. package/build/types/utils/mapStatus.d.ts.map +1 -1
  32. package/build/types/utils/parseSpanDescription.d.ts.map +1 -1
  33. package/build/types/utils/setupCheck.d.ts +1 -1
  34. package/build/types/utils/setupCheck.d.ts.map +1 -1
  35. package/build/types/utils/spanTypes.d.ts.map +1 -1
  36. package/build/types-ts3.8/applyOtelSpanData.d.ts +18 -0
  37. package/build/types-ts3.8/exports.d.ts +5 -1
  38. package/build/types-ts3.8/tracer.d.ts +13 -0
  39. package/build/types-ts3.8/tracerProvider.d.ts +23 -0
  40. package/build/types-ts3.8/types.d.ts +6 -2
  41. package/build/types-ts3.8/utils/backfillStreamedSpanData.d.ts +16 -0
  42. package/build/types-ts3.8/utils/getSamplingDecision.d.ts +17 -0
  43. package/build/types-ts3.8/utils/mapStatus.d.ts +3 -1
  44. package/build/types-ts3.8/utils/setupCheck.d.ts +1 -1
  45. package/package.json +20 -27
  46. package/build/cjs/asyncContextStrategy-CvLbRw2N.js.map +0 -1
  47. package/build/esm/asyncContextStrategy-DfM95hx5.js.map +0 -1
  48. package/build/types/utils/addOriginToSpan.d.ts +0 -5
  49. package/build/types/utils/addOriginToSpan.d.ts.map +0 -1
  50. package/build/types-ts3.8/utils/addOriginToSpan.d.ts +0 -5
package/README.md CHANGED
@@ -85,6 +85,35 @@ function setupSentry() {
85
85
  A full setup example can be found in
86
86
  [node-experimental](https://github.com/getsentry/sentry-javascript/blob/develop/packages/node-experimental).
87
87
 
88
+ ## Sentry Tracer Provider
89
+
90
+ `SentryTracerProvider` is a minimal OpenTelemetry tracer provider which creates native Sentry spans directly.
91
+ It is useful when code uses the global OpenTelemetry API and you do not need the full OpenTelemetry SDK span processor
92
+ and exporter pipeline.
93
+
94
+ ```js
95
+ import { trace } from '@opentelemetry/api';
96
+ import { SentryTracerProvider } from '@sentry/opentelemetry';
97
+
98
+ trace.setGlobalTracerProvider(new SentryTracerProvider());
99
+
100
+ const span = trace.getTracer('example').startSpan('work');
101
+ span.end();
102
+ ```
103
+
104
+ In `@sentry/node`, this is the default tracer provider. To use the full OpenTelemetry SDK `BasicTracerProvider`
105
+ instead, opt out with:
106
+
107
+ ```js
108
+ Sentry.init({
109
+ dsn: 'xxx',
110
+ openTelemetryBasicTracerProvider: true,
111
+ });
112
+ ```
113
+
114
+ Providing `openTelemetrySpanProcessors` also falls back to the full OpenTelemetry SDK provider, since custom span
115
+ processors require the SDK span pipeline. The `SentryTracerProvider` does not handle OpenTelemetry logs and metrics.
116
+
88
117
  ## Links
89
118
 
90
119
  - [Official SDK Docs](https://docs.sentry.io/quickstart/)
@@ -18,7 +18,7 @@ function getParentSpanId(span) {
18
18
 
19
19
  function spanHasAttributes(span) {
20
20
  const castSpan = span;
21
- return !!castSpan.attributes && typeof castSpan.attributes === "object";
21
+ return core.isObjectLike(castSpan.attributes);
22
22
  }
23
23
  function spanHasKind(span) {
24
24
  const castSpan = span;
@@ -164,6 +164,17 @@ function getSamplingDecision(spanContext) {
164
164
  }
165
165
  return void 0;
166
166
  }
167
+ function getSampledForPropagation(span, client) {
168
+ const spanContext = span.spanContext();
169
+ const samplingDecision = getSamplingDecision(spanContext);
170
+ if (samplingDecision !== void 0) {
171
+ return samplingDecision;
172
+ }
173
+ if (!core.hasSpansEnabled(client?.getOptions()) || spanContext.isRemote || !core.spanIsSentrySpan(core.getRootSpan(span))) {
174
+ return void 0;
175
+ }
176
+ return core.spanIsSampled(span);
177
+ }
167
178
 
168
179
  function inferSpanData(spanName, attributes$1, kind) {
169
180
  const httpMethod = attributes$1[attributes.HTTP_REQUEST_METHOD] || attributes$1[attributes.HTTP_METHOD];
@@ -200,8 +211,16 @@ function inferSpanData(spanName, attributes$1, kind) {
200
211
  return { op: void 0, description: spanName, source: "custom" };
201
212
  }
202
213
  function parseSpanDescription(span) {
203
- const attributes = spanHasAttributes(span) ? span.attributes : {};
204
- const name = spanHasName(span) ? span.name : "<unknown>";
214
+ let attributes;
215
+ let name;
216
+ if (spanHasAttributes(span)) {
217
+ attributes = span.attributes;
218
+ name = spanHasName(span) ? span.name : "<unknown>";
219
+ } else {
220
+ const json = typeof span.spanContext === "function" ? core.spanToJSON(span) : void 0;
221
+ attributes = json?.data || {};
222
+ name = spanHasName(span) ? span.name : json?.description || "<unknown>";
223
+ }
205
224
  const kind = getSpanKind(span);
206
225
  return inferSpanData(name, attributes, kind);
207
226
  }
@@ -319,12 +338,16 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
319
338
  const jsonSpan = core.spanToJSON(rootSpan);
320
339
  const attributes = jsonSpan.data;
321
340
  const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
322
- const { description } = spanHasName(rootSpan) ? parseSpanDescription(rootSpan) : { description: void 0 };
323
- if (source !== "url" && description) {
324
- dsc.transaction = description;
341
+ const sampled = getSampledForPropagation(rootSpan, client);
342
+ if (sampled === false) {
343
+ delete dsc.transaction;
344
+ } else if (jsonSpan.description) {
345
+ const { description } = parseSpanDescription(rootSpan);
346
+ if (source !== "url" && description) {
347
+ dsc.transaction = description;
348
+ }
325
349
  }
326
350
  if (core.hasSpansEnabled()) {
327
- const sampled = getSamplingDecision(rootSpan.spanContext());
328
351
  dsc.sampled = sampled == void 0 ? void 0 : String(sampled);
329
352
  }
330
353
  });
@@ -476,8 +499,7 @@ function getInjectionData(context2, options = {}) {
476
499
  dynamicSamplingContext: dynamicSamplingContext2,
477
500
  traceId: spanContext.traceId,
478
501
  spanId: spanContext.spanId,
479
- sampled: getSamplingDecision(spanContext)
480
- // TODO: Do we need to change something here?
502
+ sampled: getSampledForPropagation(span, options.client)
481
503
  };
482
504
  }
483
505
  const scope = options.scope || getScopesFromContext(context2)?.scope || core.getCurrentScope();
@@ -1221,6 +1243,28 @@ function getData(span) {
1221
1243
  return data;
1222
1244
  }
1223
1245
 
1246
+ function backfillStreamedSpanDataFromOtel(spanJSON, hint) {
1247
+ const attributes$1 = spanJSON.attributes ?? {};
1248
+ const kind = hint?.spanKind ?? core.SPAN_KIND.INTERNAL;
1249
+ const { op, description, source, data } = inferSpanData(spanJSON.name, attributes$1, kind);
1250
+ spanJSON.name = description;
1251
+ core.safeSetSpanJSONAttributes(spanJSON, {
1252
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
1253
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
1254
+ // If nothing in the chain previously set an origin, we now default it to 'manual'
1255
+ // For transactions, this is done in the SpanExporter.
1256
+ // TODO (v11): Remove this again once we fully moved away from OTel's TracerProvider.
1257
+ // at this point, we use `SentrySpan` everywhere, which defaults its origin to 'manual'.
1258
+ [attributes.SENTRY_ORIGIN]: "manual",
1259
+ ...data
1260
+ });
1261
+ if (kind !== core.SPAN_KIND.INTERNAL) {
1262
+ core.safeSetSpanJSONAttributes(spanJSON, {
1263
+ "otel.kind": core.spanKindToName(kind)
1264
+ });
1265
+ }
1266
+ }
1267
+
1224
1268
  class SentrySpanProcessor {
1225
1269
  constructor(options) {
1226
1270
  this._unsubscribePreprocessSpan = void 0;
@@ -1279,25 +1323,6 @@ class SentrySpanProcessor {
1279
1323
  }
1280
1324
  }
1281
1325
  }
1282
- function backfillStreamedSpanDataFromOtel(spanJSON, hint) {
1283
- const attributes = spanJSON.attributes;
1284
- if (!attributes) {
1285
- return;
1286
- }
1287
- const kind = hint?.spanKind ?? core.SPAN_KIND.INTERNAL;
1288
- const { op, description, source, data } = inferSpanData(spanJSON.name, attributes, kind);
1289
- spanJSON.name = description;
1290
- core.safeSetSpanJSONAttributes(spanJSON, {
1291
- [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
1292
- [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
1293
- ...data
1294
- });
1295
- if (kind !== core.SPAN_KIND.INTERNAL) {
1296
- core.safeSetSpanJSONAttributes(spanJSON, {
1297
- "otel.kind": core.spanKindToName(kind)
1298
- });
1299
- }
1300
- }
1301
1326
 
1302
1327
  class SentrySampler {
1303
1328
  constructor(client) {
@@ -1509,6 +1534,169 @@ function getValidSpan(context) {
1509
1534
  return span && api.isSpanContextValid(span.spanContext()) ? span : void 0;
1510
1535
  }
1511
1536
 
1537
+ function applyOtelSpanData(span, options = {}) {
1538
+ const spanJSON = core.spanToJSON(span);
1539
+ const attributes = spanJSON.data;
1540
+ const kind = span.kind ?? api.SpanKind.INTERNAL;
1541
+ const mayInferSource = core.spanShouldInferOtelSource(span);
1542
+ const hasCustomSpanName = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME] !== void 0;
1543
+ const canInferSource = mayInferSource && !hasCustomSpanName && !core.spanSourceWasExplicitlySet(span);
1544
+ const attributesForInference = canInferSource && attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === "custom" ? { ...attributes, [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: void 0 } : attributes;
1545
+ const inferred = inferSpanData(spanJSON.description || "<unknown>", attributesForInference, kind);
1546
+ if (kind !== api.SpanKind.INTERNAL && attributes["otel.kind"] === void 0) {
1547
+ span.setAttribute("otel.kind", api.SpanKind[kind]);
1548
+ }
1549
+ if (inferred.op && attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] === void 0) {
1550
+ span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_OP, inferred.op);
1551
+ }
1552
+ const shouldApplyInferredSource = inferred.source !== void 0 && inferred.source !== "custom" && (options.finalizeStatus || inferred.source !== "url") && (spanJSON.parent_span_id === void 0 || kind === api.SpanKind.SERVER);
1553
+ if (shouldApplyInferredSource && (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === void 0 || canInferSource)) {
1554
+ span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, inferred.source);
1555
+ }
1556
+ if (inferred.data) {
1557
+ Object.entries(inferred.data).forEach(([key, value]) => {
1558
+ if (value !== void 0 && attributes[key] === void 0) {
1559
+ span.setAttribute(key, value);
1560
+ }
1561
+ });
1562
+ }
1563
+ if (options.finalizeStatus) {
1564
+ applyOtelCompatibilityAttributes(span, attributes);
1565
+ const client = core.getClient();
1566
+ applyOtelSpanStatus(span, attributes, spanJSON.status, !!client && core.hasSpanStreamingEnabled(client));
1567
+ }
1568
+ if (mayInferSource && inferred.description !== spanJSON.description && (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] !== "custom" || canInferSource)) {
1569
+ span.updateName(inferred.description);
1570
+ }
1571
+ }
1572
+ function applyOtelSpanKind(span, kind) {
1573
+ core.addNonEnumerableProperty(span, "kind", kind ?? api.SpanKind.INTERNAL);
1574
+ }
1575
+ function applyOtelSpanStatus(span, attributes, status, spanStreamingEnabled) {
1576
+ if (status === void 0) {
1577
+ span.setStatus(inferStatusFromAttributes(attributes) || { code: core.SPAN_STATUS_OK });
1578
+ return;
1579
+ }
1580
+ if (!spanStreamingEnabled && status !== "ok" && !isStatusErrorMessageValid(status)) {
1581
+ span.setStatus({ code: core.SPAN_STATUS_ERROR, message: "internal_error" });
1582
+ }
1583
+ }
1584
+ function applyOtelCompatibilityAttributes(span, attributes$1) {
1585
+ const legacyHttpStatusCode = attributes$1[attributes.HTTP_STATUS_CODE];
1586
+ if (attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] === void 0 && legacyHttpStatusCode !== void 0) {
1587
+ span.setAttribute(attributes.HTTP_RESPONSE_STATUS_CODE, legacyHttpStatusCode);
1588
+ attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] = legacyHttpStatusCode;
1589
+ }
1590
+ }
1591
+
1592
+ class SentryTracer {
1593
+ /** @inheritdoc */
1594
+ startSpan(name, options = {}, ctx) {
1595
+ const parentContext = ctx || api.context.active();
1596
+ const parentSpan = options.root ? void 0 : api.trace.getSpan(parentContext);
1597
+ if (core$1.isTracingSuppressed(parentContext)) {
1598
+ return this._createNonRecordingSpan(parentSpan);
1599
+ }
1600
+ const span = this._startSentrySpan(name, options, parentSpan, ctx !== void 0);
1601
+ core.markSpanAsTracerProviderSpan(span);
1602
+ applyOtelSpanKind(span, options.kind);
1603
+ if (options.attributes?.[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === void 0) {
1604
+ core.markSpanForOtelSourceInference(span);
1605
+ }
1606
+ applyOtelSpanData(span);
1607
+ return span;
1608
+ }
1609
+ startActiveSpan(name, optionsOrFn, contextOrFn, fn) {
1610
+ const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
1611
+ const ctx = typeof contextOrFn === "function" || contextOrFn === void 0 ? api.context.active() : contextOrFn;
1612
+ const callback = typeof optionsOrFn === "function" ? optionsOrFn : typeof contextOrFn === "function" ? contextOrFn : fn;
1613
+ const span = this.startSpan(name, options, ctx);
1614
+ const capturedIsolationScope = core.getCapturedScopesOnSpan(span).isolationScope;
1615
+ const withCapturedIsolationScope = (contextToFork) => capturedIsolationScope ? contextToFork.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, capturedIsolationScope) : contextToFork;
1616
+ if (core.spanIsIgnored(span) && api.trace.getSpan(ctx)) {
1617
+ return api.context.with(withCapturedIsolationScope(ctx), () => callback(span));
1618
+ }
1619
+ return api.context.with(withCapturedIsolationScope(api.trace.setSpan(ctx, span)), () => {
1620
+ core._INTERNAL_setSpanForScope(core.getCurrentScope(), span);
1621
+ return callback(span);
1622
+ });
1623
+ }
1624
+ _startSentrySpan(name, options, parentSpan, hasExplicitContext) {
1625
+ const sentryOptions = {
1626
+ name,
1627
+ attributes: options.attributes,
1628
+ links: options.links,
1629
+ startTime: options.startTime
1630
+ };
1631
+ if (options.root) {
1632
+ return core.startNewTrace(() => core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan: null }));
1633
+ }
1634
+ if (parentSpan?.spanContext().isRemote) {
1635
+ return this._startRootSpanWithRemoteParent(sentryOptions, parentSpan);
1636
+ }
1637
+ if (parentSpan) {
1638
+ return core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan });
1639
+ }
1640
+ return core.startNewTrace(
1641
+ () => core._INTERNAL_startInactiveSpan({
1642
+ ...sentryOptions,
1643
+ parentSpan: hasExplicitContext ? null : void 0
1644
+ })
1645
+ );
1646
+ }
1647
+ _startRootSpanWithRemoteParent(options, parentSpan) {
1648
+ const { spanId, traceId, traceState } = parentSpan.spanContext();
1649
+ const dsc = core.getDynamicSamplingContextFromSpan(parentSpan);
1650
+ const sampleRand = typeof dsc.sample_rand === "string" ? Number(dsc.sample_rand) : void 0;
1651
+ const hasIncomingDsc = !!traceState?.get(SENTRY_TRACE_STATE_DSC);
1652
+ return core.withScope((scope) => {
1653
+ scope.setPropagationContext({
1654
+ traceId,
1655
+ parentSpanId: spanId,
1656
+ sampled: getSamplingDecision(parentSpan.spanContext()),
1657
+ dsc: hasIncomingDsc ? dsc : void 0,
1658
+ sampleRand: typeof sampleRand === "number" && !Number.isNaN(sampleRand) ? sampleRand : core._INTERNAL_safeMathRandom()
1659
+ });
1660
+ core._INTERNAL_setSpanForScope(scope, void 0);
1661
+ return core._INTERNAL_startInactiveSpan({ ...options, parentSpan: null });
1662
+ });
1663
+ }
1664
+ _createNonRecordingSpan(parentSpan) {
1665
+ const span = new core.SentryNonRecordingSpan({ traceId: parentSpan?.spanContext().traceId });
1666
+ if (parentSpan) {
1667
+ core.addChildSpanToSpan(parentSpan, span);
1668
+ }
1669
+ core.setCapturedScopesOnSpan(span, core.getCurrentScope(), core.getIsolationScope());
1670
+ return span;
1671
+ }
1672
+ }
1673
+
1674
+ class SentryTracerProvider {
1675
+ constructor(options = {}) {
1676
+ this._tracers = /* @__PURE__ */ new Map();
1677
+ this.resource = options.resource;
1678
+ }
1679
+ /** @inheritdoc */
1680
+ getTracer(name, version, options) {
1681
+ const key = JSON.stringify([name, version, options]);
1682
+ const cachedTracer = this._tracers.get(key);
1683
+ if (cachedTracer) {
1684
+ return cachedTracer;
1685
+ }
1686
+ const tracer = new SentryTracer();
1687
+ this._tracers.set(key, tracer);
1688
+ return tracer;
1689
+ }
1690
+ /** Compatibility with SDK tracer providers. */
1691
+ forceFlush() {
1692
+ return Promise.resolve();
1693
+ }
1694
+ /** Compatibility with SDK tracer providers. */
1695
+ shutdown() {
1696
+ return Promise.resolve();
1697
+ }
1698
+ }
1699
+
1512
1700
  const ATTR_TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language";
1513
1701
  const ATTR_TELEMETRY_SDK_NAME = "telemetry.sdk.name";
1514
1702
  const ATTR_TELEMETRY_SDK_VERSION = "telemetry.sdk.version";
@@ -1665,6 +1853,9 @@ exports.SENTRY_SCOPES_CONTEXT_KEY = SENTRY_SCOPES_CONTEXT_KEY;
1665
1853
  exports.SentryPropagator = SentryPropagator;
1666
1854
  exports.SentrySampler = SentrySampler;
1667
1855
  exports.SentrySpanProcessor = SentrySpanProcessor;
1856
+ exports.SentryTracerProvider = SentryTracerProvider;
1857
+ exports.applyOtelSpanData = applyOtelSpanData;
1858
+ exports.backfillStreamedSpanDataFromOtel = backfillStreamedSpanDataFromOtel;
1668
1859
  exports.buildContextWithSentryScopes = buildContextWithSentryScopes;
1669
1860
  exports.continueTrace = continueTrace;
1670
1861
  exports.enhanceDscWithOpenTelemetryRootSpanName = enhanceDscWithOpenTelemetryRootSpanName;
@@ -1693,4 +1884,4 @@ exports.withActiveSpan = withActiveSpan;
1693
1884
  exports.wrapClientClass = wrapClientClass;
1694
1885
  exports.wrapContextManagerClass = wrapContextManagerClass;
1695
1886
  exports.wrapSamplingDecision = wrapSamplingDecision;
1696
- //# sourceMappingURL=asyncContextStrategy-CvLbRw2N.js.map
1887
+ //# sourceMappingURL=asyncContextStrategy-DP1H8zYV.js.map