@sentry/opentelemetry 10.31.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.
@@ -1648,7 +1648,7 @@ const DEFAULT_TIMEOUT = 300; // 5 min
1648
1648
  /**
1649
1649
  * A Sentry-specific exporter that converts OpenTelemetry Spans to Sentry Spans & Transactions.
1650
1650
  */
1651
- class SentrySpanExporter {
1651
+ class SentrySpanExporter {
1652
1652
  /*
1653
1653
  * A quick explanation on the buckets: We do bucketing of finished spans for efficiency. This span exporter is
1654
1654
  * accumulating spans until a root span is encountered and then it flushes all the spans that are descendants of that
@@ -1979,6 +1979,9 @@ function createAndFinishSpanForOtelSpan(node, spans, sentSpans) {
1979
1979
  });
1980
1980
  }
1981
1981
 
1982
+ /**
1983
+ * Get span data from the OTEL span
1984
+ */
1982
1985
  function getSpanData(span)
1983
1986
 
1984
1987
  {
@@ -2045,6 +2048,63 @@ function getData(span) {
2045
2048
  return data;
2046
2049
  }
2047
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 core.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
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: origin,
2073
+ [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
2074
+ ...data,
2075
+ };
2076
+ core.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
+ core.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
+
2048
2108
  function onSpanStart(span, parentContext) {
2049
2109
  // This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
2050
2110
  const parentSpan = api.trace.getSpan(parentContext);
@@ -2081,13 +2141,6 @@ function onSpanStart(span, parentContext) {
2081
2141
  client?.emit('spanStart', span);
2082
2142
  }
2083
2143
 
2084
- function onSpanEnd(span) {
2085
- core.logSpanEnd(span);
2086
-
2087
- const client = core.getClient();
2088
- client?.emit('spanEnd', span);
2089
- }
2090
-
2091
2144
  /**
2092
2145
  * Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
2093
2146
  * the Sentry SDK.
@@ -2096,7 +2149,12 @@ class SentrySpanProcessor {
2096
2149
 
2097
2150
  constructor(options) {
2098
2151
  setIsSetup('SentrySpanProcessor');
2099
- this._exporter = new SentrySpanExporter(options);
2152
+ this._client = options?.client ?? core.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
+ }
2100
2158
  }
2101
2159
 
2102
2160
  /**
@@ -2122,7 +2180,9 @@ class SentrySpanProcessor {
2122
2180
 
2123
2181
  /** @inheritDoc */
2124
2182
  onEnd(span) {
2125
- onSpanEnd(span);
2183
+ core.logSpanEnd(span);
2184
+
2185
+ this._client?.emit('spanEnd', span);
2126
2186
 
2127
2187
  this._exporter.export(span);
2128
2188
  }