@sentry/opentelemetry 8.13.0 → 8.15.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.
@@ -3,7 +3,7 @@ import { SEMATTRS_HTTP_URL, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_R
3
3
  import { parseUrl, getSanitizedUrlString, addNonEnumerableProperty, stripUrlQueryAndFragment, baggageHeaderToDynamicSamplingContext, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, propagationContextFromHeaders, stringMatchesSomePattern, dynamicSamplingContextToSentryBaggageHeader, dropUndefinedKeys } from '@sentry/utils';
4
4
  import * as api from '@opentelemetry/api';
5
5
  import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, INVALID_SPANID, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
6
- import { SDK_VERSION, isSentryRequestUrl, getClient, SEMANTIC_ATTRIBUTE_SENTRY_OP, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getCurrentScope, getIsolationScope, hasTracingEnabled, getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan, getRootSpan, handleCallbackErrors, continueTrace as continueTrace$1, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, getCapturedScopesOnSpan, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getStatusMessage, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
6
+ import { SDK_VERSION, isSentryRequestUrl, getClient, SEMANTIC_ATTRIBUTE_SENTRY_OP, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getCurrentScope, getIsolationScope, hasTracingEnabled, getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan, getRootSpan, handleCallbackErrors, continueTrace as continueTrace$1, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, spanTimeInputToSeconds, getCapturedScopesOnSpan, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getStatusMessage, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
7
7
  export { getClient, getCurrentHubShim, getDynamicSamplingContextFromSpan } from '@sentry/core';
8
8
  import { W3CBaggagePropagator, isTracingSuppressed, TraceState, suppressTracing as suppressTracing$1 } from '@opentelemetry/core';
9
9
  import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
@@ -1297,11 +1297,6 @@ function wrapContextManagerClass(
1297
1297
  /** If this attribute is true, it means that the parent is a remote span. */
1298
1298
  const SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE = 'sentry.parentIsRemote';
1299
1299
 
1300
- /** Convert an OTEL time to seconds */
1301
- function convertOtelTimeToSeconds([seconds, nano]) {
1302
- return seconds + nano / 1000000000;
1303
- }
1304
-
1305
1300
  /**
1306
1301
  * This function runs through a list of OTEL Spans, and wraps them in an `SpanNode`
1307
1302
  * where each node holds a reference to their parent node.
@@ -1608,7 +1603,7 @@ function getCompletedRootNodes(nodes) {
1608
1603
 
1609
1604
  function shouldCleanupSpan(span, maxStartTimeOffsetSeconds) {
1610
1605
  const cutoff = Date.now() / 1000 - maxStartTimeOffsetSeconds;
1611
- return convertOtelTimeToSeconds(span.startTime) < cutoff;
1606
+ return spanTimeInputToSeconds(span.startTime) < cutoff;
1612
1607
  }
1613
1608
 
1614
1609
  function parseSpan(span) {
@@ -1668,8 +1663,8 @@ function createTransactionForOtelSpan(span) {
1668
1663
  },
1669
1664
  },
1670
1665
  spans: [],
1671
- start_timestamp: convertOtelTimeToSeconds(span.startTime),
1672
- timestamp: convertOtelTimeToSeconds(span.endTime),
1666
+ start_timestamp: spanTimeInputToSeconds(span.startTime),
1667
+ timestamp: spanTimeInputToSeconds(span.endTime),
1673
1668
  transaction: description,
1674
1669
  type: 'transaction',
1675
1670
  sdkProcessingMetadata: {
@@ -1726,9 +1721,9 @@ function createAndFinishSpanForOtelSpan(node, spans, remaining) {
1726
1721
  data: allData,
1727
1722
  description,
1728
1723
  parent_span_id: parentSpanId,
1729
- start_timestamp: convertOtelTimeToSeconds(startTime),
1724
+ start_timestamp: spanTimeInputToSeconds(startTime),
1730
1725
  // This is [0,0] by default in OTEL, in which case we want to interpret this as no end time
1731
- timestamp: convertOtelTimeToSeconds(endTime) || undefined,
1726
+ timestamp: spanTimeInputToSeconds(endTime) || undefined,
1732
1727
  status: getStatusMessage(status), // As per protocol, span status is allowed to be undefined
1733
1728
  op,
1734
1729
  origin,
@@ -1914,16 +1909,8 @@ class SentrySampler {
1914
1909
  const parentSpan = trace.getSpan(context);
1915
1910
  const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
1916
1911
 
1917
- let traceState = _optionalChain([parentContext, 'optionalAccess', _3 => _3.traceState]) || new TraceState();
1918
-
1919
- // We always keep the URL on the trace state, so we can access it in the propagator
1920
- const url = spanAttributes[SEMATTRS_HTTP_URL];
1921
- if (url && typeof url === 'string') {
1922
- traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
1923
- }
1924
-
1925
1912
  if (!hasTracingEnabled(options)) {
1926
- return { decision: SamplingDecision.NOT_RECORD, traceState };
1913
+ return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1927
1914
  }
1928
1915
 
1929
1916
  // If we have a http.client span that has no local parent, we never want to sample it
@@ -1931,9 +1918,9 @@ class SentrySampler {
1931
1918
  if (
1932
1919
  spanKind === SpanKind.CLIENT &&
1933
1920
  spanAttributes[SEMATTRS_HTTP_METHOD] &&
1934
- (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _4 => _4.isRemote]))
1921
+ (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _3 => _3.isRemote]))
1935
1922
  ) {
1936
- return { decision: SamplingDecision.NOT_RECORD, traceState };
1923
+ return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1937
1924
  }
1938
1925
 
1939
1926
  const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
@@ -1950,7 +1937,7 @@ class SentrySampler {
1950
1937
  mutableSamplingDecision,
1951
1938
  );
1952
1939
  if (!mutableSamplingDecision.decision) {
1953
- return { decision: SamplingDecision.NOT_RECORD, traceState: traceState };
1940
+ return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1954
1941
  }
1955
1942
 
1956
1943
  const [sampled, sampleRate] = sampleSpan(options, {
@@ -1970,25 +1957,22 @@ class SentrySampler {
1970
1957
  const method = `${spanAttributes[SEMATTRS_HTTP_METHOD]}`.toUpperCase();
1971
1958
  if (method === 'OPTIONS' || method === 'HEAD') {
1972
1959
  DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
1960
+
1973
1961
  return {
1974
- decision: SamplingDecision.NOT_RECORD,
1962
+ ...wrapSamplingDecision({ decision: SamplingDecision.NOT_RECORD, context, spanAttributes }),
1975
1963
  attributes,
1976
- traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
1977
1964
  };
1978
1965
  }
1979
1966
 
1980
1967
  if (!sampled) {
1981
1968
  return {
1982
- decision: SamplingDecision.NOT_RECORD,
1969
+ ...wrapSamplingDecision({ decision: SamplingDecision.NOT_RECORD, context, spanAttributes }),
1983
1970
  attributes,
1984
- traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
1985
1971
  };
1986
1972
  }
1987
-
1988
1973
  return {
1989
- decision: SamplingDecision.RECORD_AND_SAMPLED,
1974
+ ...wrapSamplingDecision({ decision: SamplingDecision.RECORD_AND_SAMPLED, context, spanAttributes }),
1990
1975
  attributes,
1991
- traceState,
1992
1976
  };
1993
1977
  }
1994
1978
 
@@ -2027,6 +2011,46 @@ function getParentSampled(parentSpan, traceId, spanName) {
2027
2011
  return undefined;
2028
2012
  }
2029
2013
 
2014
+ /**
2015
+ * Wrap a sampling decision with data that Sentry needs to work properly with it.
2016
+ * If you pass `decision: undefined`, it will be treated as `NOT_RECORDING`, but in contrast to passing `NOT_RECORDING`
2017
+ * it will not propagate this decision to downstream Sentry SDKs.
2018
+ */
2019
+ function wrapSamplingDecision({
2020
+ decision,
2021
+ context,
2022
+ spanAttributes,
2023
+ }) {
2024
+ const traceState = getBaseTraceState(context, spanAttributes);
2025
+
2026
+ // If the decision is undefined, we treat it as NOT_RECORDING, but we don't propagate this decision to downstream SDKs
2027
+ // Which is done by not setting `SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING` traceState
2028
+ if (decision == undefined) {
2029
+ return { decision: SamplingDecision.NOT_RECORD, traceState };
2030
+ }
2031
+
2032
+ if (decision === SamplingDecision.NOT_RECORD) {
2033
+ return { decision, traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') };
2034
+ }
2035
+
2036
+ return { decision, traceState };
2037
+ }
2038
+
2039
+ function getBaseTraceState(context, spanAttributes) {
2040
+ const parentSpan = trace.getSpan(context);
2041
+ const parentContext = _optionalChain([parentSpan, 'optionalAccess', _4 => _4.spanContext, 'call', _5 => _5()]);
2042
+
2043
+ let traceState = _optionalChain([parentContext, 'optionalAccess', _6 => _6.traceState]) || new TraceState();
2044
+
2045
+ // We always keep the URL on the trace state, so we can access it in the propagator
2046
+ const url = spanAttributes[SEMATTRS_HTTP_URL];
2047
+ if (url && typeof url === 'string') {
2048
+ traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
2049
+ }
2050
+
2051
+ return traceState;
2052
+ }
2053
+
2030
2054
  /**
2031
2055
  * This method takes an OpenTelemetry instrumentation or
2032
2056
  * array of instrumentations and registers them with OpenTelemetry.
@@ -2037,5 +2061,5 @@ function addOpenTelemetryInstrumentation(...instrumentations) {
2037
2061
  });
2038
2062
  }
2039
2063
 
2040
- export { SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, getActiveSpan, getRequestSpanData, getScopesFromContext, getSpanKind, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass };
2064
+ export { SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, getActiveSpan, getRequestSpanData, getScopesFromContext, getSpanKind, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass, wrapSamplingDecision };
2041
2065
  //# sourceMappingURL=index.js.map