@sentry/opentelemetry 8.18.0 → 8.20.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,11 +1,11 @@
1
1
  import { _optionalChain } from '@sentry/utils';
2
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
- import { parseUrl, getSanitizedUrlString, addNonEnumerableProperty, stripUrlQueryAndFragment, baggageHeaderToDynamicSamplingContext, LRUMap, logger, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, propagationContextFromHeaders, stringMatchesSomePattern, dynamicSamplingContextToSentryBaggageHeader, dropUndefinedKeys } from '@sentry/utils';
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, spanToJSON, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getCurrentScope, getIsolationScope, hasTracingEnabled, getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan, getRootSpan, handleCallbackErrors, continueTrace as continueTrace$1, setAsyncContextStrategy, getDefaultCurrentScope, getDefaultIsolationScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, timedEventsToMeasurements, captureEvent, spanTimeInputToSeconds, getCapturedScopesOnSpan, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getStatusMessage, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
6
+ import { SDK_VERSION, isSentryRequestUrl, getClient, SEMANTIC_ATTRIBUTE_SENTRY_OP, 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, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, getStatusMessage, getMetricSummaryJsonForSpan, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, sampleSpan } from '@sentry/core';
7
7
  export { getClient, getCurrentHubShim, getDynamicSamplingContextFromSpan } from '@sentry/core';
8
- import { W3CBaggagePropagator, isTracingSuppressed, TraceState, suppressTracing as suppressTracing$1 } from '@opentelemetry/core';
8
+ import { TraceState, W3CBaggagePropagator, isTracingSuppressed, suppressTracing as suppressTracing$1 } from '@opentelemetry/core';
9
9
  import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
10
10
  import { registerInstrumentations } from '@opentelemetry/instrumentation';
11
11
 
@@ -258,14 +258,9 @@ function isSentryRequestSpan(span) {
258
258
  }
259
259
 
260
260
  /**
261
- * Extract better op/description from an otel span.
262
- *
263
- * Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
261
+ * Infer the op & description for a set of name, attributes and kind of a span.
264
262
  */
265
- function parseSpanDescription(span) {
266
- const attributes = spanHasAttributes(span) ? span.attributes : {};
267
- const name = spanHasName(span) ? span.name : '<unknown>';
268
-
263
+ function inferSpanData(name, attributes, kind) {
269
264
  // This attribute is intentionally exported as a SEMATTR constant because it should stay intimite API
270
265
  if (attributes['sentry.skip_span_data_inference']) {
271
266
  return {
@@ -285,7 +280,7 @@ function parseSpanDescription(span) {
285
280
  // conventions export an attribute key for it.
286
281
  const httpMethod = attributes['http.request.method'] || attributes[SEMATTRS_HTTP_METHOD];
287
282
  if (httpMethod) {
288
- return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
283
+ return descriptionForHttpMethod({ attributes, name, kind }, httpMethod);
289
284
  }
290
285
 
291
286
  const dbSystem = attributes[SEMATTRS_DB_SYSTEM];
@@ -328,6 +323,19 @@ function parseSpanDescription(span) {
328
323
  return { op: undefined, description: name, source: 'custom' };
329
324
  }
330
325
 
326
+ /**
327
+ * Extract better op/description from an otel span.
328
+ *
329
+ * Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
330
+ */
331
+ function parseSpanDescription(span) {
332
+ const attributes = spanHasAttributes(span) ? span.attributes : {};
333
+ const name = spanHasName(span) ? span.name : '<unknown>';
334
+ const kind = getSpanKind(span);
335
+
336
+ return inferSpanData(name, attributes, kind);
337
+ }
338
+
331
339
  function descriptionForDbSystem({ attributes, name }) {
332
340
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
333
341
  const statement = attributes[SEMATTRS_DB_STATEMENT];
@@ -448,6 +456,55 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
448
456
  });
449
457
  }
450
458
 
459
+ /**
460
+ * Generate a TraceState for the given data.
461
+ */
462
+ function makeTraceState({
463
+ parentSpanId,
464
+ dsc,
465
+ sampled,
466
+ }
467
+
468
+ ) {
469
+ // We store the DSC as OTEL trace state on the span context
470
+ const dscString = dsc ? dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
471
+
472
+ // We _always_ set the parent span ID, even if it is empty
473
+ // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
474
+ // vs the trace state was not set at all (in which case we want to do fallback handling)
475
+ // If `''`, it should be considered "no parent"
476
+ const traceStateBase = new TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
477
+
478
+ const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
479
+
480
+ // We also specifically want to store if this is sampled to be not recording,
481
+ // or unsampled (=could be either sampled or not)
482
+ return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') : traceStateWithDsc;
483
+ }
484
+
485
+ /**
486
+ * Generates a SpanContext that represents a PropagationContext.
487
+ * This can be set on a `context` to make this a (virtual) active span.
488
+ */
489
+ function generateSpanContextForPropagationContext(propagationContext) {
490
+ // We store the DSC as OTEL trace state on the span context
491
+ const traceState = makeTraceState({
492
+ parentSpanId: propagationContext.parentSpanId,
493
+ dsc: propagationContext.dsc,
494
+ sampled: propagationContext.sampled,
495
+ });
496
+
497
+ const spanContext = {
498
+ traceId: propagationContext.traceId,
499
+ spanId: propagationContext.parentSpanId || '',
500
+ isRemote: true,
501
+ traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
502
+ traceState,
503
+ };
504
+
505
+ return spanContext;
506
+ }
507
+
451
508
  /**
452
509
  * Returns the currently active span.
453
510
  */
@@ -667,30 +724,6 @@ class SentryPropagator extends W3CBaggagePropagator {
667
724
  }
668
725
  }
669
726
 
670
- /** Exported for tests. */
671
- function makeTraceState({
672
- parentSpanId,
673
- dsc,
674
- sampled,
675
- }
676
-
677
- ) {
678
- // We store the DSC as OTEL trace state on the span context
679
- const dscString = dsc ? dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
680
-
681
- // We _always_ set the parent span ID, even if it is empty
682
- // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
683
- // vs the trace state was not set at all (in which case we want to do fallback handling)
684
- // If `''`, it should be considered "no parent"
685
- const traceStateBase = new TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
686
-
687
- const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
688
-
689
- // We also specifically want to store if this is sampled to be not recording,
690
- // or unsampled (=could be either sampled or not)
691
- return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') : traceStateWithDsc;
692
- }
693
-
694
727
  function getInjectionData(context)
695
728
 
696
729
  {
@@ -750,21 +783,7 @@ function getContextWithRemoteActiveSpan(
750
783
  ) {
751
784
  const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
752
785
 
753
- // We store the DSC as OTEL trace state on the span context
754
- const traceState = makeTraceState({
755
- parentSpanId: propagationContext.parentSpanId,
756
- dsc: propagationContext.dsc,
757
- sampled: propagationContext.sampled,
758
- });
759
-
760
- const spanContext = {
761
- traceId: propagationContext.traceId,
762
- spanId: propagationContext.parentSpanId || '',
763
- isRemote: true,
764
- traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
765
- traceState,
766
- };
767
-
786
+ const spanContext = generateSpanContextForPropagationContext(propagationContext);
768
787
  return trace.setSpanContext(ctx, spanContext);
769
788
  }
770
789
 
@@ -844,8 +863,6 @@ function startSpan(options, callback) {
844
863
  const spanOptions = getSpanOptions(options);
845
864
 
846
865
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
847
- _applySentryAttributesToSpan(span, options);
848
-
849
866
  return handleCallbackErrors(
850
867
  () => callback(span),
851
868
  () => {
@@ -889,8 +906,6 @@ function startSpanManual(
889
906
  const spanOptions = getSpanOptions(options);
890
907
 
891
908
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
892
- _applySentryAttributesToSpan(span, options);
893
-
894
909
  return handleCallbackErrors(
895
910
  () => callback(span, () => span.end()),
896
911
  () => {
@@ -930,8 +945,6 @@ function startInactiveSpan(options) {
930
945
 
931
946
  const span = tracer.startSpan(name, spanOptions, ctx);
932
947
 
933
- _applySentryAttributesToSpan(span, options);
934
-
935
948
  return span;
936
949
  });
937
950
  }
@@ -955,22 +968,19 @@ function getTracer() {
955
968
  return (client && client.tracer) || trace.getTracer('@sentry/opentelemetry', SDK_VERSION);
956
969
  }
957
970
 
958
- function _applySentryAttributesToSpan(span, options) {
959
- const { op } = options;
960
-
961
- if (op) {
962
- span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
963
- }
964
- }
965
-
966
971
  function getSpanOptions(options) {
967
- const { startTime, attributes, kind } = options;
972
+ const { startTime, attributes, kind, op } = options;
968
973
 
969
974
  // OTEL expects timestamps in ms, not seconds
970
975
  const fixedStartTime = typeof startTime === 'number' ? ensureTimestampInMilliseconds(startTime) : startTime;
971
976
 
972
977
  return {
973
- attributes,
978
+ attributes: op
979
+ ? {
980
+ [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
981
+ ...attributes,
982
+ }
983
+ : attributes,
974
984
  kind,
975
985
  startTime: fixedStartTime,
976
986
  };
@@ -1088,9 +1098,7 @@ function continueTrace(options, callback) {
1088
1098
  function getActiveSpanWrapper(parentSpan) {
1089
1099
  return parentSpan !== undefined
1090
1100
  ? (callback) => {
1091
- // We cast this, because the OTEL Span has a few more methods than our Span interface
1092
- // TODO: Add these missing methods to the Span interface
1093
- return withActiveSpan(parentSpan , callback);
1101
+ return withActiveSpan(parentSpan, callback);
1094
1102
  }
1095
1103
  : (callback) => callback();
1096
1104
  }
@@ -1940,12 +1948,28 @@ class SentrySampler {
1940
1948
 
1941
1949
  const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
1942
1950
 
1951
+ // We want to pass the inferred name & attributes to the sampler method
1952
+ const {
1953
+ description: inferredSpanName,
1954
+ data: inferredAttributes,
1955
+ op,
1956
+ } = inferSpanData(spanName, spanAttributes, spanKind);
1957
+
1958
+ const mergedAttributes = {
1959
+ ...inferredAttributes,
1960
+ ...spanAttributes,
1961
+ };
1962
+
1963
+ if (op) {
1964
+ mergedAttributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] = op;
1965
+ }
1966
+
1943
1967
  const mutableSamplingDecision = { decision: true };
1944
1968
  this._client.emit(
1945
1969
  'beforeSampling',
1946
1970
  {
1947
- spanAttributes: spanAttributes,
1948
- spanName: spanName,
1971
+ spanAttributes: mergedAttributes,
1972
+ spanName: inferredSpanName,
1949
1973
  parentSampled: parentSampled,
1950
1974
  parentContext: parentContext,
1951
1975
  },
@@ -1956,10 +1980,10 @@ class SentrySampler {
1956
1980
  }
1957
1981
 
1958
1982
  const [sampled, sampleRate] = sampleSpan(options, {
1959
- name: spanName,
1960
- attributes: spanAttributes,
1983
+ name: inferredSpanName,
1984
+ attributes: mergedAttributes,
1961
1985
  transactionContext: {
1962
- name: spanName,
1986
+ name: inferredSpanName,
1963
1987
  parentSampled,
1964
1988
  },
1965
1989
  parentSampled,
@@ -2076,5 +2100,5 @@ function addOpenTelemetryInstrumentation(...instrumentations) {
2076
2100
  });
2077
2101
  }
2078
2102
 
2079
- export { SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, getActiveSpan, getRequestSpanData, getScopesFromContext, getSpanKind, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass, wrapSamplingDecision };
2103
+ export { SentryPropagator, SentrySampler, SentrySpanProcessor, addOpenTelemetryInstrumentation, continueTrace, enhanceDscWithOpenTelemetryRootSpanName, generateSpanContextForPropagationContext, getActiveSpan, getPropagationContextFromSpan, getRequestSpanData, getScopesFromContext, getSpanKind, isSentryRequestSpan, openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace, spanHasAttributes, spanHasEvents, spanHasKind, spanHasName, spanHasParentId, spanHasStatus, startInactiveSpan, startSpan, startSpanManual, suppressTracing, withActiveSpan, wrapClientClass, wrapContextManagerClass, wrapSamplingDecision };
2080
2104
  //# sourceMappingURL=index.js.map