@encore.dev/sdk 0.0.0 → 2.0.0-beta.5
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/LICENSE +362 -0
- package/README.md +122 -2
- package/app_meta.test.ts +134 -0
- package/app_meta.ts +186 -0
- package/config/config.test.ts +381 -0
- package/config/config.ts +178 -0
- package/config/mod.ts +14 -0
- package/config/secrets.ts +147 -0
- package/config/value.ts +243 -0
- package/cron/mod.ts +23 -0
- package/dist/app_meta.d.ts +93 -0
- package/dist/app_meta.js +65 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +105 -0
- package/dist/config/config.js +87 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +4 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +92 -0
- package/dist/config/secrets.js +80 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +51 -0
- package/dist/config/value.js +178 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +22 -0
- package/dist/cron/mod.js +9 -0
- package/dist/cron/mod.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +50 -0
- package/dist/internal/otel/backend.js +2 -0
- package/dist/internal/otel/backend.js.map +1 -0
- package/dist/internal/otel/context.d.ts +10 -0
- package/dist/internal/otel/context.js +34 -0
- package/dist/internal/otel/context.js.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +22 -0
- package/dist/internal/otel/context_manager.js +120 -0
- package/dist/internal/otel/context_manager.js.map +1 -0
- package/dist/internal/otel/encode.d.ts +112 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.js +9 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +73 -0
- package/dist/internal/otel/meter.js +265 -0
- package/dist/internal/otel/meter.js.map +1 -0
- package/dist/internal/otel/propagator.d.ts +14 -0
- package/dist/internal/otel/propagator.js +61 -0
- package/dist/internal/otel/propagator.js.map +1 -0
- package/dist/internal/otel/provider.d.ts +9 -0
- package/dist/internal/otel/provider.js +19 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/request_context.d.ts +11 -0
- package/dist/internal/otel/request_context.js +20 -0
- package/dist/internal/otel/request_context.js.map +1 -0
- package/dist/internal/otel/span.d.ts +61 -0
- package/dist/internal/otel/span.js +206 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +15 -0
- package/dist/internal/otel/tracer.js +65 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- package/dist/internal/reqtrack/mod.d.ts +6 -0
- package/dist/internal/reqtrack/mod.js +28 -0
- package/dist/internal/reqtrack/mod.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +11 -0
- package/dist/internal/runtime/meter.js +22 -0
- package/dist/internal/runtime/meter.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +12 -0
- package/dist/internal/runtime/mod.js +28 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +122 -0
- package/dist/internal/runtime/napi/napi.d.cts +601 -0
- package/dist/internal/runtime/shutdown.d.ts +13 -0
- package/dist/internal/runtime/shutdown.js +56 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +9 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +39 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +123 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +19 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +9 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +315 -0
- package/dist/metrics/mod.js +488 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +9 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +3 -0
- package/dist/pubsub/encoding.js +10 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +6 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +12 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +128 -0
- package/dist/pubsub/subscription.js +39 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +148 -0
- package/dist/pubsub/topic.js +164 -0
- package/dist/pubsub/topic.js.map +1 -0
- package/dist/req_meta.d.ts +47 -0
- package/dist/req_meta.js +26 -0
- package/dist/req_meta.js.map +1 -0
- package/dist/services/index.d.ts +12 -0
- package/dist/services/index.js +22 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +23 -0
- package/dist/shutdown/mod.js +24 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +216 -0
- package/dist/storage/objects/bucket.js +123 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +14 -0
- package/dist/storage/objects/error.js +81 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +4 -0
- package/dist/storage/objects/mod.js +3 -0
- package/dist/storage/objects/mod.js.map +1 -0
- package/dist/storage/objects/refs.d.ts +31 -0
- package/dist/storage/objects/refs.js +20 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +295 -0
- package/dist/storage/postgres/database.js +378 -0
- package/dist/storage/postgres/database.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +2 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +573 -0
- package/dist/storage/redis/commands.js +637 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +302 -0
- package/dist/storage/redis/database.js +336 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +88 -0
- package/dist/storage/redis/mod.js +84 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.js +11 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +89 -0
- package/dist/storage/redis/options.js +53 -0
- package/dist/storage/redis/options.js.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +53 -0
- package/dist/storage/redis/pubsub.js +88 -0
- package/dist/storage/redis/pubsub.js.map +1 -0
- package/dist/storage/redis/reply.d.ts +256 -0
- package/dist/storage/redis/reply.js +572 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +11 -0
- package/dist/storage/redis/scan.js +76 -0
- package/dist/storage/redis/scan.js.map +1 -0
- package/dist/storage/redis/script.d.ts +91 -0
- package/dist/storage/redis/script.js +143 -0
- package/dist/storage/redis/script.js.map +1 -0
- package/dist/storage/redis/target.d.ts +47 -0
- package/dist/storage/redis/target.js +82 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +105 -0
- package/dist/storage/redis/wire.js +333 -0
- package/dist/storage/redis/wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +42 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.map +1 -0
- package/internal/otel/backend.ts +60 -0
- package/internal/otel/context.test.ts +70 -0
- package/internal/otel/context.ts +48 -0
- package/internal/otel/context_manager.test.ts +154 -0
- package/internal/otel/context_manager.ts +141 -0
- package/internal/otel/encode.test.ts +266 -0
- package/internal/otel/encode.ts +408 -0
- package/internal/otel/index.ts +20 -0
- package/internal/otel/meter.test.ts +231 -0
- package/internal/otel/meter.ts +332 -0
- package/internal/otel/propagator.test.ts +105 -0
- package/internal/otel/propagator.ts +68 -0
- package/internal/otel/provider.ts +23 -0
- package/internal/otel/request_context.ts +32 -0
- package/internal/otel/span.ts +252 -0
- package/internal/otel/tracer.test.ts +352 -0
- package/internal/otel/tracer.ts +104 -0
- package/internal/reqtrack/mod.ts +39 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +41 -0
- package/internal/runtime/napi/napi.cjs +122 -0
- package/internal/runtime/napi/napi.d.cts +601 -0
- package/internal/runtime/shutdown.test.ts +248 -0
- package/internal/runtime/shutdown.ts +64 -0
- package/internal/utils/constraints.ts +12 -0
- package/log/index.ts +107 -0
- package/log/logger.test.ts +194 -0
- package/log/logger.ts +308 -0
- package/log/runtime.test.ts +65 -0
- package/log/runtime.ts +40 -0
- package/log/stderr.test.ts +23 -0
- package/log/stderr.ts +24 -0
- package/metrics/export.test.ts +337 -0
- package/metrics/mod.test.ts +180 -0
- package/metrics/mod.ts +619 -0
- package/mod.ts +22 -0
- package/otel.test.ts +84 -0
- package/otel.ts +17 -0
- package/package.json +121 -5
- package/pubsub/encoding.ts +8 -0
- package/pubsub/mod.ts +14 -0
- package/pubsub/pubsub.test.ts +186 -0
- package/pubsub/refs.ts +22 -0
- package/pubsub/subscription.ts +180 -0
- package/pubsub/topic.test.ts +121 -0
- package/pubsub/topic.ts +212 -0
- package/req_meta.ts +75 -0
- package/services/index.test.ts +30 -0
- package/services/index.ts +21 -0
- package/shutdown/mod.ts +24 -0
- package/storage/objects/bucket.ts +338 -0
- package/storage/objects/error.ts +94 -0
- package/storage/objects/mod.ts +35 -0
- package/storage/objects/refs.ts +69 -0
- package/storage/postgres/database.integration.test.ts +175 -0
- package/storage/postgres/database.test.ts +241 -0
- package/storage/postgres/database.ts +497 -0
- package/storage/postgres/mod.ts +15 -0
- package/storage/redis/commands.ts +1221 -0
- package/storage/redis/database.ts +466 -0
- package/storage/redis/mod.ts +135 -0
- package/storage/redis/native.ts +11 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +706 -0
- package/storage/redis/reply.ts +705 -0
- package/storage/redis/runtime.test.ts +233 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +131 -0
- package/storage/redis/wire.ts +369 -0
- package/types/mod.ts +92 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTraceState,
|
|
3
|
+
diag,
|
|
4
|
+
isSpanContextValid,
|
|
5
|
+
SpanStatusCode,
|
|
6
|
+
trace,
|
|
7
|
+
TraceFlags,
|
|
8
|
+
type Attributes,
|
|
9
|
+
type AttributeValue,
|
|
10
|
+
type Context,
|
|
11
|
+
type Exception,
|
|
12
|
+
type Link,
|
|
13
|
+
type Span as ApiSpan,
|
|
14
|
+
type SpanContext,
|
|
15
|
+
type SpanKind,
|
|
16
|
+
type SpanStatus,
|
|
17
|
+
type TimeInput,
|
|
18
|
+
} from '@opentelemetry/api';
|
|
19
|
+
import type {Backend} from './backend';
|
|
20
|
+
import {
|
|
21
|
+
ctxBytes,
|
|
22
|
+
encodeEnd,
|
|
23
|
+
isTimeInput,
|
|
24
|
+
MAX_COUNT,
|
|
25
|
+
nowUnixNanos,
|
|
26
|
+
timeToUnixNanos,
|
|
27
|
+
type PackedEvent,
|
|
28
|
+
type Scope,
|
|
29
|
+
} from './encode';
|
|
30
|
+
|
|
31
|
+
export interface SpanInit {
|
|
32
|
+
backend: Backend;
|
|
33
|
+
scope: Scope;
|
|
34
|
+
/** 25 bytes. */
|
|
35
|
+
ctxBytes: Buffer;
|
|
36
|
+
tracestate: string;
|
|
37
|
+
name: string;
|
|
38
|
+
kind: SpanKind;
|
|
39
|
+
/** 8 bytes; absent for a root. */
|
|
40
|
+
parentSpanId?: Buffer;
|
|
41
|
+
startUnixNanos: bigint;
|
|
42
|
+
startMonoNanos: bigint;
|
|
43
|
+
attributes?: Attributes;
|
|
44
|
+
links?: Link[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A recording span: identity plus accumulated data; `end()` crosses to the
|
|
48
|
+
* runtime once. */
|
|
49
|
+
export class Span implements ApiSpan {
|
|
50
|
+
readonly ctxBytes: Buffer;
|
|
51
|
+
private readonly backend: Backend;
|
|
52
|
+
private readonly scope: Scope;
|
|
53
|
+
private readonly tracestate: string;
|
|
54
|
+
private readonly kind: SpanKind;
|
|
55
|
+
private readonly parentSpanId?: Buffer;
|
|
56
|
+
private readonly startUnixNanos: bigint;
|
|
57
|
+
private readonly startMonoNanos: bigint;
|
|
58
|
+
private ctx?: SpanContext;
|
|
59
|
+
private name: string;
|
|
60
|
+
private attrs: Attributes;
|
|
61
|
+
private links: Link[];
|
|
62
|
+
private events: PackedEvent[] = [];
|
|
63
|
+
private status: SpanStatus = {code: SpanStatusCode.UNSET};
|
|
64
|
+
private ended = false;
|
|
65
|
+
private readonly sampled: boolean;
|
|
66
|
+
|
|
67
|
+
constructor(init: SpanInit) {
|
|
68
|
+
this.backend = init.backend;
|
|
69
|
+
this.scope = init.scope;
|
|
70
|
+
this.ctxBytes = init.ctxBytes;
|
|
71
|
+
this.tracestate = init.tracestate;
|
|
72
|
+
this.name = init.name;
|
|
73
|
+
this.kind = init.kind;
|
|
74
|
+
this.parentSpanId = init.parentSpanId;
|
|
75
|
+
this.startUnixNanos = init.startUnixNanos;
|
|
76
|
+
this.startMonoNanos = init.startMonoNanos;
|
|
77
|
+
this.sampled = (init.ctxBytes[24]! & TraceFlags.SAMPLED) !== 0;
|
|
78
|
+
this.attrs = {};
|
|
79
|
+
if (init.attributes) this.setAttributes(init.attributes);
|
|
80
|
+
this.links = init.links ? init.links.slice(0, MAX_COUNT) : [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
spanContext(): SpanContext {
|
|
84
|
+
this.ctx ??= {
|
|
85
|
+
traceId: this.ctxBytes.toString('hex', 0, 16),
|
|
86
|
+
spanId: this.ctxBytes.toString('hex', 16, 24),
|
|
87
|
+
traceFlags: this.ctxBytes[24]!,
|
|
88
|
+
traceState: this.tracestate
|
|
89
|
+
? createTraceState(this.tracestate)
|
|
90
|
+
: undefined,
|
|
91
|
+
isRemote: false,
|
|
92
|
+
};
|
|
93
|
+
return this.ctx;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** False once ended, and for a span of an unsampled trace, which the
|
|
97
|
+
* runtime does not record. */
|
|
98
|
+
isRecording(): boolean {
|
|
99
|
+
return this.sampled && !this.ended;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private mutable(op: string): boolean {
|
|
103
|
+
if (this.ended) {
|
|
104
|
+
diag.warn(`encore: ${op} on ended span ${this.name}`);
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
setAttribute(key: string, value: AttributeValue): this {
|
|
111
|
+
if (!this.mutable('setAttribute')) return this;
|
|
112
|
+
if (key.length === 0 || value == null) return this;
|
|
113
|
+
if (!(key in this.attrs) && Object.keys(this.attrs).length >= MAX_COUNT) {
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
this.attrs[key] = value;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
setAttributes(attributes: Attributes): this {
|
|
121
|
+
for (const [k, v] of Object.entries(attributes)) {
|
|
122
|
+
if (v !== undefined) this.setAttribute(k, v);
|
|
123
|
+
}
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
addEvent(
|
|
128
|
+
name: string,
|
|
129
|
+
attributesOrStartTime?: Attributes | TimeInput,
|
|
130
|
+
startTime?: TimeInput,
|
|
131
|
+
): this {
|
|
132
|
+
if (!this.mutable('addEvent')) return this;
|
|
133
|
+
if (this.events.length >= MAX_COUNT) return this;
|
|
134
|
+
let attributes: Attributes | undefined;
|
|
135
|
+
if (isTimeInput(attributesOrStartTime)) {
|
|
136
|
+
startTime = attributesOrStartTime;
|
|
137
|
+
} else {
|
|
138
|
+
attributes = attributesOrStartTime;
|
|
139
|
+
}
|
|
140
|
+
this.events.push({
|
|
141
|
+
name,
|
|
142
|
+
attributes,
|
|
143
|
+
timeUnixNanos:
|
|
144
|
+
startTime === undefined ? nowUnixNanos() : timeToUnixNanos(startTime),
|
|
145
|
+
});
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
addLink(link: Link): this {
|
|
150
|
+
if (!this.mutable('addLink')) return this;
|
|
151
|
+
if (this.links.length < MAX_COUNT) this.links.push(link);
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
addLinks(links: Link[]): this {
|
|
156
|
+
for (const l of links) this.addLink(l);
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
setStatus(status: SpanStatus): this {
|
|
161
|
+
if (!this.mutable('setStatus')) return this;
|
|
162
|
+
// OK is final; UNSET never overrides.
|
|
163
|
+
if (status.code === SpanStatusCode.UNSET) return this;
|
|
164
|
+
if (this.status.code === SpanStatusCode.OK) return this;
|
|
165
|
+
this.status =
|
|
166
|
+
status.code === SpanStatusCode.ERROR
|
|
167
|
+
? {code: status.code, message: status.message}
|
|
168
|
+
: {code: status.code};
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
updateName(name: string): this {
|
|
173
|
+
if (!this.mutable('updateName')) return this;
|
|
174
|
+
this.name = name;
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
recordException(exception: Exception, time?: TimeInput): void {
|
|
179
|
+
const attributes: Attributes = {};
|
|
180
|
+
if (typeof exception === 'string') {
|
|
181
|
+
attributes['exception.message'] = exception;
|
|
182
|
+
} else if (exception) {
|
|
183
|
+
if (exception.code !== undefined) {
|
|
184
|
+
attributes['exception.type'] = String(exception.code);
|
|
185
|
+
} else if (exception.name) {
|
|
186
|
+
attributes['exception.type'] = exception.name;
|
|
187
|
+
}
|
|
188
|
+
if (exception.message) {
|
|
189
|
+
attributes['exception.message'] = exception.message;
|
|
190
|
+
}
|
|
191
|
+
if (exception.stack) attributes['exception.stacktrace'] = exception.stack;
|
|
192
|
+
}
|
|
193
|
+
this.addEvent('exception', attributes, time);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
end(endTime?: TimeInput): void {
|
|
197
|
+
if (this.ended) {
|
|
198
|
+
diag.warn(`encore: end called twice on span ${this.name}`);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
this.ended = true;
|
|
202
|
+
try {
|
|
203
|
+
this.backend.endSpan(
|
|
204
|
+
encodeEnd({
|
|
205
|
+
scope: this.scope,
|
|
206
|
+
name: this.name,
|
|
207
|
+
kind: this.kind,
|
|
208
|
+
statusCode: this.status.code,
|
|
209
|
+
statusMessage: this.status.message,
|
|
210
|
+
tracestate: this.tracestate,
|
|
211
|
+
ctx: this.ctxBytes,
|
|
212
|
+
parentSpanId: this.parentSpanId,
|
|
213
|
+
startUnixNanos: this.startUnixNanos,
|
|
214
|
+
startMonoNanos: this.startMonoNanos,
|
|
215
|
+
endUnixNanos: endTime === undefined ? 0n : timeToUnixNanos(endTime),
|
|
216
|
+
attributes: this.attrs,
|
|
217
|
+
events: this.events,
|
|
218
|
+
links: this.links,
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
diag.error('encore: failed to end span', err);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The 25-byte context of `span`; `undefined` when invalid. */
|
|
228
|
+
export function spanCtxBytes(span: ApiSpan): Buffer | undefined {
|
|
229
|
+
if (span instanceof Span) return span.ctxBytes;
|
|
230
|
+
const sc = span.spanContext();
|
|
231
|
+
return isSpanContextValid(sc) ? ctxBytes(sc) : undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The 25-byte context of the span in `ctx`, if any. */
|
|
235
|
+
export function contextCtxBytes(ctx: Context): Buffer | undefined {
|
|
236
|
+
const span = trace.getSpan(ctx);
|
|
237
|
+
return span ? spanCtxBytes(span) : undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The 25-byte context of the span in `ctx` when it is sampled: what a
|
|
241
|
+
* measurement's exemplar names. */
|
|
242
|
+
export function sampledCtxBytes(ctx: Context): Buffer | undefined {
|
|
243
|
+
const span = trace.getSpan(ctx);
|
|
244
|
+
if (span === undefined) return undefined;
|
|
245
|
+
if (span instanceof Span) {
|
|
246
|
+
return span.ctxBytes[24]! & TraceFlags.SAMPLED ? span.ctxBytes : undefined;
|
|
247
|
+
}
|
|
248
|
+
const sc = span.spanContext();
|
|
249
|
+
return sc.traceFlags & TraceFlags.SAMPLED && isSpanContextValid(sc)
|
|
250
|
+
? ctxBytes(sc)
|
|
251
|
+
: undefined;
|
|
252
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, test} from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
context,
|
|
4
|
+
createTraceState,
|
|
5
|
+
ROOT_CONTEXT,
|
|
6
|
+
SpanKind,
|
|
7
|
+
SpanStatusCode,
|
|
8
|
+
trace,
|
|
9
|
+
type Span as ApiSpan,
|
|
10
|
+
} from '@opentelemetry/api';
|
|
11
|
+
import {ATTR_LEN, EVENT_LEN, HEADER_LEN, ITEM_LEN, LINK_LEN} from './encode';
|
|
12
|
+
import {
|
|
13
|
+
activeCtxBytes,
|
|
14
|
+
Span,
|
|
15
|
+
ContextManager,
|
|
16
|
+
getRequestFromContext,
|
|
17
|
+
TracerProvider,
|
|
18
|
+
withRequestContext,
|
|
19
|
+
type Backend,
|
|
20
|
+
} from './index';
|
|
21
|
+
|
|
22
|
+
interface StartCall {
|
|
23
|
+
name: string;
|
|
24
|
+
kind: number;
|
|
25
|
+
parent?: Buffer;
|
|
26
|
+
tracestate?: string;
|
|
27
|
+
startUnixNanos?: bigint;
|
|
28
|
+
packed: Buffer;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Deterministic ids: span id n = 00..0n, a root's trace id = n repeated.
|
|
32
|
+
* A child keeps its parent's flags; a root gets `rootFlags`. */
|
|
33
|
+
class FakeBackend implements Backend {
|
|
34
|
+
n = 0;
|
|
35
|
+
rootFlags = 1;
|
|
36
|
+
starts: StartCall[] = [];
|
|
37
|
+
ends: Buffer[] = [];
|
|
38
|
+
|
|
39
|
+
startSpan(
|
|
40
|
+
name: string,
|
|
41
|
+
kind: number,
|
|
42
|
+
parent: Buffer | undefined,
|
|
43
|
+
tracestate: string | undefined,
|
|
44
|
+
startUnixNanos: bigint | undefined,
|
|
45
|
+
packed: Buffer,
|
|
46
|
+
) {
|
|
47
|
+
this.starts.push({
|
|
48
|
+
name,
|
|
49
|
+
kind,
|
|
50
|
+
parent,
|
|
51
|
+
tracestate,
|
|
52
|
+
startUnixNanos,
|
|
53
|
+
packed,
|
|
54
|
+
});
|
|
55
|
+
const n = ++this.n;
|
|
56
|
+
const ctx = Buffer.alloc(25);
|
|
57
|
+
if (parent) parent.copy(ctx, 0, 0, 16);
|
|
58
|
+
else ctx.fill(n, 0, 16);
|
|
59
|
+
ctx[23] = n;
|
|
60
|
+
ctx[24] = parent ? parent[24]! : this.rootFlags;
|
|
61
|
+
return {
|
|
62
|
+
ctx,
|
|
63
|
+
tracestate: tracestate ?? '',
|
|
64
|
+
startUnixNanos: startUnixNanos ?? 1000n,
|
|
65
|
+
startMonoNanos: 7n,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
endSpan(packed: Buffer) {
|
|
70
|
+
this.ends.push(packed);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const backend = new FakeBackend();
|
|
75
|
+
// A no-op when another test file registered one first.
|
|
76
|
+
context.setGlobalContextManager(new ContextManager());
|
|
77
|
+
trace.setGlobalTracerProvider(new TracerProvider(backend));
|
|
78
|
+
const tracer = trace.getTracer('test');
|
|
79
|
+
|
|
80
|
+
function tableOff(buf: Buffer): number {
|
|
81
|
+
return (
|
|
82
|
+
HEADER_LEN +
|
|
83
|
+
buf.readUInt32LE(8) * ATTR_LEN +
|
|
84
|
+
buf.readUInt32LE(12) * ITEM_LEN +
|
|
85
|
+
buf.readUInt32LE(16) * EVENT_LEN +
|
|
86
|
+
buf.readUInt32LE(20) * LINK_LEN
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function str(buf: Buffer, off: number): string {
|
|
91
|
+
const start = tableOff(buf) + buf.readUInt32LE(off);
|
|
92
|
+
return buf.subarray(start, start + buf.readUInt32LE(off + 4)).toString();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
backend.n = 0;
|
|
97
|
+
backend.rootFlags = 1;
|
|
98
|
+
backend.starts = [];
|
|
99
|
+
backend.ends = [];
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('Tracer', () => {
|
|
103
|
+
test("spans carry the tracer's scope on start and end", () => {
|
|
104
|
+
const lib = trace.getTracer('lib', '1.2.3');
|
|
105
|
+
expect(trace.getTracer('lib', '1.2.3')).toBe(lib);
|
|
106
|
+
expect(trace.getTracer('lib')).not.toBe(lib);
|
|
107
|
+
lib.startSpan('s').end();
|
|
108
|
+
for (const packed of [backend.starts[0]!.packed, backend.ends[0]!]) {
|
|
109
|
+
expect(str(packed, 104)).toBe('lib');
|
|
110
|
+
expect(str(packed, 112)).toBe('1.2.3');
|
|
111
|
+
}
|
|
112
|
+
tracer.startSpan('t').end();
|
|
113
|
+
expect(str(backend.ends[1]!, 104)).toBe('test');
|
|
114
|
+
expect(str(backend.ends[1]!, 112)).toBe('');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("child span carries the parent's trace id and span id", () => {
|
|
118
|
+
const parent = tracer.startSpan('parent');
|
|
119
|
+
expect(parent).toBeInstanceOf(Span);
|
|
120
|
+
expect(backend.starts[0]!.parent).toBeUndefined();
|
|
121
|
+
const child = tracer.startSpan(
|
|
122
|
+
'child',
|
|
123
|
+
{kind: SpanKind.CLIENT},
|
|
124
|
+
trace.setSpan(ROOT_CONTEXT, parent),
|
|
125
|
+
);
|
|
126
|
+
const p = parent.spanContext();
|
|
127
|
+
const c = child.spanContext();
|
|
128
|
+
expect(c.traceId).toBe(p.traceId);
|
|
129
|
+
expect(c.spanId).not.toBe(p.spanId);
|
|
130
|
+
expect(backend.starts[1]!.parent!.toString('hex', 16, 24)).toBe(p.spanId);
|
|
131
|
+
|
|
132
|
+
child.end();
|
|
133
|
+
const packed = backend.ends[0]!;
|
|
134
|
+
expect(str(packed, 24)).toBe('child');
|
|
135
|
+
expect(packed[1]).toBe(SpanKind.CLIENT);
|
|
136
|
+
expect(packed[3]).toBe(1);
|
|
137
|
+
expect(packed.toString('hex', 48, 64)).toBe(p.traceId);
|
|
138
|
+
expect(packed.toString('hex', 64, 72)).toBe(c.spanId);
|
|
139
|
+
expect(packed.toString('hex', 72, 80)).toBe(p.spanId);
|
|
140
|
+
expect(packed.readBigUInt64LE(80)).toBe(1000n);
|
|
141
|
+
expect(packed.readBigUInt64LE(88)).toBe(7n);
|
|
142
|
+
parent.end();
|
|
143
|
+
expect(backend.ends[1]!.subarray(72, 80).every(b => b === 0)).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('startActiveSpan makes the span active inside the callback', async () => {
|
|
147
|
+
await tracer.startActiveSpan('outer', async (outer: ApiSpan) => {
|
|
148
|
+
expect(trace.getActiveSpan()).toBe(outer);
|
|
149
|
+
await new Promise(r => setTimeout(r, 1));
|
|
150
|
+
expect(trace.getActiveSpan()).toBe(outer);
|
|
151
|
+
const child = tracer.startSpan('inner');
|
|
152
|
+
expect(child.spanContext().traceId).toBe(outer.spanContext().traceId);
|
|
153
|
+
expect(activeCtxBytes()).toBe((outer as Span).ctxBytes);
|
|
154
|
+
outer.end();
|
|
155
|
+
});
|
|
156
|
+
expect(trace.getActiveSpan()).toBeUndefined();
|
|
157
|
+
expect(activeCtxBytes()).toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('startActiveSpan overloads', () => {
|
|
161
|
+
const opts = tracer.startActiveSpan(
|
|
162
|
+
'a',
|
|
163
|
+
{kind: SpanKind.SERVER},
|
|
164
|
+
(s: ApiSpan) => {
|
|
165
|
+
s.end();
|
|
166
|
+
return 1;
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
expect(opts).toBe(1);
|
|
170
|
+
expect(backend.starts[0]!.kind).toBe(SpanKind.SERVER);
|
|
171
|
+
const parent = tracer.startSpan('p');
|
|
172
|
+
tracer.startActiveSpan(
|
|
173
|
+
'b',
|
|
174
|
+
{},
|
|
175
|
+
trace.setSpan(ROOT_CONTEXT, parent),
|
|
176
|
+
(s: ApiSpan) => {
|
|
177
|
+
expect(s.spanContext().traceId).toBe(parent.spanContext().traceId);
|
|
178
|
+
},
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('root ignores the active span', () => {
|
|
183
|
+
tracer.startActiveSpan('a', (a: ApiSpan) => {
|
|
184
|
+
const b = tracer.startSpan('b', {root: true});
|
|
185
|
+
expect(backend.starts[1]!.parent).toBeUndefined();
|
|
186
|
+
expect(b.spanContext().traceId).not.toBe(a.spanContext().traceId);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('a non-Encore parent is passed as bytes with its tracestate', () => {
|
|
191
|
+
const remote = {
|
|
192
|
+
traceId: '0102030405060708090a0b0c0d0e0f10',
|
|
193
|
+
spanId: '1112131415161718',
|
|
194
|
+
traceFlags: 1,
|
|
195
|
+
traceState: createTraceState('vendor=x'),
|
|
196
|
+
isRemote: true,
|
|
197
|
+
};
|
|
198
|
+
const ctx = trace.setSpanContext(ROOT_CONTEXT, remote);
|
|
199
|
+
const span = context.with(ctx, () => tracer.startSpan('s'));
|
|
200
|
+
const start = backend.starts[0]!;
|
|
201
|
+
expect(start.parent!.toString('hex')).toBe(
|
|
202
|
+
remote.traceId + remote.spanId + '01',
|
|
203
|
+
);
|
|
204
|
+
expect(start.tracestate).toBe('vendor=x');
|
|
205
|
+
expect(span.spanContext().traceId).toBe(remote.traceId);
|
|
206
|
+
expect(span.spanContext().traceState!.serialize()).toBe('vendor=x');
|
|
207
|
+
span.end();
|
|
208
|
+
expect(backend.ends[0]!.toString('hex', 72, 80)).toBe(remote.spanId);
|
|
209
|
+
expect(str(backend.ends[0]!, 40)).toBe('vendor=x');
|
|
210
|
+
expect(context.with(ctx, activeCtxBytes)!.toString('hex')).toBe(
|
|
211
|
+
remote.traceId + remote.spanId + '01',
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('start attributes, links, and time cross at start', () => {
|
|
216
|
+
const link = {
|
|
217
|
+
context: {
|
|
218
|
+
traceId: 'a'.repeat(32),
|
|
219
|
+
spanId: 'b'.repeat(16),
|
|
220
|
+
traceFlags: 1,
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
tracer.startSpan('s', {
|
|
224
|
+
attributes: {a: 1},
|
|
225
|
+
links: [link],
|
|
226
|
+
startTime: [2, 5],
|
|
227
|
+
});
|
|
228
|
+
const start = backend.starts[0]!;
|
|
229
|
+
expect(start.startUnixNanos).toBe(2_000_000_005n);
|
|
230
|
+
expect(start.packed.readUInt32LE(4)).toBe(1);
|
|
231
|
+
expect(start.packed.readUInt32LE(20)).toBe(1);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('end twice packs once', () => {
|
|
235
|
+
const span = tracer.startSpan('s');
|
|
236
|
+
expect(span.isRecording()).toBe(true);
|
|
237
|
+
span.end();
|
|
238
|
+
span.setAttribute('late', 1);
|
|
239
|
+
span.end();
|
|
240
|
+
expect(backend.ends.length).toBe(1);
|
|
241
|
+
expect(span.isRecording()).toBe(false);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('recordException yields an event', () => {
|
|
245
|
+
const span = tracer.startSpan('s', {attributes: {a: 1}});
|
|
246
|
+
span.setAttributes({b: 'x'});
|
|
247
|
+
span.recordException(new TypeError('bad'));
|
|
248
|
+
span.setStatus({code: SpanStatusCode.ERROR, message: 'bad'});
|
|
249
|
+
span.setStatus({code: SpanStatusCode.UNSET});
|
|
250
|
+
span.end([3, 0]);
|
|
251
|
+
const packed = backend.ends[0]!;
|
|
252
|
+
expect(packed[2]).toBe(SpanStatusCode.ERROR);
|
|
253
|
+
expect(str(packed, 32)).toBe('bad');
|
|
254
|
+
expect(packed.readBigUInt64LE(96)).toBe(3_000_000_000n);
|
|
255
|
+
expect(packed.readUInt32LE(4)).toBe(2);
|
|
256
|
+
expect(packed.readUInt32LE(16)).toBe(1);
|
|
257
|
+
expect(packed.readUInt32LE(8)).toBe(5);
|
|
258
|
+
const event = HEADER_LEN + 5 * ATTR_LEN;
|
|
259
|
+
expect(str(packed, event)).toBe('exception');
|
|
260
|
+
const keys = [2, 3, 4].map(i => str(packed, HEADER_LEN + i * ATTR_LEN));
|
|
261
|
+
expect(keys).toEqual([
|
|
262
|
+
'exception.type',
|
|
263
|
+
'exception.message',
|
|
264
|
+
'exception.stacktrace',
|
|
265
|
+
]);
|
|
266
|
+
expect(str(packed, HEADER_LEN + 2 * ATTR_LEN + 16)).toBe('TypeError');
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe('non-recording spans', () => {
|
|
271
|
+
const unsampled = {
|
|
272
|
+
traceId: '0102030405060708090a0b0c0d0e0f10',
|
|
273
|
+
spanId: '1112131415161718',
|
|
274
|
+
traceFlags: 0,
|
|
275
|
+
isRemote: true,
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// The runtime records nothing of a span whose flags are 00; the host
|
|
279
|
+
// crosses as usual.
|
|
280
|
+
test('a span under an unsampled parent is not recording', () => {
|
|
281
|
+
const ctx = trace.setSpanContext(ROOT_CONTEXT, unsampled);
|
|
282
|
+
const span = tracer.startSpan(
|
|
283
|
+
's',
|
|
284
|
+
{attributes: {a: 1}, links: [{context: unsampled}]},
|
|
285
|
+
ctx,
|
|
286
|
+
);
|
|
287
|
+
expect(span.isRecording()).toBe(false);
|
|
288
|
+
const sc = span.spanContext();
|
|
289
|
+
expect(sc.traceId).toBe(unsampled.traceId);
|
|
290
|
+
expect(sc.spanId).not.toBe(unsampled.spanId);
|
|
291
|
+
expect(sc.traceFlags).toBe(0);
|
|
292
|
+
const start = backend.starts[0]!;
|
|
293
|
+
expect(start.parent!.toString('hex')).toBe(
|
|
294
|
+
unsampled.traceId + unsampled.spanId + '00',
|
|
295
|
+
);
|
|
296
|
+
expect(start.packed.readUInt32LE(4)).toBe(1);
|
|
297
|
+
|
|
298
|
+
// Propagation keeps the flag.
|
|
299
|
+
const child = tracer.startSpan('c', {}, trace.setSpan(ROOT_CONTEXT, span));
|
|
300
|
+
expect(child.spanContext().traceId).toBe(unsampled.traceId);
|
|
301
|
+
expect(backend.starts[1]!.parent![24]).toBe(0);
|
|
302
|
+
expect(child.isRecording()).toBe(false);
|
|
303
|
+
child.end();
|
|
304
|
+
span.end();
|
|
305
|
+
expect(backend.ends.length).toBe(2);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('a root the runtime did not sample is not recording', () => {
|
|
309
|
+
backend.rootFlags = 0;
|
|
310
|
+
const span = tracer.startSpan('root', {attributes: {a: 1}});
|
|
311
|
+
expect(span.isRecording()).toBe(false);
|
|
312
|
+
expect(span.spanContext().traceFlags).toBe(0);
|
|
313
|
+
span.end();
|
|
314
|
+
expect(backend.ends.length).toBe(1);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('a span under a sampled parent records', () => {
|
|
318
|
+
const ctx = trace.setSpanContext(ROOT_CONTEXT, {
|
|
319
|
+
...unsampled,
|
|
320
|
+
traceFlags: 1,
|
|
321
|
+
});
|
|
322
|
+
const span = tracer.startSpan('s', {attributes: {a: 1}}, ctx);
|
|
323
|
+
expect(span.isRecording()).toBe(true);
|
|
324
|
+
expect(span.spanContext().traceFlags).toBe(1);
|
|
325
|
+
expect(backend.starts[0]!.packed.readUInt32LE(4)).toBe(1);
|
|
326
|
+
span.end();
|
|
327
|
+
expect(backend.ends.length).toBe(1);
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
describe('request context', () => {
|
|
332
|
+
test('carries the request and its span context', () => {
|
|
333
|
+
const req = {id: 1};
|
|
334
|
+
const sc = {
|
|
335
|
+
traceId: '0102030405060708090a0b0c0d0e0f10',
|
|
336
|
+
spanId: '1112131415161718',
|
|
337
|
+
traceFlags: 1,
|
|
338
|
+
};
|
|
339
|
+
const ctx = withRequestContext(ROOT_CONTEXT, req, sc);
|
|
340
|
+
expect(getRequestFromContext(ctx)).toBe(req);
|
|
341
|
+
expect(trace.getSpanContext(ctx)).toEqual(sc);
|
|
342
|
+
const span = context.with(ctx, () => tracer.startSpan('user'));
|
|
343
|
+
expect(backend.starts[0]!.parent!.toString('hex', 16, 24)).toBe(sc.spanId);
|
|
344
|
+
expect(span.spanContext().traceId).toBe(sc.traceId);
|
|
345
|
+
expect(getRequestFromContext(withRequestContext(ROOT_CONTEXT, req))).toBe(
|
|
346
|
+
req,
|
|
347
|
+
);
|
|
348
|
+
expect(
|
|
349
|
+
trace.getSpanContext(withRequestContext(ROOT_CONTEXT, req)),
|
|
350
|
+
).toBeUndefined();
|
|
351
|
+
});
|
|
352
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isSpanContextValid,
|
|
3
|
+
SpanKind,
|
|
4
|
+
trace,
|
|
5
|
+
type Context,
|
|
6
|
+
type Span as ApiSpan,
|
|
7
|
+
type SpanOptions,
|
|
8
|
+
type Tracer as ApiTracer,
|
|
9
|
+
} from '@opentelemetry/api';
|
|
10
|
+
import type {Backend} from './backend';
|
|
11
|
+
import {activeContext, withContext} from './context';
|
|
12
|
+
import {encodeStart, timeToUnixNanos, type Scope} from './encode';
|
|
13
|
+
import {Span, spanCtxBytes} from './span';
|
|
14
|
+
|
|
15
|
+
/** Every span records under the tracer's scope; the runtime assigns ids
|
|
16
|
+
* and clocks. Spans are parented on, and made active in, the context
|
|
17
|
+
* `activeContext` and `withContext` track. */
|
|
18
|
+
export class Tracer implements ApiTracer {
|
|
19
|
+
constructor(
|
|
20
|
+
private readonly backend: Backend,
|
|
21
|
+
private readonly scope: Scope,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
startSpan(name: string, options: SpanOptions = {}, ctx?: Context): ApiSpan {
|
|
25
|
+
const parentSpan = options.root
|
|
26
|
+
? undefined
|
|
27
|
+
: trace.getSpan(ctx ?? activeContext());
|
|
28
|
+
const parent =
|
|
29
|
+
parentSpan && isSpanContextValid(parentSpan.spanContext())
|
|
30
|
+
? parentSpan
|
|
31
|
+
: undefined;
|
|
32
|
+
const parentBytes = parent && spanCtxBytes(parent);
|
|
33
|
+
const kind = options.kind ?? SpanKind.INTERNAL;
|
|
34
|
+
|
|
35
|
+
const res = this.backend.startSpan(
|
|
36
|
+
name,
|
|
37
|
+
kind,
|
|
38
|
+
parentBytes,
|
|
39
|
+
parent?.spanContext().traceState?.serialize(),
|
|
40
|
+
options.startTime === undefined
|
|
41
|
+
? undefined
|
|
42
|
+
: timeToUnixNanos(options.startTime),
|
|
43
|
+
encodeStart(this.scope, options.attributes, options.links),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return new Span({
|
|
47
|
+
backend: this.backend,
|
|
48
|
+
scope: this.scope,
|
|
49
|
+
ctxBytes: res.ctx,
|
|
50
|
+
tracestate: res.tracestate,
|
|
51
|
+
name,
|
|
52
|
+
kind,
|
|
53
|
+
parentSpanId: parentBytes?.subarray(16, 24),
|
|
54
|
+
startUnixNanos: res.startUnixNanos,
|
|
55
|
+
startMonoNanos: res.startMonoNanos,
|
|
56
|
+
attributes: options.attributes,
|
|
57
|
+
links: options.links,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
startActiveSpan<F extends (span: ApiSpan) => unknown>(
|
|
62
|
+
name: string,
|
|
63
|
+
fn: F,
|
|
64
|
+
): ReturnType<F>;
|
|
65
|
+
startActiveSpan<F extends (span: ApiSpan) => unknown>(
|
|
66
|
+
name: string,
|
|
67
|
+
options: SpanOptions,
|
|
68
|
+
fn: F,
|
|
69
|
+
): ReturnType<F>;
|
|
70
|
+
startActiveSpan<F extends (span: ApiSpan) => unknown>(
|
|
71
|
+
name: string,
|
|
72
|
+
options: SpanOptions,
|
|
73
|
+
ctx: Context,
|
|
74
|
+
fn: F,
|
|
75
|
+
): ReturnType<F>;
|
|
76
|
+
startActiveSpan<F extends (span: ApiSpan) => unknown>(
|
|
77
|
+
name: string,
|
|
78
|
+
a?: SpanOptions | F,
|
|
79
|
+
b?: Context | F,
|
|
80
|
+
c?: F,
|
|
81
|
+
): ReturnType<F> | undefined {
|
|
82
|
+
let options: SpanOptions | undefined;
|
|
83
|
+
let ctx: Context | undefined;
|
|
84
|
+
let fn: F;
|
|
85
|
+
if (typeof a === 'function') {
|
|
86
|
+
fn = a;
|
|
87
|
+
} else if (typeof b === 'function') {
|
|
88
|
+
options = a;
|
|
89
|
+
fn = b;
|
|
90
|
+
} else if (typeof c === 'function') {
|
|
91
|
+
options = a;
|
|
92
|
+
ctx = b;
|
|
93
|
+
fn = c;
|
|
94
|
+
} else {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const parent = ctx ?? activeContext();
|
|
98
|
+
const span = this.startSpan(name, options, parent);
|
|
99
|
+
return withContext(
|
|
100
|
+
trace.setSpan(parent, span),
|
|
101
|
+
() => fn(span) as ReturnType<F>,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|