@sentry/opentelemetry 8.0.0-beta.1 → 8.0.0-beta.2

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.
package/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { _optionalChain } from '@sentry/utils';
2
- import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
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';
3
3
  import { parseUrl, getSanitizedUrlString, addNonEnumerableProperty, baggageHeaderToDynamicSamplingContext, stripUrlQueryAndFragment, 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, context, SpanStatusCode, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
@@ -86,8 +86,8 @@ function getRequestSpanData(span) {
86
86
  }
87
87
 
88
88
  const data = {
89
- url: span.attributes[SemanticAttributes.HTTP_URL] ,
90
- 'http.method': span.attributes[SemanticAttributes.HTTP_METHOD] ,
89
+ url: span.attributes[SEMATTRS_HTTP_URL] ,
90
+ 'http.method': span.attributes[SEMATTRS_HTTP_METHOD] ,
91
91
  };
92
92
 
93
93
  // Default to GET if URL is set but method is not
@@ -96,7 +96,7 @@ function getRequestSpanData(span) {
96
96
  }
97
97
 
98
98
  try {
99
- const urlStr = span.attributes[SemanticAttributes.HTTP_URL];
99
+ const urlStr = span.attributes[SEMATTRS_HTTP_URL];
100
100
  if (typeof urlStr === 'string') {
101
101
  const url = parseUrl(urlStr);
102
102
 
@@ -285,19 +285,19 @@ function parseSpanDescription(span) {
285
285
  const name = spanHasName(span) ? span.name : '<unknown>';
286
286
 
287
287
  // if http.method exists, this is an http request span
288
- const httpMethod = attributes[SemanticAttributes.HTTP_METHOD];
288
+ const httpMethod = attributes[SEMATTRS_HTTP_METHOD];
289
289
  if (httpMethod) {
290
290
  return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
291
291
  }
292
292
 
293
293
  // If db.type exists then this is a database call span.
294
- const dbSystem = attributes[SemanticAttributes.DB_SYSTEM];
294
+ const dbSystem = attributes[SEMATTRS_DB_SYSTEM];
295
295
  if (dbSystem) {
296
296
  return descriptionForDbSystem({ attributes, name });
297
297
  }
298
298
 
299
299
  // If rpc.service exists then this is a rpc call span.
300
- const rpcService = attributes[SemanticAttributes.RPC_SERVICE];
300
+ const rpcService = attributes[SEMATTRS_RPC_SERVICE];
301
301
  if (rpcService) {
302
302
  return {
303
303
  op: 'rpc',
@@ -307,7 +307,7 @@ function parseSpanDescription(span) {
307
307
  }
308
308
 
309
309
  // If messaging.system exists then this is a messaging system span.
310
- const messagingSystem = attributes[SemanticAttributes.MESSAGING_SYSTEM];
310
+ const messagingSystem = attributes[SEMATTRS_MESSAGING_SYSTEM];
311
311
  if (messagingSystem) {
312
312
  return {
313
313
  op: 'message',
@@ -317,7 +317,7 @@ function parseSpanDescription(span) {
317
317
  }
318
318
 
319
319
  // If faas.trigger exists then this is a function as a service span.
320
- const faasTrigger = attributes[SemanticAttributes.FAAS_TRIGGER];
320
+ const faasTrigger = attributes[SEMATTRS_FAAS_TRIGGER];
321
321
  if (faasTrigger) {
322
322
  return { op: faasTrigger.toString(), description: name, source: 'route' };
323
323
  }
@@ -327,7 +327,7 @@ function parseSpanDescription(span) {
327
327
 
328
328
  function descriptionForDbSystem({ attributes, name }) {
329
329
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
330
- const statement = attributes[SemanticAttributes.DB_STATEMENT];
330
+ const statement = attributes[SEMATTRS_DB_STATEMENT];
331
331
 
332
332
  const description = statement ? statement.toString() : name;
333
333
 
@@ -390,11 +390,11 @@ function getSanitizedUrl(
390
390
 
391
391
  {
392
392
  // This is the relative path of the URL, e.g. /sub
393
- const httpTarget = attributes[SemanticAttributes.HTTP_TARGET];
393
+ const httpTarget = attributes[SEMATTRS_HTTP_TARGET];
394
394
  // This is the full URL, including host & query params etc., e.g. https://example.com/sub?foo=bar
395
- const httpUrl = attributes[SemanticAttributes.HTTP_URL];
395
+ const httpUrl = attributes[SEMATTRS_HTTP_URL];
396
396
  // This is the normalized route name - may not always be available!
397
- const httpRoute = attributes[SemanticAttributes.HTTP_ROUTE];
397
+ const httpRoute = attributes[SEMATTRS_HTTP_ROUTE];
398
398
 
399
399
  const parsedUrl = typeof httpUrl === 'string' ? parseUrl(httpUrl) : undefined;
400
400
  const url = parsedUrl ? getSanitizedUrlString(parsedUrl) : undefined;
@@ -486,7 +486,7 @@ function isSentryRequestSpan(span) {
486
486
 
487
487
  const { attributes } = span;
488
488
 
489
- const httpUrl = attributes[SemanticAttributes.HTTP_URL];
489
+ const httpUrl = attributes[SEMATTRS_HTTP_URL];
490
490
 
491
491
  if (!httpUrl) {
492
492
  return false;
@@ -811,7 +811,7 @@ function getExistingBaggage(carrier) {
811
811
  * 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).
812
812
  */
813
813
  function getCurrentURL(span) {
814
- const urlAttribute = _optionalChain$4([spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[SemanticAttributes.HTTP_URL]]);
814
+ const urlAttribute = _optionalChain$4([spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[SEMATTRS_HTTP_URL]]);
815
815
  if (urlAttribute) {
816
816
  return urlAttribute;
817
817
  }
@@ -1096,12 +1096,12 @@ function setupGlobalHub() {
1096
1096
 
1097
1097
  /** Ensure the `trace` context is set on all events. */
1098
1098
  function setupEventContextTrace(client) {
1099
- client.addEventProcessor(event => {
1099
+ client.on('preprocessEvent', event => {
1100
1100
  const span = getActiveSpan();
1101
1101
  // For transaction events, this is handled separately
1102
1102
  // Because the active span may not be the span that is actually the transaction event
1103
1103
  if (!span || event.type === 'transaction') {
1104
- return event;
1104
+ return;
1105
1105
  }
1106
1106
 
1107
1107
  const spanContext = span.spanContext();
@@ -1116,13 +1116,16 @@ function setupEventContextTrace(client) {
1116
1116
  ...event.contexts,
1117
1117
  };
1118
1118
 
1119
+ event.sdkProcessingMetadata = {
1120
+ dynamicSamplingContext: getDynamicSamplingContextFromSpan(span),
1121
+ ...event.sdkProcessingMetadata,
1122
+ };
1123
+
1119
1124
  const rootSpan = getRootSpan(span);
1120
1125
  const transactionName = spanHasName(rootSpan) ? rootSpan.name : undefined;
1121
1126
  if (transactionName && !event.transaction) {
1122
1127
  event.transaction = transactionName;
1123
1128
  }
1124
-
1125
- return event;
1126
1129
  });
1127
1130
  }
1128
1131
 
@@ -1415,8 +1418,8 @@ function mapStatus(span) {
1415
1418
 
1416
1419
  // If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.
1417
1420
 
1418
- const httpCodeAttribute = attributes[SemanticAttributes.HTTP_STATUS_CODE];
1419
- const grpcCodeAttribute = attributes[SemanticAttributes.RPC_GRPC_STATUS_CODE];
1421
+ const httpCodeAttribute = attributes[SEMATTRS_HTTP_STATUS_CODE];
1422
+ const grpcCodeAttribute = attributes[SEMATTRS_RPC_GRPC_STATUS_CODE];
1420
1423
 
1421
1424
  const numberHttpCode =
1422
1425
  typeof httpCodeAttribute === 'number'
@@ -1739,8 +1742,8 @@ function getData(span) {
1739
1742
  'otel.kind': SpanKind[span.kind],
1740
1743
  };
1741
1744
 
1742
- if (attributes[SemanticAttributes.HTTP_STATUS_CODE]) {
1743
- const statusCode = attributes[SemanticAttributes.HTTP_STATUS_CODE] ;
1745
+ if (attributes[SEMATTRS_HTTP_STATUS_CODE]) {
1746
+ const statusCode = attributes[SEMATTRS_HTTP_STATUS_CODE] ;
1744
1747
  data['http.response.status_code'] = statusCode;
1745
1748
  }
1746
1749
 
@@ -1872,7 +1875,7 @@ class SentrySampler {
1872
1875
  let traceState = _optionalChain([parentContext, 'optionalAccess', _3 => _3.traceState]) || new TraceState();
1873
1876
 
1874
1877
  // We always keep the URL on the trace state, so we can access it in the propagator
1875
- const url = spanAttributes[SemanticAttributes.HTTP_URL];
1878
+ const url = spanAttributes[SEMATTRS_HTTP_URL];
1876
1879
  if (url && typeof url === 'string') {
1877
1880
  traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
1878
1881
  }
@@ -1885,25 +1888,20 @@ class SentrySampler {
1885
1888
  // but we want to leave downstream sampling decisions up to the server
1886
1889
  if (
1887
1890
  spanKind === SpanKind.CLIENT &&
1888
- spanAttributes[SemanticAttributes.HTTP_METHOD] &&
1891
+ spanAttributes[SEMATTRS_HTTP_METHOD] &&
1889
1892
  (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _4 => _4.isRemote]))
1890
1893
  ) {
1891
1894
  return { decision: SamplingDecision.NOT_RECORD, traceState };
1892
1895
  }
1893
1896
 
1894
- let parentSampled = undefined;
1897
+ const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
1895
1898
 
1896
- // Only inherit sample rate if `traceId` is the same
1897
- // Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
1898
- if (parentSpan && parentContext && isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
1899
- if (parentContext.isRemote) {
1900
- parentSampled = getParentRemoteSampled(parentSpan);
1901
- DEBUG_BUILD &&
1902
- logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
1903
- } else {
1904
- parentSampled = getSamplingDecision(parentContext);
1905
- DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
1906
- }
1899
+ // If we encounter a span emitted by Next.js, we do not want to sample it
1900
+ // The reason for this is that the data quality of the spans varies, it is different per version of Next,
1901
+ // and we need to keep our manual instrumentation around for the edge runtime anyhow.
1902
+ // BUT we only do this if we don't have a parent span with a sampling decision yet
1903
+ if (spanAttributes['next.span_type'] && typeof parentSampled !== 'boolean') {
1904
+ return { decision: SamplingDecision.NOT_RECORD, traceState: traceState };
1907
1905
  }
1908
1906
 
1909
1907
  const [sampled, sampleRate] = sampleSpan(options, {
@@ -1920,7 +1918,7 @@ class SentrySampler {
1920
1918
  [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
1921
1919
  };
1922
1920
 
1923
- const method = `${spanAttributes[SemanticAttributes.HTTP_METHOD]}`.toUpperCase();
1921
+ const method = `${spanAttributes[SEMATTRS_HTTP_METHOD]}`.toUpperCase();
1924
1922
  if (method === 'OPTIONS' || method === 'HEAD') {
1925
1923
  DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
1926
1924
  return {
@@ -1959,5 +1957,26 @@ function getParentRemoteSampled(parentSpan) {
1959
1957
  return traceparentData && traceId === traceparentData.traceId ? traceparentData.sampled : undefined;
1960
1958
  }
1961
1959
 
1960
+ function getParentSampled(parentSpan, traceId, spanName) {
1961
+ const parentContext = parentSpan.spanContext();
1962
+
1963
+ // Only inherit sample rate if `traceId` is the same
1964
+ // Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
1965
+ if (isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
1966
+ if (parentContext.isRemote) {
1967
+ const parentSampled = getParentRemoteSampled(parentSpan);
1968
+ DEBUG_BUILD &&
1969
+ logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
1970
+ return parentSampled;
1971
+ }
1972
+
1973
+ const parentSampled = getSamplingDecision(parentContext);
1974
+ DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
1975
+ return parentSampled;
1976
+ }
1977
+
1978
+ return undefined;
1979
+ }
1980
+
1962
1981
  export { SentryPropagator, SentrySampler, SentrySpanProcessor, continueTrace, getActiveSpan, getDynamicSamplingContextFromSpan, getRequestSpanData, getScopesFromContext, getSpanKind, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, setupGlobalHub, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass };
1963
1982
  //# sourceMappingURL=index.js.map