@sentry/opentelemetry 10.30.0 → 10.32.0-alpha.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,5 +1,5 @@
1
1
  import { ATTR_URL_FULL, SEMATTRS_HTTP_URL, ATTR_HTTP_REQUEST_METHOD, SEMATTRS_HTTP_METHOD, SEMATTRS_DB_SYSTEM, SEMATTRS_RPC_SERVICE, SEMATTRS_MESSAGING_SYSTEM, SEMATTRS_FAAS_TRIGGER, SEMATTRS_DB_STATEMENT, SEMATTRS_HTTP_TARGET, ATTR_HTTP_ROUTE, ATTR_HTTP_RESPONSE_STATUS_CODE, SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
2
- import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, stripUrlQueryAndFragment, spanToJSON, hasSpansEnabled, dynamicSamplingContextToSentryBaggageHeader, LRUMap, debug, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, stringMatchesSomePattern, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, shouldContinueTrace, spanToTraceContext, getTraceContextFromScope, handleCallbackErrors, getRootSpan, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, debounce, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, logSpanEnd, parseSampleRate, sampleSpan } from '@sentry/core';
2
+ import { parseUrl, getSanitizedUrlString, SDK_VERSION, addNonEnumerableProperty, isSentryRequestUrl, getClient, baggageHeaderToDynamicSamplingContext, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, stripUrlQueryAndFragment, spanToJSON, hasSpansEnabled, dynamicSamplingContextToSentryBaggageHeader, LRUMap, debug, parseBaggageHeader, SENTRY_BAGGAGE_KEY_PREFIX, generateSentryTraceHeader, generateTraceparentHeader, stringMatchesSomePattern, getDynamicSamplingContextFromSpan, getCurrentScope, getDynamicSamplingContextFromScope, getIsolationScope, propagationContextFromHeaders, shouldContinueTrace, spanToTraceContext, getTraceContextFromScope, handleCallbackErrors, getRootSpan, getCapturedScopesOnSpan, setAsyncContextStrategy, getDefaultIsolationScope, getDefaultCurrentScope, SPAN_STATUS_OK, SPAN_STATUS_ERROR, getSpanStatusFromHttpCode, debounce, timedEventsToMeasurements, captureEvent, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, convertSpanLinksForEnvelope, getStatusMessage, spanTimeInputToSeconds, SpanBuffer, safeSetSpanJSONAttributes, captureSpan, logSpanEnd, addChildSpanToSpan, setCapturedScopesOnSpan, logSpanStart, parseSampleRate, sampleSpan } from '@sentry/core';
3
3
  export { getClient, getDynamicSamplingContextFromSpan } from '@sentry/core';
4
4
  import * as api from '@opentelemetry/api';
5
5
  import { trace, SpanKind, createContextKey, TraceFlags, propagation, INVALID_TRACEID, context, SpanStatusCode, ROOT_CONTEXT, isSpanContextValid } from '@opentelemetry/api';
@@ -696,7 +696,7 @@ class SentryPropagator extends W3CBaggagePropagator {
696
696
  const activeSpan = trace.getSpan(context);
697
697
  const url = activeSpan && getCurrentURL(activeSpan);
698
698
 
699
- const tracePropagationTargets = getClient()?.getOptions()?.tracePropagationTargets;
699
+ const { tracePropagationTargets, propagateTraceparent } = getClient()?.getOptions() || {};
700
700
  if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
701
701
  DEBUG_BUILD &&
702
702
  debug.log('[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:', url);
@@ -730,6 +730,10 @@ class SentryPropagator extends W3CBaggagePropagator {
730
730
  // We also want to avoid setting the default OTEL trace ID, if we get that for whatever reason
731
731
  if (traceId && traceId !== INVALID_TRACEID) {
732
732
  setter.set(carrier, SENTRY_TRACE_HEADER, generateSentryTraceHeader(traceId, spanId, sampled));
733
+
734
+ if (propagateTraceparent) {
735
+ setter.set(carrier, 'traceparent', generateTraceparentHeader(traceId, spanId, sampled));
736
+ }
733
737
  }
734
738
 
735
739
  super.inject(propagation.setBaggage(context, baggage), carrier, setter);
@@ -757,7 +761,7 @@ class SentryPropagator extends W3CBaggagePropagator {
757
761
  * @inheritDoc
758
762
  */
759
763
  fields() {
760
- return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER];
764
+ return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER, 'traceparent'];
761
765
  }
762
766
  }
763
767
 
@@ -1276,6 +1280,7 @@ function getTraceData({
1276
1280
  span,
1277
1281
  scope,
1278
1282
  client,
1283
+ propagateTraceparent,
1279
1284
  } = {}) {
1280
1285
  let ctx = (scope && getContextFromScope(scope)) ?? api.context.active();
1281
1286
 
@@ -1287,10 +1292,16 @@ function getTraceData({
1287
1292
 
1288
1293
  const { traceId, spanId, sampled, dynamicSamplingContext } = getInjectionData(ctx, { scope, client });
1289
1294
 
1290
- return {
1295
+ const traceData = {
1291
1296
  'sentry-trace': generateSentryTraceHeader(traceId, spanId, sampled),
1292
1297
  baggage: dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext),
1293
1298
  };
1299
+
1300
+ if (propagateTraceparent) {
1301
+ traceData.traceparent = generateTraceparentHeader(traceId, spanId, sampled);
1302
+ }
1303
+
1304
+ return traceData;
1294
1305
  }
1295
1306
 
1296
1307
  /**
@@ -1637,7 +1648,7 @@ const DEFAULT_TIMEOUT = 300; // 5 min
1637
1648
  /**
1638
1649
  * A Sentry-specific exporter that converts OpenTelemetry Spans to Sentry Spans & Transactions.
1639
1650
  */
1640
- class SentrySpanExporter {
1651
+ class SentrySpanExporter {
1641
1652
  /*
1642
1653
  * A quick explanation on the buckets: We do bucketing of finished spans for efficiency. This span exporter is
1643
1654
  * accumulating spans until a root span is encountered and then it flushes all the spans that are descendants of that
@@ -1968,6 +1979,9 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1968
1979
  });
1969
1980
  }
1970
1981
 
1982
+ /**
1983
+ * Get span data from the OTEL span
1984
+ */
1971
1985
  function getSpanData(span)
1972
1986
 
1973
1987
  {
@@ -2034,6 +2048,63 @@ function getData(span) {
2034
2048
  return data;
2035
2049
  }
2036
2050
 
2051
+ /**
2052
+ * A Sentry-specific exporter that buffers span JSON objects and streams them to Sentry
2053
+ * in Span v2 envelopes. This exporter works with pre-serialized span JSON rather than
2054
+ * OTel span instances to avoid mutating already-ended spans.
2055
+ */
2056
+ class StreamingSpanExporter {
2057
+
2058
+ constructor(client, options) {
2059
+ this._client = client;
2060
+ this._buffer = new SpanBuffer(client, {
2061
+ maxSpanLimit: options?.maxSpanLimit,
2062
+ flushInterval: options?.flushInterval,
2063
+ });
2064
+
2065
+ // OTel-specific: add span attributes from ReadableSpan
2066
+ this._client.on('processSpan', (spanJSON, hint) => {
2067
+ const { readOnlySpan } = hint;
2068
+ // TODO: This can be simplified by using spanJSON to get the data instead of the readOnlySpan
2069
+ // for now this is the easiest backwards-compatible way to get the data.
2070
+ const { op, description, data, origin = 'manual' } = getSpanData(readOnlySpan );
2071
+ const allData = {
2072
+ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: origin,
2073
+ [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
2074
+ ...data,
2075
+ };
2076
+ safeSetSpanJSONAttributes(spanJSON, allData);
2077
+ spanJSON.name = description;
2078
+ });
2079
+
2080
+ this._client.on('enqueueSpan', spanJSON => {
2081
+ this._buffer.addSpan(spanJSON);
2082
+ });
2083
+ }
2084
+
2085
+ /**
2086
+ * Enqueue a span JSON into the buffer
2087
+ */
2088
+ export(span) {
2089
+ captureSpan(span, this._client);
2090
+ }
2091
+
2092
+ /**
2093
+ * Try to flush any pending spans immediately.
2094
+ */
2095
+ flush() {
2096
+ this._buffer.flush();
2097
+ }
2098
+
2099
+ /**
2100
+ * Clear the exporter.
2101
+ * This is called when the span processor is shut down.
2102
+ */
2103
+ clear() {
2104
+ // No-op for streaming exporter - spans are flushed immediately on interval
2105
+ }
2106
+ }
2107
+
2037
2108
  function onSpanStart(span, parentContext) {
2038
2109
  // This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
2039
2110
  const parentSpan = trace.getSpan(parentContext);
@@ -2070,13 +2141,6 @@ function onSpanStart(span, parentContext) {
2070
2141
  client?.emit('spanStart', span);
2071
2142
  }
2072
2143
 
2073
- function onSpanEnd(span) {
2074
- logSpanEnd(span);
2075
-
2076
- const client = getClient();
2077
- client?.emit('spanEnd', span);
2078
- }
2079
-
2080
2144
  /**
2081
2145
  * Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
2082
2146
  * the Sentry SDK.
@@ -2085,7 +2149,12 @@ class SentrySpanProcessor {
2085
2149
 
2086
2150
  constructor(options) {
2087
2151
  setIsSetup('SentrySpanProcessor');
2088
- this._exporter = new SentrySpanExporter(options);
2152
+ this._client = options?.client ?? getClient();
2153
+ if (this._client?.getOptions().traceLifecycle === 'stream') {
2154
+ this._exporter = new StreamingSpanExporter(this._client, { flushInterval: options?.timeout });
2155
+ } else {
2156
+ this._exporter = new SentrySpanExporter(options);
2157
+ }
2089
2158
  }
2090
2159
 
2091
2160
  /**
@@ -2111,7 +2180,9 @@ class SentrySpanProcessor {
2111
2180
 
2112
2181
  /** @inheritDoc */
2113
2182
  onEnd(span) {
2114
- onSpanEnd(span);
2183
+ logSpanEnd(span);
2184
+
2185
+ this._client?.emit('spanEnd', span);
2115
2186
 
2116
2187
  this._exporter.export(span);
2117
2188
  }