@sentry/opentelemetry 8.13.0 → 8.14.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.
@@ -1917,16 +1917,8 @@ class SentrySampler {
1917
1917
  const parentSpan = api.trace.getSpan(context);
1918
1918
  const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
1919
1919
 
1920
- let traceState = _optionalChain([parentContext, 'optionalAccess', _3 => _3.traceState]) || new core$1.TraceState();
1921
-
1922
- // We always keep the URL on the trace state, so we can access it in the propagator
1923
- const url = spanAttributes[semanticConventions.SEMATTRS_HTTP_URL];
1924
- if (url && typeof url === 'string') {
1925
- traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
1926
- }
1927
-
1928
1920
  if (!core.hasTracingEnabled(options)) {
1929
- return { decision: sdkTraceBase.SamplingDecision.NOT_RECORD, traceState };
1921
+ return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1930
1922
  }
1931
1923
 
1932
1924
  // If we have a http.client span that has no local parent, we never want to sample it
@@ -1934,9 +1926,9 @@ class SentrySampler {
1934
1926
  if (
1935
1927
  spanKind === api.SpanKind.CLIENT &&
1936
1928
  spanAttributes[semanticConventions.SEMATTRS_HTTP_METHOD] &&
1937
- (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _4 => _4.isRemote]))
1929
+ (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _3 => _3.isRemote]))
1938
1930
  ) {
1939
- return { decision: sdkTraceBase.SamplingDecision.NOT_RECORD, traceState };
1931
+ return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1940
1932
  }
1941
1933
 
1942
1934
  const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
@@ -1953,7 +1945,7 @@ class SentrySampler {
1953
1945
  mutableSamplingDecision,
1954
1946
  );
1955
1947
  if (!mutableSamplingDecision.decision) {
1956
- return { decision: sdkTraceBase.SamplingDecision.NOT_RECORD, traceState: traceState };
1948
+ return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1957
1949
  }
1958
1950
 
1959
1951
  const [sampled, sampleRate] = core.sampleSpan(options, {
@@ -1973,25 +1965,22 @@ class SentrySampler {
1973
1965
  const method = `${spanAttributes[semanticConventions.SEMATTRS_HTTP_METHOD]}`.toUpperCase();
1974
1966
  if (method === 'OPTIONS' || method === 'HEAD') {
1975
1967
  DEBUG_BUILD && utils.logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
1968
+
1976
1969
  return {
1977
- decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
1970
+ ...wrapSamplingDecision({ decision: sdkTraceBase.SamplingDecision.NOT_RECORD, context, spanAttributes }),
1978
1971
  attributes,
1979
- traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
1980
1972
  };
1981
1973
  }
1982
1974
 
1983
1975
  if (!sampled) {
1984
1976
  return {
1985
- decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
1977
+ ...wrapSamplingDecision({ decision: sdkTraceBase.SamplingDecision.NOT_RECORD, context, spanAttributes }),
1986
1978
  attributes,
1987
- traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
1988
1979
  };
1989
1980
  }
1990
-
1991
1981
  return {
1992
- decision: sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED,
1982
+ ...wrapSamplingDecision({ decision: sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED, context, spanAttributes }),
1993
1983
  attributes,
1994
- traceState,
1995
1984
  };
1996
1985
  }
1997
1986
 
@@ -2030,6 +2019,46 @@ function getParentSampled(parentSpan, traceId, spanName) {
2030
2019
  return undefined;
2031
2020
  }
2032
2021
 
2022
+ /**
2023
+ * Wrap a sampling decision with data that Sentry needs to work properly with it.
2024
+ * If you pass `decision: undefined`, it will be treated as `NOT_RECORDING`, but in contrast to passing `NOT_RECORDING`
2025
+ * it will not propagate this decision to downstream Sentry SDKs.
2026
+ */
2027
+ function wrapSamplingDecision({
2028
+ decision,
2029
+ context,
2030
+ spanAttributes,
2031
+ }) {
2032
+ const traceState = getBaseTraceState(context, spanAttributes);
2033
+
2034
+ // If the decision is undefined, we treat it as NOT_RECORDING, but we don't propagate this decision to downstream SDKs
2035
+ // Which is done by not setting `SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING` traceState
2036
+ if (decision == undefined) {
2037
+ return { decision: sdkTraceBase.SamplingDecision.NOT_RECORD, traceState };
2038
+ }
2039
+
2040
+ if (decision === sdkTraceBase.SamplingDecision.NOT_RECORD) {
2041
+ return { decision, traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') };
2042
+ }
2043
+
2044
+ return { decision, traceState };
2045
+ }
2046
+
2047
+ function getBaseTraceState(context, spanAttributes) {
2048
+ const parentSpan = api.trace.getSpan(context);
2049
+ const parentContext = _optionalChain([parentSpan, 'optionalAccess', _4 => _4.spanContext, 'call', _5 => _5()]);
2050
+
2051
+ let traceState = _optionalChain([parentContext, 'optionalAccess', _6 => _6.traceState]) || new core$1.TraceState();
2052
+
2053
+ // We always keep the URL on the trace state, so we can access it in the propagator
2054
+ const url = spanAttributes[semanticConventions.SEMATTRS_HTTP_URL];
2055
+ if (url && typeof url === 'string') {
2056
+ traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
2057
+ }
2058
+
2059
+ return traceState;
2060
+ }
2061
+
2033
2062
  /**
2034
2063
  * This method takes an OpenTelemetry instrumentation or
2035
2064
  * array of instrumentations and registers them with OpenTelemetry.
@@ -2070,4 +2099,5 @@ exports.suppressTracing = suppressTracing;
2070
2099
  exports.withActiveSpan = withActiveSpan;
2071
2100
  exports.wrapClientClass = wrapClientClass;
2072
2101
  exports.wrapContextManagerClass = wrapContextManagerClass;
2102
+ exports.wrapSamplingDecision = wrapSamplingDecision;
2073
2103
  //# sourceMappingURL=index.js.map