-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtracer.ts
More file actions
199 lines (169 loc) · 5.72 KB
/
tracer.ts
File metadata and controls
199 lines (169 loc) · 5.72 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import type { Span } from '@sentry/core';
import {
getActiveSpan,
getIsolationScope,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
startInactiveSpan,
withActiveSpan,
} from '@sentry/core';
import type * as Context from 'effect/Context';
import * as Exit from 'effect/Exit';
import * as Option from 'effect/Option';
import * as EffectTracer from 'effect/Tracer';
const KIND_MAP: Record<EffectTracer.SpanKind, 'internal' | 'server' | 'client' | 'producer' | 'consumer'> = {
internal: 'internal',
client: 'client',
server: 'server',
producer: 'producer',
consumer: 'consumer',
};
function deriveOp(name: string, kind: EffectTracer.SpanKind): string {
if (name.startsWith('http.server')) {
return 'http.server';
}
if (name.startsWith('http.client')) {
return 'http.client';
}
return KIND_MAP[kind];
}
function deriveOrigin(name: string): string {
if (name.startsWith('http.server') || name.startsWith('http.client')) {
return 'auto.http.effect';
}
return 'auto.function.effect';
}
function deriveSpanName(name: string, kind: EffectTracer.SpanKind): string {
if (name.startsWith('http.server') && kind === 'server') {
const isolationScope = getIsolationScope();
const transactionName = isolationScope.getScopeData().transactionName;
if (transactionName) {
return transactionName;
}
}
return name;
}
type HrTime = [number, number];
const SENTRY_SPAN_SYMBOL = Symbol.for('@sentry/effect.SentrySpan');
function nanosToHrTime(nanos: bigint): HrTime {
const seconds = Number(nanos / BigInt(1_000_000_000));
const remainingNanos = Number(nanos % BigInt(1_000_000_000));
return [seconds, remainingNanos];
}
interface SentrySpanLike extends EffectTracer.Span {
readonly [SENTRY_SPAN_SYMBOL]: true;
readonly sentrySpan: Span;
}
function isSentrySpan(span: EffectTracer.AnySpan): span is SentrySpanLike {
return SENTRY_SPAN_SYMBOL in span;
}
class SentrySpanWrapper implements SentrySpanLike {
public readonly [SENTRY_SPAN_SYMBOL]: true;
public readonly _tag: 'Span';
public readonly spanId: string;
public readonly traceId: string;
public readonly attributes: Map<string, unknown>;
public readonly sampled: boolean;
public readonly parent: Option.Option<EffectTracer.AnySpan>;
public readonly links: Array<EffectTracer.SpanLink>;
public status: EffectTracer.SpanStatus;
public readonly sentrySpan: Span;
public constructor(
public readonly name: string,
parent: Option.Option<EffectTracer.AnySpan>,
public readonly context: Context.Context<never>,
links: ReadonlyArray<EffectTracer.SpanLink>,
startTime: bigint,
public readonly kind: EffectTracer.SpanKind,
existingSpan: Span,
) {
this[SENTRY_SPAN_SYMBOL] = true as const;
this._tag = 'Span' as const;
this.attributes = new Map<string, unknown>();
this.parent = parent;
this.links = [...links];
this.sentrySpan = existingSpan;
const spanContext = this.sentrySpan.spanContext();
this.spanId = spanContext.spanId;
this.traceId = spanContext.traceId;
this.sampled = this.sentrySpan.isRecording();
this.status = {
_tag: 'Started',
startTime,
};
}
public attribute(key: string, value: unknown): void {
if (!this.sentrySpan.isRecording()) {
return;
}
this.sentrySpan.setAttribute(key, value as Parameters<Span['setAttribute']>[1]);
this.attributes.set(key, value);
}
public addLinks(links: ReadonlyArray<EffectTracer.SpanLink>): void {
this.links.push(...links);
}
public end(endTime: bigint, exit: Exit.Exit<unknown, unknown>): void {
this.status = {
_tag: 'Ended',
endTime,
exit,
startTime: this.status.startTime,
};
if (!this.sentrySpan.isRecording()) {
return;
}
if (Exit.isFailure(exit)) {
const cause = exit.cause;
const message =
cause._tag === 'Fail' ? String(cause.error) : cause._tag === 'Die' ? String(cause.defect) : 'internal_error';
this.sentrySpan.setStatus({ code: 2, message });
} else {
this.sentrySpan.setStatus({ code: 1 });
}
this.sentrySpan.end(nanosToHrTime(endTime));
}
public event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void {
if (!this.sentrySpan.isRecording()) {
return;
}
this.sentrySpan.addEvent(name, attributes as Parameters<Span['addEvent']>[1], nanosToHrTime(startTime));
}
}
function createSentrySpan(
name: string,
parent: Option.Option<EffectTracer.AnySpan>,
context: Context.Context<never>,
links: ReadonlyArray<EffectTracer.SpanLink>,
startTime: bigint,
kind: EffectTracer.SpanKind,
): SentrySpanLike {
const parentSentrySpan =
Option.isSome(parent) && isSentrySpan(parent.value) ? parent.value.sentrySpan : (getActiveSpan() ?? null);
const spanName = deriveSpanName(name, kind);
const newSpan = startInactiveSpan({
name: spanName,
op: deriveOp(name, kind),
startTime: nanosToHrTime(startTime),
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: deriveOrigin(name),
},
...(parentSentrySpan ? { parentSpan: parentSentrySpan } : {}),
});
return new SentrySpanWrapper(name, parent, context, links, startTime, kind, newSpan);
}
const makeSentryTracer = (): EffectTracer.Tracer =>
EffectTracer.make({
span(name, parent, context, links, startTime, kind) {
return createSentrySpan(name, parent, context, links, startTime, kind);
},
context(execution, fiber) {
const currentSpan = fiber.currentSpan;
if (currentSpan === undefined || !isSentrySpan(currentSpan)) {
return execution();
}
return withActiveSpan(currentSpan.sentrySpan, execution);
},
});
/**
* Effect Layer that sets up the Sentry tracer for Effect spans.
*/
export const SentryEffectTracer = makeSentryTracer();