@sentry/opentelemetry 9.9.0 → 9.10.1

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, convertSpanLinksForEnvelope, getStatusMessage, dropUndefinedKeys, 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';
@@ -1810,7 +1810,7 @@ function createTransactionForOtelSpan(span) {
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: {
@@ -1834,7 +1834,7 @@ function createTransactionForOtelSpan(span) {
1834
1834
  source,
1835
1835
  },
1836
1836
  }),
1837
- });
1837
+ };
1838
1838
 
1839
1839
  return transactionEvent;
1840
1840
  }
@@ -1871,7 +1871,7 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1871
1871
 
1872
1872
  const status = mapStatus(span);
1873
1873
 
1874
- const spanJSON = dropUndefinedKeys({
1874
+ const spanJSON = {
1875
1875
  span_id,
1876
1876
  trace_id,
1877
1877
  data: allData,
@@ -1885,7 +1885,7 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1885
1885
  origin,
1886
1886
  measurements: timedEventsToMeasurements(span.events),
1887
1887
  links: convertSpanLinksForEnvelope(links),
1888
- });
1888
+ };
1889
1889
 
1890
1890
  spans.push(spanJSON);
1891
1891
 
@@ -2082,6 +2082,17 @@ class SentrySampler {
2082
2082
  }
2083
2083
 
2084
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
+ }
2085
2096
 
2086
2097
  // We want to pass the inferred name & attributes to the sampler method
2087
2098
  const {
@@ -2114,76 +2125,60 @@ class SentrySampler {
2114
2125
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
2115
2126
  }
2116
2127
 
2117
- const isRootSpan = !parentSpan || parentContext?.isRemote;
2128
+ const { isolationScope } = getScopesFromContext(context) ?? {};
2118
2129
 
2119
- // We only sample based on parameters (like tracesSampleRate or tracesSampler) for root spans (which is done in sampleSpan).
2120
- // Non-root-spans simply inherit the sampling decision from their parent.
2121
- if (isRootSpan) {
2122
- const { isolationScope } = getScopesFromContext(context) ?? {};
2123
-
2124
- const dscString = parentContext?.traceState ? parentContext.traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
2125
- const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
2126
-
2127
- const sampleRand = parseSampleRate(dsc?.sample_rand) ?? Math.random();
2128
-
2129
- const [sampled, sampleRate, localSampleRateWasApplied] = sampleSpan(
2130
- options,
2131
- {
2132
- name: inferredSpanName,
2133
- attributes: mergedAttributes,
2134
- normalizedRequest: isolationScope?.getScopeData().sdkProcessingMetadata.normalizedRequest,
2135
- parentSampled,
2136
- parentSampleRate: parseSampleRate(dsc?.sample_rate),
2137
- },
2138
- sampleRand,
2139
- );
2130
+ const dscString = parentContext?.traceState ? parentContext.traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
2131
+ const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
2140
2132
 
2141
- const method = `${maybeSpanHttpMethod}`.toUpperCase();
2142
- if (method === 'OPTIONS' || method === 'HEAD') {
2143
- DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
2144
-
2145
- return {
2146
- ...wrapSamplingDecision({
2147
- decision: SamplingDecision.NOT_RECORD,
2148
- context,
2149
- spanAttributes,
2150
- sampleRand,
2151
- downstreamTraceSampleRate: 0, // we don't want to sample anything in the downstream trace either
2152
- }),
2153
- };
2154
- }
2133
+ const sampleRand = parseSampleRate(dsc?.sample_rand) ?? Math.random();
2155
2134
 
2156
- if (
2157
- !sampled &&
2158
- // 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)
2159
- parentSampled === undefined
2160
- ) {
2161
- DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
2162
- this._client.recordDroppedEvent('sample_rate', 'transaction');
2163
- }
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
+ );
2164
2146
 
2165
- return {
2166
- ...wrapSamplingDecision({
2167
- decision: sampled ? SamplingDecision.RECORD_AND_SAMPLED : SamplingDecision.NOT_RECORD,
2168
- context,
2169
- spanAttributes,
2170
- sampleRand,
2171
- downstreamTraceSampleRate: localSampleRateWasApplied ? sampleRate : undefined,
2172
- }),
2173
- attributes: {
2174
- // We set the sample rate on the span when a local sample rate was applied to better understand how traces were sampled in Sentry
2175
- [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: localSampleRateWasApplied ? sampleRate : undefined,
2176
- },
2177
- };
2178
- } else {
2179
- return {
2180
- ...wrapSamplingDecision({
2181
- decision: parentSampled ? SamplingDecision.RECORD_AND_SAMPLED : SamplingDecision.NOT_RECORD,
2182
- context,
2183
- spanAttributes,
2184
- }),
2185
- };
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
+ });
2186
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
+ };
2187
2182
  }
2188
2183
 
2189
2184
  /** Returns the sampler name or short description with the configuration. */