@sentry/opentelemetry 10.63.0 → 10.64.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 (46) hide show
  1. package/README.md +29 -0
  2. package/build/cjs/{asyncContextStrategy-CvLbRw2N.js → asyncContextStrategy-xEzxBKVX.js} +214 -28
  3. package/build/cjs/asyncContextStrategy-xEzxBKVX.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-CIqtsDXI.js} +213 -30
  9. package/build/esm/asyncContextStrategy-CIqtsDXI.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-ts3.8/applyOtelSpanData.d.ts +18 -0
  36. package/build/types-ts3.8/exports.d.ts +5 -1
  37. package/build/types-ts3.8/tracer.d.ts +13 -0
  38. package/build/types-ts3.8/tracerProvider.d.ts +23 -0
  39. package/build/types-ts3.8/types.d.ts +6 -2
  40. package/build/types-ts3.8/utils/backfillStreamedSpanData.d.ts +16 -0
  41. package/build/types-ts3.8/utils/getSamplingDecision.d.ts +17 -0
  42. package/build/types-ts3.8/utils/mapStatus.d.ts +3 -1
  43. package/build/types-ts3.8/utils/setupCheck.d.ts +1 -1
  44. package/package.json +5 -5
  45. package/build/cjs/asyncContextStrategy-CvLbRw2N.js.map +0 -1
  46. package/build/esm/asyncContextStrategy-DfM95hx5.js.map +0 -1
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/)
@@ -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,23 @@ function getData(span) {
1221
1243
  return data;
1222
1244
  }
1223
1245
 
1246
+ function backfillStreamedSpanDataFromOtel(spanJSON, hint) {
1247
+ const attributes = spanJSON.attributes ?? {};
1248
+ const kind = hint?.spanKind ?? core.SPAN_KIND.INTERNAL;
1249
+ const { op, description, source, data } = inferSpanData(spanJSON.name, attributes, kind);
1250
+ spanJSON.name = description;
1251
+ core.safeSetSpanJSONAttributes(spanJSON, {
1252
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
1253
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
1254
+ ...data
1255
+ });
1256
+ if (kind !== core.SPAN_KIND.INTERNAL) {
1257
+ core.safeSetSpanJSONAttributes(spanJSON, {
1258
+ "otel.kind": core.spanKindToName(kind)
1259
+ });
1260
+ }
1261
+ }
1262
+
1224
1263
  class SentrySpanProcessor {
1225
1264
  constructor(options) {
1226
1265
  this._unsubscribePreprocessSpan = void 0;
@@ -1279,25 +1318,6 @@ class SentrySpanProcessor {
1279
1318
  }
1280
1319
  }
1281
1320
  }
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
1321
 
1302
1322
  class SentrySampler {
1303
1323
  constructor(client) {
@@ -1509,6 +1529,169 @@ function getValidSpan(context) {
1509
1529
  return span && api.isSpanContextValid(span.spanContext()) ? span : void 0;
1510
1530
  }
1511
1531
 
1532
+ function applyOtelSpanData(span, options = {}) {
1533
+ const spanJSON = core.spanToJSON(span);
1534
+ const attributes = spanJSON.data;
1535
+ const kind = span.kind ?? api.SpanKind.INTERNAL;
1536
+ const mayInferSource = core.spanShouldInferOtelSource(span);
1537
+ const hasCustomSpanName = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME] !== void 0;
1538
+ const canInferSource = mayInferSource && !hasCustomSpanName && !core.spanSourceWasExplicitlySet(span);
1539
+ const attributesForInference = canInferSource && attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === "custom" ? { ...attributes, [core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: void 0 } : attributes;
1540
+ const inferred = inferSpanData(spanJSON.description || "<unknown>", attributesForInference, kind);
1541
+ if (kind !== api.SpanKind.INTERNAL && attributes["otel.kind"] === void 0) {
1542
+ span.setAttribute("otel.kind", api.SpanKind[kind]);
1543
+ }
1544
+ if (inferred.op && attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] === void 0) {
1545
+ span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_OP, inferred.op);
1546
+ }
1547
+ const shouldApplyInferredSource = inferred.source !== void 0 && inferred.source !== "custom" && (options.finalizeStatus || inferred.source !== "url") && (spanJSON.parent_span_id === void 0 || kind === api.SpanKind.SERVER);
1548
+ if (shouldApplyInferredSource && (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === void 0 || canInferSource)) {
1549
+ span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, inferred.source);
1550
+ }
1551
+ if (inferred.data) {
1552
+ Object.entries(inferred.data).forEach(([key, value]) => {
1553
+ if (value !== void 0 && attributes[key] === void 0) {
1554
+ span.setAttribute(key, value);
1555
+ }
1556
+ });
1557
+ }
1558
+ if (options.finalizeStatus) {
1559
+ applyOtelCompatibilityAttributes(span, attributes);
1560
+ const client = core.getClient();
1561
+ applyOtelSpanStatus(span, attributes, spanJSON.status, !!client && core.hasSpanStreamingEnabled(client));
1562
+ }
1563
+ if (mayInferSource && inferred.description !== spanJSON.description && (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] !== "custom" || canInferSource)) {
1564
+ span.updateName(inferred.description);
1565
+ }
1566
+ }
1567
+ function applyOtelSpanKind(span, kind) {
1568
+ core.addNonEnumerableProperty(span, "kind", kind ?? api.SpanKind.INTERNAL);
1569
+ }
1570
+ function applyOtelSpanStatus(span, attributes, status, spanStreamingEnabled) {
1571
+ if (status === void 0) {
1572
+ span.setStatus(inferStatusFromAttributes(attributes) || { code: core.SPAN_STATUS_OK });
1573
+ return;
1574
+ }
1575
+ if (!spanStreamingEnabled && status !== "ok" && !isStatusErrorMessageValid(status)) {
1576
+ span.setStatus({ code: core.SPAN_STATUS_ERROR, message: "internal_error" });
1577
+ }
1578
+ }
1579
+ function applyOtelCompatibilityAttributes(span, attributes$1) {
1580
+ const legacyHttpStatusCode = attributes$1[attributes.HTTP_STATUS_CODE];
1581
+ if (attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] === void 0 && legacyHttpStatusCode !== void 0) {
1582
+ span.setAttribute(attributes.HTTP_RESPONSE_STATUS_CODE, legacyHttpStatusCode);
1583
+ attributes$1[attributes.HTTP_RESPONSE_STATUS_CODE] = legacyHttpStatusCode;
1584
+ }
1585
+ }
1586
+
1587
+ class SentryTracer {
1588
+ /** @inheritdoc */
1589
+ startSpan(name, options = {}, ctx) {
1590
+ const parentContext = ctx || api.context.active();
1591
+ const parentSpan = options.root ? void 0 : api.trace.getSpan(parentContext);
1592
+ if (core$1.isTracingSuppressed(parentContext)) {
1593
+ return this._createNonRecordingSpan(parentSpan);
1594
+ }
1595
+ const span = this._startSentrySpan(name, options, parentSpan, ctx !== void 0);
1596
+ core.markSpanAsTracerProviderSpan(span);
1597
+ applyOtelSpanKind(span, options.kind);
1598
+ if (options.attributes?.[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === void 0) {
1599
+ core.markSpanForOtelSourceInference(span);
1600
+ }
1601
+ applyOtelSpanData(span);
1602
+ return span;
1603
+ }
1604
+ startActiveSpan(name, optionsOrFn, contextOrFn, fn) {
1605
+ const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
1606
+ const ctx = typeof contextOrFn === "function" || contextOrFn === void 0 ? api.context.active() : contextOrFn;
1607
+ const callback = typeof optionsOrFn === "function" ? optionsOrFn : typeof contextOrFn === "function" ? contextOrFn : fn;
1608
+ const span = this.startSpan(name, options, ctx);
1609
+ const capturedIsolationScope = core.getCapturedScopesOnSpan(span).isolationScope;
1610
+ const withCapturedIsolationScope = (contextToFork) => capturedIsolationScope ? contextToFork.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, capturedIsolationScope) : contextToFork;
1611
+ if (core.spanIsIgnored(span) && api.trace.getSpan(ctx)) {
1612
+ return api.context.with(withCapturedIsolationScope(ctx), () => callback(span));
1613
+ }
1614
+ return api.context.with(withCapturedIsolationScope(api.trace.setSpan(ctx, span)), () => {
1615
+ core._INTERNAL_setSpanForScope(core.getCurrentScope(), span);
1616
+ return callback(span);
1617
+ });
1618
+ }
1619
+ _startSentrySpan(name, options, parentSpan, hasExplicitContext) {
1620
+ const sentryOptions = {
1621
+ name,
1622
+ attributes: options.attributes,
1623
+ links: options.links,
1624
+ startTime: options.startTime
1625
+ };
1626
+ if (options.root) {
1627
+ return core.startNewTrace(() => core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan: null }));
1628
+ }
1629
+ if (parentSpan?.spanContext().isRemote) {
1630
+ return this._startRootSpanWithRemoteParent(sentryOptions, parentSpan);
1631
+ }
1632
+ if (parentSpan) {
1633
+ return core._INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan });
1634
+ }
1635
+ return core.startNewTrace(
1636
+ () => core._INTERNAL_startInactiveSpan({
1637
+ ...sentryOptions,
1638
+ parentSpan: hasExplicitContext ? null : void 0
1639
+ })
1640
+ );
1641
+ }
1642
+ _startRootSpanWithRemoteParent(options, parentSpan) {
1643
+ const { spanId, traceId, traceState } = parentSpan.spanContext();
1644
+ const dsc = core.getDynamicSamplingContextFromSpan(parentSpan);
1645
+ const sampleRand = typeof dsc.sample_rand === "string" ? Number(dsc.sample_rand) : void 0;
1646
+ const hasIncomingDsc = !!traceState?.get(SENTRY_TRACE_STATE_DSC);
1647
+ return core.withScope((scope) => {
1648
+ scope.setPropagationContext({
1649
+ traceId,
1650
+ parentSpanId: spanId,
1651
+ sampled: getSamplingDecision(parentSpan.spanContext()),
1652
+ dsc: hasIncomingDsc ? dsc : void 0,
1653
+ sampleRand: typeof sampleRand === "number" && !Number.isNaN(sampleRand) ? sampleRand : core._INTERNAL_safeMathRandom()
1654
+ });
1655
+ core._INTERNAL_setSpanForScope(scope, void 0);
1656
+ return core._INTERNAL_startInactiveSpan({ ...options, parentSpan: null });
1657
+ });
1658
+ }
1659
+ _createNonRecordingSpan(parentSpan) {
1660
+ const span = new core.SentryNonRecordingSpan({ traceId: parentSpan?.spanContext().traceId });
1661
+ if (parentSpan) {
1662
+ core.addChildSpanToSpan(parentSpan, span);
1663
+ }
1664
+ core.setCapturedScopesOnSpan(span, core.getCurrentScope(), core.getIsolationScope());
1665
+ return span;
1666
+ }
1667
+ }
1668
+
1669
+ class SentryTracerProvider {
1670
+ constructor(options = {}) {
1671
+ this._tracers = /* @__PURE__ */ new Map();
1672
+ this.resource = options.resource;
1673
+ }
1674
+ /** @inheritdoc */
1675
+ getTracer(name, version, options) {
1676
+ const key = JSON.stringify([name, version, options]);
1677
+ const cachedTracer = this._tracers.get(key);
1678
+ if (cachedTracer) {
1679
+ return cachedTracer;
1680
+ }
1681
+ const tracer = new SentryTracer();
1682
+ this._tracers.set(key, tracer);
1683
+ return tracer;
1684
+ }
1685
+ /** Compatibility with SDK tracer providers. */
1686
+ forceFlush() {
1687
+ return Promise.resolve();
1688
+ }
1689
+ /** Compatibility with SDK tracer providers. */
1690
+ shutdown() {
1691
+ return Promise.resolve();
1692
+ }
1693
+ }
1694
+
1512
1695
  const ATTR_TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language";
1513
1696
  const ATTR_TELEMETRY_SDK_NAME = "telemetry.sdk.name";
1514
1697
  const ATTR_TELEMETRY_SDK_VERSION = "telemetry.sdk.version";
@@ -1665,6 +1848,9 @@ exports.SENTRY_SCOPES_CONTEXT_KEY = SENTRY_SCOPES_CONTEXT_KEY;
1665
1848
  exports.SentryPropagator = SentryPropagator;
1666
1849
  exports.SentrySampler = SentrySampler;
1667
1850
  exports.SentrySpanProcessor = SentrySpanProcessor;
1851
+ exports.SentryTracerProvider = SentryTracerProvider;
1852
+ exports.applyOtelSpanData = applyOtelSpanData;
1853
+ exports.backfillStreamedSpanDataFromOtel = backfillStreamedSpanDataFromOtel;
1668
1854
  exports.buildContextWithSentryScopes = buildContextWithSentryScopes;
1669
1855
  exports.continueTrace = continueTrace;
1670
1856
  exports.enhanceDscWithOpenTelemetryRootSpanName = enhanceDscWithOpenTelemetryRootSpanName;
@@ -1693,4 +1879,4 @@ exports.withActiveSpan = withActiveSpan;
1693
1879
  exports.wrapClientClass = wrapClientClass;
1694
1880
  exports.wrapContextManagerClass = wrapContextManagerClass;
1695
1881
  exports.wrapSamplingDecision = wrapSamplingDecision;
1696
- //# sourceMappingURL=asyncContextStrategy-CvLbRw2N.js.map
1882
+ //# sourceMappingURL=asyncContextStrategy-xEzxBKVX.js.map