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

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';
@@ -7,6 +7,7 @@ import { SDK_VERSION, getClient, getDynamicSamplingContextFromClient, SEMANTIC_A
7
7
  export { getClient, getCurrentHubShim } from '@sentry/core';
8
8
  import { W3CBaggagePropagator, isTracingSuppressed, TraceState, suppressTracing as suppressTracing$1 } from '@opentelemetry/core';
9
9
  import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
10
+ import { registerInstrumentations } from '@opentelemetry/instrumentation';
10
11
 
11
12
  /**
12
13
  * Check if a given span has attributes.
@@ -86,8 +87,8 @@ function getRequestSpanData(span) {
86
87
  }
87
88
 
88
89
  const data = {
89
- url: span.attributes[SemanticAttributes.HTTP_URL] ,
90
- 'http.method': span.attributes[SemanticAttributes.HTTP_METHOD] ,
90
+ url: span.attributes[SEMATTRS_HTTP_URL] ,
91
+ 'http.method': span.attributes[SEMATTRS_HTTP_METHOD] ,
91
92
  };
92
93
 
93
94
  // Default to GET if URL is set but method is not
@@ -96,7 +97,7 @@ function getRequestSpanData(span) {
96
97
  }
97
98
 
98
99
  try {
99
- const urlStr = span.attributes[SemanticAttributes.HTTP_URL];
100
+ const urlStr = span.attributes[SEMATTRS_HTTP_URL];
100
101
  if (typeof urlStr === 'string') {
101
102
  const url = parseUrl(urlStr);
102
103
 
@@ -285,19 +286,19 @@ function parseSpanDescription(span) {
285
286
  const name = spanHasName(span) ? span.name : '<unknown>';
286
287
 
287
288
  // if http.method exists, this is an http request span
288
- const httpMethod = attributes[SemanticAttributes.HTTP_METHOD];
289
+ const httpMethod = attributes[SEMATTRS_HTTP_METHOD];
289
290
  if (httpMethod) {
290
291
  return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
291
292
  }
292
293
 
293
294
  // If db.type exists then this is a database call span.
294
- const dbSystem = attributes[SemanticAttributes.DB_SYSTEM];
295
+ const dbSystem = attributes[SEMATTRS_DB_SYSTEM];
295
296
  if (dbSystem) {
296
297
  return descriptionForDbSystem({ attributes, name });
297
298
  }
298
299
 
299
300
  // If rpc.service exists then this is a rpc call span.
300
- const rpcService = attributes[SemanticAttributes.RPC_SERVICE];
301
+ const rpcService = attributes[SEMATTRS_RPC_SERVICE];
301
302
  if (rpcService) {
302
303
  return {
303
304
  op: 'rpc',
@@ -307,7 +308,7 @@ function parseSpanDescription(span) {
307
308
  }
308
309
 
309
310
  // If messaging.system exists then this is a messaging system span.
310
- const messagingSystem = attributes[SemanticAttributes.MESSAGING_SYSTEM];
311
+ const messagingSystem = attributes[SEMATTRS_MESSAGING_SYSTEM];
311
312
  if (messagingSystem) {
312
313
  return {
313
314
  op: 'message',
@@ -317,7 +318,7 @@ function parseSpanDescription(span) {
317
318
  }
318
319
 
319
320
  // If faas.trigger exists then this is a function as a service span.
320
- const faasTrigger = attributes[SemanticAttributes.FAAS_TRIGGER];
321
+ const faasTrigger = attributes[SEMATTRS_FAAS_TRIGGER];
321
322
  if (faasTrigger) {
322
323
  return { op: faasTrigger.toString(), description: name, source: 'route' };
323
324
  }
@@ -327,7 +328,7 @@ function parseSpanDescription(span) {
327
328
 
328
329
  function descriptionForDbSystem({ attributes, name }) {
329
330
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
330
- const statement = attributes[SemanticAttributes.DB_STATEMENT];
331
+ const statement = attributes[SEMATTRS_DB_STATEMENT];
331
332
 
332
333
  const description = statement ? statement.toString() : name;
333
334
 
@@ -390,11 +391,11 @@ function getSanitizedUrl(
390
391
 
391
392
  {
392
393
  // This is the relative path of the URL, e.g. /sub
393
- const httpTarget = attributes[SemanticAttributes.HTTP_TARGET];
394
+ const httpTarget = attributes[SEMATTRS_HTTP_TARGET];
394
395
  // 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];
396
+ const httpUrl = attributes[SEMATTRS_HTTP_URL];
396
397
  // This is the normalized route name - may not always be available!
397
- const httpRoute = attributes[SemanticAttributes.HTTP_ROUTE];
398
+ const httpRoute = attributes[SEMATTRS_HTTP_ROUTE];
398
399
 
399
400
  const parsedUrl = typeof httpUrl === 'string' ? parseUrl(httpUrl) : undefined;
400
401
  const url = parsedUrl ? getSanitizedUrlString(parsedUrl) : undefined;
@@ -486,7 +487,7 @@ function isSentryRequestSpan(span) {
486
487
 
487
488
  const { attributes } = span;
488
489
 
489
- const httpUrl = attributes[SemanticAttributes.HTTP_URL];
490
+ const httpUrl = attributes[SEMATTRS_HTTP_URL];
490
491
 
491
492
  if (!httpUrl) {
492
493
  return false;
@@ -811,7 +812,7 @@ function getExistingBaggage(carrier) {
811
812
  * 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
813
  */
813
814
  function getCurrentURL(span) {
814
- const urlAttribute = _optionalChain$4([spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[SemanticAttributes.HTTP_URL]]);
815
+ const urlAttribute = _optionalChain$4([spanToJSON, 'call', _11 => _11(span), 'access', _12 => _12.data, 'optionalAccess', _13 => _13[SEMATTRS_HTTP_URL]]);
815
816
  if (urlAttribute) {
816
817
  return urlAttribute;
817
818
  }
@@ -1085,23 +1086,14 @@ function suppressTracing(callback) {
1085
1086
  return context.with(ctx, callback);
1086
1087
  }
1087
1088
 
1088
- /**
1089
- * Ensure the global hub is setup.
1090
- *
1091
- * @deprecated This will be removed in the next major version.
1092
- */
1093
- function setupGlobalHub() {
1094
- // noop
1095
- }
1096
-
1097
1089
  /** Ensure the `trace` context is set on all events. */
1098
1090
  function setupEventContextTrace(client) {
1099
- client.addEventProcessor(event => {
1091
+ client.on('preprocessEvent', event => {
1100
1092
  const span = getActiveSpan();
1101
1093
  // For transaction events, this is handled separately
1102
1094
  // Because the active span may not be the span that is actually the transaction event
1103
1095
  if (!span || event.type === 'transaction') {
1104
- return event;
1096
+ return;
1105
1097
  }
1106
1098
 
1107
1099
  const spanContext = span.spanContext();
@@ -1117,12 +1109,16 @@ function setupEventContextTrace(client) {
1117
1109
  };
1118
1110
 
1119
1111
  const rootSpan = getRootSpan(span);
1112
+
1113
+ event.sdkProcessingMetadata = {
1114
+ dynamicSamplingContext: getDynamicSamplingContextFromSpan(rootSpan),
1115
+ ...event.sdkProcessingMetadata,
1116
+ };
1117
+
1120
1118
  const transactionName = spanHasName(rootSpan) ? rootSpan.name : undefined;
1121
1119
  if (transactionName && !event.transaction) {
1122
1120
  event.transaction = transactionName;
1123
1121
  }
1124
-
1125
- return event;
1126
1122
  });
1127
1123
  }
1128
1124
 
@@ -1415,8 +1411,8 @@ function mapStatus(span) {
1415
1411
 
1416
1412
  // If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.
1417
1413
 
1418
- const httpCodeAttribute = attributes[SemanticAttributes.HTTP_STATUS_CODE];
1419
- const grpcCodeAttribute = attributes[SemanticAttributes.RPC_GRPC_STATUS_CODE];
1414
+ const httpCodeAttribute = attributes[SEMATTRS_HTTP_STATUS_CODE];
1415
+ const grpcCodeAttribute = attributes[SEMATTRS_RPC_GRPC_STATUS_CODE];
1420
1416
 
1421
1417
  const numberHttpCode =
1422
1418
  typeof httpCodeAttribute === 'number'
@@ -1739,8 +1735,8 @@ function getData(span) {
1739
1735
  'otel.kind': SpanKind[span.kind],
1740
1736
  };
1741
1737
 
1742
- if (attributes[SemanticAttributes.HTTP_STATUS_CODE]) {
1743
- const statusCode = attributes[SemanticAttributes.HTTP_STATUS_CODE] ;
1738
+ if (attributes[SEMATTRS_HTTP_STATUS_CODE]) {
1739
+ const statusCode = attributes[SEMATTRS_HTTP_STATUS_CODE] ;
1744
1740
  data['http.response.status_code'] = statusCode;
1745
1741
  }
1746
1742
 
@@ -1872,7 +1868,7 @@ class SentrySampler {
1872
1868
  let traceState = _optionalChain([parentContext, 'optionalAccess', _3 => _3.traceState]) || new TraceState();
1873
1869
 
1874
1870
  // We always keep the URL on the trace state, so we can access it in the propagator
1875
- const url = spanAttributes[SemanticAttributes.HTTP_URL];
1871
+ const url = spanAttributes[SEMATTRS_HTTP_URL];
1876
1872
  if (url && typeof url === 'string') {
1877
1873
  traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
1878
1874
  }
@@ -1885,25 +1881,20 @@ class SentrySampler {
1885
1881
  // but we want to leave downstream sampling decisions up to the server
1886
1882
  if (
1887
1883
  spanKind === SpanKind.CLIENT &&
1888
- spanAttributes[SemanticAttributes.HTTP_METHOD] &&
1884
+ spanAttributes[SEMATTRS_HTTP_METHOD] &&
1889
1885
  (!parentSpan || _optionalChain([parentContext, 'optionalAccess', _4 => _4.isRemote]))
1890
1886
  ) {
1891
1887
  return { decision: SamplingDecision.NOT_RECORD, traceState };
1892
1888
  }
1893
1889
 
1894
- let parentSampled = undefined;
1890
+ const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
1895
1891
 
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
- }
1892
+ // If we encounter a span emitted by Next.js, we do not want to sample it
1893
+ // The reason for this is that the data quality of the spans varies, it is different per version of Next,
1894
+ // and we need to keep our manual instrumentation around for the edge runtime anyhow.
1895
+ // BUT we only do this if we don't have a parent span with a sampling decision yet (or if the parent is remote)
1896
+ if (spanAttributes['next.span_type'] && (typeof parentSampled !== 'boolean' || _optionalChain([parentContext, 'optionalAccess', _5 => _5.isRemote]))) {
1897
+ return { decision: SamplingDecision.NOT_RECORD, traceState: traceState };
1907
1898
  }
1908
1899
 
1909
1900
  const [sampled, sampleRate] = sampleSpan(options, {
@@ -1920,7 +1911,7 @@ class SentrySampler {
1920
1911
  [SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
1921
1912
  };
1922
1913
 
1923
- const method = `${spanAttributes[SemanticAttributes.HTTP_METHOD]}`.toUpperCase();
1914
+ const method = `${spanAttributes[SEMATTRS_HTTP_METHOD]}`.toUpperCase();
1924
1915
  if (method === 'OPTIONS' || method === 'HEAD') {
1925
1916
  DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
1926
1917
  return {
@@ -1959,5 +1950,38 @@ function getParentRemoteSampled(parentSpan) {
1959
1950
  return traceparentData && traceId === traceparentData.traceId ? traceparentData.sampled : undefined;
1960
1951
  }
1961
1952
 
1962
- 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 };
1953
+ function getParentSampled(parentSpan, traceId, spanName) {
1954
+ const parentContext = parentSpan.spanContext();
1955
+
1956
+ // Only inherit sample rate if `traceId` is the same
1957
+ // Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
1958
+ if (isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
1959
+ if (parentContext.isRemote) {
1960
+ const parentSampled = getParentRemoteSampled(parentSpan);
1961
+ DEBUG_BUILD &&
1962
+ logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
1963
+ return parentSampled;
1964
+ }
1965
+
1966
+ const parentSampled = getSamplingDecision(parentContext);
1967
+ DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
1968
+ return parentSampled;
1969
+ }
1970
+
1971
+ return undefined;
1972
+ }
1973
+
1974
+ /**
1975
+ * This method takes an OpenTelemetry instrumentation or
1976
+ * array of instrumentations and registers them with OpenTelemetry.
1977
+ */
1978
+ function addOpenTelemetryInstrumentation(
1979
+ instrumentation,
1980
+ ) {
1981
+ registerInstrumentations({
1982
+ instrumentations: Array.isArray(instrumentation) ? instrumentation : [instrumentation],
1983
+ });
1984
+ }
1985
+
1986
+ export { SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, getActiveSpan, getDynamicSamplingContextFromSpan, getRequestSpanData, getScopesFromContext, getSpanKind, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass };
1963
1987
  //# sourceMappingURL=index.js.map