@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.
@@ -261,14 +261,9 @@ function isSentryRequestSpan(span) {
261
261
  }
262
262
 
263
263
  /**
264
- * Extract better op/description from an otel span.
265
- *
266
- * Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
264
+ * Infer the op & description for a set of name, attributes and kind of a span.
267
265
  */
268
- function parseSpanDescription(span) {
269
- const attributes = spanHasAttributes(span) ? span.attributes : {};
270
- const name = spanHasName(span) ? span.name : '<unknown>';
271
-
266
+ function inferSpanData(name, attributes, kind) {
272
267
  // This attribute is intentionally exported as a SEMATTR constant because it should stay intimite API
273
268
  if (attributes['sentry.skip_span_data_inference']) {
274
269
  return {
@@ -288,7 +283,7 @@ function parseSpanDescription(span) {
288
283
  // conventions export an attribute key for it.
289
284
  const httpMethod = attributes['http.request.method'] || attributes[semanticConventions.SEMATTRS_HTTP_METHOD];
290
285
  if (httpMethod) {
291
- return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
286
+ return descriptionForHttpMethod({ attributes, name, kind }, httpMethod);
292
287
  }
293
288
 
294
289
  const dbSystem = attributes[semanticConventions.SEMATTRS_DB_SYSTEM];
@@ -331,6 +326,19 @@ function parseSpanDescription(span) {
331
326
  return { op: undefined, description: name, source: 'custom' };
332
327
  }
333
328
 
329
+ /**
330
+ * Extract better op/description from an otel span.
331
+ *
332
+ * Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
333
+ */
334
+ function parseSpanDescription(span) {
335
+ const attributes = spanHasAttributes(span) ? span.attributes : {};
336
+ const name = spanHasName(span) ? span.name : '<unknown>';
337
+ const kind = getSpanKind(span);
338
+
339
+ return inferSpanData(name, attributes, kind);
340
+ }
341
+
334
342
  function descriptionForDbSystem({ attributes, name }) {
335
343
  // Use DB statement (Ex "SELECT * FROM table") if possible as description.
336
344
  const statement = attributes[semanticConventions.SEMATTRS_DB_STATEMENT];
@@ -451,6 +459,55 @@ function enhanceDscWithOpenTelemetryRootSpanName(client) {
451
459
  });
452
460
  }
453
461
 
462
+ /**
463
+ * Generate a TraceState for the given data.
464
+ */
465
+ function makeTraceState({
466
+ parentSpanId,
467
+ dsc,
468
+ sampled,
469
+ }
470
+
471
+ ) {
472
+ // We store the DSC as OTEL trace state on the span context
473
+ const dscString = dsc ? utils.dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
474
+
475
+ // We _always_ set the parent span ID, even if it is empty
476
+ // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
477
+ // vs the trace state was not set at all (in which case we want to do fallback handling)
478
+ // If `''`, it should be considered "no parent"
479
+ const traceStateBase = new core$1.TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
480
+
481
+ const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
482
+
483
+ // We also specifically want to store if this is sampled to be not recording,
484
+ // or unsampled (=could be either sampled or not)
485
+ return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') : traceStateWithDsc;
486
+ }
487
+
488
+ /**
489
+ * Generates a SpanContext that represents a PropagationContext.
490
+ * This can be set on a `context` to make this a (virtual) active span.
491
+ */
492
+ function generateSpanContextForPropagationContext(propagationContext) {
493
+ // We store the DSC as OTEL trace state on the span context
494
+ const traceState = makeTraceState({
495
+ parentSpanId: propagationContext.parentSpanId,
496
+ dsc: propagationContext.dsc,
497
+ sampled: propagationContext.sampled,
498
+ });
499
+
500
+ const spanContext = {
501
+ traceId: propagationContext.traceId,
502
+ spanId: propagationContext.parentSpanId || '',
503
+ isRemote: true,
504
+ traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
505
+ traceState,
506
+ };
507
+
508
+ return spanContext;
509
+ }
510
+
454
511
  /**
455
512
  * Returns the currently active span.
456
513
  */
@@ -670,30 +727,6 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
670
727
  }
671
728
  }
672
729
 
673
- /** Exported for tests. */
674
- function makeTraceState({
675
- parentSpanId,
676
- dsc,
677
- sampled,
678
- }
679
-
680
- ) {
681
- // We store the DSC as OTEL trace state on the span context
682
- const dscString = dsc ? utils.dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;
683
-
684
- // We _always_ set the parent span ID, even if it is empty
685
- // If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
686
- // vs the trace state was not set at all (in which case we want to do fallback handling)
687
- // If `''`, it should be considered "no parent"
688
- const traceStateBase = new core$1.TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
689
-
690
- const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
691
-
692
- // We also specifically want to store if this is sampled to be not recording,
693
- // or unsampled (=could be either sampled or not)
694
- return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1') : traceStateWithDsc;
695
- }
696
-
697
730
  function getInjectionData(context)
698
731
 
699
732
  {
@@ -753,21 +786,7 @@ function getContextWithRemoteActiveSpan(
753
786
  ) {
754
787
  const propagationContext = utils.propagationContextFromHeaders(sentryTrace, baggage);
755
788
 
756
- // We store the DSC as OTEL trace state on the span context
757
- const traceState = makeTraceState({
758
- parentSpanId: propagationContext.parentSpanId,
759
- dsc: propagationContext.dsc,
760
- sampled: propagationContext.sampled,
761
- });
762
-
763
- const spanContext = {
764
- traceId: propagationContext.traceId,
765
- spanId: propagationContext.parentSpanId || '',
766
- isRemote: true,
767
- traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
768
- traceState,
769
- };
770
-
789
+ const spanContext = generateSpanContextForPropagationContext(propagationContext);
771
790
  return api.trace.setSpanContext(ctx, spanContext);
772
791
  }
773
792
 
@@ -847,8 +866,6 @@ function startSpan(options, callback) {
847
866
  const spanOptions = getSpanOptions(options);
848
867
 
849
868
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
850
- _applySentryAttributesToSpan(span, options);
851
-
852
869
  return core.handleCallbackErrors(
853
870
  () => callback(span),
854
871
  () => {
@@ -892,8 +909,6 @@ function startSpanManual(
892
909
  const spanOptions = getSpanOptions(options);
893
910
 
894
911
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
895
- _applySentryAttributesToSpan(span, options);
896
-
897
912
  return core.handleCallbackErrors(
898
913
  () => callback(span, () => span.end()),
899
914
  () => {
@@ -933,8 +948,6 @@ function startInactiveSpan(options) {
933
948
 
934
949
  const span = tracer.startSpan(name, spanOptions, ctx);
935
950
 
936
- _applySentryAttributesToSpan(span, options);
937
-
938
951
  return span;
939
952
  });
940
953
  }
@@ -958,22 +971,19 @@ function getTracer() {
958
971
  return (client && client.tracer) || api.trace.getTracer('@sentry/opentelemetry', core.SDK_VERSION);
959
972
  }
960
973
 
961
- function _applySentryAttributesToSpan(span, options) {
962
- const { op } = options;
963
-
964
- if (op) {
965
- span.setAttribute(core.SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
966
- }
967
- }
968
-
969
974
  function getSpanOptions(options) {
970
- const { startTime, attributes, kind } = options;
975
+ const { startTime, attributes, kind, op } = options;
971
976
 
972
977
  // OTEL expects timestamps in ms, not seconds
973
978
  const fixedStartTime = typeof startTime === 'number' ? ensureTimestampInMilliseconds(startTime) : startTime;
974
979
 
975
980
  return {
976
- attributes,
981
+ attributes: op
982
+ ? {
983
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
984
+ ...attributes,
985
+ }
986
+ : attributes,
977
987
  kind,
978
988
  startTime: fixedStartTime,
979
989
  };
@@ -1091,9 +1101,7 @@ function continueTrace(options, callback) {
1091
1101
  function getActiveSpanWrapper(parentSpan) {
1092
1102
  return parentSpan !== undefined
1093
1103
  ? (callback) => {
1094
- // We cast this, because the OTEL Span has a few more methods than our Span interface
1095
- // TODO: Add these missing methods to the Span interface
1096
- return withActiveSpan(parentSpan , callback);
1104
+ return withActiveSpan(parentSpan, callback);
1097
1105
  }
1098
1106
  : (callback) => callback();
1099
1107
  }
@@ -1943,12 +1951,28 @@ class SentrySampler {
1943
1951
 
1944
1952
  const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
1945
1953
 
1954
+ // We want to pass the inferred name & attributes to the sampler method
1955
+ const {
1956
+ description: inferredSpanName,
1957
+ data: inferredAttributes,
1958
+ op,
1959
+ } = inferSpanData(spanName, spanAttributes, spanKind);
1960
+
1961
+ const mergedAttributes = {
1962
+ ...inferredAttributes,
1963
+ ...spanAttributes,
1964
+ };
1965
+
1966
+ if (op) {
1967
+ mergedAttributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] = op;
1968
+ }
1969
+
1946
1970
  const mutableSamplingDecision = { decision: true };
1947
1971
  this._client.emit(
1948
1972
  'beforeSampling',
1949
1973
  {
1950
- spanAttributes: spanAttributes,
1951
- spanName: spanName,
1974
+ spanAttributes: mergedAttributes,
1975
+ spanName: inferredSpanName,
1952
1976
  parentSampled: parentSampled,
1953
1977
  parentContext: parentContext,
1954
1978
  },
@@ -1959,10 +1983,10 @@ class SentrySampler {
1959
1983
  }
1960
1984
 
1961
1985
  const [sampled, sampleRate] = core.sampleSpan(options, {
1962
- name: spanName,
1963
- attributes: spanAttributes,
1986
+ name: inferredSpanName,
1987
+ attributes: mergedAttributes,
1964
1988
  transactionContext: {
1965
- name: spanName,
1989
+ name: inferredSpanName,
1966
1990
  parentSampled,
1967
1991
  },
1968
1992
  parentSampled,
@@ -2088,7 +2112,9 @@ exports.SentrySpanProcessor = SentrySpanProcessor;
2088
2112
  exports.addOpenTelemetryInstrumentation = addOpenTelemetryInstrumentation;
2089
2113
  exports.continueTrace = continueTrace;
2090
2114
  exports.enhanceDscWithOpenTelemetryRootSpanName = enhanceDscWithOpenTelemetryRootSpanName;
2115
+ exports.generateSpanContextForPropagationContext = generateSpanContextForPropagationContext;
2091
2116
  exports.getActiveSpan = getActiveSpan;
2117
+ exports.getPropagationContextFromSpan = getPropagationContextFromSpan;
2092
2118
  exports.getRequestSpanData = getRequestSpanData;
2093
2119
  exports.getScopesFromContext = getScopesFromContext;
2094
2120
  exports.getSpanKind = getSpanKind;