@encore.dev/sdk 0.0.0 → 2.0.0-beta.4
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 +7 -0
- package/dist/services/index.js +17 -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 +16 -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,332 @@
|
|
|
1
|
+
import {
|
|
2
|
+
diag,
|
|
3
|
+
ValueType,
|
|
4
|
+
type BatchObservableCallback,
|
|
5
|
+
type BatchObservableResult,
|
|
6
|
+
type Context,
|
|
7
|
+
type Counter as ApiCounter,
|
|
8
|
+
type Gauge as ApiGauge,
|
|
9
|
+
type Histogram as ApiHistogram,
|
|
10
|
+
type Meter as ApiMeter,
|
|
11
|
+
type MeterOptions,
|
|
12
|
+
type MeterProvider as ApiMeterProvider,
|
|
13
|
+
type MetricAttributes,
|
|
14
|
+
type MetricOptions,
|
|
15
|
+
type Observable,
|
|
16
|
+
type ObservableCallback,
|
|
17
|
+
type ObservableResult,
|
|
18
|
+
type UpDownCounter as ApiUpDownCounter,
|
|
19
|
+
} from '@opentelemetry/api';
|
|
20
|
+
import type {CollectReply, InstrumentHandle, MeterBackend} from './backend';
|
|
21
|
+
import {activeContext} from './context';
|
|
22
|
+
import {sampledCtxBytes} from './span';
|
|
23
|
+
|
|
24
|
+
/** Instrument kinds, as the runtime numbers them. */
|
|
25
|
+
enum Kind {
|
|
26
|
+
Counter = 0,
|
|
27
|
+
UpDownCounter = 1,
|
|
28
|
+
Histogram = 2,
|
|
29
|
+
Gauge = 3,
|
|
30
|
+
ObservableCounter = 4,
|
|
31
|
+
ObservableUpDownCounter = 5,
|
|
32
|
+
ObservableGauge = 6,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Number types, as the runtime numbers them. */
|
|
36
|
+
enum NumberKind {
|
|
37
|
+
I64 = 0,
|
|
38
|
+
F64 = 2,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One meter per instrumentation scope; one collector for every
|
|
42
|
+
* observable instrument of the thread. */
|
|
43
|
+
export class MeterProvider implements ApiMeterProvider {
|
|
44
|
+
private readonly meters = new Map<string, Meter>();
|
|
45
|
+
private readonly collector: Collector;
|
|
46
|
+
|
|
47
|
+
constructor(private readonly backend: MeterBackend) {
|
|
48
|
+
this.collector = new Collector(backend);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getMeter(name: string, version?: string, options?: MeterOptions): ApiMeter {
|
|
52
|
+
const key = JSON.stringify([name, version ?? '', options?.schemaUrl ?? '']);
|
|
53
|
+
let meter = this.meters.get(key);
|
|
54
|
+
if (!meter) {
|
|
55
|
+
meter = new Meter(this.backend, this.collector, {
|
|
56
|
+
name,
|
|
57
|
+
version,
|
|
58
|
+
schemaUrl: options?.schemaUrl,
|
|
59
|
+
});
|
|
60
|
+
this.meters.set(key, meter);
|
|
61
|
+
}
|
|
62
|
+
return meter;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface MeterScope {
|
|
67
|
+
name: string;
|
|
68
|
+
version?: string;
|
|
69
|
+
schemaUrl?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Each instrument is registered with the runtime when created; a
|
|
73
|
+
* measurement crosses to it once, its attributes as JSON text. */
|
|
74
|
+
export class Meter implements ApiMeter {
|
|
75
|
+
constructor(
|
|
76
|
+
private readonly backend: MeterBackend,
|
|
77
|
+
private readonly collector: Collector,
|
|
78
|
+
private readonly scope: MeterScope,
|
|
79
|
+
) {}
|
|
80
|
+
|
|
81
|
+
createCounter(name: string, options?: MetricOptions): ApiCounter {
|
|
82
|
+
return new Recorder(this.register(Kind.Counter, name, options));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
createUpDownCounter(name: string, options?: MetricOptions): ApiUpDownCounter {
|
|
86
|
+
return new Recorder(this.register(Kind.UpDownCounter, name, options));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
createHistogram(name: string, options?: MetricOptions): ApiHistogram {
|
|
90
|
+
return new Recorder(this.register(Kind.Histogram, name, options));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
createGauge(name: string, options?: MetricOptions): ApiGauge {
|
|
94
|
+
return new Recorder(this.register(Kind.Gauge, name, options));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
createObservableCounter(name: string, options?: MetricOptions): Observable {
|
|
98
|
+
return this.observable(Kind.ObservableCounter, name, options);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
createObservableUpDownCounter(
|
|
102
|
+
name: string,
|
|
103
|
+
options?: MetricOptions,
|
|
104
|
+
): Observable {
|
|
105
|
+
return this.observable(Kind.ObservableUpDownCounter, name, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
createObservableGauge(name: string, options?: MetricOptions): Observable {
|
|
109
|
+
return this.observable(Kind.ObservableGauge, name, options);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
addBatchObservableCallback(
|
|
113
|
+
callback: BatchObservableCallback,
|
|
114
|
+
observables: Observable[],
|
|
115
|
+
): void {
|
|
116
|
+
this.collector.addBatch(callback, observables);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
removeBatchObservableCallback(
|
|
120
|
+
callback: BatchObservableCallback,
|
|
121
|
+
observables: Observable[],
|
|
122
|
+
): void {
|
|
123
|
+
this.collector.removeBatch(callback, observables);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private observable(
|
|
127
|
+
kind: Kind,
|
|
128
|
+
name: string,
|
|
129
|
+
options: MetricOptions | undefined,
|
|
130
|
+
): Observable {
|
|
131
|
+
return new ObservableInstrument(
|
|
132
|
+
this.register(kind, name, options).id,
|
|
133
|
+
this.collector,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private register(
|
|
138
|
+
kind: Kind,
|
|
139
|
+
name: string,
|
|
140
|
+
options: MetricOptions | undefined,
|
|
141
|
+
): InstrumentHandle {
|
|
142
|
+
return this.backend.instrument({
|
|
143
|
+
scopeName: this.scope.name,
|
|
144
|
+
scopeVersion: this.scope.version,
|
|
145
|
+
scopeSchemaUrl: this.scope.schemaUrl,
|
|
146
|
+
kind,
|
|
147
|
+
number:
|
|
148
|
+
options?.valueType === ValueType.INT ? NumberKind.I64 : NumberKind.F64,
|
|
149
|
+
name,
|
|
150
|
+
unit: options?.unit,
|
|
151
|
+
description: options?.description,
|
|
152
|
+
boundaries:
|
|
153
|
+
kind === Kind.Histogram
|
|
154
|
+
? options?.advice?.explicitBucketBoundaries
|
|
155
|
+
: undefined,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A synchronous instrument: counter, up-down counter, histogram, gauge. */
|
|
161
|
+
export class Recorder
|
|
162
|
+
implements ApiCounter, ApiUpDownCounter, ApiHistogram, ApiGauge
|
|
163
|
+
{
|
|
164
|
+
constructor(private readonly handle: InstrumentHandle) {}
|
|
165
|
+
|
|
166
|
+
add(value: number, attributes?: MetricAttributes, ctx?: Context): void {
|
|
167
|
+
this.recordEncoded(
|
|
168
|
+
value,
|
|
169
|
+
attributes === undefined ? undefined : JSON.stringify(attributes),
|
|
170
|
+
ctx,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
record(value: number, attributes?: MetricAttributes, ctx?: Context): void {
|
|
175
|
+
this.add(value, attributes, ctx);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Records `value` with `attrs`, attributes as a JSON object's text. */
|
|
179
|
+
recordEncoded(value: number, attrs?: string, ctx?: Context): void {
|
|
180
|
+
this.handle.record(value, attrs, sampledCtxBytes(ctx ?? activeContext()));
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** An observable instrument: its callbacks run at each collection. */
|
|
185
|
+
class ObservableInstrument implements Observable {
|
|
186
|
+
readonly callbacks: ObservableCallback[] = [];
|
|
187
|
+
|
|
188
|
+
constructor(
|
|
189
|
+
readonly id: number,
|
|
190
|
+
private readonly collector: Collector,
|
|
191
|
+
) {}
|
|
192
|
+
|
|
193
|
+
addCallback(callback: ObservableCallback): void {
|
|
194
|
+
if (this.callbacks.includes(callback)) return;
|
|
195
|
+
this.callbacks.push(callback);
|
|
196
|
+
this.collector.watch(this);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
removeCallback(callback: ObservableCallback): void {
|
|
200
|
+
const i = this.callbacks.indexOf(callback);
|
|
201
|
+
if (i >= 0) this.callbacks.splice(i, 1);
|
|
202
|
+
if (this.callbacks.length === 0) this.collector.unwatch(this);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface Batch {
|
|
207
|
+
callback: BatchObservableCallback;
|
|
208
|
+
observables: Set<Observable>;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Runs every observable callback of the thread, at each collection, and
|
|
212
|
+
* replies with all their observations at once: those of callbacks
|
|
213
|
+
* returning promises once they settle, or at the collection's deadline
|
|
214
|
+
* with those settled by then. */
|
|
215
|
+
class Collector {
|
|
216
|
+
private readonly observables = new Set<ObservableInstrument>();
|
|
217
|
+
private batches: Batch[] = [];
|
|
218
|
+
private added = false;
|
|
219
|
+
|
|
220
|
+
constructor(private readonly backend: MeterBackend) {}
|
|
221
|
+
|
|
222
|
+
watch(o: ObservableInstrument): void {
|
|
223
|
+
this.observables.add(o);
|
|
224
|
+
this.add();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
unwatch(o: ObservableInstrument): void {
|
|
228
|
+
this.observables.delete(o);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
addBatch(callback: BatchObservableCallback, observables: Observable[]) {
|
|
232
|
+
const own = observables.filter(o => o instanceof ObservableInstrument);
|
|
233
|
+
const batch = this.batches.find(b => b.callback === callback);
|
|
234
|
+
if (batch) {
|
|
235
|
+
for (const o of own) batch.observables.add(o);
|
|
236
|
+
} else {
|
|
237
|
+
this.batches.push({callback, observables: new Set(own)});
|
|
238
|
+
}
|
|
239
|
+
this.add();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
removeBatch(callback: BatchObservableCallback, observables: Observable[]) {
|
|
243
|
+
const batch = this.batches.find(b => b.callback === callback);
|
|
244
|
+
if (!batch) return;
|
|
245
|
+
for (const o of observables) batch.observables.delete(o);
|
|
246
|
+
if (batch.observables.size === 0) {
|
|
247
|
+
this.batches = this.batches.filter(b => b !== batch);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Runs every callback; never throws. */
|
|
252
|
+
collect(reply: CollectReply): void {
|
|
253
|
+
const out = new Observations();
|
|
254
|
+
const pending: PromiseLike<unknown>[] = [];
|
|
255
|
+
const run = (f: () => unknown) => {
|
|
256
|
+
try {
|
|
257
|
+
const res = f();
|
|
258
|
+
if (isThenable(res)) pending.push(res);
|
|
259
|
+
} catch (err) {
|
|
260
|
+
diag.error('encore: an observable callback failed', err);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
for (const o of this.observables) {
|
|
264
|
+
const result: ObservableResult = {
|
|
265
|
+
observe: (value, attributes) => out.add(o.id, value, attributes),
|
|
266
|
+
};
|
|
267
|
+
for (const cb of o.callbacks) run(() => cb(result));
|
|
268
|
+
}
|
|
269
|
+
for (const b of this.batches) {
|
|
270
|
+
const result: BatchObservableResult = {
|
|
271
|
+
observe: (metric, value, attributes) => {
|
|
272
|
+
if (b.observables.has(metric)) {
|
|
273
|
+
out.add((metric as ObservableInstrument).id, value, attributes);
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
run(() => b.callback(result));
|
|
278
|
+
}
|
|
279
|
+
if (pending.length === 0) {
|
|
280
|
+
out.send(reply);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
// By the deadline, with what has settled: a promise that never settles
|
|
284
|
+
// holds back no collection.
|
|
285
|
+
const timer = setTimeout(() => out.send(reply), reply.timeout);
|
|
286
|
+
(timer as {unref?: () => void}).unref?.();
|
|
287
|
+
void Promise.allSettled(pending).then(() => {
|
|
288
|
+
clearTimeout(timer);
|
|
289
|
+
out.send(reply);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Adds the collector to the runtime with the first callback. */
|
|
294
|
+
private add(): void {
|
|
295
|
+
if (this.added) return;
|
|
296
|
+
this.added = true;
|
|
297
|
+
this.backend.addCollector(reply => this.collect(reply));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** A collection's observations; closed once sent. */
|
|
302
|
+
class Observations {
|
|
303
|
+
private ids: number[] = [];
|
|
304
|
+
private values: number[] = [];
|
|
305
|
+
private attrs: string[] = [];
|
|
306
|
+
private sent = false;
|
|
307
|
+
|
|
308
|
+
add(id: number, value: number, attributes?: MetricAttributes): void {
|
|
309
|
+
if (this.sent || typeof value !== 'number') return;
|
|
310
|
+
this.ids.push(id);
|
|
311
|
+
this.values.push(value);
|
|
312
|
+
this.attrs.push(attributes === undefined ? '' : JSON.stringify(attributes));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
send(reply: CollectReply): void {
|
|
316
|
+
if (this.sent) return;
|
|
317
|
+
this.sent = true;
|
|
318
|
+
try {
|
|
319
|
+
reply.send(this.ids, this.values, this.attrs);
|
|
320
|
+
} catch (err) {
|
|
321
|
+
diag.error('encore: could not send observations', err);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function isThenable(v: unknown): v is PromiseLike<unknown> {
|
|
327
|
+
return (
|
|
328
|
+
typeof v === 'object' &&
|
|
329
|
+
v !== null &&
|
|
330
|
+
typeof (v as PromiseLike<unknown>).then === 'function'
|
|
331
|
+
);
|
|
332
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {describe, expect, test} from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
createTraceState,
|
|
4
|
+
defaultTextMapGetter,
|
|
5
|
+
defaultTextMapSetter,
|
|
6
|
+
ROOT_CONTEXT,
|
|
7
|
+
trace,
|
|
8
|
+
} from '@opentelemetry/api';
|
|
9
|
+
import {W3CPropagator, formatTraceparent, parseTraceparent} from './propagator';
|
|
10
|
+
|
|
11
|
+
const TRACE = '0af7651916cd43dd8448eb211c80319c';
|
|
12
|
+
const SPAN = 'b7ad6b7169203331';
|
|
13
|
+
|
|
14
|
+
const prop = new W3CPropagator();
|
|
15
|
+
|
|
16
|
+
function extract(carrier: Record<string, string | string[]>) {
|
|
17
|
+
return trace.getSpanContext(
|
|
18
|
+
prop.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('W3CPropagator', () => {
|
|
23
|
+
test('inject then extract', () => {
|
|
24
|
+
const ctx = trace.setSpanContext(ROOT_CONTEXT, {
|
|
25
|
+
traceId: TRACE,
|
|
26
|
+
spanId: SPAN,
|
|
27
|
+
traceFlags: 1,
|
|
28
|
+
traceState: createTraceState('vendor=abc,other=x'),
|
|
29
|
+
});
|
|
30
|
+
const carrier: Record<string, string> = {};
|
|
31
|
+
prop.inject(ctx, carrier, defaultTextMapSetter);
|
|
32
|
+
expect(carrier).toEqual({
|
|
33
|
+
traceparent: `00-${TRACE}-${SPAN}-01`,
|
|
34
|
+
tracestate: 'vendor=abc,other=x',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const sc = extract(carrier)!;
|
|
38
|
+
expect(sc.traceId).toBe(TRACE);
|
|
39
|
+
expect(sc.spanId).toBe(SPAN);
|
|
40
|
+
expect(sc.traceFlags).toBe(1);
|
|
41
|
+
expect(sc.isRemote).toBe(true);
|
|
42
|
+
expect(sc.traceState!.serialize()).toBe('vendor=abc,other=x');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('keeps foreign tracestate members', () => {
|
|
46
|
+
const sc = extract({
|
|
47
|
+
traceparent: `00-${TRACE}-${SPAN}-01`,
|
|
48
|
+
tracestate: 'rojo=00f067aa0ba902b7,congo=t61rcWkgMzE',
|
|
49
|
+
})!;
|
|
50
|
+
expect(sc.traceState!.get('rojo')).toBe('00f067aa0ba902b7');
|
|
51
|
+
expect(sc.traceState!.get('congo')).toBe('t61rcWkgMzE');
|
|
52
|
+
const carrier: Record<string, string> = {};
|
|
53
|
+
prop.inject(
|
|
54
|
+
trace.setSpanContext(ROOT_CONTEXT, sc),
|
|
55
|
+
carrier,
|
|
56
|
+
defaultTextMapSetter,
|
|
57
|
+
);
|
|
58
|
+
expect(carrier.tracestate).toBe('rojo=00f067aa0ba902b7,congo=t61rcWkgMzE');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('nothing injected without a valid span context', () => {
|
|
62
|
+
const carrier: Record<string, string> = {};
|
|
63
|
+
prop.inject(ROOT_CONTEXT, carrier, defaultTextMapSetter);
|
|
64
|
+
expect(carrier).toEqual({});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('rejects invalid traceparent', () => {
|
|
68
|
+
for (const tp of [
|
|
69
|
+
'',
|
|
70
|
+
'garbage',
|
|
71
|
+
`00-${TRACE}-${SPAN}`,
|
|
72
|
+
`00-${TRACE.toUpperCase()}-${SPAN}-01`,
|
|
73
|
+
`00-${'0'.repeat(32)}-${SPAN}-01`,
|
|
74
|
+
`00-${TRACE}-${'0'.repeat(16)}-01`,
|
|
75
|
+
`ff-${TRACE}-${SPAN}-01`,
|
|
76
|
+
`00-${TRACE}-${SPAN}-01-extra`,
|
|
77
|
+
`01-${TRACE}-${SPAN}-01extra`,
|
|
78
|
+
`0-${TRACE}-${SPAN}-01`,
|
|
79
|
+
`00-${TRACE}-${SPAN}-1`,
|
|
80
|
+
]) {
|
|
81
|
+
expect(extract({traceparent: tp, tracestate: 'a=b'})).toBeUndefined();
|
|
82
|
+
}
|
|
83
|
+
expect(
|
|
84
|
+
extract({
|
|
85
|
+
traceparent: [`00-${TRACE}-${SPAN}-01`, `00-${TRACE}-${SPAN}-01`],
|
|
86
|
+
}),
|
|
87
|
+
).toBeUndefined();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('future versions are read by their prefix', () => {
|
|
91
|
+
expect(parseTraceparent(`01-${TRACE}-${SPAN}-01-extra`)?.traceId).toBe(
|
|
92
|
+
TRACE,
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('formatTraceparent', () => {
|
|
97
|
+
expect(
|
|
98
|
+
formatTraceparent({traceId: TRACE, spanId: SPAN, traceFlags: 1}),
|
|
99
|
+
).toBe(`00-${TRACE}-${SPAN}-01`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('fields', () => {
|
|
103
|
+
expect(prop.fields()).toEqual(['traceparent', 'tracestate']);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTraceState,
|
|
3
|
+
isSpanContextValid,
|
|
4
|
+
trace,
|
|
5
|
+
type Context,
|
|
6
|
+
type SpanContext,
|
|
7
|
+
type TextMapGetter,
|
|
8
|
+
type TextMapPropagator,
|
|
9
|
+
type TextMapSetter,
|
|
10
|
+
} from '@opentelemetry/api';
|
|
11
|
+
|
|
12
|
+
export const TRACEPARENT = 'traceparent';
|
|
13
|
+
export const TRACESTATE = 'tracestate';
|
|
14
|
+
|
|
15
|
+
const TRACEPARENT_RE =
|
|
16
|
+
/^\s*([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?\s*$/;
|
|
17
|
+
|
|
18
|
+
/** Parses a W3C `traceparent`; `undefined` when malformed or all-zero.
|
|
19
|
+
* Versions above 00 are read by their version-00 prefix. */
|
|
20
|
+
export function parseTraceparent(value: string): SpanContext | undefined {
|
|
21
|
+
const m = TRACEPARENT_RE.exec(value);
|
|
22
|
+
if (!m) return undefined;
|
|
23
|
+
const [, version, traceId, spanId, flags, rest] = m;
|
|
24
|
+
if (version === 'ff') return undefined;
|
|
25
|
+
if (version === '00' && rest) return undefined;
|
|
26
|
+
const sc: SpanContext = {
|
|
27
|
+
traceId: traceId!,
|
|
28
|
+
spanId: spanId!,
|
|
29
|
+
traceFlags: parseInt(flags!, 16),
|
|
30
|
+
isRemote: true,
|
|
31
|
+
};
|
|
32
|
+
return isSpanContextValid(sc) ? sc : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The version-00 `traceparent` of `sc`. */
|
|
36
|
+
export function formatTraceparent(sc: SpanContext): string {
|
|
37
|
+
const flags = (sc.traceFlags & 0xff).toString(16).padStart(2, '0');
|
|
38
|
+
return `00-${sc.traceId}-${sc.spanId}-${flags}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** W3C Trace Context propagation. */
|
|
42
|
+
export class W3CPropagator implements TextMapPropagator {
|
|
43
|
+
inject(ctx: Context, carrier: unknown, setter: TextMapSetter): void {
|
|
44
|
+
const sc = trace.getSpanContext(ctx);
|
|
45
|
+
if (!sc || !isSpanContextValid(sc)) return;
|
|
46
|
+
setter.set(carrier, TRACEPARENT, formatTraceparent(sc));
|
|
47
|
+
const ts = sc.traceState?.serialize();
|
|
48
|
+
if (ts) setter.set(carrier, TRACESTATE, ts);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
extract(ctx: Context, carrier: unknown, getter: TextMapGetter): Context {
|
|
52
|
+
const tp = getter.get(carrier, TRACEPARENT);
|
|
53
|
+
// Multiple traceparent values are invalid.
|
|
54
|
+
if (Array.isArray(tp) && tp.length !== 1) return ctx;
|
|
55
|
+
const value = Array.isArray(tp) ? tp[0] : tp;
|
|
56
|
+
if (!value) return ctx;
|
|
57
|
+
const sc = parseTraceparent(value);
|
|
58
|
+
if (!sc) return ctx;
|
|
59
|
+
const ts = getter.get(carrier, TRACESTATE);
|
|
60
|
+
const tsValue = Array.isArray(ts) ? ts.join(',') : ts;
|
|
61
|
+
if (tsValue) sc.traceState = createTraceState(tsValue);
|
|
62
|
+
return trace.setSpanContext(ctx, sc);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fields(): string[] {
|
|
66
|
+
return [TRACEPARENT, TRACESTATE];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Tracer as ApiTracer,
|
|
3
|
+
TracerProvider as ApiTracerProvider,
|
|
4
|
+
} from '@opentelemetry/api';
|
|
5
|
+
import type {Backend} from './backend';
|
|
6
|
+
import {Tracer} from './tracer';
|
|
7
|
+
|
|
8
|
+
/** One tracer per instrumentation scope. */
|
|
9
|
+
export class TracerProvider implements ApiTracerProvider {
|
|
10
|
+
private readonly tracers = new Map<string, Tracer>();
|
|
11
|
+
|
|
12
|
+
constructor(private readonly backend: Backend) {}
|
|
13
|
+
|
|
14
|
+
getTracer(name: string, version?: string): ApiTracer {
|
|
15
|
+
const key = `${name}\0${version ?? ''}`;
|
|
16
|
+
let tracer = this.tracers.get(key);
|
|
17
|
+
if (!tracer) {
|
|
18
|
+
tracer = new Tracer(this.backend, {name, version});
|
|
19
|
+
this.tracers.set(key, tracer);
|
|
20
|
+
}
|
|
21
|
+
return tracer;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContextKey,
|
|
3
|
+
trace,
|
|
4
|
+
type Context,
|
|
5
|
+
type SpanContext,
|
|
6
|
+
} from '@opentelemetry/api';
|
|
7
|
+
import {activeContext} from './context';
|
|
8
|
+
import {contextCtxBytes} from './span';
|
|
9
|
+
|
|
10
|
+
/** The context key of the runtime request being handled. */
|
|
11
|
+
export const ENCORE_REQUEST_KEY = createContextKey('encore.request');
|
|
12
|
+
|
|
13
|
+
/** `ctx` carrying `req` and, when given, `spanCtx` as the active span
|
|
14
|
+
* context. */
|
|
15
|
+
export function withRequestContext(
|
|
16
|
+
ctx: Context,
|
|
17
|
+
req: unknown,
|
|
18
|
+
spanCtx?: SpanContext,
|
|
19
|
+
): Context {
|
|
20
|
+
const out = ctx.setValue(ENCORE_REQUEST_KEY, req);
|
|
21
|
+
return spanCtx ? trace.setSpanContext(out, spanCtx) : out;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The runtime request `ctx` carries, if any. */
|
|
25
|
+
export function getRequestFromContext(ctx: Context): unknown {
|
|
26
|
+
return ctx.getValue(ENCORE_REQUEST_KEY);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The active span's 25-byte context, if any. */
|
|
30
|
+
export function activeCtxBytes(): Buffer | undefined {
|
|
31
|
+
return contextCtxBytes(activeContext());
|
|
32
|
+
}
|