-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcaptureExceptionForTimedEvent.ts
More file actions
57 lines (49 loc) · 1.62 KB
/
Copy pathcaptureExceptionForTimedEvent.ts
File metadata and controls
57 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { Span as OtelSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import type { Hub } from '@sentry/types';
import { isString } from '@sentry/utils';
/**
* Maybe capture a Sentry exception for an OTEL timed event.
* This will check if the event is exception-like and in that case capture it as an exception.
*/
// eslint-disable-next-line deprecation/deprecation
export function maybeCaptureExceptionForTimedEvent(hub: Hub, event: TimedEvent, otelSpan?: OtelSpan): void {
if (event.name !== 'exception') {
return;
}
const attributes = event.attributes;
if (!attributes) {
return;
}
const message = attributes[SemanticAttributes.EXCEPTION_MESSAGE];
if (typeof message !== 'string') {
return;
}
const syntheticError = new Error(message);
const stack = attributes[SemanticAttributes.EXCEPTION_STACKTRACE];
if (isString(stack)) {
syntheticError.stack = stack;
}
const type = attributes[SemanticAttributes.EXCEPTION_TYPE];
if (isString(type)) {
syntheticError.name = type;
}
// eslint-disable-next-line deprecation/deprecation
hub.captureException(syntheticError, {
captureContext: otelSpan
? {
contexts: {
otel: {
attributes: otelSpan.attributes,
resource: otelSpan.resource.attributes,
},
trace: {
trace_id: otelSpan.spanContext().traceId,
span_id: otelSpan.spanContext().spanId,
parent_span_id: otelSpan.parentSpanId,
},
},
}
: undefined,
});
}