@sentry/opentelemetry 9.8.0 → 9.10.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.
@@ -1,5 +1,5 @@
1
1
  import { ATTR_URL_FULL, SEMATTRS_HTTP_URL, ATTR_HTTP_REQUEST_METHOD, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_RPC_SERVICE, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_FAAS_TRIGGER, SEMATTRS_DB_STATEMENT, SEMATTRS_HTTP_TARGET, ATTR_HTTP_ROUTE, ATTR_HTTP_RESPONSE_STATUS_CODE, SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
2
- import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, stripUrlQueryAndFragment, spanToJSON, hasSpansEnabled, dynamicSamplingContextToSentryBaggageHeader, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, handleCallbackErrors, spanToTraceContext, getTraceContextFromScope, getRootSpan, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, dropUndefinedKeys, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, parseSampleRate, sampleSpan } from '@sentry/core';
2
+ import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, stripUrlQueryAndFragment, spanToJSON, hasSpansEnabled, dynamicSamplingContextToSentryBaggageHeader, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, handleCallbackErrors, spanToTraceContext, getTraceContextFromScope, getRootSpan, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, parseSampleRate, sampleSpan } from '@sentry/core';
3
3
  export { getClient, getDynamicSamplingContextFromSpan } from '@sentry/core';
4
4
  import * as api from '@opentelemetry/api';
5
5
  import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
@@ -1775,14 +1775,14 @@ function createTransactionForOtelSpan(span) {
1775
1775
 
1776
1776
  const sampleRate = span.attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE] ;
1777
1777
 
1778
- const attributes = dropUndefinedKeys({
1778
+ const attributes = {
1779
1779
  [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
1780
1780
  [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
1781
1781
  [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
1782
1782
  [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: origin,
1783
1783
  ...data,
1784
1784
  ...removeSentryAttributes(span.attributes),
1785
- });
1785
+ };
1786
1786
 
1787
1787
  const { links } = span;
1788
1788
  const { traceId: trace_id, spanId: span_id } = span.spanContext();
@@ -1796,7 +1796,7 @@ function createTransactionForOtelSpan(span) {
1796
1796
 
1797
1797
  const status = mapStatus(span);
1798
1798
 
1799
- const traceContext = dropUndefinedKeys({
1799
+ const traceContext = {
1800
1800
  parent_span_id,
1801
1801
  span_id,
1802
1802
  trace_id,
@@ -1805,12 +1805,12 @@ function createTransactionForOtelSpan(span) {
1805
1805
  op,
1806
1806
  status: getStatusMessage(status), // As per protocol, span status is allowed to be undefined
1807
1807
  links: convertSpanLinksForEnvelope(links),
1808
- });
1808
+ };
1809
1809
 
1810
1810
  const statusCode = attributes[ATTR_HTTP_RESPONSE_STATUS_CODE];
1811
1811
  const responseContext = typeof statusCode === 'number' ? { response: { status_code: statusCode } } : undefined;
1812
1812
 
1813
- const transactionEvent = dropUndefinedKeys({
1813
+ const transactionEvent = {
1814
1814
  contexts: {
1815
1815
  trace: traceContext,
1816
1816
  otel: {
@@ -1824,19 +1824,17 @@ function createTransactionForOtelSpan(span) {
1824
1824
  transaction: description,
1825
1825
  type: 'transaction',
1826
1826
  sdkProcessingMetadata: {
1827
- ...dropUndefinedKeys({
1828
- capturedSpanScope: capturedSpanScopes.scope,
1829
- capturedSpanIsolationScope: capturedSpanScopes.isolationScope,
1830
- sampleRate,
1831
- dynamicSamplingContext: getDynamicSamplingContextFromSpan(span ),
1832
- }),
1827
+ capturedSpanScope: capturedSpanScopes.scope,
1828
+ capturedSpanIsolationScope: capturedSpanScopes.isolationScope,
1829
+ sampleRate,
1830
+ dynamicSamplingContext: getDynamicSamplingContextFromSpan(span ),
1833
1831
  },
1834
1832
  ...(source && {
1835
1833
  transaction_info: {
1836
1834
  source,
1837
1835
  },
1838
1836
  }),
1839
- });
1837
+ };
1840
1838
 
1841
1839
  return transactionEvent;
1842
1840
  }
@@ -1864,16 +1862,16 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1864
1862
  const { attributes, startTime, endTime, parentSpanId, links } = span;
1865
1863
 
1866
1864
  const { op, description, data, origin = 'manual' } = getSpanData(span);
1867
- const allData = dropUndefinedKeys({
1865
+ const allData = {
1868
1866
  [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: origin,
1869
1867
  [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
1870
1868
  ...removeSentryAttributes(attributes),
1871
1869
  ...data,
1872
- });
1870
+ };
1873
1871
 
1874
1872
  const status = mapStatus(span);
1875
1873
 
1876
- const spanJSON = dropUndefinedKeys({
1874
+ const spanJSON = {
1877
1875
  span_id,
1878
1876
  trace_id,
1879
1877
  data: allData,
@@ -1887,7 +1885,7 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1887
1885
  origin,
1888
1886
  measurements: timedEventsToMeasurements(span.events),
1889
1887
  links: convertSpanLinksForEnvelope(links),
1890
- });
1888
+ };
1891
1889
 
1892
1890
  spans.push(spanJSON);
1893
1891
 
@@ -2084,6 +2082,17 @@ class SentrySampler {
2084
2082
  }
2085
2083
 
2086
2084
  const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
2085
+ const isRootSpan = !parentSpan || parentContext?.isRemote;
2086
+
2087
+ // We only sample based on parameters (like tracesSampleRate or tracesSampler) for root spans (which is done in sampleSpan).
2088
+ // Non-root-spans simply inherit the sampling decision from their parent.
2089
+ if (!isRootSpan) {
2090
+ return wrapSamplingDecision({
2091
+ decision: parentSampled ? SamplingDecision.RECORD_AND_SAMPLED : SamplingDecision.NOT_RECORD,
2092
+ context,
2093
+ spanAttributes,
2094
+ });
2095
+ }
2087
2096
 
2088
2097
  // We want to pass the inferred name & attributes to the sampler method
2089
2098
  const {
@@ -2116,76 +2125,60 @@ class SentrySampler {
2116
2125
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
2117
2126
  }
2118
2127
 
2119
- const isRootSpan = !parentSpan || parentContext?.isRemote;
2128
+ const { isolationScope } = getScopesFromContext(context) ?? {};
2120
2129
 
2121
- // We only sample based on parameters (like tracesSampleRate or tracesSampler) for root spans (which is done in sampleSpan).
2122
- // Non-root-spans simply inherit the sampling decision from their parent.
2123
- if (isRootSpan) {
2124
- const { isolationScope } = getScopesFromContext(context) ?? {};
2125
-
2126
- const dscString = parentContext?.traceState ? parentContext.traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
2127
- const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
2128
-
2129
- const sampleRand = parseSampleRate(dsc?.sample_rand) ?? Math.random();
2130
-
2131
- const [sampled, sampleRate, localSampleRateWasApplied] = sampleSpan(
2132
- options,
2133
- {
2134
- name: inferredSpanName,
2135
- attributes: mergedAttributes,
2136
- normalizedRequest: isolationScope?.getScopeData().sdkProcessingMetadata.normalizedRequest,
2137
- parentSampled,
2138
- parentSampleRate: parseSampleRate(dsc?.sample_rate),
2139
- },
2140
- sampleRand,
2141
- );
2130
+ const dscString = parentContext?.traceState ? parentContext.traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
2131
+ const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
2142
2132
 
2143
- const method = `${maybeSpanHttpMethod}`.toUpperCase();
2144
- if (method === 'OPTIONS' || method === 'HEAD') {
2145
- DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
2146
-
2147
- return {
2148
- ...wrapSamplingDecision({
2149
- decision: SamplingDecision.NOT_RECORD,
2150
- context,
2151
- spanAttributes,
2152
- sampleRand,
2153
- downstreamTraceSampleRate: 0, // we don't want to sample anything in the downstream trace either
2154
- }),
2155
- };
2156
- }
2133
+ const sampleRand = parseSampleRate(dsc?.sample_rand) ?? Math.random();
2157
2134
 
2158
- if (
2159
- !sampled &&
2160
- // We check for `parentSampled === undefined` because we only want to record client reports for spans that are trace roots (ie. when there was incoming trace)
2161
- parentSampled === undefined
2162
- ) {
2163
- DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
2164
- this._client.recordDroppedEvent('sample_rate', 'transaction');
2165
- }
2135
+ const [sampled, sampleRate, localSampleRateWasApplied] = sampleSpan(
2136
+ options,
2137
+ {
2138
+ name: inferredSpanName,
2139
+ attributes: mergedAttributes,
2140
+ normalizedRequest: isolationScope?.getScopeData().sdkProcessingMetadata.normalizedRequest,
2141
+ parentSampled,
2142
+ parentSampleRate: parseSampleRate(dsc?.sample_rate),
2143
+ },
2144
+ sampleRand,
2145
+ );
2166
2146
 
2167
- return {
2168
- ...wrapSamplingDecision({
2169
- decision: sampled ? SamplingDecision.RECORD_AND_SAMPLED : SamplingDecision.NOT_RECORD,
2170
- context,
2171
- spanAttributes,
2172
- sampleRand,
2173
- downstreamTraceSampleRate: localSampleRateWasApplied ? sampleRate : undefined,
2174
- }),
2175
- attributes: {
2176
- // We set the sample rate on the span when a local sample rate was applied to better understand how traces were sampled in Sentry
2177
- [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: localSampleRateWasApplied ? sampleRate : undefined,
2178
- },
2179
- };
2180
- } else {
2181
- return {
2182
- ...wrapSamplingDecision({
2183
- decision: parentSampled ? SamplingDecision.RECORD_AND_SAMPLED : SamplingDecision.NOT_RECORD,
2184
- context,
2185
- spanAttributes,
2186
- }),
2187
- };
2147
+ const method = `${maybeSpanHttpMethod}`.toUpperCase();
2148
+ if (method === 'OPTIONS' || method === 'HEAD') {
2149
+ DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
2150
+
2151
+ return wrapSamplingDecision({
2152
+ decision: SamplingDecision.NOT_RECORD,
2153
+ context,
2154
+ spanAttributes,
2155
+ sampleRand,
2156
+ downstreamTraceSampleRate: 0, // we don't want to sample anything in the downstream trace either
2157
+ });
2188
2158
  }
2159
+
2160
+ if (
2161
+ !sampled &&
2162
+ // We check for `parentSampled === undefined` because we only want to record client reports for spans that are trace roots (ie. when there was incoming trace)
2163
+ parentSampled === undefined
2164
+ ) {
2165
+ DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
2166
+ this._client.recordDroppedEvent('sample_rate', 'transaction');
2167
+ }
2168
+
2169
+ return {
2170
+ ...wrapSamplingDecision({
2171
+ decision: sampled ? SamplingDecision.RECORD_AND_SAMPLED : SamplingDecision.NOT_RECORD,
2172
+ context,
2173
+ spanAttributes,
2174
+ sampleRand,
2175
+ downstreamTraceSampleRate: localSampleRateWasApplied ? sampleRate : undefined,
2176
+ }),
2177
+ attributes: {
2178
+ // We set the sample rate on the span when a local sample rate was applied to better understand how traces were sampled in Sentry
2179
+ [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: localSampleRateWasApplied ? sampleRate : undefined,
2180
+ },
2181
+ };
2189
2182
  }
2190
2183
 
2191
2184
  /** Returns the sampler name or short description with the configuration. */