@sentry/opentelemetry 8.18.0 → 8.19.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];
@@ -847,8 +855,6 @@ function startSpan(options, callback) {
847
855
  const spanOptions = getSpanOptions(options);
848
856
 
849
857
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
850
- _applySentryAttributesToSpan(span, options);
851
-
852
858
  return core.handleCallbackErrors(
853
859
  () => callback(span),
854
860
  () => {
@@ -892,8 +898,6 @@ function startSpanManual(
892
898
  const spanOptions = getSpanOptions(options);
893
899
 
894
900
  return tracer.startActiveSpan(name, spanOptions, ctx, span => {
895
- _applySentryAttributesToSpan(span, options);
896
-
897
901
  return core.handleCallbackErrors(
898
902
  () => callback(span, () => span.end()),
899
903
  () => {
@@ -933,8 +937,6 @@ function startInactiveSpan(options) {
933
937
 
934
938
  const span = tracer.startSpan(name, spanOptions, ctx);
935
939
 
936
- _applySentryAttributesToSpan(span, options);
937
-
938
940
  return span;
939
941
  });
940
942
  }
@@ -958,22 +960,19 @@ function getTracer() {
958
960
  return (client && client.tracer) || api.trace.getTracer('@sentry/opentelemetry', core.SDK_VERSION);
959
961
  }
960
962
 
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
963
  function getSpanOptions(options) {
970
- const { startTime, attributes, kind } = options;
964
+ const { startTime, attributes, kind, op } = options;
971
965
 
972
966
  // OTEL expects timestamps in ms, not seconds
973
967
  const fixedStartTime = typeof startTime === 'number' ? ensureTimestampInMilliseconds(startTime) : startTime;
974
968
 
975
969
  return {
976
- attributes,
970
+ attributes: op
971
+ ? {
972
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
973
+ ...attributes,
974
+ }
975
+ : attributes,
977
976
  kind,
978
977
  startTime: fixedStartTime,
979
978
  };
@@ -1091,9 +1090,7 @@ function continueTrace(options, callback) {
1091
1090
  function getActiveSpanWrapper(parentSpan) {
1092
1091
  return parentSpan !== undefined
1093
1092
  ? (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);
1093
+ return withActiveSpan(parentSpan, callback);
1097
1094
  }
1098
1095
  : (callback) => callback();
1099
1096
  }
@@ -1943,12 +1940,28 @@ class SentrySampler {
1943
1940
 
1944
1941
  const parentSampled = parentSpan ? getParentSampled(parentSpan, traceId, spanName) : undefined;
1945
1942
 
1943
+ // We want to pass the inferred name & attributes to the sampler method
1944
+ const {
1945
+ description: inferredSpanName,
1946
+ data: inferredAttributes,
1947
+ op,
1948
+ } = inferSpanData(spanName, spanAttributes, spanKind);
1949
+
1950
+ const mergedAttributes = {
1951
+ ...inferredAttributes,
1952
+ ...spanAttributes,
1953
+ };
1954
+
1955
+ if (op) {
1956
+ mergedAttributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] = op;
1957
+ }
1958
+
1946
1959
  const mutableSamplingDecision = { decision: true };
1947
1960
  this._client.emit(
1948
1961
  'beforeSampling',
1949
1962
  {
1950
- spanAttributes: spanAttributes,
1951
- spanName: spanName,
1963
+ spanAttributes: mergedAttributes,
1964
+ spanName: inferredSpanName,
1952
1965
  parentSampled: parentSampled,
1953
1966
  parentContext: parentContext,
1954
1967
  },
@@ -1959,10 +1972,10 @@ class SentrySampler {
1959
1972
  }
1960
1973
 
1961
1974
  const [sampled, sampleRate] = core.sampleSpan(options, {
1962
- name: spanName,
1963
- attributes: spanAttributes,
1975
+ name: inferredSpanName,
1976
+ attributes: mergedAttributes,
1964
1977
  transactionContext: {
1965
- name: spanName,
1978
+ name: inferredSpanName,
1966
1979
  parentSampled,
1967
1980
  },
1968
1981
  parentSampled,