@sentry/opentelemetry 10.69.0 → 11.0.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.
- package/README.md +2 -15
- package/build/cjs/index.js +844 -95
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +837 -58
- package/build/esm/index.js.map +1 -1
- package/build/esm/package.json +1 -1
- package/build/types/applyOtelSpanData.d.ts +1 -4
- package/build/types/applyOtelSpanData.d.ts.map +1 -1
- package/build/types/asyncContextStrategy.d.ts +2 -4
- package/build/types/asyncContextStrategy.d.ts.map +1 -1
- package/build/types/asyncLocalStorageContextManager.d.ts +16 -4
- package/build/types/asyncLocalStorageContextManager.d.ts.map +1 -1
- package/build/types/constants.d.ts +0 -10
- package/build/types/constants.d.ts.map +1 -1
- package/build/types/index.d.ts +8 -4
- package/build/types/index.d.ts.map +1 -1
- package/build/types/propagator.d.ts +12 -33
- package/build/types/propagator.d.ts.map +1 -1
- package/build/types/trace.d.ts +1 -27
- package/build/types/trace.d.ts.map +1 -1
- package/build/types/tracer.d.ts.map +1 -1
- package/build/types/tracerProvider.d.ts +0 -9
- package/build/types/tracerProvider.d.ts.map +1 -1
- package/build/types/types.d.ts +2 -21
- package/build/types/types.d.ts.map +1 -1
- package/build/types/utils/TraceState.d.ts.map +1 -1
- package/build/types/utils/backfillStreamedSpanData.d.ts +1 -3
- package/build/types/utils/backfillStreamedSpanData.d.ts.map +1 -1
- package/build/types/utils/mapStatus.d.ts +0 -6
- package/build/types/utils/mapStatus.d.ts.map +1 -1
- package/build/types/utils/parseSpanDescription.d.ts +6 -30
- package/build/types/utils/parseSpanDescription.d.ts.map +1 -1
- package/build/types/utils/reconcileDscSampled.d.ts +14 -0
- package/build/types/utils/reconcileDscSampled.d.ts.map +1 -0
- package/build/types/utils/suppressTracing.d.ts +5 -4
- package/build/types/utils/suppressTracing.d.ts.map +1 -1
- package/package.json +10 -37
- package/build/cjs/asyncContextStrategy-volGaYqZ.js +0 -1892
- package/build/cjs/asyncContextStrategy-volGaYqZ.js.map +0 -1
- package/build/cjs/index.browser.js +0 -53
- package/build/cjs/index.browser.js.map +0 -1
- package/build/esm/asyncContextStrategy-jD8YWNDU.js +0 -1857
- package/build/esm/asyncContextStrategy-jD8YWNDU.js.map +0 -1
- package/build/esm/index.browser.js +0 -14
- package/build/esm/index.browser.js.map +0 -1
- package/build/types/contextManager.d.ts +0 -22
- package/build/types/contextManager.d.ts.map +0 -1
- package/build/types/custom/client.d.ts +0 -12
- package/build/types/custom/client.d.ts.map +0 -1
- package/build/types/exports.d.ts +0 -27
- package/build/types/exports.d.ts.map +0 -1
- package/build/types/index.browser.d.ts +0 -10
- package/build/types/index.browser.d.ts.map +0 -1
- package/build/types/nodeAsyncContextStrategy.d.ts +0 -4
- package/build/types/nodeAsyncContextStrategy.d.ts.map +0 -1
- package/build/types/resource.d.ts +0 -26
- package/build/types/resource.d.ts.map +0 -1
- package/build/types/sampler.d.ts +0 -32
- package/build/types/sampler.d.ts.map +0 -1
- package/build/types/semanticAttributes.d.ts +0 -4
- package/build/types/semanticAttributes.d.ts.map +0 -1
- package/build/types/spanExporter.d.ts +0 -50
- package/build/types/spanExporter.d.ts.map +0 -1
- package/build/types/spanProcessor.d.ts +0 -31
- package/build/types/spanProcessor.d.ts.map +0 -1
- package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +0 -7
- package/build/types/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts.map +0 -1
- package/build/types/utils/getParentSpanId.d.ts +0 -8
- package/build/types/utils/getParentSpanId.d.ts.map +0 -1
- package/build/types/utils/getRequestSpanData.d.ts +0 -8
- package/build/types/utils/getRequestSpanData.d.ts.map +0 -1
- package/build/types/utils/getSpanKind.d.ts +0 -10
- package/build/types/utils/getSpanKind.d.ts.map +0 -1
- package/build/types/utils/getTraceData.d.ts +0 -12
- package/build/types/utils/getTraceData.d.ts.map +0 -1
- package/build/types/utils/groupSpansWithParents.d.ts +0 -17
- package/build/types/utils/groupSpansWithParents.d.ts.map +0 -1
- package/build/types/utils/isSentryRequest.d.ts +0 -8
- package/build/types/utils/isSentryRequest.d.ts.map +0 -1
- package/build/types/utils/setupCheck.d.ts +0 -9
- package/build/types/utils/setupCheck.d.ts.map +0 -1
- package/build/types/utils/spanTypes.d.ts +0 -52
- package/build/types/utils/spanTypes.d.ts.map +0 -1
- package/build/types-ts3.8/applyOtelSpanData.d.ts +0 -18
- package/build/types-ts3.8/asyncContextStrategy.d.ts +0 -9
- package/build/types-ts3.8/asyncLocalStorageContextManager.d.ts +0 -31
- package/build/types-ts3.8/constants.d.ts +0 -24
- package/build/types-ts3.8/contextManager.d.ts +0 -22
- package/build/types-ts3.8/custom/client.d.ts +0 -12
- package/build/types-ts3.8/debug-build.d.ts +0 -7
- package/build/types-ts3.8/exports.d.ts +0 -27
- package/build/types-ts3.8/index.browser.d.ts +0 -10
- package/build/types-ts3.8/index.d.ts +0 -5
- package/build/types-ts3.8/nodeAsyncContextStrategy.d.ts +0 -4
- package/build/types-ts3.8/propagator.d.ts +0 -43
- package/build/types-ts3.8/resource.d.ts +0 -29
- package/build/types-ts3.8/sampler.d.ts +0 -32
- package/build/types-ts3.8/semanticAttributes.d.ts +0 -4
- package/build/types-ts3.8/setupEventContextTrace.d.ts +0 -4
- package/build/types-ts3.8/spanExporter.d.ts +0 -50
- package/build/types-ts3.8/spanProcessor.d.ts +0 -31
- package/build/types-ts3.8/trace.d.ts +0 -75
- package/build/types-ts3.8/tracer.d.ts +0 -13
- package/build/types-ts3.8/tracerProvider.d.ts +0 -23
- package/build/types-ts3.8/types.d.ts +0 -29
- package/build/types-ts3.8/utils/TraceState.d.ts +0 -21
- package/build/types-ts3.8/utils/backfillStreamedSpanData.d.ts +0 -16
- package/build/types-ts3.8/utils/buildContextWithSentryScopes.d.ts +0 -12
- package/build/types-ts3.8/utils/contextData.d.ts +0 -30
- package/build/types-ts3.8/utils/enhanceDscWithOpenTelemetryRootSpanName.d.ts +0 -7
- package/build/types-ts3.8/utils/getActiveSpan.d.ts +0 -6
- package/build/types-ts3.8/utils/getParentSpanId.d.ts +0 -8
- package/build/types-ts3.8/utils/getRequestSpanData.d.ts +0 -8
- package/build/types-ts3.8/utils/getSamplingDecision.d.ts +0 -27
- package/build/types-ts3.8/utils/getSpanKind.d.ts +0 -10
- package/build/types-ts3.8/utils/getTraceData.d.ts +0 -12
- package/build/types-ts3.8/utils/groupSpansWithParents.d.ts +0 -17
- package/build/types-ts3.8/utils/isSentryRequest.d.ts +0 -8
- package/build/types-ts3.8/utils/makeTraceState.d.ts +0 -10
- package/build/types-ts3.8/utils/mapStatus.d.ts +0 -9
- package/build/types-ts3.8/utils/parseSpanDescription.d.ts +0 -56
- package/build/types-ts3.8/utils/setupCheck.d.ts +0 -9
- package/build/types-ts3.8/utils/spanTypes.d.ts +0 -52
- package/build/types-ts3.8/utils/suppressTracing.d.ts +0 -5
package/build/esm/index.js
CHANGED
|
@@ -1,19 +1,725 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { S as SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION, a as SentryPropagator, b as SentrySampler, c as SentrySpanProcessor, d as SentryTracerProvider, e as applyOtelSpanData, f as backfillStreamedSpanDataFromOtel, g as continueTrace, h as enhanceDscWithOpenTelemetryRootSpanName, i as getActiveSpan, j as getRequestSpanData, k as getScopesFromContext, l as getSentryResource, m as getSpanKind, n as getTraceContextForScope, o as isSentryRequestSpan, p as openTelemetrySetupCheck, r as setupEventContextTrace, t as spanHasAttributes, u as spanHasEvents, v as spanHasKind, w as spanHasName, x as spanHasParentId, y as spanHasStatus, z as startInactiveSpan, A as startSpan, B as startSpanManual, C as suppressTracing, D as withActiveSpan, E as wrapClientClass, F as wrapContextManagerClass, G as wrapSamplingDecision } from './asyncContextStrategy-jD8YWNDU.js';
|
|
3
|
-
import { getAsyncContextStrategy, getMainCarrier, spanIsIgnored, getRootSpan } from '@sentry/core';
|
|
4
|
-
export { getClient, getDynamicSamplingContextFromSpan, shouldPropagateTraceForUrl, withStreamedSpan } from '@sentry/core';
|
|
1
|
+
import { derefWeakRef, addNonEnumerableProperty, makeWeakRef, spanToTraceContext, getRootSpan, getDynamicSamplingContextFromSpan, dynamicSamplingContextToSentryBaggageHeader, consoleSandbox, isTracingSuppressed as isTracingSuppressed$1, getClient, getTraceData, propagationContextFromHeaders, baggageHeaderToDynamicSamplingContext, shouldContinueTrace, getCurrentScope, _INTERNAL_safeMathRandom, generateTraceId, getIsolationScope, getSpanStatusFromHttpCode, SPAN_STATUS_ERROR, SEMANTIC_ATTRIBUTE_SENTRY_OP, filterCollectedUrl, filterCollectedUrlQuery, getUrlQuery, getUrlFragment, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, parseUrl, getSanitizedUrlString, stripUrlQueryAndFragment, spanToJSON, spanShouldInferOtelSource, spanSourceWasExplicitlySet, hasSpanStreamingEnabled, SPAN_STATUS_OK, isStatusErrorMessageValid, safeSetSpanJSONAttributes, markSpanAsTracerProviderSpan, markSpanForOtelSourceInference, getCapturedScopesOnSpan, spanIsIgnored, _INTERNAL_setSpanForScope, spanKindToName, startNewTrace, _INTERNAL_startInactiveSpan, withScope, SentryNonRecordingSpan, addChildSpanToSpan, setCapturedScopesOnSpan, hasSpansEnabled, SDK_VERSION, handleCallbackErrors, getAsyncContextStrategy, getMainCarrier, setAsyncContextStrategy, isContinuingTrace, getDefaultIsolationScope, getDefaultCurrentScope } from '@sentry/core';
|
|
5
2
|
import * as api from '@opentelemetry/api';
|
|
6
|
-
import { ROOT_CONTEXT } from '@opentelemetry/api';
|
|
3
|
+
import { createContextKey, trace, context, TraceFlags, isSpanContextValid, SpanStatusCode, ROOT_CONTEXT } from '@opentelemetry/api';
|
|
4
|
+
import { HTTP_RESPONSE_STATUS_CODE, HTTP_STATUS_CODE, RPC_GRPC_STATUS_CODE, HTTP_REQUEST_METHOD, HTTP_METHOD, DB_SYSTEM_NAME, DB_SYSTEM, RPC_SERVICE, MESSAGING_SYSTEM, MESSAGING_OPERATION_TYPE, FAAS_TRIGGER, SENTRY_KIND, URL_FULL, URL_QUERY, URL_FRAGMENT, HTTP_TARGET, HTTP_ROUTE, SENTRY_ORIGIN } from '@sentry/conventions/attributes';
|
|
5
|
+
import { WEB_SERVER_HTTP_CLIENT_SPAN_OP, WEB_SERVER_HTTP_SERVER_SPAN_OP, WEB_SERVER_HTTP_SPAN_OP, MESSAGING_QUEUE_SPAN_OP, MESSAGING_QUEUE_PROCESS_SPAN_OP, MESSAGING_QUEUE_RECEIVE_SPAN_OP, MESSAGING_QUEUE_PUBLISH_SPAN_OP, GENERAL_FUNCTION_SPAN_OP } from '@sentry/conventions/op';
|
|
7
6
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
8
|
-
|
|
7
|
+
|
|
8
|
+
const SENTRY_TRACE_STATE_DSC = "sentry.dsc";
|
|
9
|
+
const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = "sentry.sampled_not_recording";
|
|
10
|
+
const SENTRY_TRACE_STATE_CHILD_IGNORED = "sentry.ignored";
|
|
11
|
+
const SENTRY_SCOPES_CONTEXT_KEY = createContextKey("sentry_scopes");
|
|
12
|
+
const SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY = createContextKey("sentry_fork_isolation_scope");
|
|
13
|
+
const SENTRY_FORK_SET_SCOPE_CONTEXT_KEY = createContextKey("sentry_fork_set_scope");
|
|
14
|
+
const SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY = createContextKey("sentry_fork_set_isolation_scope");
|
|
15
|
+
|
|
16
|
+
const SCOPE_CONTEXT_FIELD = "_scopeContext";
|
|
17
|
+
function getScopesFromContext(context) {
|
|
18
|
+
return context.getValue(SENTRY_SCOPES_CONTEXT_KEY);
|
|
19
|
+
}
|
|
20
|
+
function setScopesOnContext(context, scopes) {
|
|
21
|
+
return context.setValue(SENTRY_SCOPES_CONTEXT_KEY, scopes);
|
|
22
|
+
}
|
|
23
|
+
function setContextOnScope(scope, context) {
|
|
24
|
+
addNonEnumerableProperty(scope, SCOPE_CONTEXT_FIELD, makeWeakRef(context));
|
|
25
|
+
}
|
|
26
|
+
function getContextFromScope(scope) {
|
|
27
|
+
return derefWeakRef(scope[SCOPE_CONTEXT_FIELD]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getActiveSpan() {
|
|
31
|
+
return trace.getActiveSpan();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function setupEventContextTrace(client) {
|
|
35
|
+
client.on("preprocessEvent", (event) => {
|
|
36
|
+
const span = getActiveSpan();
|
|
37
|
+
if (!span || event.type === "transaction") {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
event.contexts = {
|
|
41
|
+
trace: spanToTraceContext(span),
|
|
42
|
+
...event.contexts
|
|
43
|
+
};
|
|
44
|
+
const rootSpan = getRootSpan(span);
|
|
45
|
+
event.sdkProcessingMetadata = {
|
|
46
|
+
dynamicSamplingContext: getDynamicSamplingContextFromSpan(rootSpan),
|
|
47
|
+
...event.sdkProcessingMetadata
|
|
48
|
+
};
|
|
49
|
+
return event;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
class TraceState {
|
|
54
|
+
constructor() {
|
|
55
|
+
this._internalState = /* @__PURE__ */ new Map();
|
|
56
|
+
}
|
|
57
|
+
/** @inheritDoc */
|
|
58
|
+
set(key, value) {
|
|
59
|
+
const next = this._clone();
|
|
60
|
+
if (next._internalState.has(key)) {
|
|
61
|
+
next._internalState.delete(key);
|
|
62
|
+
}
|
|
63
|
+
next._internalState.set(key, value);
|
|
64
|
+
return next;
|
|
65
|
+
}
|
|
66
|
+
/** @inheritDoc */
|
|
67
|
+
unset(key) {
|
|
68
|
+
const next = this._clone();
|
|
69
|
+
next._internalState.delete(key);
|
|
70
|
+
return next;
|
|
71
|
+
}
|
|
72
|
+
/** @inheritDoc */
|
|
73
|
+
get(key) {
|
|
74
|
+
return this._internalState.get(key);
|
|
75
|
+
}
|
|
76
|
+
/** @inheritDoc */
|
|
77
|
+
serialize() {
|
|
78
|
+
return Array.from(this._internalState.keys()).reverse().map((key) => `${key}=${this._internalState.get(key)}`).join(",");
|
|
79
|
+
}
|
|
80
|
+
_clone() {
|
|
81
|
+
const next = new TraceState();
|
|
82
|
+
next._internalState = new Map(this._internalState);
|
|
83
|
+
return next;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function makeTraceState({
|
|
88
|
+
dsc,
|
|
89
|
+
sampled
|
|
90
|
+
}) {
|
|
91
|
+
const dscString = dsc ? dynamicSamplingContextToSentryBaggageHeader(dsc) : void 0;
|
|
92
|
+
const traceStateBase = new TraceState();
|
|
93
|
+
const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;
|
|
94
|
+
return sampled === false ? traceStateWithDsc.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, "1") : traceStateWithDsc;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const SENTRY_TRACE_HEADER = "sentry-trace";
|
|
98
|
+
const SENTRY_BAGGAGE_HEADER = "baggage";
|
|
99
|
+
const W3C_TRACEPARENT_HEADER = "traceparent";
|
|
100
|
+
class SentryPropagator {
|
|
101
|
+
/** @inheritDoc */
|
|
102
|
+
inject(ctx, carrier, setter) {
|
|
103
|
+
if (ctx !== context.active()) {
|
|
104
|
+
consoleSandbox(() => {
|
|
105
|
+
console.warn(
|
|
106
|
+
"SentryPropagator: Injecting trace data of a different context than the active one is not supported. Skipping injection."
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (isTracingSuppressed$1()) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const { propagateTraceparent } = getClient()?.getOptions() ?? {};
|
|
115
|
+
const { "sentry-trace": sentryTrace, baggage, traceparent } = getTraceData({ propagateTraceparent });
|
|
116
|
+
if (sentryTrace) {
|
|
117
|
+
setter.set(carrier, SENTRY_TRACE_HEADER, sentryTrace);
|
|
118
|
+
}
|
|
119
|
+
if (baggage) {
|
|
120
|
+
setter.set(carrier, SENTRY_BAGGAGE_HEADER, baggage);
|
|
121
|
+
}
|
|
122
|
+
if (traceparent) {
|
|
123
|
+
setter.set(carrier, W3C_TRACEPARENT_HEADER, traceparent);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** @inheritDoc */
|
|
127
|
+
extract(ctx, carrier, getter) {
|
|
128
|
+
const maybeSentryTraceHeader = getter.get(carrier, SENTRY_TRACE_HEADER);
|
|
129
|
+
const baggage = getter.get(carrier, SENTRY_BAGGAGE_HEADER);
|
|
130
|
+
const sentryTrace = Array.isArray(maybeSentryTraceHeader) ? maybeSentryTraceHeader[0] : maybeSentryTraceHeader;
|
|
131
|
+
return getContextWithRemoteActiveSpanAndScopes(ctx, { sentryTrace, baggage });
|
|
132
|
+
}
|
|
133
|
+
/** @inheritDoc */
|
|
134
|
+
fields() {
|
|
135
|
+
return [SENTRY_TRACE_HEADER, SENTRY_BAGGAGE_HEADER, W3C_TRACEPARENT_HEADER];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function getContextWithRemoteActiveSpan(ctx, { sentryTrace, baggage }) {
|
|
139
|
+
const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
|
|
140
|
+
const { traceId, parentSpanId, sampled, dsc } = propagationContext;
|
|
141
|
+
const client = getClient();
|
|
142
|
+
const incomingDsc = baggageHeaderToDynamicSamplingContext(baggage);
|
|
143
|
+
if (!parentSpanId || client && !shouldContinueTrace(client, incomingDsc?.org_id)) {
|
|
144
|
+
return ctx;
|
|
145
|
+
}
|
|
146
|
+
const spanContext = generateRemoteSpanContext({
|
|
147
|
+
traceId,
|
|
148
|
+
spanId: parentSpanId,
|
|
149
|
+
sampled,
|
|
150
|
+
dsc
|
|
151
|
+
});
|
|
152
|
+
return trace.setSpanContext(ctx, spanContext);
|
|
153
|
+
}
|
|
154
|
+
function getContextWithRemoteActiveSpanAndScopes(ctx, options) {
|
|
155
|
+
const ctxWithRemoteSpan = getContextWithRemoteActiveSpan(ctx, options);
|
|
156
|
+
const isContinuingTrace = trace.getSpanContext(ctxWithRemoteSpan) !== void 0;
|
|
157
|
+
return ensureScopesOnContext(ctxWithRemoteSpan, isContinuingTrace);
|
|
158
|
+
}
|
|
159
|
+
function ensureScopesOnContext(ctx, isContinuingTrace) {
|
|
160
|
+
const scopes = getScopesFromContext(ctx);
|
|
161
|
+
const scope = scopes ? scopes.scope : getCurrentScope().clone();
|
|
162
|
+
if (!scopes && !isContinuingTrace) {
|
|
163
|
+
const propagationContext = scope.getPropagationContext();
|
|
164
|
+
scope.setPropagationContext({
|
|
165
|
+
...propagationContext,
|
|
166
|
+
traceId: generateTraceId(),
|
|
167
|
+
sampleRand: _INTERNAL_safeMathRandom()
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
const newScopes = {
|
|
171
|
+
scope,
|
|
172
|
+
isolationScope: scopes ? scopes.isolationScope : getIsolationScope()
|
|
173
|
+
};
|
|
174
|
+
return setScopesOnContext(ctx, newScopes);
|
|
175
|
+
}
|
|
176
|
+
function generateRemoteSpanContext({
|
|
177
|
+
spanId,
|
|
178
|
+
traceId,
|
|
179
|
+
sampled,
|
|
180
|
+
dsc
|
|
181
|
+
}) {
|
|
182
|
+
const traceState = makeTraceState({
|
|
183
|
+
dsc,
|
|
184
|
+
sampled
|
|
185
|
+
});
|
|
186
|
+
const spanContext = {
|
|
187
|
+
traceId,
|
|
188
|
+
spanId,
|
|
189
|
+
isRemote: true,
|
|
190
|
+
traceFlags: sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
|
|
191
|
+
traceState
|
|
192
|
+
};
|
|
193
|
+
return spanContext;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const canonicalGrpcErrorCodesMap = {
|
|
197
|
+
"1": "cancelled",
|
|
198
|
+
"2": "unknown_error",
|
|
199
|
+
"3": "invalid_argument",
|
|
200
|
+
"4": "deadline_exceeded",
|
|
201
|
+
"5": "not_found",
|
|
202
|
+
"6": "already_exists",
|
|
203
|
+
"7": "permission_denied",
|
|
204
|
+
"8": "resource_exhausted",
|
|
205
|
+
"9": "failed_precondition",
|
|
206
|
+
"10": "aborted",
|
|
207
|
+
"11": "out_of_range",
|
|
208
|
+
"12": "unimplemented",
|
|
209
|
+
"13": "internal_error",
|
|
210
|
+
"14": "unavailable",
|
|
211
|
+
"15": "data_loss",
|
|
212
|
+
"16": "unauthenticated"
|
|
213
|
+
};
|
|
214
|
+
function inferStatusFromAttributes(attributes) {
|
|
215
|
+
const httpCodeAttribute = attributes[HTTP_RESPONSE_STATUS_CODE] || attributes[HTTP_STATUS_CODE];
|
|
216
|
+
const grpcCodeAttribute = attributes[RPC_GRPC_STATUS_CODE];
|
|
217
|
+
const numberHttpCode = typeof httpCodeAttribute === "number" ? httpCodeAttribute : typeof httpCodeAttribute === "string" ? parseInt(httpCodeAttribute) : void 0;
|
|
218
|
+
if (typeof numberHttpCode === "number") {
|
|
219
|
+
return getSpanStatusFromHttpCode(numberHttpCode);
|
|
220
|
+
}
|
|
221
|
+
if (typeof grpcCodeAttribute === "string") {
|
|
222
|
+
return { code: SPAN_STATUS_ERROR, message: canonicalGrpcErrorCodesMap[grpcCodeAttribute] || "unknown_error" };
|
|
223
|
+
}
|
|
224
|
+
return void 0;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function inferSpanData(attributes) {
|
|
228
|
+
const httpMethod = attributes[HTTP_REQUEST_METHOD] || attributes[HTTP_METHOD];
|
|
229
|
+
if (httpMethod) {
|
|
230
|
+
return descriptionForHttpMethod(attributes);
|
|
231
|
+
}
|
|
232
|
+
const dbSystem = attributes[DB_SYSTEM_NAME] || attributes[DB_SYSTEM];
|
|
233
|
+
const opIsCache = typeof attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] === "string" && attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP].startsWith("cache.");
|
|
234
|
+
if (dbSystem && !opIsCache) {
|
|
235
|
+
return descriptionForDbSystem(attributes);
|
|
236
|
+
}
|
|
237
|
+
const rpcService = attributes[RPC_SERVICE];
|
|
238
|
+
if (rpcService) {
|
|
239
|
+
return {
|
|
240
|
+
op: "rpc"
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const messagingSystem = attributes[MESSAGING_SYSTEM];
|
|
244
|
+
if (messagingSystem) {
|
|
245
|
+
return {
|
|
246
|
+
op: getMessagingOp(attributes[MESSAGING_OPERATION_TYPE])
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
const faasTrigger = attributes[FAAS_TRIGGER];
|
|
250
|
+
if (faasTrigger) {
|
|
251
|
+
return {
|
|
252
|
+
op: getFaasOp(faasTrigger)
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
return { op: void 0 };
|
|
256
|
+
}
|
|
257
|
+
function getFaasOp(trigger) {
|
|
258
|
+
switch (trigger) {
|
|
259
|
+
case "http":
|
|
260
|
+
return WEB_SERVER_HTTP_SERVER_SPAN_OP;
|
|
261
|
+
case "pubsub":
|
|
262
|
+
return MESSAGING_QUEUE_PROCESS_SPAN_OP;
|
|
263
|
+
default:
|
|
264
|
+
return GENERAL_FUNCTION_SPAN_OP;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function getMessagingOp(operationType) {
|
|
268
|
+
switch (operationType) {
|
|
269
|
+
case "publish":
|
|
270
|
+
case "send":
|
|
271
|
+
return MESSAGING_QUEUE_PUBLISH_SPAN_OP;
|
|
272
|
+
case "receive":
|
|
273
|
+
return MESSAGING_QUEUE_RECEIVE_SPAN_OP;
|
|
274
|
+
case "process":
|
|
275
|
+
return MESSAGING_QUEUE_PROCESS_SPAN_OP;
|
|
276
|
+
default:
|
|
277
|
+
return MESSAGING_QUEUE_SPAN_OP;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function descriptionForDbSystem(attributes) {
|
|
281
|
+
const userDefinedName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME];
|
|
282
|
+
if (typeof userDefinedName === "string") {
|
|
283
|
+
return {
|
|
284
|
+
op: "db"
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === "custom") {
|
|
288
|
+
return { op: "db" };
|
|
289
|
+
}
|
|
290
|
+
return { op: "db" };
|
|
291
|
+
}
|
|
292
|
+
function descriptionForHttpMethod(attributes) {
|
|
293
|
+
const kind = attributes[SENTRY_KIND];
|
|
294
|
+
const op = kind === "client" ? WEB_SERVER_HTTP_CLIENT_SPAN_OP : kind === "server" ? WEB_SERVER_HTTP_SERVER_SPAN_OP : WEB_SERVER_HTTP_SPAN_OP;
|
|
295
|
+
const { urlPath, url, query, fragment } = getSanitizedUrl(attributes);
|
|
296
|
+
if (!urlPath) {
|
|
297
|
+
return { op };
|
|
298
|
+
}
|
|
299
|
+
const data = {};
|
|
300
|
+
if (url) {
|
|
301
|
+
data[URL_FULL] = filterCollectedUrl(url);
|
|
302
|
+
}
|
|
303
|
+
const urlQuery = filterCollectedUrlQuery(getUrlQuery(query));
|
|
304
|
+
if (urlQuery) {
|
|
305
|
+
data[URL_QUERY] = urlQuery;
|
|
306
|
+
}
|
|
307
|
+
const urlFragment = getUrlFragment(fragment);
|
|
308
|
+
if (urlFragment) {
|
|
309
|
+
data[URL_FRAGMENT] = urlFragment;
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
op,
|
|
313
|
+
data
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
function getSanitizedUrl(attributes) {
|
|
317
|
+
const kind = attributes[SENTRY_KIND];
|
|
318
|
+
const httpTarget = attributes[HTTP_TARGET];
|
|
319
|
+
const httpUrl = attributes[URL_FULL];
|
|
320
|
+
const httpRoute = attributes[HTTP_ROUTE];
|
|
321
|
+
const parsedUrl = typeof httpUrl === "string" ? parseUrl(httpUrl) : void 0;
|
|
322
|
+
const url = parsedUrl ? getSanitizedUrlString(parsedUrl) : void 0;
|
|
323
|
+
const query = parsedUrl?.search || void 0;
|
|
324
|
+
const fragment = parsedUrl?.hash || void 0;
|
|
325
|
+
if (typeof httpRoute === "string") {
|
|
326
|
+
return { urlPath: httpRoute, url, query, fragment, hasRoute: true };
|
|
327
|
+
}
|
|
328
|
+
if (kind === "server" && typeof httpTarget === "string") {
|
|
329
|
+
return { urlPath: stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
|
|
330
|
+
}
|
|
331
|
+
if (parsedUrl) {
|
|
332
|
+
return { urlPath: url, url, query, fragment, hasRoute: false };
|
|
333
|
+
}
|
|
334
|
+
if (typeof httpTarget === "string") {
|
|
335
|
+
return { urlPath: stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
|
|
336
|
+
}
|
|
337
|
+
return { urlPath: void 0, url, query, fragment, hasRoute: false };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function applyOtelSpanData(span, options = {}) {
|
|
341
|
+
const spanJSON = spanToJSON(span);
|
|
342
|
+
const attributes = spanJSON.data;
|
|
343
|
+
const mayInferSource = spanShouldInferOtelSource(span);
|
|
344
|
+
const hasCustomSpanName = attributes[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME] !== void 0;
|
|
345
|
+
const canInferSource = mayInferSource && !hasCustomSpanName && !spanSourceWasExplicitlySet(span);
|
|
346
|
+
const attributesForInference = canInferSource && attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === "custom" ? { ...attributes, [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: void 0 } : attributes;
|
|
347
|
+
const inferred = inferSpanData(attributesForInference);
|
|
348
|
+
if (inferred.op && attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] === void 0) {
|
|
349
|
+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, inferred.op);
|
|
350
|
+
}
|
|
351
|
+
if (inferred.data) {
|
|
352
|
+
Object.entries(inferred.data).forEach(([key, value]) => {
|
|
353
|
+
if (value !== void 0 && attributes[key] === void 0) {
|
|
354
|
+
span.setAttribute(key, value);
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (options.finalizeStatus) {
|
|
359
|
+
applyOtelCompatibilityAttributes(span, attributes);
|
|
360
|
+
const client = getClient();
|
|
361
|
+
applyOtelSpanStatus(span, attributes, spanJSON.status, !!client && hasSpanStreamingEnabled(client));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function applyOtelSpanStatus(span, attributes, status, spanStreamingEnabled) {
|
|
365
|
+
if (status === "ok") {
|
|
366
|
+
span.setStatus(inferStatusFromAttributes(attributes) || { code: SPAN_STATUS_OK });
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (!spanStreamingEnabled && status !== "ok" && !isStatusErrorMessageValid(status)) {
|
|
370
|
+
span.setStatus({ code: SPAN_STATUS_ERROR, message: "internal_error" });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function applyOtelCompatibilityAttributes(span, attributes) {
|
|
374
|
+
const legacyHttpStatusCode = attributes[HTTP_STATUS_CODE];
|
|
375
|
+
if (attributes[HTTP_RESPONSE_STATUS_CODE] === void 0 && legacyHttpStatusCode !== void 0) {
|
|
376
|
+
span.setAttribute(HTTP_RESPONSE_STATUS_CODE, legacyHttpStatusCode);
|
|
377
|
+
attributes[HTTP_RESPONSE_STATUS_CODE] = legacyHttpStatusCode;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function backfillStreamedSpanDataFromOtel(spanJSON) {
|
|
382
|
+
const attributes = spanJSON.attributes ?? {};
|
|
383
|
+
const { op, data } = inferSpanData(attributes);
|
|
384
|
+
safeSetSpanJSONAttributes(spanJSON, {
|
|
385
|
+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
|
|
386
|
+
// If nothing in the chain previously set an origin, we now default it to 'manual'
|
|
387
|
+
// For transactions, this is done in the SpanExporter.
|
|
388
|
+
// TODO (v11): Remove this again once we fully moved away from OTel's TracerProvider.
|
|
389
|
+
// at this point, we use `SentrySpan` everywhere, which defaults its origin to 'manual'.
|
|
390
|
+
[SENTRY_ORIGIN]: "manual",
|
|
391
|
+
...data
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const SUPPRESS_TRACING_KEY = createContextKey("OpenTelemetry SDK Context Key SUPPRESS_TRACING");
|
|
396
|
+
function suppressTracing(context) {
|
|
397
|
+
return context.setValue(SUPPRESS_TRACING_KEY, true);
|
|
398
|
+
}
|
|
399
|
+
function isTracingSuppressed(context) {
|
|
400
|
+
return context.getValue(SUPPRESS_TRACING_KEY) === true;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function getSamplingDecision(spanContext) {
|
|
404
|
+
const { traceFlags, traceState } = spanContext;
|
|
405
|
+
const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === "1" : false;
|
|
406
|
+
if (traceFlags === TraceFlags.SAMPLED) {
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
if (sampledNotRecording) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : void 0;
|
|
413
|
+
const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : void 0;
|
|
414
|
+
if (dsc?.sampled === "true") {
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
if (dsc?.sampled === "false") {
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
return void 0;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
class SentryTracer {
|
|
424
|
+
/** @inheritdoc */
|
|
425
|
+
startSpan(name, options = {}, ctx) {
|
|
426
|
+
const parentContext = ctx || context.active();
|
|
427
|
+
const parentSpanCandidate = options.root ? void 0 : trace.getSpan(parentContext);
|
|
428
|
+
const parentSpan = parentSpanCandidate && isSpanContextValid(parentSpanCandidate.spanContext()) ? parentSpanCandidate : void 0;
|
|
429
|
+
if (isTracingSuppressed(parentContext)) {
|
|
430
|
+
return this._createNonRecordingSpan(parentSpan);
|
|
431
|
+
}
|
|
432
|
+
const span = this._startSentrySpan(name, options, parentSpan, ctx !== void 0);
|
|
433
|
+
markSpanAsTracerProviderSpan(span);
|
|
434
|
+
if (options.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] === void 0) {
|
|
435
|
+
markSpanForOtelSourceInference(span);
|
|
436
|
+
}
|
|
437
|
+
applyOtelSpanData(span);
|
|
438
|
+
return span;
|
|
439
|
+
}
|
|
440
|
+
startActiveSpan(name, optionsOrFn, contextOrFn, fn) {
|
|
441
|
+
const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
|
|
442
|
+
const ctx = typeof contextOrFn === "function" || contextOrFn === void 0 ? context.active() : contextOrFn;
|
|
443
|
+
const callback = typeof optionsOrFn === "function" ? optionsOrFn : typeof contextOrFn === "function" ? contextOrFn : fn;
|
|
444
|
+
const span = this.startSpan(name, options, ctx);
|
|
445
|
+
const capturedIsolationScope = getCapturedScopesOnSpan(span).isolationScope;
|
|
446
|
+
const withCapturedIsolationScope = (contextToFork) => capturedIsolationScope ? contextToFork.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, capturedIsolationScope) : contextToFork;
|
|
447
|
+
if (spanIsIgnored(span) && trace.getSpan(ctx)) {
|
|
448
|
+
return context.with(withCapturedIsolationScope(ctx), () => callback(span));
|
|
449
|
+
}
|
|
450
|
+
return context.with(withCapturedIsolationScope(trace.setSpan(ctx, span)), () => {
|
|
451
|
+
_INTERNAL_setSpanForScope(getCurrentScope(), span);
|
|
452
|
+
return callback(span);
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
_startSentrySpan(name, options, parentSpan, hasExplicitContext) {
|
|
456
|
+
const sentryOptions = {
|
|
457
|
+
name,
|
|
458
|
+
attributes: options.attributes || {},
|
|
459
|
+
links: options.links,
|
|
460
|
+
startTime: options.startTime
|
|
461
|
+
};
|
|
462
|
+
if (options.kind) {
|
|
463
|
+
sentryOptions.attributes[SENTRY_KIND] = spanKindToName(options.kind);
|
|
464
|
+
}
|
|
465
|
+
if (options.root) {
|
|
466
|
+
return startNewTrace(() => _INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan: null }));
|
|
467
|
+
}
|
|
468
|
+
if (parentSpan?.spanContext().isRemote) {
|
|
469
|
+
return this._startRootSpanWithRemoteParent(sentryOptions, parentSpan);
|
|
470
|
+
}
|
|
471
|
+
if (parentSpan) {
|
|
472
|
+
return _INTERNAL_startInactiveSpan({ ...sentryOptions, parentSpan });
|
|
473
|
+
}
|
|
474
|
+
return _INTERNAL_startInactiveSpan({
|
|
475
|
+
...sentryOptions,
|
|
476
|
+
parentSpan: hasExplicitContext ? null : void 0
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
_startRootSpanWithRemoteParent(options, parentSpan) {
|
|
480
|
+
const { spanId, traceId, traceState } = parentSpan.spanContext();
|
|
481
|
+
const dsc = getDynamicSamplingContextFromSpan(parentSpan);
|
|
482
|
+
const sampleRand = typeof dsc.sample_rand === "string" ? Number(dsc.sample_rand) : void 0;
|
|
483
|
+
const hasIncomingDsc = !!traceState?.get(SENTRY_TRACE_STATE_DSC);
|
|
484
|
+
return withScope((scope) => {
|
|
485
|
+
scope.setPropagationContext({
|
|
486
|
+
traceId,
|
|
487
|
+
parentSpanId: spanId,
|
|
488
|
+
sampled: getSamplingDecision(parentSpan.spanContext()),
|
|
489
|
+
dsc: hasIncomingDsc ? dsc : void 0,
|
|
490
|
+
sampleRand: typeof sampleRand === "number" && !Number.isNaN(sampleRand) ? sampleRand : _INTERNAL_safeMathRandom()
|
|
491
|
+
});
|
|
492
|
+
_INTERNAL_setSpanForScope(scope, void 0);
|
|
493
|
+
return _INTERNAL_startInactiveSpan({ ...options, parentSpan: null });
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
_createNonRecordingSpan(parentSpan) {
|
|
497
|
+
const traceId = parentSpan?.spanContext().traceId ?? getCurrentScope().getPropagationContext().traceId;
|
|
498
|
+
const span = new SentryNonRecordingSpan({ traceId });
|
|
499
|
+
if (parentSpan) {
|
|
500
|
+
addChildSpanToSpan(parentSpan, span);
|
|
501
|
+
}
|
|
502
|
+
setCapturedScopesOnSpan(span, getCurrentScope(), getIsolationScope());
|
|
503
|
+
return span;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
class SentryTracerProvider {
|
|
508
|
+
constructor() {
|
|
509
|
+
this._tracers = /* @__PURE__ */ new Map();
|
|
510
|
+
}
|
|
511
|
+
/** @inheritdoc */
|
|
512
|
+
getTracer(name, version, options) {
|
|
513
|
+
const key = JSON.stringify([name, version, options]);
|
|
514
|
+
const cachedTracer = this._tracers.get(key);
|
|
515
|
+
if (cachedTracer) {
|
|
516
|
+
return cachedTracer;
|
|
517
|
+
}
|
|
518
|
+
const tracer = new SentryTracer();
|
|
519
|
+
this._tracers.set(key, tracer);
|
|
520
|
+
return tracer;
|
|
521
|
+
}
|
|
522
|
+
/** Compatibility with SDK tracer providers. */
|
|
523
|
+
forceFlush() {
|
|
524
|
+
return Promise.resolve();
|
|
525
|
+
}
|
|
526
|
+
/** Compatibility with SDK tracer providers. */
|
|
527
|
+
shutdown() {
|
|
528
|
+
return Promise.resolve();
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function reconcileDscSampled(dsc, sampled) {
|
|
533
|
+
const reconciled = { ...dsc };
|
|
534
|
+
if (sampled === void 0) {
|
|
535
|
+
delete reconciled.sampled;
|
|
536
|
+
} else {
|
|
537
|
+
reconciled.sampled = String(sampled);
|
|
538
|
+
if (sampled === false) {
|
|
539
|
+
delete reconciled.transaction;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return reconciled;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function _startSpan(options, callback, autoEnd) {
|
|
546
|
+
const tracer = getTracer();
|
|
547
|
+
const { name, parentSpan: customParentSpan } = options;
|
|
548
|
+
const wrapper = getActiveSpanWrapper(customParentSpan);
|
|
549
|
+
return wrapper(() => {
|
|
550
|
+
const activeCtx = getContext(options.scope, options.forceTransaction);
|
|
551
|
+
const missingRequiredParent = options.onlyIfParent && !trace.getSpan(activeCtx);
|
|
552
|
+
const ctx = missingRequiredParent ? suppressTracing(activeCtx) : activeCtx;
|
|
553
|
+
if (missingRequiredParent) {
|
|
554
|
+
getClient()?.recordDroppedEvent("no_parent_span", "span");
|
|
555
|
+
}
|
|
556
|
+
const spanOptions = getSpanOptions(options);
|
|
557
|
+
if (!hasSpansEnabled()) {
|
|
558
|
+
const suppressedCtx = isTracingSuppressed(ctx) ? ctx : suppressTracing(ctx);
|
|
559
|
+
return context.with(suppressedCtx, () => {
|
|
560
|
+
return tracer.startActiveSpan(name, spanOptions, suppressedCtx, (span) => {
|
|
561
|
+
patchSpanEnd(span);
|
|
562
|
+
return context.with(trace.setSpan(activeCtx, span), () => {
|
|
563
|
+
return handleCallbackErrors(
|
|
564
|
+
() => callback(span),
|
|
565
|
+
() => {
|
|
566
|
+
if (spanToJSON(span).status === "ok") {
|
|
567
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
autoEnd ? () => span.end() : void 0
|
|
571
|
+
);
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
return tracer.startActiveSpan(name, spanOptions, ctx, (span) => {
|
|
577
|
+
patchSpanEnd(span);
|
|
578
|
+
return handleCallbackErrors(
|
|
579
|
+
() => callback(span),
|
|
580
|
+
() => {
|
|
581
|
+
if (spanToJSON(span).status === "ok") {
|
|
582
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
583
|
+
}
|
|
584
|
+
},
|
|
585
|
+
autoEnd ? () => span.end() : void 0
|
|
586
|
+
);
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
function startSpan(options, callback) {
|
|
591
|
+
return _startSpan(options, callback, true);
|
|
592
|
+
}
|
|
593
|
+
function startSpanManual(options, callback) {
|
|
594
|
+
return _startSpan(options, (span) => callback(span, () => span.end()), false);
|
|
595
|
+
}
|
|
596
|
+
function startInactiveSpan(options) {
|
|
597
|
+
const tracer = getTracer();
|
|
598
|
+
const { name, parentSpan: customParentSpan } = options;
|
|
599
|
+
const wrapper = getActiveSpanWrapper(customParentSpan);
|
|
600
|
+
return wrapper(() => {
|
|
601
|
+
const activeCtx = getContext(options.scope, options.forceTransaction);
|
|
602
|
+
const missingRequiredParent = options.onlyIfParent && !trace.getSpan(activeCtx);
|
|
603
|
+
let ctx = missingRequiredParent ? suppressTracing(activeCtx) : activeCtx;
|
|
604
|
+
if (missingRequiredParent) {
|
|
605
|
+
getClient()?.recordDroppedEvent("no_parent_span", "span");
|
|
606
|
+
}
|
|
607
|
+
const spanOptions = getSpanOptions(options);
|
|
608
|
+
if (!hasSpansEnabled()) {
|
|
609
|
+
ctx = isTracingSuppressed(ctx) ? ctx : suppressTracing(ctx);
|
|
610
|
+
}
|
|
611
|
+
const span = tracer.startSpan(name, spanOptions, ctx);
|
|
612
|
+
patchSpanEnd(span);
|
|
613
|
+
return span;
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
function withActiveSpan(span, callback) {
|
|
617
|
+
const newContextWithActiveSpan = span ? trace.setSpan(context.active(), span) : trace.deleteSpan(context.active());
|
|
618
|
+
return context.with(newContextWithActiveSpan, () => callback(getCurrentScope()));
|
|
619
|
+
}
|
|
620
|
+
function getTracer() {
|
|
621
|
+
const client = getClient();
|
|
622
|
+
return client?.tracer || trace.getTracer("@sentry/opentelemetry", SDK_VERSION);
|
|
623
|
+
}
|
|
624
|
+
function getSpanOptions(options) {
|
|
625
|
+
const { startTime, attributes, kind, op, links } = options;
|
|
626
|
+
const fixedStartTime = typeof startTime === "number" ? ensureTimestampInMilliseconds(startTime) : startTime;
|
|
627
|
+
return {
|
|
628
|
+
attributes: op ? {
|
|
629
|
+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: op,
|
|
630
|
+
...attributes
|
|
631
|
+
} : attributes,
|
|
632
|
+
kind,
|
|
633
|
+
links,
|
|
634
|
+
startTime: fixedStartTime
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
function ensureTimestampInMilliseconds(timestamp) {
|
|
638
|
+
const isMs = timestamp < 9999999999;
|
|
639
|
+
return isMs ? timestamp * 1e3 : timestamp;
|
|
640
|
+
}
|
|
641
|
+
function patchSpanEnd(span) {
|
|
642
|
+
const originalEnd = span.end.bind(span);
|
|
643
|
+
span.end = (endTime) => {
|
|
644
|
+
return originalEnd(typeof endTime === "number" ? ensureTimestampInMilliseconds(endTime) : endTime);
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
function getContext(scope, forceTransaction) {
|
|
648
|
+
const ctx = getContextForScope(scope);
|
|
649
|
+
const parentSpan = trace.getSpan(ctx);
|
|
650
|
+
if (!parentSpan) {
|
|
651
|
+
return ctx;
|
|
652
|
+
}
|
|
653
|
+
if (!forceTransaction) {
|
|
654
|
+
return ctx;
|
|
655
|
+
}
|
|
656
|
+
const ctxWithoutSpan = trace.deleteSpan(ctx);
|
|
657
|
+
const { spanId, traceId } = parentSpan.spanContext();
|
|
658
|
+
const sampled = getSamplingDecision(parentSpan.spanContext());
|
|
659
|
+
const rootSpan = getRootSpan(parentSpan);
|
|
660
|
+
const rawDsc = getDynamicSamplingContextFromSpan(rootSpan);
|
|
661
|
+
const hasIncomingFrozenDsc = !!rootSpan.spanContext().traceState?.get(SENTRY_TRACE_STATE_DSC);
|
|
662
|
+
const dsc = hasIncomingFrozenDsc ? rawDsc : reconcileDscSampled(rawDsc, sampled);
|
|
663
|
+
const traceState = makeTraceState({
|
|
664
|
+
dsc,
|
|
665
|
+
sampled
|
|
666
|
+
});
|
|
667
|
+
const spanOptions = {
|
|
668
|
+
traceId,
|
|
669
|
+
spanId,
|
|
670
|
+
isRemote: true,
|
|
671
|
+
traceFlags: sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
|
|
672
|
+
traceState
|
|
673
|
+
};
|
|
674
|
+
const ctxWithSpanContext = trace.setSpanContext(ctxWithoutSpan, spanOptions);
|
|
675
|
+
return ctxWithSpanContext;
|
|
676
|
+
}
|
|
677
|
+
function getContextForScope(scope) {
|
|
678
|
+
if (scope) {
|
|
679
|
+
const ctx = getContextFromScope(scope);
|
|
680
|
+
if (ctx) {
|
|
681
|
+
return ctx;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return context.active();
|
|
685
|
+
}
|
|
686
|
+
function getActiveSpanWrapper(parentSpan) {
|
|
687
|
+
return parentSpan !== void 0 ? (callback) => {
|
|
688
|
+
return withActiveSpan(parentSpan, callback);
|
|
689
|
+
} : (callback) => callback();
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function buildContextWithSentryScopes(context, activeContext) {
|
|
693
|
+
const span = trace.getSpan(context);
|
|
694
|
+
let effectiveContext;
|
|
695
|
+
if (span?.spanContext().traceState?.get(SENTRY_TRACE_STATE_CHILD_IGNORED) === "1") {
|
|
696
|
+
const contextWithoutSpan = trace.deleteSpan(context);
|
|
697
|
+
const parentSpan = trace.getSpan(activeContext);
|
|
698
|
+
effectiveContext = parentSpan ? trace.setSpan(contextWithoutSpan, parentSpan) : contextWithoutSpan;
|
|
699
|
+
} else {
|
|
700
|
+
effectiveContext = context;
|
|
701
|
+
}
|
|
702
|
+
const currentScopes = getScopesFromContext(effectiveContext);
|
|
703
|
+
const currentScope = currentScopes?.scope || getCurrentScope();
|
|
704
|
+
const currentIsolationScope = currentScopes?.isolationScope || getIsolationScope();
|
|
705
|
+
const shouldForkIsolationScope = effectiveContext.getValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY) === true;
|
|
706
|
+
const scope = effectiveContext.getValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY);
|
|
707
|
+
const isolationScope = effectiveContext.getValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY);
|
|
708
|
+
const newCurrentScope = scope || currentScope.clone();
|
|
709
|
+
const newIsolationScope = isolationScope || (shouldForkIsolationScope ? currentIsolationScope.clone() : currentIsolationScope);
|
|
710
|
+
const scopes = { scope: newCurrentScope, isolationScope: newIsolationScope };
|
|
711
|
+
const ctx1 = setScopesOnContext(effectiveContext, scopes);
|
|
712
|
+
const ctx2 = ctx1.deleteValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY).deleteValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY).deleteValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY);
|
|
713
|
+
setContextOnScope(newCurrentScope, ctx2);
|
|
714
|
+
return ctx2;
|
|
715
|
+
}
|
|
9
716
|
|
|
10
717
|
const ADD_LISTENER_METHODS = ["addListener", "on", "once", "prependListener", "prependOnceListener"];
|
|
11
718
|
class SentryAsyncLocalStorageContextManager {
|
|
12
|
-
constructor() {
|
|
719
|
+
constructor(asyncLocalStorage) {
|
|
13
720
|
this._kOtListeners = /* @__PURE__ */ Symbol("OtListeners");
|
|
14
721
|
this._wrapped = false;
|
|
15
|
-
|
|
16
|
-
this._asyncLocalStorage = getAsyncContextStrategy(getMainCarrier()).getTracingChannelBinding?.()?.asyncLocalStorage ?? new AsyncLocalStorage();
|
|
722
|
+
this._asyncLocalStorage = asyncLocalStorage;
|
|
17
723
|
}
|
|
18
724
|
active() {
|
|
19
725
|
return this._asyncLocalStorage.getStore() ?? ROOT_CONTEXT;
|
|
@@ -27,11 +733,14 @@ class SentryAsyncLocalStorageContextManager {
|
|
|
27
733
|
return this;
|
|
28
734
|
}
|
|
29
735
|
disable() {
|
|
30
|
-
|
|
736
|
+
try {
|
|
737
|
+
this._asyncLocalStorage.disable();
|
|
738
|
+
} catch {
|
|
739
|
+
}
|
|
31
740
|
return this;
|
|
32
741
|
}
|
|
33
742
|
bind(context, target) {
|
|
34
|
-
if (target
|
|
743
|
+
if (isEventEmitter(target)) {
|
|
35
744
|
return this._bindEventEmitter(context, target);
|
|
36
745
|
}
|
|
37
746
|
if (typeof target === "function") {
|
|
@@ -66,27 +775,22 @@ class SentryAsyncLocalStorageContextManager {
|
|
|
66
775
|
if (this._getPatchMap(ee) !== void 0) {
|
|
67
776
|
return ee;
|
|
68
777
|
}
|
|
69
|
-
this._createPatchMap(ee)
|
|
70
|
-
|
|
71
|
-
if (ee[methodName] === void 0) continue;
|
|
72
|
-
ee[methodName] = this._patchAddListener(
|
|
73
|
-
ee,
|
|
74
|
-
ee[methodName],
|
|
75
|
-
context
|
|
76
|
-
);
|
|
778
|
+
if (this._createPatchMap(ee) === void 0) {
|
|
779
|
+
return ee;
|
|
77
780
|
}
|
|
78
|
-
|
|
79
|
-
|
|
781
|
+
for (const methodName of ADD_LISTENER_METHODS) {
|
|
782
|
+
const original = getMethod(ee, methodName);
|
|
783
|
+
if (!original) continue;
|
|
784
|
+
trySetMethod(ee, methodName, this._patchAddListener(ee, original, context));
|
|
80
785
|
}
|
|
81
|
-
|
|
82
|
-
|
|
786
|
+
for (const methodName of ["removeListener", "off"]) {
|
|
787
|
+
const original = getMethod(ee, methodName);
|
|
788
|
+
if (!original) continue;
|
|
789
|
+
trySetMethod(ee, methodName, this._patchRemoveListener(ee, original));
|
|
83
790
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// oxlint-disable-next-line @typescript-eslint/unbound-method
|
|
88
|
-
ee.removeAllListeners
|
|
89
|
-
);
|
|
791
|
+
const removeAllListeners = getMethod(ee, "removeAllListeners");
|
|
792
|
+
if (removeAllListeners) {
|
|
793
|
+
trySetMethod(ee, "removeAllListeners", this._patchRemoveAllListeners(ee, removeAllListeners));
|
|
90
794
|
}
|
|
91
795
|
return ee;
|
|
92
796
|
}
|
|
@@ -121,9 +825,9 @@ class SentryAsyncLocalStorageContextManager {
|
|
|
121
825
|
if (contextManager._wrapped) {
|
|
122
826
|
return original.call(this, event, listener);
|
|
123
827
|
}
|
|
124
|
-
|
|
828
|
+
const map = contextManager._getPatchMap(ee) ?? contextManager._createPatchMap(ee);
|
|
125
829
|
if (map === void 0) {
|
|
126
|
-
|
|
830
|
+
return original.call(this, event, listener);
|
|
127
831
|
}
|
|
128
832
|
let listeners = map[event];
|
|
129
833
|
if (listeners === void 0) {
|
|
@@ -140,43 +844,118 @@ class SentryAsyncLocalStorageContextManager {
|
|
|
140
844
|
}
|
|
141
845
|
};
|
|
142
846
|
}
|
|
847
|
+
/**
|
|
848
|
+
* Attach a fresh patch map to the emitter. Returns `undefined` if the emitter does not accept the
|
|
849
|
+
* property (e.g. it is frozen or sealed), in which case the emitter must not be patched at all —
|
|
850
|
+
* without a patch map the remove-listener patches could not resolve their wrapped listeners.
|
|
851
|
+
*/
|
|
143
852
|
_createPatchMap(ee) {
|
|
144
853
|
const map = /* @__PURE__ */ Object.create(null);
|
|
145
|
-
|
|
146
|
-
|
|
854
|
+
try {
|
|
855
|
+
ee[this._kOtListeners] = map;
|
|
856
|
+
} catch {
|
|
857
|
+
return void 0;
|
|
858
|
+
}
|
|
859
|
+
return this._getPatchMap(ee) === map ? map : void 0;
|
|
147
860
|
}
|
|
148
861
|
_getPatchMap(ee) {
|
|
149
862
|
return ee[this._kOtListeners];
|
|
150
863
|
}
|
|
151
864
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
865
|
+
function isEventEmitter(target) {
|
|
866
|
+
if (typeof target !== "object" || !target) {
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
const candidate = target;
|
|
870
|
+
return typeof candidate.on === "function" && typeof candidate.emit === "function";
|
|
871
|
+
}
|
|
872
|
+
function getMethod(ee, methodName) {
|
|
873
|
+
const value = ee[methodName];
|
|
874
|
+
return typeof value === "function" ? value : void 0;
|
|
157
875
|
}
|
|
158
|
-
function
|
|
159
|
-
|
|
160
|
-
|
|
876
|
+
function trySetMethod(ee, methodName, patched) {
|
|
877
|
+
try {
|
|
878
|
+
ee[methodName] = patched;
|
|
879
|
+
} catch {
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
function setOpenTelemetryContextAsyncContextStrategy() {
|
|
884
|
+
const existingAsyncLocalStorage = getAsyncContextStrategy(getMainCarrier()).getTracingChannelBinding?.()?.asyncLocalStorage;
|
|
885
|
+
const asyncLocalStorage = existingAsyncLocalStorage ?? new AsyncLocalStorage();
|
|
886
|
+
function getScopes() {
|
|
887
|
+
const ctx = api.context.active();
|
|
888
|
+
const scopes = getScopesFromContext(ctx);
|
|
889
|
+
if (scopes) {
|
|
890
|
+
return scopes;
|
|
891
|
+
}
|
|
161
892
|
return {
|
|
162
|
-
|
|
893
|
+
scope: getDefaultCurrentScope(),
|
|
894
|
+
isolationScope: getDefaultIsolationScope()
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
function withScope(callback) {
|
|
898
|
+
const ctx = api.context.active();
|
|
899
|
+
return api.context.with(ctx, () => {
|
|
900
|
+
return callback(getCurrentScope());
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
function withSetScope(scope, callback) {
|
|
904
|
+
const ctx = getContextFromScope(scope) || api.context.active();
|
|
905
|
+
return api.context.with(ctx.setValue(SENTRY_FORK_SET_SCOPE_CONTEXT_KEY, scope), () => {
|
|
906
|
+
return callback(scope);
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
function withIsolationScope(callback) {
|
|
910
|
+
const ctx = api.context.active();
|
|
911
|
+
return api.context.with(ctx.setValue(SENTRY_FORK_ISOLATION_SCOPE_CONTEXT_KEY, true), () => {
|
|
912
|
+
const scope = getCurrentScope();
|
|
913
|
+
if (!isContinuingTrace(scope.getPropagationContext())) {
|
|
914
|
+
scope.setPropagationContext({
|
|
915
|
+
traceId: generateTraceId(),
|
|
916
|
+
sampleRand: _INTERNAL_safeMathRandom()
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
return callback(getIsolationScope());
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
function withSetIsolationScope(isolationScope, callback) {
|
|
923
|
+
const ctx = api.context.active();
|
|
924
|
+
return api.context.with(ctx.setValue(SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY, isolationScope), () => {
|
|
925
|
+
return callback(getIsolationScope());
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
function getCurrentScope() {
|
|
929
|
+
return getScopes().scope;
|
|
930
|
+
}
|
|
931
|
+
function getIsolationScope() {
|
|
932
|
+
return getScopes().isolationScope;
|
|
933
|
+
}
|
|
934
|
+
function getTracingChannelBinding() {
|
|
935
|
+
return {
|
|
936
|
+
asyncLocalStorage,
|
|
163
937
|
getStoreWithActiveSpan
|
|
164
938
|
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
939
|
+
}
|
|
940
|
+
setAsyncContextStrategy({
|
|
941
|
+
withScope,
|
|
942
|
+
withSetScope,
|
|
943
|
+
withSetIsolationScope,
|
|
944
|
+
withIsolationScope,
|
|
945
|
+
getCurrentScope,
|
|
946
|
+
getIsolationScope,
|
|
947
|
+
startSpan,
|
|
948
|
+
startSpanManual,
|
|
949
|
+
startInactiveSpan,
|
|
950
|
+
getActiveSpan,
|
|
951
|
+
// The types here don't fully align, because our own `Span` type is narrower
|
|
952
|
+
// than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
|
|
953
|
+
withActiveSpan,
|
|
954
|
+
getTracingChannelBinding
|
|
955
|
+
});
|
|
956
|
+
const ctxManager = new SentryAsyncLocalStorageContextManager(asyncLocalStorage);
|
|
957
|
+
api.context.setGlobalContextManager(ctxManager);
|
|
958
|
+
return ctxManager.getAsyncLocalStorageLookup();
|
|
180
959
|
}
|
|
181
960
|
function getStoreWithActiveSpan(span) {
|
|
182
961
|
const activeContext = api.context.active();
|
|
@@ -184,5 +963,5 @@ function getStoreWithActiveSpan(span) {
|
|
|
184
963
|
return isIgnoredChild ? activeContext : api.trace.setSpan(activeContext, span);
|
|
185
964
|
}
|
|
186
965
|
|
|
187
|
-
export {
|
|
966
|
+
export { SentryPropagator, SentryTracerProvider, applyOtelSpanData, backfillStreamedSpanDataFromOtel, getScopesFromContext, setOpenTelemetryContextAsyncContextStrategy, setupEventContextTrace };
|
|
188
967
|
//# sourceMappingURL=index.js.map
|