@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,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a config's value is built from: the JSON the runtime hands over,
|
|
3
|
+
* turned into what the declared type says, guided by the config's schema.
|
|
4
|
+
* Durations become {@link Duration}s, and the runtime's placeholder for each
|
|
5
|
+
* secret field becomes the secret itself. Kept apart from the runtime, so it
|
|
6
|
+
* can be tested without one.
|
|
7
|
+
*/
|
|
8
|
+
/** What a duration string looks like: one or more numbers, each with a unit. */
|
|
9
|
+
const DURATION = /^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$/;
|
|
10
|
+
const UNIT_MS = {
|
|
11
|
+
ns: 1e-6,
|
|
12
|
+
us: 1e-3,
|
|
13
|
+
µs: 1e-3,
|
|
14
|
+
ms: 1,
|
|
15
|
+
s: 1000,
|
|
16
|
+
m: 60000,
|
|
17
|
+
h: 3600000,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A length of time, written in config files as a number with a unit, or
|
|
21
|
+
* several: `"30s"`, `"250ms"`, `"1h30m"` (units `ns`, `us`, `ms`, `s`, `m`,
|
|
22
|
+
* `h`, as Go's `time.ParseDuration` reads them, without a sign).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* interface Checkout { paymentTimeout: Duration }
|
|
26
|
+
* setTimeout(retry, checkout().paymentTimeout.milliseconds);
|
|
27
|
+
*/
|
|
28
|
+
export class Duration {
|
|
29
|
+
ms;
|
|
30
|
+
constructor(ms) {
|
|
31
|
+
this.ms = ms;
|
|
32
|
+
}
|
|
33
|
+
/** Reads a duration string; throws on anything else. */
|
|
34
|
+
static parse(text) {
|
|
35
|
+
if (!DURATION.test(text)) {
|
|
36
|
+
throw new Error(`${JSON.stringify(text)} is not a duration, such as "30s", "250ms" or "1h30m"`);
|
|
37
|
+
}
|
|
38
|
+
let ms = 0;
|
|
39
|
+
for (const m of text.matchAll(/([0-9]+(?:\.[0-9]+)?)(ns|us|µs|ms|s|m|h)/g)) {
|
|
40
|
+
ms += parseFloat(m[1]) * UNIT_MS[m[2]];
|
|
41
|
+
}
|
|
42
|
+
// Whole nanoseconds, so that "0.1s" is 100ms rather than 100.00000000000001.
|
|
43
|
+
return new Duration(Math.round(ms * 1e6) / 1e6);
|
|
44
|
+
}
|
|
45
|
+
/** A duration of `ms` milliseconds. */
|
|
46
|
+
static ofMilliseconds(ms) {
|
|
47
|
+
if (!(ms >= 0)) {
|
|
48
|
+
throw new Error('a duration is not negative');
|
|
49
|
+
}
|
|
50
|
+
return new Duration(ms);
|
|
51
|
+
}
|
|
52
|
+
/** The duration in milliseconds: what `setTimeout` takes. */
|
|
53
|
+
get milliseconds() {
|
|
54
|
+
return this.ms;
|
|
55
|
+
}
|
|
56
|
+
/** The duration in seconds. */
|
|
57
|
+
get seconds() {
|
|
58
|
+
return this.ms / 1000;
|
|
59
|
+
}
|
|
60
|
+
/** As a config file writes it: `"1h30m"`, `"250ms"`, `"0s"`. */
|
|
61
|
+
toString() {
|
|
62
|
+
let ms = this.ms;
|
|
63
|
+
if (ms === 0)
|
|
64
|
+
return '0s';
|
|
65
|
+
let out = '';
|
|
66
|
+
for (const [unit, size] of [
|
|
67
|
+
['h', 3600000],
|
|
68
|
+
['m', 60000],
|
|
69
|
+
['s', 1000],
|
|
70
|
+
]) {
|
|
71
|
+
const n = Math.floor(ms / size);
|
|
72
|
+
if (n > 0) {
|
|
73
|
+
out += `${n}${unit}`;
|
|
74
|
+
ms -= n * size;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (ms > 0) {
|
|
78
|
+
// What is left is under a second.
|
|
79
|
+
out += Number.isInteger(ms) ? `${ms}ms` : `${Math.round(ms * 1e6)}ns`;
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
toJSON() {
|
|
84
|
+
return this.toString();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The key of the runtime's placeholder for a secret field. */
|
|
88
|
+
export const PLACEHOLDER_KEY = '$encore_secret';
|
|
89
|
+
/**
|
|
90
|
+
* `value`, as the declared type has it: the JSON the runtime hands over, with
|
|
91
|
+
* each duration string `schema` marks made a {@link Duration}, and the
|
|
92
|
+
* runtime's placeholder at each secret field `schema` marks made the secret
|
|
93
|
+
* `secretOf` builds.
|
|
94
|
+
*/
|
|
95
|
+
export function build(schema, value, secretOf) {
|
|
96
|
+
return convert(schema, schema, value, secretOf, 0);
|
|
97
|
+
}
|
|
98
|
+
function resolve(root, schema) {
|
|
99
|
+
const ref = schema?.['$ref'];
|
|
100
|
+
if (typeof ref === 'string' && ref.startsWith('#/')) {
|
|
101
|
+
let node = root;
|
|
102
|
+
for (const token of ref.slice(2).split('/')) {
|
|
103
|
+
node = node?.[token.replace(/~1/g, '/').replace(/~0/g, '~')];
|
|
104
|
+
}
|
|
105
|
+
if (node && typeof node === 'object')
|
|
106
|
+
return node;
|
|
107
|
+
}
|
|
108
|
+
return schema;
|
|
109
|
+
}
|
|
110
|
+
function convert(root, schema, value, secretOf, depth) {
|
|
111
|
+
if (!schema || typeof schema !== 'object' || depth > 128) {
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
schema = resolve(root, schema);
|
|
115
|
+
// Only a field the schema says reads a secret is one: anywhere else, a
|
|
116
|
+
// value shaped like a placeholder is just data, and must not reach a secret.
|
|
117
|
+
const secret = schema['x-encore-secret'];
|
|
118
|
+
if (secret) {
|
|
119
|
+
if (!isPlaceholder(value) ||
|
|
120
|
+
value[PLACEHOLDER_KEY].name !== secret.name ||
|
|
121
|
+
value[PLACEHOLDER_KEY].scope !== secret.scope) {
|
|
122
|
+
// Never say what the value is: it may be a pasted secret.
|
|
123
|
+
throw new Error(`a secret field reads the secret ${secret.name}, and is not set in config`);
|
|
124
|
+
}
|
|
125
|
+
return secretOf(secret.name, secret.scope);
|
|
126
|
+
}
|
|
127
|
+
if (schema['x-encore-format'] === 'duration' && typeof value === 'string') {
|
|
128
|
+
return Duration.parse(value);
|
|
129
|
+
}
|
|
130
|
+
if (Array.isArray(schema.anyOf) && value !== null) {
|
|
131
|
+
// `T | null`: the value is a `T`.
|
|
132
|
+
const branch = schema.anyOf.find((b) => b?.type !== 'null');
|
|
133
|
+
return convert(root, branch, value, secretOf, depth + 1);
|
|
134
|
+
}
|
|
135
|
+
if (Array.isArray(schema.oneOf)) {
|
|
136
|
+
return convert(root, branchOf(root, schema.oneOf, value), value, secretOf, depth + 1);
|
|
137
|
+
}
|
|
138
|
+
if (Array.isArray(value)) {
|
|
139
|
+
return value.map(item => convert(root, schema.items, item, secretOf, depth + 1));
|
|
140
|
+
}
|
|
141
|
+
if (value !== null && typeof value === 'object') {
|
|
142
|
+
const props = schema.properties ?? {};
|
|
143
|
+
const extra = typeof schema.additionalProperties === 'object'
|
|
144
|
+
? schema.additionalProperties
|
|
145
|
+
: undefined;
|
|
146
|
+
const out = {};
|
|
147
|
+
for (const [k, v] of Object.entries(value)) {
|
|
148
|
+
out[k] = convert(root, props[k] ?? extra, v, secretOf, depth + 1);
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
/** The branch of a `oneOf` a value is: the one whose constant tag it has. */
|
|
155
|
+
function branchOf(root, branches, value) {
|
|
156
|
+
const resolved = branches.map(b => resolve(root, b));
|
|
157
|
+
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
158
|
+
for (const b of resolved) {
|
|
159
|
+
const props = b?.properties;
|
|
160
|
+
if (!props)
|
|
161
|
+
continue;
|
|
162
|
+
const tagged = Object.entries(props).every(([k, p]) => !('const' in (p ?? {})) ||
|
|
163
|
+
value[k] === p.const);
|
|
164
|
+
if (tagged &&
|
|
165
|
+
Object.values(props).some((p) => 'const' in (p ?? {}))) {
|
|
166
|
+
return b;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
function isPlaceholder(value) {
|
|
173
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
174
|
+
return false;
|
|
175
|
+
const keys = Object.keys(value);
|
|
176
|
+
return keys.length === 1 && keys[0] === PLACEHOLDER_KEY;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=value.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value.js","sourceRoot":"","sources":["../../config/value.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,gFAAgF;AAChF,MAAM,QAAQ,GAAG,2CAA2C,CAAC;AAC7D,MAAM,OAAO,GAA2B;IACtC,EAAE,EAAE,IAAI;IACR,EAAE,EAAE,IAAI;IACR,EAAE,EAAE,IAAI;IACR,EAAE,EAAE,CAAC;IACL,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,KAAM;IACT,CAAC,EAAE,OAAS;CACb,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,OAAO,QAAQ;IAOF,EAAE,CAAS;IAE5B,YAAoB,EAAU;QAC5B,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACf,CAAC;IAED,wDAAwD;IACxD,MAAM,CAAC,KAAK,CAAC,IAAY;QACvB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAA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,OAAO,CAAC,KAAK,CAAC;QACrE,OAAO,KAAK,CAAC;IACf,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAChC,OAAO,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,eAAe,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DurationString } from '../types/mod.js';
|
|
2
|
+
export declare class CronJob {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly cfg: CronJobConfig;
|
|
5
|
+
constructor(name: string, cfg: CronJobConfig);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The configuration of a cron job: the path it requests and when.
|
|
9
|
+
*
|
|
10
|
+
* When the job is due, Encore sends a `POST` request for `path` to the service
|
|
11
|
+
* that declares the job. The fields must be literals: Encore reads them from
|
|
12
|
+
* the source.
|
|
13
|
+
*/
|
|
14
|
+
export type CronJobConfig = {
|
|
15
|
+
/** The path the job requests of its service, starting with `/`. */
|
|
16
|
+
path: string;
|
|
17
|
+
title?: string;
|
|
18
|
+
} & ({
|
|
19
|
+
every: DurationString;
|
|
20
|
+
} | {
|
|
21
|
+
schedule: string;
|
|
22
|
+
});
|
package/dist/cron/mod.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../cron/mod.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,OAAO;IACT,IAAI,CAAS;IACb,GAAG,CAAgB;IAC5B,YAAY,IAAY,EAAE,GAAkB;QAC1C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/** The runtime's span calls (the napi `Pipeline` class). */
|
|
3
|
+
export interface Backend {
|
|
4
|
+
/** `parent`: 25-byte context. `packed`: start scope, attributes, and
|
|
5
|
+
* links in the packed layout. Returns the new span's 25-byte context. */
|
|
6
|
+
startSpan(name: string, kind: number, parent: Buffer | undefined, tracestate: string | undefined, startUnixNanos: bigint | undefined, packed: Buffer): {
|
|
7
|
+
ctx: Buffer;
|
|
8
|
+
tracestate: string;
|
|
9
|
+
startUnixNanos: bigint;
|
|
10
|
+
startMonoNanos: bigint;
|
|
11
|
+
};
|
|
12
|
+
/** `packed`: the finished span in the packed layout. */
|
|
13
|
+
endSpan(packed: Buffer): void;
|
|
14
|
+
}
|
|
15
|
+
/** An instrument to register (the napi `InstrumentOptions`). */
|
|
16
|
+
export interface InstrumentOptions {
|
|
17
|
+
scopeName: string;
|
|
18
|
+
scopeVersion?: string;
|
|
19
|
+
scopeSchemaUrl?: string;
|
|
20
|
+
/** 0 counter, 1 up-down counter, 2 histogram, 3 gauge, 4 observable
|
|
21
|
+
* counter, 5 observable up-down counter, 6 observable gauge. */
|
|
22
|
+
kind: number;
|
|
23
|
+
/** 0 `i64`, 1 `u64`, 2 `f64`. */
|
|
24
|
+
number: number;
|
|
25
|
+
name: string;
|
|
26
|
+
unit?: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
boundaries?: number[];
|
|
29
|
+
}
|
|
30
|
+
/** A registered instrument (the napi `Instrument` class). */
|
|
31
|
+
export interface InstrumentHandle {
|
|
32
|
+
/** Identifies an observable instrument's observations. */
|
|
33
|
+
readonly id: number;
|
|
34
|
+
/** `attrs`: a JSON object's text. `span`: 25-byte context. */
|
|
35
|
+
record(value: number, attrs?: string, span?: Buffer): void;
|
|
36
|
+
}
|
|
37
|
+
/** What a collector replies with, once (the napi `CollectReply` class). */
|
|
38
|
+
export interface CollectReply {
|
|
39
|
+
/** The milliseconds left to send in; what is sent later is dropped. */
|
|
40
|
+
readonly timeout: number;
|
|
41
|
+
/** The `i`th observation is `values[i]` for the observable instrument
|
|
42
|
+
* `ids[i]`, with the attributes `attrs[i]`, a JSON object's text. */
|
|
43
|
+
send(ids: number[], values: number[], attrs: string[]): void;
|
|
44
|
+
}
|
|
45
|
+
/** The runtime's instrument calls (the napi `Meters` class). */
|
|
46
|
+
export interface MeterBackend {
|
|
47
|
+
instrument(opts: InstrumentOptions): InstrumentHandle;
|
|
48
|
+
/** Adds this thread's collector, called once per collection. */
|
|
49
|
+
addCollector(collect: (reply: CollectReply) => void): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../../internal/otel/backend.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Context } from '@opentelemetry/api';
|
|
2
|
+
/** Registers Encore's context manager as this thread's OTel API global,
|
|
3
|
+
* unless another is registered. Returns whether Encore's is the global. */
|
|
4
|
+
export declare function installContextManager(): boolean;
|
|
5
|
+
/** The active context: the global context manager's, falling back to
|
|
6
|
+
* Encore's own when the global has none. */
|
|
7
|
+
export declare function activeContext(): Context;
|
|
8
|
+
/** Runs `fn` with `ctx` active, in Encore's context manager too when it is
|
|
9
|
+
* not the global. */
|
|
10
|
+
export declare function withContext<T>(ctx: Context, fn: () => T): T;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { context, createContextKey, ROOT_CONTEXT, } from '@opentelemetry/api';
|
|
2
|
+
import { ContextManager } from './context_manager.js';
|
|
3
|
+
/** Encore's context manager. Without a global one, Encore's own requests
|
|
4
|
+
* are tracked in it, so infra calls, logs, and exemplars still see them. */
|
|
5
|
+
const manager = new ContextManager();
|
|
6
|
+
let isGlobal = false;
|
|
7
|
+
const PROBE = createContextKey('encore.context-manager-probe');
|
|
8
|
+
/** Whether this thread's OTel API has a context manager: the API's no-op
|
|
9
|
+
* default keeps no context. */
|
|
10
|
+
function hasContextManager() {
|
|
11
|
+
return (context.with(ROOT_CONTEXT.setValue(PROBE, true), () => context.active().getValue(PROBE)) === true);
|
|
12
|
+
}
|
|
13
|
+
/** Registers Encore's context manager as this thread's OTel API global,
|
|
14
|
+
* unless another is registered. Returns whether Encore's is the global. */
|
|
15
|
+
export function installContextManager() {
|
|
16
|
+
if (!isGlobal && !hasContextManager()) {
|
|
17
|
+
isGlobal = context.setGlobalContextManager(manager);
|
|
18
|
+
}
|
|
19
|
+
return isGlobal;
|
|
20
|
+
}
|
|
21
|
+
/** The active context: the global context manager's, falling back to
|
|
22
|
+
* Encore's own when the global has none. */
|
|
23
|
+
export function activeContext() {
|
|
24
|
+
const ctx = context.active();
|
|
25
|
+
return isGlobal || ctx !== ROOT_CONTEXT ? ctx : manager.active();
|
|
26
|
+
}
|
|
27
|
+
/** Runs `fn` with `ctx` active, in Encore's context manager too when it is
|
|
28
|
+
* not the global. */
|
|
29
|
+
export function withContext(ctx, fn) {
|
|
30
|
+
return isGlobal
|
|
31
|
+
? context.with(ctx, fn)
|
|
32
|
+
: context.with(ctx, () => manager.with(ctx, fn));
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../internal/otel/context.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,gBAAgB,EAChB,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAC,cAAc,EAAC,MAAM,mBAAmB,CAAC;AAEjD;4EAC4E;AAC5E,MAAM,OAAO,GAAG,IAAI,cAAc,EAAE,CAAC;AACrC,IAAI,QAAQ,GAAG,KAAK,CAAC;AAErB,MAAM,KAAK,GAAG,gBAAgB,CAAC,8BAA8B,CAAC,CAAC;AAE/D;+BAC+B;AAC/B,SAAS,iBAAiB;IACxB,OAAO,CACL,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,CACpD,OAAO,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CACjC,KAAK,IAAI,CACX,CAAC;AACJ,CAAC;AAED;2EAC2E;AAC3E,MAAM,UAAU,qBAAqB;IACnC,IAAI,CAAC,QAAQ,IAAI,CAAC,iBAAiB,EAAE,EAAE,CAAC;QACtC,QAAQ,GAAG,OAAO,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAAC;IACtD,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;4CAC4C;AAC5C,MAAM,UAAU,aAAa;IAC3B,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAC7B,OAAO,QAAQ,IAAI,GAAG,KAAK,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;AACnE,CAAC;AAED;qBACqB;AACrB,MAAM,UAAU,WAAW,CAAI,GAAY,EAAE,EAAW;IACtD,OAAO,QAAQ;QACb,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;QACvB,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Context, type ContextManager as ApiContextManager } from '@opentelemetry/api';
|
|
2
|
+
/** OTel context on `AsyncLocalStorage`: follows `await`, timers, and
|
|
3
|
+
* callbacks. */
|
|
4
|
+
export declare class ContextManager implements ApiContextManager {
|
|
5
|
+
private als;
|
|
6
|
+
/** Bound emitters' patched listeners. */
|
|
7
|
+
private patched;
|
|
8
|
+
/** Set while a patched add method runs, so one that calls another (`once`
|
|
9
|
+
* calls `on`) binds the listener once. */
|
|
10
|
+
private adding;
|
|
11
|
+
active(): Context;
|
|
12
|
+
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(ctx: Context, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
|
|
13
|
+
/** Binds a function, or an `EventEmitter`'s listeners added from now
|
|
14
|
+
* on, to `ctx`. An emitter keeps the context it was first bound to. */
|
|
15
|
+
bind<T>(ctx: Context, target: T): T;
|
|
16
|
+
private bindEmitter;
|
|
17
|
+
private patchAdd;
|
|
18
|
+
private patchRemove;
|
|
19
|
+
private patchRemoveAll;
|
|
20
|
+
enable(): this;
|
|
21
|
+
disable(): this;
|
|
22
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { ROOT_CONTEXT, } from '@opentelemetry/api';
|
|
4
|
+
const ADD_LISTENER_METHODS = [
|
|
5
|
+
'addListener',
|
|
6
|
+
'on',
|
|
7
|
+
'once',
|
|
8
|
+
'prependListener',
|
|
9
|
+
'prependOnceListener',
|
|
10
|
+
];
|
|
11
|
+
const REMOVE_LISTENER_METHODS = ['removeListener', 'off'];
|
|
12
|
+
/** OTel context on `AsyncLocalStorage`: follows `await`, timers, and
|
|
13
|
+
* callbacks. */
|
|
14
|
+
export class ContextManager {
|
|
15
|
+
als = new AsyncLocalStorage();
|
|
16
|
+
/** Bound emitters' patched listeners. */
|
|
17
|
+
patched = new WeakMap();
|
|
18
|
+
/** Set while a patched add method runs, so one that calls another (`once`
|
|
19
|
+
* calls `on`) binds the listener once. */
|
|
20
|
+
adding = false;
|
|
21
|
+
active() {
|
|
22
|
+
return this.als.getStore() ?? ROOT_CONTEXT;
|
|
23
|
+
}
|
|
24
|
+
with(ctx, fn, thisArg, ...args) {
|
|
25
|
+
const cb = thisArg == null ? fn : fn.bind(thisArg);
|
|
26
|
+
return this.als.run(ctx, cb, ...args);
|
|
27
|
+
}
|
|
28
|
+
/** Binds a function, or an `EventEmitter`'s listeners added from now
|
|
29
|
+
* on, to `ctx`. An emitter keeps the context it was first bound to. */
|
|
30
|
+
bind(ctx, target) {
|
|
31
|
+
if (target instanceof EventEmitter) {
|
|
32
|
+
this.bindEmitter(ctx, target);
|
|
33
|
+
return target;
|
|
34
|
+
}
|
|
35
|
+
if (typeof target !== 'function')
|
|
36
|
+
return target;
|
|
37
|
+
// Preserve the manager while the wrapper receives the caller's dynamic this.
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
39
|
+
const manager = this;
|
|
40
|
+
const fn = target;
|
|
41
|
+
const bound = function (...args) {
|
|
42
|
+
return manager.with(ctx, () => fn.apply(this, args));
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(bound, 'length', { value: fn.length });
|
|
45
|
+
return bound;
|
|
46
|
+
}
|
|
47
|
+
bindEmitter(ctx, ee) {
|
|
48
|
+
if (this.patched.has(ee))
|
|
49
|
+
return;
|
|
50
|
+
this.patched.set(ee, new Map());
|
|
51
|
+
const e = ee;
|
|
52
|
+
for (const name of ADD_LISTENER_METHODS) {
|
|
53
|
+
if (typeof e[name] === 'function') {
|
|
54
|
+
e[name] = this.patchAdd(ee, e[name], ctx);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const name of REMOVE_LISTENER_METHODS) {
|
|
58
|
+
if (typeof e[name] === 'function') {
|
|
59
|
+
e[name] = this.patchRemove(ee, e[name]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (typeof ee.removeAllListeners === 'function') {
|
|
63
|
+
e.removeAllListeners = this.patchRemoveAll(ee, ee.removeAllListeners);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
patchAdd(ee, original, ctx) {
|
|
67
|
+
// Preserve the manager while the wrapper receives the caller's dynamic this.
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
69
|
+
const manager = this;
|
|
70
|
+
return function (event, listener) {
|
|
71
|
+
if (manager.adding)
|
|
72
|
+
return original.call(this, event, listener);
|
|
73
|
+
const map = manager.patched.get(ee);
|
|
74
|
+
let listeners = map.get(event);
|
|
75
|
+
if (listeners === undefined) {
|
|
76
|
+
listeners = new WeakMap();
|
|
77
|
+
map.set(event, listeners);
|
|
78
|
+
}
|
|
79
|
+
const bound = manager.bind(ctx, listener);
|
|
80
|
+
listeners.set(listener, bound);
|
|
81
|
+
manager.adding = true;
|
|
82
|
+
try {
|
|
83
|
+
return original.call(this, event, bound);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
manager.adding = false;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
patchRemove(ee, original) {
|
|
91
|
+
// Preserve the manager while the wrapper receives the caller's dynamic this.
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
93
|
+
const manager = this;
|
|
94
|
+
return function (event, listener) {
|
|
95
|
+
const bound = manager.patched.get(ee)?.get(event)?.get(listener);
|
|
96
|
+
return original.call(this, event, bound ?? listener);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
patchRemoveAll(ee, original) {
|
|
100
|
+
// Preserve the manager while the wrapper receives the caller's dynamic this.
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
102
|
+
const manager = this;
|
|
103
|
+
return function (...args) {
|
|
104
|
+
const map = manager.patched.get(ee);
|
|
105
|
+
if (args.length === 0)
|
|
106
|
+
map?.clear();
|
|
107
|
+
else
|
|
108
|
+
map?.delete(args[0]);
|
|
109
|
+
return original.apply(this, args);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
enable() {
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
disable() {
|
|
116
|
+
this.als.disable();
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=context_manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context_manager.js","sourceRoot":"","sources":["../../../internal/otel/context_manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAC,YAAY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,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,QAAQ,CAAC,CAAC;QACvD,CAAC,CAAC;IACJ,CAAC;IAEO,cAAc,CAAC,EAAgB,EAAE,QAAkB;QACzD,6EAA6E;QAC7E,4DAA4D;QAC5D,MAAM,OAAO,GAAG,IAAI,CAAC;QACrB,OAAO,UAAyB,GAAG,IAAe;YAChD,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACpC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;gBAAE,GAAG,EAAE,KAAK,EAAE,CAAC;;gBAC/B,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAU,CAAC,CAAC;YACnC,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Attributes, Link, SpanContext, TimeInput } from '@opentelemetry/api';
|
|
3
|
+
export declare const VERSION = 1;
|
|
4
|
+
export declare const HEADER_LEN = 120;
|
|
5
|
+
export declare const ATTR_LEN = 24;
|
|
6
|
+
export declare const ITEM_LEN = 8;
|
|
7
|
+
export declare const EVENT_LEN = 24;
|
|
8
|
+
export declare const LINK_LEN = 48;
|
|
9
|
+
/** Length of a span context buffer: trace id, span id, flags. */
|
|
10
|
+
export declare const CTX_LEN = 25;
|
|
11
|
+
/** Bounds the buffer; the runtime enforces the real limits. */
|
|
12
|
+
export declare const MAX_COUNT = 128;
|
|
13
|
+
export declare const TYPE_STR = 0;
|
|
14
|
+
export declare const TYPE_BOOL = 1;
|
|
15
|
+
export declare const TYPE_I64 = 2;
|
|
16
|
+
export declare const TYPE_F64 = 3;
|
|
17
|
+
export declare const TYPE_STR_ARRAY = 4;
|
|
18
|
+
export declare const TYPE_BOOL_ARRAY = 5;
|
|
19
|
+
export declare const TYPE_I64_ARRAY = 6;
|
|
20
|
+
export declare const TYPE_F64_ARRAY = 7;
|
|
21
|
+
type Value = {
|
|
22
|
+
t: typeof TYPE_STR;
|
|
23
|
+
v: string;
|
|
24
|
+
} | {
|
|
25
|
+
t: typeof TYPE_BOOL;
|
|
26
|
+
v: boolean;
|
|
27
|
+
} | {
|
|
28
|
+
t: typeof TYPE_I64;
|
|
29
|
+
v: bigint;
|
|
30
|
+
} | {
|
|
31
|
+
t: typeof TYPE_F64;
|
|
32
|
+
v: number;
|
|
33
|
+
} | {
|
|
34
|
+
t: typeof TYPE_STR_ARRAY;
|
|
35
|
+
v: string[];
|
|
36
|
+
} | {
|
|
37
|
+
t: typeof TYPE_BOOL_ARRAY;
|
|
38
|
+
v: boolean[];
|
|
39
|
+
} | {
|
|
40
|
+
t: typeof TYPE_I64_ARRAY;
|
|
41
|
+
v: bigint[];
|
|
42
|
+
} | {
|
|
43
|
+
t: typeof TYPE_F64_ARRAY;
|
|
44
|
+
v: number[];
|
|
45
|
+
};
|
|
46
|
+
/** An attribute value as a packed value; `undefined` when unsupported. */
|
|
47
|
+
export declare function toValue(v: unknown): Value | undefined;
|
|
48
|
+
/** An instrumentation scope. */
|
|
49
|
+
export interface Scope {
|
|
50
|
+
name: string;
|
|
51
|
+
version?: string;
|
|
52
|
+
}
|
|
53
|
+
/** The span-level fields of a packed buffer. */
|
|
54
|
+
export interface PackedHeader {
|
|
55
|
+
scope: Scope;
|
|
56
|
+
name: string;
|
|
57
|
+
kind: number;
|
|
58
|
+
statusCode: number;
|
|
59
|
+
/** Read for ERROR only. */
|
|
60
|
+
statusMessage?: string;
|
|
61
|
+
tracestate?: string;
|
|
62
|
+
/** 25-byte span context; zeros when absent. */
|
|
63
|
+
ctx?: Buffer;
|
|
64
|
+
/** 8 bytes; zeros when absent. */
|
|
65
|
+
parentSpanId?: Buffer;
|
|
66
|
+
startUnixNanos?: bigint;
|
|
67
|
+
startMonoNanos?: bigint;
|
|
68
|
+
/** Zero or absent: the runtime's clock. */
|
|
69
|
+
endUnixNanos?: bigint;
|
|
70
|
+
}
|
|
71
|
+
/** Builds one packed buffer. Span attributes first, then events and links
|
|
72
|
+
* in any order, then `finish`. */
|
|
73
|
+
export declare class PackedWriter {
|
|
74
|
+
private readonly attrs;
|
|
75
|
+
private spanAttrCount;
|
|
76
|
+
private readonly events;
|
|
77
|
+
private readonly links;
|
|
78
|
+
spanAttrs(attrs: Attributes | undefined): this;
|
|
79
|
+
event(name: string, timeUnixNanos: bigint, attrs?: Attributes): this;
|
|
80
|
+
/** `ctx`: 25 bytes. */
|
|
81
|
+
link(ctx: Buffer, tracestate: string, attrs?: Attributes): this;
|
|
82
|
+
private pushAttrs;
|
|
83
|
+
finish(h: PackedHeader): Buffer;
|
|
84
|
+
/** Lays out header, attributes, items, events, links, and the data
|
|
85
|
+
* table. `header` runs first, so its strings lead the table. */
|
|
86
|
+
private pack;
|
|
87
|
+
}
|
|
88
|
+
/** The 25-byte buffer of a hex span context. */
|
|
89
|
+
export declare function ctxBytes(sc: SpanContext): Buffer;
|
|
90
|
+
/** The start buffer: scope, attributes, and links; other fields zero. */
|
|
91
|
+
export declare function encodeStart(scope: Scope, attrs: Attributes | undefined, links: readonly Link[] | undefined): Buffer;
|
|
92
|
+
export interface PackedEvent {
|
|
93
|
+
name: string;
|
|
94
|
+
timeUnixNanos: bigint;
|
|
95
|
+
attributes?: Attributes;
|
|
96
|
+
}
|
|
97
|
+
/** Everything a finished span carries. */
|
|
98
|
+
export interface PackedSpanEnd extends PackedHeader {
|
|
99
|
+
ctx: Buffer;
|
|
100
|
+
attributes: Attributes;
|
|
101
|
+
events: readonly PackedEvent[];
|
|
102
|
+
links: readonly Link[];
|
|
103
|
+
}
|
|
104
|
+
/** The end buffer of a finished span. */
|
|
105
|
+
export declare function encodeEnd(span: PackedSpanEnd): Buffer;
|
|
106
|
+
export declare function nowUnixNanos(): bigint;
|
|
107
|
+
export declare function isTimeInput(t: unknown): t is TimeInput;
|
|
108
|
+
/** Unix nanoseconds of a time input. Numbers are epoch milliseconds, or
|
|
109
|
+
* `performance.now()` offsets when not above `performance.now()`, as in
|
|
110
|
+
* the OTel SDK. */
|
|
111
|
+
export declare function timeToUnixNanos(t: TimeInput): bigint;
|
|
112
|
+
export {};
|