@sentry/opentelemetry 8.0.0-alpha.4 → 8.0.0-alpha.7

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.
package/cjs/index.js CHANGED
@@ -5,63 +5,12 @@ var {
5
5
  Object.defineProperty(exports, '__esModule', { value: true });
6
6
 
7
7
  const semanticConventions = require('@opentelemetry/semantic-conventions');
8
- const core = require('@sentry/core');
9
8
  const utils = require('@sentry/utils');
10
9
  const api = require('@opentelemetry/api');
10
+ const core = require('@sentry/core');
11
11
  const core$1 = require('@opentelemetry/core');
12
12
  const sdkTraceBase = require('@opentelemetry/sdk-trace-base');
13
13
 
14
- /**
15
- * Maybe capture a Sentry exception for an OTEL timed event.
16
- * This will check if the event is exception-like and in that case capture it as an exception.
17
- */
18
- function maybeCaptureExceptionForTimedEvent(event, otelSpan) {
19
- if (event.name !== 'exception') {
20
- return;
21
- }
22
-
23
- const attributes = event.attributes;
24
- if (!attributes) {
25
- return;
26
- }
27
-
28
- const message = attributes[semanticConventions.SemanticAttributes.EXCEPTION_MESSAGE];
29
-
30
- if (typeof message !== 'string') {
31
- return;
32
- }
33
-
34
- const syntheticError = new Error(message);
35
-
36
- const stack = attributes[semanticConventions.SemanticAttributes.EXCEPTION_STACKTRACE];
37
- if (utils.isString(stack)) {
38
- syntheticError.stack = stack;
39
- }
40
-
41
- const type = attributes[semanticConventions.SemanticAttributes.EXCEPTION_TYPE];
42
- if (utils.isString(type)) {
43
- syntheticError.name = type;
44
- }
45
-
46
- core.captureException(syntheticError, {
47
- captureContext: otelSpan
48
- ? {
49
- contexts: {
50
- otel: {
51
- attributes: otelSpan.attributes,
52
- resource: otelSpan.resource.attributes,
53
- },
54
- trace: {
55
- trace_id: otelSpan.spanContext().traceId,
56
- span_id: otelSpan.spanContext().spanId,
57
- parent_span_id: otelSpan.parentSpanId,
58
- },
59
- },
60
- }
61
- : undefined,
62
- });
63
- }
64
-
65
14
  /**
66
15
  * Check if a given span has attributes.
67
16
  * This is necessary because the base `Span` type does not have attributes,
@@ -316,6 +265,13 @@ function getContextFromScope(scope) {
316
265
  return SCOPE_CONTEXT_MAP.get(scope);
317
266
  }
318
267
 
268
+ /**
269
+ * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
270
+ *
271
+ * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
272
+ */
273
+ const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
274
+
319
275
  const setupElements = new Set();
320
276
 
321
277
  /** Get all the OpenTelemetry elements that have been set up. */
@@ -353,9 +309,14 @@ function getPropagationContextFromSpanContext(spanContext) {
353
309
  * Injects and extracts `sentry-trace` and `baggage` headers from carriers.
354
310
  */
355
311
  class SentryPropagator extends core$1.W3CBaggagePropagator {
312
+ /** A map of URLs that have already been checked for if they match tracePropagationTargets. */
313
+
356
314
  constructor() {
357
315
  super();
358
316
  setIsSetup('SentryPropagator');
317
+
318
+ // We're caching results so we don't have to recompute regexp every time we create a request.
319
+ this._urlMatchesTargetsMap = new utils.LRUMap(100);
359
320
  }
360
321
 
361
322
  /**
@@ -363,6 +324,20 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
363
324
  */
364
325
  inject(context, carrier, setter) {
365
326
  if (core$1.isTracingSuppressed(context)) {
327
+ DEBUG_BUILD && utils.logger.log('[Tracing] Not injecting trace data for url because tracing is suppressed.');
328
+ return;
329
+ }
330
+
331
+ const activeSpan = api.trace.getSpan(context);
332
+ const url = activeSpan && _optionalChain$7([core.spanToJSON, 'call', _ => _(activeSpan), 'access', _2 => _2.data, 'optionalAccess', _3 => _3[semanticConventions.SemanticAttributes.HTTP_URL]]);
333
+ const tracePropagationTargets = _optionalChain$7([core.getClient, 'call', _4 => _4(), 'optionalAccess', _5 => _5.getOptions, 'call', _6 => _6(), 'optionalAccess', _7 => _7.tracePropagationTargets]);
334
+ if (
335
+ typeof url === 'string' &&
336
+ tracePropagationTargets &&
337
+ !this._shouldInjectTraceData(tracePropagationTargets, url)
338
+ ) {
339
+ DEBUG_BUILD &&
340
+ utils.logger.log('[Tracing] Not injecting trace data for url because it does not matchTracePropagationTargets:', url);
366
341
  return;
367
342
  }
368
343
 
@@ -419,6 +394,22 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
419
394
  fields() {
420
395
  return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER];
421
396
  }
397
+
398
+ /** If we want to inject trace data for a given URL. */
399
+ _shouldInjectTraceData(tracePropagationTargets, url) {
400
+ if (tracePropagationTargets === undefined) {
401
+ return true;
402
+ }
403
+
404
+ const cachedDecision = this._urlMatchesTargetsMap.get(url);
405
+ if (cachedDecision !== undefined) {
406
+ return cachedDecision;
407
+ }
408
+
409
+ const decision = utils.stringMatchesSomePattern(url, tracePropagationTargets);
410
+ this._urlMatchesTargetsMap.set(url, decision);
411
+ return decision;
412
+ }
422
413
  }
423
414
 
424
415
  /** Exported for tests. */
@@ -451,7 +442,7 @@ function getInjectionData(context)
451
442
 
452
443
  {
453
444
  const span = api.trace.getSpan(context);
454
- const spanIsRemote = _optionalChain$7([span, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2(), 'access', _3 => _3.isRemote]);
445
+ const spanIsRemote = _optionalChain$7([span, 'optionalAccess', _8 => _8.spanContext, 'call', _9 => _9(), 'access', _10 => _10.isRemote]);
455
446
 
456
447
  // If we have a local span, we can just pick everything from it
457
448
  if (span && !spanIsRemote) {
@@ -467,7 +458,7 @@ function getInjectionData(context)
467
458
  }
468
459
 
469
460
  // Else we try to use the propagation context from the scope
470
- const scope = _optionalChain$7([getScopesFromContext, 'call', _4 => _4(context), 'optionalAccess', _5 => _5.scope]);
461
+ const scope = _optionalChain$7([getScopesFromContext, 'call', _11 => _11(context), 'optionalAccess', _12 => _12.scope]);
471
462
  if (scope) {
472
463
  const propagationContext = scope.getPropagationContext();
473
464
  const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, propagationContext.traceId);
@@ -509,7 +500,7 @@ function getDynamicSamplingContext(
509
500
  traceId,
510
501
  ) {
511
502
  // If we have a DSC on the propagation context, we just use it
512
- if (_optionalChain$7([propagationContext, 'optionalAccess', _6 => _6.dsc])) {
503
+ if (_optionalChain$7([propagationContext, 'optionalAccess', _13 => _13.dsc])) {
513
504
  return propagationContext.dsc;
514
505
  }
515
506
 
@@ -589,10 +580,10 @@ function getSamplingDecision(spanContext) {
589
580
  const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
590
581
  const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
591
582
 
592
- if (_optionalChain$7([dsc, 'optionalAccess', _7 => _7.sampled]) === 'true') {
583
+ if (_optionalChain$7([dsc, 'optionalAccess', _14 => _14.sampled]) === 'true') {
593
584
  return true;
594
585
  }
595
- if (_optionalChain$7([dsc, 'optionalAccess', _8 => _8.sampled]) === 'false') {
586
+ if (_optionalChain$7([dsc, 'optionalAccess', _15 => _15.sampled]) === 'false') {
596
587
  return false;
597
588
  }
598
589
 
@@ -683,11 +674,12 @@ function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[
683
674
  /**
684
675
  * Wraps a function with a transaction/span and finishes the span after the function is done.
685
676
  * The created span is the active span and will be used as parent by other spans created inside the function
686
- * and can be accessed via `Sentry.getSpan()`, as long as the function is executed while the scope is active.
677
+ * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
687
678
  *
688
679
  * If you want to create a span that is not set as active, use {@link startInactiveSpan}.
689
680
  *
690
- * Note that you'll always get a span passed to the callback, it may just be a NonRecordingSpan if the span is not sampled.
681
+ * You'll always get a span passed to the callback,
682
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
691
683
  */
692
684
  function startSpan(options, callback) {
693
685
  const tracer = getTracer();
@@ -706,7 +698,10 @@ function startSpan(options, callback) {
706
698
  return core.handleCallbackErrors(
707
699
  () => callback(span),
708
700
  () => {
709
- span.setStatus({ code: api.SpanStatusCode.ERROR });
701
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
702
+ if (core.spanToJSON(span).status === undefined) {
703
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
704
+ }
710
705
  },
711
706
  () => span.end(),
712
707
  );
@@ -720,7 +715,8 @@ function startSpan(options, callback) {
720
715
  * The created span is the active span and will be used as parent by other spans created inside the function
721
716
  * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active.
722
717
  *
723
- * Note that you'll always get a span passed to the callback, it may just be a NonRecordingSpan if the span is not sampled.
718
+ * You'll always get a span passed to the callback,
719
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
724
720
  */
725
721
  function startSpanManual(
726
722
  options,
@@ -742,7 +738,10 @@ function startSpanManual(
742
738
  return core.handleCallbackErrors(
743
739
  () => callback(span, () => span.end()),
744
740
  () => {
745
- span.setStatus({ code: api.SpanStatusCode.ERROR });
741
+ // Only set the span status to ERROR when there wasn't any status set before, in order to avoid stomping useful span statuses
742
+ if (core.spanToJSON(span).status === undefined) {
743
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
744
+ }
746
745
  },
747
746
  );
748
747
  });
@@ -750,13 +749,12 @@ function startSpanManual(
750
749
 
751
750
  /**
752
751
  * Creates a span. This span is not set as active, so will not get automatic instrumentation spans
753
- * as children or be able to be accessed via `Sentry.getSpan()`.
752
+ * as children or be able to be accessed via `Sentry.getActiveSpan()`.
754
753
  *
755
754
  * If you want to create a span that is set as active, use {@link startSpan}.
756
755
  *
757
- * Note that if you have not enabled tracing extensions via `addTracingExtensions`
758
- * or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
759
- * and the `span` returned from the callback will be undefined.
756
+ * This function will always return a span,
757
+ * it may just be a non-recording span if the span is not sampled or if tracing is disabled.
760
758
  */
761
759
  function startInactiveSpan(options) {
762
760
  const tracer = getTracer();
@@ -781,7 +779,7 @@ function startInactiveSpan(options) {
781
779
  * passed `null` to start an entirely new span tree.
782
780
  *
783
781
  * @param span Spans started in the context of the provided callback will be children of this span. If `null` is passed,
784
- * spans started within the callback will not be attached to a parent span.
782
+ * spans started within the callback will be root spans.
785
783
  * @param callback Execution context in which the provided span will be active. Is passed the newly forked scope.
786
784
  * @returns the value returned from the provided callback function.
787
785
  */
@@ -980,8 +978,7 @@ function getCurrentHub() {
980
978
  setContext: core.setContext,
981
979
 
982
980
  getIntegration(integration) {
983
- // eslint-disable-next-line deprecation/deprecation
984
- return _optionalChain$4([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getIntegration, 'call', _3 => _3(integration)]) || null;
981
+ return _optionalChain$4([core.getClient, 'call', _ => _(), 'optionalAccess', _2 => _2.getIntegrationByName, 'call', _3 => _3(integration.id)]) || null;
985
982
  },
986
983
 
987
984
  startSession: core.startSession,
@@ -1229,13 +1226,6 @@ function wrapContextManagerClass(
1229
1226
  return SentryContextManager ;
1230
1227
  }
1231
1228
 
1232
- /**
1233
- * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
1234
- *
1235
- * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
1236
- */
1237
- const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
1238
-
1239
1229
  /** If this attribute is true, it means that the parent is a remote span. */
1240
1230
  const SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE = 'sentry.parentIsRemote';
1241
1231
 
@@ -1260,14 +1250,19 @@ function groupSpansWithParents(spans) {
1260
1250
  });
1261
1251
  }
1262
1252
 
1263
- function createOrUpdateSpanNodeAndRefs(nodeMap, span) {
1253
+ /**
1254
+ * This returns the _local_ parent ID - `parentId` on the span may point to a remote span.
1255
+ */
1256
+ function getLocalParentId(span) {
1264
1257
  const parentIsRemote = span.attributes[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE] === true;
1265
-
1266
- const id = span.spanContext().spanId;
1267
-
1268
1258
  // If the parentId is the trace parent ID, we pretend it's undefined
1269
1259
  // As this means the parent exists somewhere else
1270
- const parentId = !parentIsRemote ? span.parentSpanId : undefined;
1260
+ return !parentIsRemote ? span.parentSpanId : undefined;
1261
+ }
1262
+
1263
+ function createOrUpdateSpanNodeAndRefs(nodeMap, span) {
1264
+ const id = span.spanContext().spanId;
1265
+ const parentId = getLocalParentId(span);
1271
1266
 
1272
1267
  if (!parentId) {
1273
1268
  createOrUpdateNode(nodeMap, { id, span, children: [] });
@@ -1311,23 +1306,8 @@ function createOrUpdateNode(nodeMap, spanNode) {
1311
1306
  return spanNode;
1312
1307
  }
1313
1308
 
1314
- // canonicalCodesHTTPMap maps some HTTP codes to Sentry's span statuses. See possible mapping in https://develop.sentry.dev/sdk/event-payloads/span/
1315
- const canonicalCodesHTTPMap = {
1316
- '400': 'failed_precondition',
1317
- '401': 'unauthenticated',
1318
- '403': 'permission_denied',
1319
- '404': 'not_found',
1320
- '409': 'aborted',
1321
- '429': 'resource_exhausted',
1322
- '499': 'cancelled',
1323
- '500': 'internal_error',
1324
- '501': 'unimplemented',
1325
- '503': 'unavailable',
1326
- '504': 'deadline_exceeded',
1327
- } ;
1328
-
1329
1309
  // canonicalCodesGrpcMap maps some GRPC codes to Sentry's span statuses. See description in grpc documentation.
1330
- const canonicalCodesGrpcMap = {
1310
+ const canonicalGrpcErrorCodesMap = {
1331
1311
  '1': 'cancelled',
1332
1312
  '2': 'unknown_error',
1333
1313
  '3': 'invalid_argument',
@@ -1353,30 +1333,42 @@ function mapStatus(span) {
1353
1333
  const attributes = spanHasAttributes(span) ? span.attributes : {};
1354
1334
  const status = spanHasStatus(span) ? span.status : undefined;
1355
1335
 
1356
- const httpCode = attributes[semanticConventions.SemanticAttributes.HTTP_STATUS_CODE];
1357
- const grpcCode = attributes[semanticConventions.SemanticAttributes.RPC_GRPC_STATUS_CODE];
1358
-
1359
- const code = typeof httpCode === 'string' ? httpCode : typeof httpCode === 'number' ? httpCode.toString() : undefined;
1360
- if (code) {
1361
- const sentryStatus = canonicalCodesHTTPMap[code];
1362
- if (sentryStatus) {
1363
- return { code: core.SPAN_STATUS_ERROR, message: sentryStatus };
1336
+ if (status) {
1337
+ // Since span status OK is not set by default, we give it priority: https://opentelemetry.io/docs/concepts/signals/traces/#span-status
1338
+ if (status.code === api.SpanStatusCode.OK) {
1339
+ return { code: core.SPAN_STATUS_OK };
1340
+ // If the span is already marked as erroneous we return that exact status
1341
+ } else if (status.code === api.SpanStatusCode.ERROR) {
1342
+ return { code: core.SPAN_STATUS_ERROR, message: status.message };
1364
1343
  }
1365
1344
  }
1366
1345
 
1367
- if (typeof grpcCode === 'string') {
1368
- const sentryStatus = canonicalCodesGrpcMap[grpcCode];
1369
- if (sentryStatus) {
1370
- return { code: core.SPAN_STATUS_ERROR, message: sentryStatus };
1371
- }
1346
+ // If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.
1347
+
1348
+ const httpCodeAttribute = attributes[semanticConventions.SemanticAttributes.HTTP_STATUS_CODE];
1349
+ const grpcCodeAttribute = attributes[semanticConventions.SemanticAttributes.RPC_GRPC_STATUS_CODE];
1350
+
1351
+ const numberHttpCode =
1352
+ typeof httpCodeAttribute === 'number'
1353
+ ? httpCodeAttribute
1354
+ : typeof httpCodeAttribute === 'string'
1355
+ ? parseInt(httpCodeAttribute)
1356
+ : undefined;
1357
+
1358
+ if (numberHttpCode) {
1359
+ return core.getSpanStatusFromHttpCode(numberHttpCode);
1372
1360
  }
1373
1361
 
1374
- const statusCode = status && status.code;
1375
- if (statusCode === api.SpanStatusCode.OK || statusCode === api.SpanStatusCode.UNSET) {
1376
- return { code: core.SPAN_STATUS_OK };
1362
+ if (typeof grpcCodeAttribute === 'string') {
1363
+ return { code: core.SPAN_STATUS_ERROR, message: canonicalGrpcErrorCodesMap[grpcCodeAttribute] || 'unknown_error' };
1377
1364
  }
1378
1365
 
1379
- return { code: core.SPAN_STATUS_ERROR, message: 'unknown_error' };
1366
+ // We default to setting the spans status to ok.
1367
+ if (status && status.code === api.SpanStatusCode.UNSET) {
1368
+ return { code: core.SPAN_STATUS_OK };
1369
+ } else {
1370
+ return { code: core.SPAN_STATUS_ERROR, message: 'unknown_error' };
1371
+ }
1380
1372
  }
1381
1373
 
1382
1374
  /**
@@ -1530,36 +1522,70 @@ function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[
1530
1522
  /**
1531
1523
  * A Sentry-specific exporter that converts OpenTelemetry Spans to Sentry Spans & Transactions.
1532
1524
  */
1533
- class SentrySpanExporter {
1525
+ class SentrySpanExporter {
1534
1526
 
1535
1527
  constructor() {
1536
- this._stopped = false;
1537
1528
  this._finishedSpans = [];
1538
1529
  }
1539
1530
 
1540
- /** @inheritDoc */
1541
- export(spans, resultCallback) {
1542
- if (this._stopped) {
1543
- return resultCallback({
1544
- code: core$1.ExportResultCode.FAILED,
1545
- error: new Error('Exporter has been stopped'),
1546
- });
1531
+ /** Export a single span. */
1532
+ export(span) {
1533
+ this._finishedSpans.push(span);
1534
+
1535
+ // If the span has a local parent ID, we don't need to export anything just yet
1536
+ if (getLocalParentId(span)) {
1537
+ const openSpanCount = this._finishedSpans.length;
1538
+ DEBUG_BUILD && utils.logger.log(`SpanExporter has ${openSpanCount} unsent spans remaining`);
1539
+ this._cleanupOldSpans();
1540
+ return;
1547
1541
  }
1548
1542
 
1549
- const openSpanCount = this._finishedSpans.length;
1550
- const newSpanCount = spans.length;
1543
+ this._clearTimeout();
1544
+
1545
+ // If we got a parent span, we try to send the span tree
1546
+ // Wait a tick for this, to ensure we avoid race conditions
1547
+ this._flushTimeout = setTimeout(() => {
1548
+ this.flush();
1549
+ }, 1);
1550
+ }
1551
1551
 
1552
- this._finishedSpans.push(...spans);
1552
+ /** Try to flush any pending spans immediately. */
1553
+ flush() {
1554
+ this._clearTimeout();
1555
+
1556
+ const openSpanCount = this._finishedSpans.length;
1553
1557
 
1554
1558
  const remainingSpans = maybeSend(this._finishedSpans);
1555
1559
 
1556
1560
  const remainingOpenSpanCount = remainingSpans.length;
1557
- const sentSpanCount = openSpanCount + newSpanCount - remainingOpenSpanCount;
1561
+ const sentSpanCount = openSpanCount - remainingOpenSpanCount;
1558
1562
 
1559
1563
  DEBUG_BUILD &&
1560
1564
  utils.logger.log(`SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} unsent spans remaining`);
1561
1565
 
1562
- this._finishedSpans = remainingSpans.filter(span => {
1566
+ this._cleanupOldSpans(remainingSpans);
1567
+ }
1568
+
1569
+ /** Clear the exporter. */
1570
+ clear() {
1571
+ this._finishedSpans = [];
1572
+ this._clearTimeout();
1573
+ }
1574
+
1575
+ /** Clear the flush timeout. */
1576
+ _clearTimeout() {
1577
+ if (this._flushTimeout) {
1578
+ clearTimeout(this._flushTimeout);
1579
+ this._flushTimeout = undefined;
1580
+ }
1581
+ }
1582
+
1583
+ /**
1584
+ * Remove any span that is older than 5min.
1585
+ * We do this to avoid leaking memory.
1586
+ */
1587
+ _cleanupOldSpans(spans = this._finishedSpans) {
1588
+ this._finishedSpans = spans.filter(span => {
1563
1589
  const shouldDrop = shouldCleanupSpan(span, 5 * 60);
1564
1590
  DEBUG_BUILD &&
1565
1591
  shouldDrop &&
@@ -1570,23 +1596,6 @@ class SentrySpanExporter {
1570
1596
  );
1571
1597
  return !shouldDrop;
1572
1598
  });
1573
-
1574
- resultCallback({ code: core$1.ExportResultCode.SUCCESS });
1575
- }
1576
-
1577
- /** @inheritDoc */
1578
- shutdown() {
1579
- const forceFlush = this.forceFlush();
1580
- this._stopped = true;
1581
- this._finishedSpans = [];
1582
- return forceFlush;
1583
- }
1584
-
1585
- /** @inheritDoc */
1586
- forceFlush() {
1587
- return new Promise(resolve => {
1588
- this.export(this._finishedSpans, () => resolve());
1589
- });
1590
1599
  }
1591
1600
  }
1592
1601
 
@@ -1629,8 +1638,12 @@ function maybeSend(spans) {
1629
1638
  .filter((span) => !!span);
1630
1639
  }
1631
1640
 
1641
+ function nodeIsCompletedRootNode(node) {
1642
+ return !!node.span && !node.parentNode;
1643
+ }
1644
+
1632
1645
  function getCompletedRootNodes(nodes) {
1633
- return nodes.filter((node) => !!node.span && !node.parentNode);
1646
+ return nodes.filter(nodeIsCompletedRootNode);
1634
1647
  }
1635
1648
 
1636
1649
  function shouldCleanupSpan(span, maxStartTimeOffsetSeconds) {
@@ -1675,7 +1688,7 @@ function createTransactionForOtelSpan(span) {
1675
1688
  data: attributes,
1676
1689
  origin,
1677
1690
  op,
1678
- status: core.getStatusMessage(status),
1691
+ status: core.getStatusMessage(status), // As per protocol, span status is allowed to be undefined
1679
1692
  });
1680
1693
 
1681
1694
  const transactionEvent = {
@@ -1749,7 +1762,7 @@ function createAndFinishSpanForOtelSpan(node, spans, remaining) {
1749
1762
  start_timestamp: convertOtelTimeToSeconds(startTime),
1750
1763
  // This is [0,0] by default in OTEL, in which case we want to interpret this as no end time
1751
1764
  timestamp: convertOtelTimeToSeconds(endTime) || undefined,
1752
- status: core.getStatusMessage(status),
1765
+ status: core.getStatusMessage(status), // As per protocol, span status is allowed to be undefined
1753
1766
  op,
1754
1767
  origin,
1755
1768
  _metrics_summary: core.getMetricSummaryJsonForSpan(span ),
@@ -1861,24 +1874,33 @@ function onSpanStart(span, parentContext) {
1861
1874
  }
1862
1875
 
1863
1876
  function onSpanEnd(span) {
1864
- // Capture exceptions as events
1865
- span.events.forEach(event => {
1866
- maybeCaptureExceptionForTimedEvent(event, span);
1867
- });
1868
-
1869
1877
  const client = core.getClient();
1870
- _optionalChain$1([client, 'optionalAccess', _3 => _3.emit, 'call', _4 => _4('spanEnd', span)]);
1878
+ _optionalChain$1([client, 'optionalAccess', _3 => _3.emit, 'call', _4 => _4('spanEnd', span )]);
1871
1879
  }
1872
1880
 
1873
1881
  /**
1874
1882
  * Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
1875
1883
  * the Sentry SDK.
1876
1884
  */
1877
- class SentrySpanProcessor extends sdkTraceBase.BatchSpanProcessor {
1878
- constructor() {
1879
- super(new SentrySpanExporter());
1885
+ class SentrySpanProcessor {
1880
1886
 
1887
+ constructor() {
1881
1888
  setIsSetup('SentrySpanProcessor');
1889
+ this._exporter = new SentrySpanExporter();
1890
+ }
1891
+
1892
+ /**
1893
+ * @inheritDoc
1894
+ */
1895
+ async forceFlush() {
1896
+ this._exporter.flush();
1897
+ }
1898
+
1899
+ /**
1900
+ * @inheritDoc
1901
+ */
1902
+ async shutdown() {
1903
+ this._exporter.clear();
1882
1904
  }
1883
1905
 
1884
1906
  /**
@@ -1891,30 +1913,20 @@ class SentrySpanProcessor extends sdkTraceBase.BatchSpanProcessor {
1891
1913
  // once we decoupled opentelemetry from SentrySpan
1892
1914
 
1893
1915
  DEBUG_BUILD && utils.logger.log(`[Tracing] Starting span "${span.name}" (${span.spanContext().spanId})`);
1894
-
1895
- return super.onStart(span, parentContext);
1896
1916
  }
1897
1917
 
1898
1918
  /** @inheritDoc */
1899
1919
  onEnd(span) {
1900
- DEBUG_BUILD && utils.logger.log(`[Tracing] Finishing span "${span.name}" (${span.spanContext().spanId})`);
1901
-
1902
- if (!this._shouldSendSpanToSentry(span)) {
1903
- // Prevent this being called to super.onEnd(), which would pass this to the span exporter
1920
+ if (span.spanContext().traceFlags !== api.TraceFlags.SAMPLED) {
1921
+ DEBUG_BUILD && utils.logger.log(`[Tracing] Finishing unsampled span "${span.name}" (${span.spanContext().spanId})`);
1904
1922
  return;
1905
1923
  }
1906
1924
 
1907
- onSpanEnd(span);
1925
+ DEBUG_BUILD && utils.logger.log(`[Tracing] Finishing span "${span.name}" (${span.spanContext().spanId})`);
1908
1926
 
1909
- return super.onEnd(span);
1910
- }
1927
+ onSpanEnd(span);
1911
1928
 
1912
- /**
1913
- * You can overwrite this in a sub class to implement custom behavior for dropping spans.
1914
- * If you return `false` here, the span will not be passed to the exporter and thus not be sent.
1915
- */
1916
- _shouldSendSpanToSentry(_span) {
1917
- return true;
1929
+ this._exporter.export(span);
1918
1930
  }
1919
1931
  }
1920
1932
 
@@ -2098,7 +2110,6 @@ function getParentRemoteSampled(spanContext) {
2098
2110
  * @hidden
2099
2111
  */
2100
2112
  const _INTERNAL = {
2101
- maybeCaptureExceptionForTimedEvent,
2102
2113
  getRequestSpanData,
2103
2114
  } ;
2104
2115