@sentry/opentelemetry 8.29.0 → 8.31.0-beta.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,9 +1,9 @@
1
1
  import { _optionalChain } from '@sentry/utils';
2
- import { SEMATTRS_HTTP_URL, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_RPC_SERVICE, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_FAAS_TRIGGER, SEMATTRS_DB_STATEMENT, SEMATTRS_HTTP_TARGET, SEMATTRS_HTTP_ROUTE, SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
2
+ 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';
3
3
  import { parseUrl, getSanitizedUrlString, addNonEnumerableProperty, stripUrlQueryAndFragment, dynamicSamplingContextToSentryBaggageHeader, baggageHeaderToDynamicSamplingContext, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, propagationContextFromHeaders, stringMatchesSomePattern, 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, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getDynamicSamplingContextFromSpan, getRootSpan, getCurrentScope, getIsolationScope, hasTracingEnabled, getDynamicSamplingContextFromClient, handleCallbackErrors, continueTrace as continueTrace$1, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, spanTimeInputToSeconds, getCapturedScopesOnSpan, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, getStatusMessage, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
6
+ import { SDK_VERSION, isSentryRequestUrl, getClient, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getDynamicSamplingContextFromSpan, getRootSpan, getCurrentScope, getIsolationScope, getDynamicSamplingContextFromClient, handleCallbackErrors, continueTrace as continueTrace$1, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, spanTimeInputToSeconds, getCapturedScopesOnSpan, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, getStatusMessage, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, hasTracingEnabled, sampleSpan } from '@sentry/core';
7
7
  export { getClient, getCurrentHubShim, getDynamicSamplingContextFromSpan } from '@sentry/core';
8
8
  import { TraceState, W3CBaggagePropagator, isTracingSuppressed, suppressTracing as suppressTracing$1 } from '@opentelemetry/core';
9
9
  import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
@@ -92,9 +92,17 @@ function getRequestSpanData(span) {
92
92
  return {};
93
93
  }
94
94
 
95
+ // eslint-disable-next-line deprecation/deprecation
96
+ const maybeUrlAttribute = (span.attributes[ATTR_URL_FULL] || span.attributes[SEMATTRS_HTTP_URL])
97
+
98
+ ;
99
+
95
100
  const data = {
96
- url: span.attributes[SEMATTRS_HTTP_URL] ,
97
- 'http.method': span.attributes[SEMATTRS_HTTP_METHOD] ,
101
+ url: maybeUrlAttribute,
102
+ // eslint-disable-next-line deprecation/deprecation
103
+ 'http.method': (span.attributes[ATTR_HTTP_REQUEST_METHOD] || span.attributes[SEMATTRS_HTTP_METHOD])
104
+
105
+ ,
98
106
  };
99
107
 
100
108
  // Default to GET if URL is set but method is not
@@ -103,9 +111,8 @@ function getRequestSpanData(span) {
103
111
  }
104
112
 
105
113
  try {
106
- const urlStr = span.attributes[SEMATTRS_HTTP_URL];
107
- if (typeof urlStr === 'string') {
108
- const url = parseUrl(urlStr);
114
+ if (typeof maybeUrlAttribute === 'string') {
115
+ const url = parseUrl(maybeUrlAttribute);
109
116
 
110
117
  data.url = getSanitizedUrlString(url);
111
118
 
@@ -254,7 +261,9 @@ function isSentryRequestSpan(span) {
254
261
 
255
262
  const { attributes } = span;
256
263
 
257
- const httpUrl = attributes[SEMATTRS_HTTP_URL];
264
+ // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `ATTR_HTTP_URL`, for now.
265
+ // eslint-disable-next-line deprecation/deprecation
266
+ const httpUrl = attributes[SEMATTRS_HTTP_URL] || attributes[ATTR_URL_FULL];
258
267
 
259
268
  if (!httpUrl) {
260
269
  return false;
@@ -281,14 +290,13 @@ function inferSpanData(name, attributes, kind) {
281
290
  }
282
291
 
283
292
  // if http.method exists, this is an http request span
284
- //
285
- // TODO: Referencing `http.request.method` is a temporary workaround until the semantic
286
- // conventions export an attribute key for it.
287
- const httpMethod = attributes['http.request.method'] || attributes[SEMATTRS_HTTP_METHOD];
293
+ // eslint-disable-next-line deprecation/deprecation
294
+ const httpMethod = attributes[ATTR_HTTP_REQUEST_METHOD] || attributes[SEMATTRS_HTTP_METHOD];
288
295
  if (httpMethod) {
289
296
  return descriptionForHttpMethod({ attributes, name, kind }, httpMethod);
290
297
  }
291
298
 
299
+ // eslint-disable-next-line deprecation/deprecation
292
300
  const dbSystem = attributes[SEMATTRS_DB_SYSTEM];
293
301
  const opIsCache =
294
302
  typeof attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'string' &&
@@ -301,6 +309,7 @@ function inferSpanData(name, attributes, kind) {
301
309
  }
302
310
 
303
311
  // If rpc.service exists then this is a rpc call span.
312
+ // eslint-disable-next-line deprecation/deprecation
304
313
  const rpcService = attributes[SEMATTRS_RPC_SERVICE];
305
314
  if (rpcService) {
306
315
  return {
@@ -311,6 +320,7 @@ function inferSpanData(name, attributes, kind) {
311
320
  }
312
321
 
313
322
  // If messaging.system exists then this is a messaging system span.
323
+ // eslint-disable-next-line deprecation/deprecation
314
324
  const messagingSystem = attributes[SEMATTRS_MESSAGING_SYSTEM];
315
325
  if (messagingSystem) {
316
326
  return {
@@ -321,6 +331,7 @@ function inferSpanData(name, attributes, kind) {
321
331
  }
322
332
 
323
333
  // If faas.trigger exists then this is a function as a service span.
334
+ // eslint-disable-next-line deprecation/deprecation
324
335
  const faasTrigger = attributes[SEMATTRS_FAAS_TRIGGER];
325
336
  if (faasTrigger) {
326
337
  return { op: faasTrigger.toString(), description: name, source: 'route' };
@@ -344,6 +355,7 @@ function parseSpanDescription(span) {
344
355
 
345
356
  function descriptionForDbSystem({ attributes, name }) {
346
357
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
358
+ // eslint-disable-next-line deprecation/deprecation
347
359
  const statement = attributes[SEMATTRS_DB_STATEMENT];
348
360
 
349
361
  const description = statement ? statement.toString() : name;
@@ -367,6 +379,11 @@ function descriptionForHttpMethod(
367
379
  break;
368
380
  }
369
381
 
382
+ // Spans for HTTP requests we have determined to be prefetch requests will have a `.prefetch` postfix in the op
383
+ if (attributes['sentry.http.prefetch']) {
384
+ opParts.push('prefetch');
385
+ }
386
+
370
387
  const { urlPath, url, query, fragment, hasRoute } = getSanitizedUrl(attributes, kind);
371
388
 
372
389
  if (!urlPath) {
@@ -443,11 +460,13 @@ function getSanitizedUrl(
443
460
 
444
461
  {
445
462
  // This is the relative path of the URL, e.g. /sub
463
+ // eslint-disable-next-line deprecation/deprecation
446
464
  const httpTarget = attributes[SEMATTRS_HTTP_TARGET];
447
465
  // This is the full URL, including host & query params etc., e.g. https://example.com/sub?foo=bar
448
- const httpUrl = attributes[SEMATTRS_HTTP_URL];
466
+ // eslint-disable-next-line deprecation/deprecation
467
+ const httpUrl = attributes[SEMATTRS_HTTP_URL] || attributes[ATTR_URL_FULL];
449
468
  // This is the normalized route name - may not always be available!
450
- const httpRoute = attributes[SEMATTRS_HTTP_ROUTE];
469
+ const httpRoute = attributes[ATTR_HTTP_ROUTE];
451
470
 
452
471
  const parsedUrl = typeof httpUrl === 'string' ? parseUrl(httpUrl) : undefined;
453
472
  const url = parsedUrl ? getSanitizedUrlString(parsedUrl) : undefined;
@@ -769,7 +788,7 @@ class SentryPropagator extends W3CBaggagePropagator {
769
788
  function getInjectionData(context)
770
789
 
771
790
  {
772
- const span = hasTracingEnabled() ? trace.getSpan(context) : undefined;
791
+ const span = trace.getSpan(context);
773
792
  const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _5 => _5.spanContext, 'call', _6 => _6(), 'access', _7 => _7.isRemote]);
774
793
 
775
794
  // If we have a local span, we can just pick everything from it
@@ -863,13 +882,16 @@ function getExistingBaggage(carrier) {
863
882
  * 2. Else, if the active span has no URL attribute (e.g. it is unsampled), we check a special trace state (which we set in our sampler).
864
883
  */
865
884
  function getCurrentURL(span) {
866
- const urlAttribute = _optionalChain$6([spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[SEMATTRS_HTTP_URL]]);
885
+ const spanData = spanToJSON(span).data;
886
+ // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_URL`, for now.
887
+ // eslint-disable-next-line deprecation/deprecation
888
+ const urlAttribute = _optionalChain$6([spanData, 'optionalAccess', _11 => _11[SEMATTRS_HTTP_URL]]) || _optionalChain$6([spanData, 'optionalAccess', _12 => _12[ATTR_URL_FULL]]);
867
889
  if (urlAttribute) {
868
890
  return urlAttribute;
869
891
  }
870
892
 
871
893
  // Also look at the traceState, which we may set in the sampler even for unsampled spans
872
- const urlTraceState = _optionalChain$6([span, 'access', _14 => _14.spanContext, 'call', _15 => _15(), 'access', _16 => _16.traceState, 'optionalAccess', _17 => _17.get, 'call', _18 => _18(SENTRY_TRACE_STATE_URL)]);
894
+ const urlTraceState = _optionalChain$6([span, 'access', _13 => _13.spanContext, 'call', _14 => _14(), 'access', _15 => _15.traceState, 'optionalAccess', _16 => _16.get, 'call', _17 => _17(SENTRY_TRACE_STATE_URL)]);
873
895
  if (urlTraceState) {
874
896
  return urlTraceState;
875
897
  }
@@ -1519,7 +1541,9 @@ function mapStatus(span) {
1519
1541
  function inferStatusFromAttributes(attributes) {
1520
1542
  // If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.
1521
1543
 
1522
- const httpCodeAttribute = attributes[SEMATTRS_HTTP_STATUS_CODE];
1544
+ // eslint-disable-next-line deprecation/deprecation
1545
+ const httpCodeAttribute = attributes[ATTR_HTTP_RESPONSE_STATUS_CODE] || attributes[SEMATTRS_HTTP_STATUS_CODE];
1546
+ // eslint-disable-next-line deprecation/deprecation
1523
1547
  const grpcCodeAttribute = attributes[SEMATTRS_RPC_GRPC_STATUS_CODE];
1524
1548
 
1525
1549
  const numberHttpCode =
@@ -1848,6 +1872,7 @@ function removeSentryAttributes(data) {
1848
1872
  /* eslint-disable @typescript-eslint/no-dynamic-delete */
1849
1873
  delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
1850
1874
  delete cleanedData[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE];
1875
+ delete cleanedData['sentry.skip_span_data_inference'];
1851
1876
  /* eslint-enable @typescript-eslint/no-dynamic-delete */
1852
1877
 
1853
1878
  return cleanedData;
@@ -1861,9 +1886,10 @@ function getData(span) {
1861
1886
  data['otel.kind'] = SpanKind[span.kind];
1862
1887
  }
1863
1888
 
1864
- if (attributes[SEMATTRS_HTTP_STATUS_CODE]) {
1865
- const statusCode = attributes[SEMATTRS_HTTP_STATUS_CODE] ;
1866
- data['http.response.status_code'] = statusCode;
1889
+ // eslint-disable-next-line deprecation/deprecation
1890
+ const maybeHttpStatusCodeAttribute = attributes[SEMATTRS_HTTP_STATUS_CODE];
1891
+ if (maybeHttpStatusCodeAttribute) {
1892
+ data[ATTR_HTTP_RESPONSE_STATUS_CODE] = maybeHttpStatusCodeAttribute ;
1867
1893
  }
1868
1894
 
1869
1895
  const requestData = getRequestSpanData(span);
@@ -1995,13 +2021,13 @@ class SentrySampler {
1995
2021
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
1996
2022
  }
1997
2023
 
2024
+ // `ATTR_HTTP_REQUEST_METHOD` is the new attribute, but we still support the old one, `SEMATTRS_HTTP_METHOD`, for now.
2025
+ // eslint-disable-next-line deprecation/deprecation
2026
+ const maybeSpanHttpMethod = spanAttributes[SEMATTRS_HTTP_METHOD] || spanAttributes[ATTR_HTTP_REQUEST_METHOD];
2027
+
1998
2028
  // If we have a http.client span that has no local parent, we never want to sample it
1999
2029
  // but we want to leave downstream sampling decisions up to the server
2000
- if (
2001
- spanKind === SpanKind.CLIENT &&
2002
- spanAttributes[SEMATTRS_HTTP_METHOD] &&
2003
- (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _3 => _3.isRemote]))
2004
- ) {
2030
+ if (spanKind === SpanKind.CLIENT && maybeSpanHttpMethod && (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _3 => _3.isRemote]))) {
2005
2031
  return wrapSamplingDecision({ decision: undefined, context, spanAttributes });
2006
2032
  }
2007
2033
 
@@ -2052,7 +2078,7 @@ class SentrySampler {
2052
2078
  [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
2053
2079
  };
2054
2080
 
2055
- const method = `${spanAttributes[SEMATTRS_HTTP_METHOD]}`.toUpperCase();
2081
+ const method = `${maybeSpanHttpMethod}`.toUpperCase();
2056
2082
  if (method === 'OPTIONS' || method === 'HEAD') {
2057
2083
  DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
2058
2084
 
@@ -2141,7 +2167,9 @@ function getBaseTraceState(context, spanAttributes) {
2141
2167
  let traceState = _optionalChain([parentContext, 'optionalAccess', _6 => _6.traceState]) || new TraceState();
2142
2168
 
2143
2169
  // We always keep the URL on the trace state, so we can access it in the propagator
2144
- const url = spanAttributes[SEMATTRS_HTTP_URL];
2170
+ // `ATTR_URL_FULL` is the new attribute, but we still support the old one, `ATTR_HTTP_URL`, for now.
2171
+ // eslint-disable-next-line deprecation/deprecation
2172
+ const url = spanAttributes[SEMATTRS_HTTP_URL] || spanAttributes[ATTR_URL_FULL];
2145
2173
  if (url && typeof url === 'string') {
2146
2174
  traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
2147
2175
  }