@libo11y/workers 0.0.0-stage → 0.9.0
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 +202 -0
- package/README.md +316 -2
- package/dist/adapters.d.ts +102 -0
- package/dist/adapters.js +282 -0
- package/dist/context.d.ts +3 -0
- package/dist/context.js +50 -0
- package/dist/env.d.ts +10 -0
- package/dist/env.js +41 -0
- package/dist/exporters.d.ts +91 -0
- package/dist/exporters.js +389 -0
- package/dist/flush.d.ts +34 -0
- package/dist/flush.js +44 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +18 -0
- package/dist/instrument.d.ts +73 -0
- package/dist/instrument.js +116 -0
- package/dist/log-processor.d.ts +23 -0
- package/dist/log-processor.js +157 -0
- package/dist/metric-reader.d.ts +45 -0
- package/dist/metric-reader.js +242 -0
- package/dist/platform.d.ts +40 -0
- package/dist/platform.js +44 -0
- package/dist/resource.d.ts +17 -0
- package/dist/resource.js +30 -0
- package/dist/rum-proxy.d.ts +69 -0
- package/dist/rum-proxy.js +174 -0
- package/dist/setup.d.ts +29 -0
- package/dist/setup.js +69 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +46 -4
package/dist/adapters.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composable Cloudflare Workers, Durable Object, alarm, scheduled, and Queues tracing primitives.
|
|
3
|
+
*
|
|
4
|
+
* Every span name, kind, and attribute comes from @libo11y/core/edge's single-owner convention
|
|
5
|
+
* builders (OpenTelemetry semconv 1.41.0). All spans emitted by these helpers use the
|
|
6
|
+
* "@libo11y/workers" instrumentation scope.
|
|
7
|
+
*/
|
|
8
|
+
import { linksFromCarriers } from "@libo11y/core";
|
|
9
|
+
import { alarmSpanSpec, durableObjectEnrichAttributes, durableObjectSpanSpec, extractQueueCarrier, queueBatchSpanSpec, scheduledSpanSpec, } from "@libo11y/core/edge";
|
|
10
|
+
import { context, propagation, SpanKind, SpanStatusCode, trace, } from "@opentelemetry/api";
|
|
11
|
+
import { VERSION } from "./version.js";
|
|
12
|
+
/** The instrumentation scope of @libo11y/workers spans. */
|
|
13
|
+
const SCOPE = "@libo11y/workers";
|
|
14
|
+
const HEADERS_SETTER = {
|
|
15
|
+
set: (carrier, key, value) => {
|
|
16
|
+
carrier.set(key, value);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const STUB_PASSTHROUGH_KEYS = new Set([
|
|
20
|
+
"then",
|
|
21
|
+
"catch",
|
|
22
|
+
"finally",
|
|
23
|
+
"toJSON",
|
|
24
|
+
"toString",
|
|
25
|
+
"valueOf",
|
|
26
|
+
"hasOwnProperty",
|
|
27
|
+
"isPrototypeOf",
|
|
28
|
+
"propertyIsEnumerable",
|
|
29
|
+
"toLocaleString",
|
|
30
|
+
"constructor",
|
|
31
|
+
"__proto__",
|
|
32
|
+
]);
|
|
33
|
+
function errorType(err) {
|
|
34
|
+
if (err instanceof Error && err.name !== "")
|
|
35
|
+
return err.name;
|
|
36
|
+
return "_OTHER";
|
|
37
|
+
}
|
|
38
|
+
function failSpan(span, err) {
|
|
39
|
+
try {
|
|
40
|
+
if (err instanceof Error)
|
|
41
|
+
span.recordException(err);
|
|
42
|
+
else
|
|
43
|
+
span.recordException(String(err));
|
|
44
|
+
span.setAttribute("error.type", errorType(err));
|
|
45
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// A span that cannot record changes nothing the caller sees.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function safeEnd(span) {
|
|
52
|
+
try {
|
|
53
|
+
span.end();
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Telemetry never fails the caller.
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function isThenable(value) {
|
|
60
|
+
return ((typeof value === "object" || typeof value === "function") &&
|
|
61
|
+
value !== null &&
|
|
62
|
+
typeof value.then === "function");
|
|
63
|
+
}
|
|
64
|
+
function mergeLinks(carriers, extraLinks) {
|
|
65
|
+
const links = carriers === undefined ? [] : linksFromCarriers(carriers);
|
|
66
|
+
if (extraLinks !== undefined && extraLinks.length > 0) {
|
|
67
|
+
const seen = new Set(links.map((l) => `${l.context.traceId}:${l.context.spanId}`));
|
|
68
|
+
for (const link of extraLinks) {
|
|
69
|
+
const key = `${link.context.traceId}:${link.context.spanId}`;
|
|
70
|
+
if (seen.has(key))
|
|
71
|
+
continue;
|
|
72
|
+
seen.add(key);
|
|
73
|
+
links.push(link);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return links.length > 0 ? links : undefined;
|
|
77
|
+
}
|
|
78
|
+
function runWithSpec(spec, fn, options, extra = {}) {
|
|
79
|
+
let start;
|
|
80
|
+
try {
|
|
81
|
+
const tp = options?.telemetry?.tracerProvider ?? trace.getTracerProvider();
|
|
82
|
+
const tracer = tp.getTracer(SCOPE, VERSION);
|
|
83
|
+
const attributes = options?.attributes === undefined ? spec.attributes : { ...spec.attributes, ...options.attributes };
|
|
84
|
+
const spanOptions = {
|
|
85
|
+
kind: spec.kind,
|
|
86
|
+
attributes,
|
|
87
|
+
...(extra.root === true ? { root: true } : {}),
|
|
88
|
+
...(extra.links !== undefined && extra.links.length > 0 ? { links: [...extra.links] } : {}),
|
|
89
|
+
};
|
|
90
|
+
start = (run) => tracer.startActiveSpan(spec.name, spanOptions, run);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
start = undefined;
|
|
94
|
+
}
|
|
95
|
+
if (start === undefined) {
|
|
96
|
+
return fn(trace.wrapSpanContext({
|
|
97
|
+
traceId: "00000000000000000000000000000000",
|
|
98
|
+
spanId: "0000000000000000",
|
|
99
|
+
traceFlags: 0,
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
return start((span) => {
|
|
103
|
+
let result;
|
|
104
|
+
try {
|
|
105
|
+
result = fn(span);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
failSpan(span, err);
|
|
109
|
+
safeEnd(span);
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
if (isThenable(result)) {
|
|
113
|
+
return Promise.resolve(result).then((value) => {
|
|
114
|
+
safeEnd(span);
|
|
115
|
+
return value;
|
|
116
|
+
}, (err) => {
|
|
117
|
+
failSpan(span, err);
|
|
118
|
+
safeEnd(span);
|
|
119
|
+
throw err;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
safeEnd(span);
|
|
123
|
+
return result;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function carrierFromQueueMessage(message, field) {
|
|
127
|
+
if (typeof message !== "object" || message === null)
|
|
128
|
+
return undefined;
|
|
129
|
+
const opts = field === undefined ? undefined : { field };
|
|
130
|
+
const body = message.body;
|
|
131
|
+
if (body !== undefined) {
|
|
132
|
+
const fromBody = extractQueueCarrier(body, opts);
|
|
133
|
+
if (fromBody !== undefined)
|
|
134
|
+
return fromBody;
|
|
135
|
+
}
|
|
136
|
+
return extractQueueCarrier(message, opts);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Enriches an existing span inside a Durable Object with `o11y.durable_object.class` without
|
|
140
|
+
* opening a duplicate span. Safe no-op when span is undefined.
|
|
141
|
+
*/
|
|
142
|
+
export function enrichDurableObjectSpan(span, className) {
|
|
143
|
+
if (span === undefined)
|
|
144
|
+
return;
|
|
145
|
+
try {
|
|
146
|
+
span.setAttributes(durableObjectEnrichAttributes(className));
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// Telemetry never fails the caller.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Wraps a Durable Object server-side method in a semconv 1.41.0 SERVER span named
|
|
154
|
+
* `"<className>/<method>"` with `rpc.system.name = "cloudflare_durable_object"`,
|
|
155
|
+
* `rpc.method = "<className>/<method>"`, and `o11y.durable_object.class = className`.
|
|
156
|
+
*/
|
|
157
|
+
export function traceDurableObjectMethod(className, method, fn, options) {
|
|
158
|
+
const spec = durableObjectSpanSpec(className, method, SpanKind.SERVER);
|
|
159
|
+
const links = mergeLinks(undefined, options?.links);
|
|
160
|
+
return function (...args) {
|
|
161
|
+
return runWithSpec(spec, () => fn.apply(this, args), options, { links });
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Wraps every own enumerable function property on `methods` with {@link traceDurableObjectMethod},
|
|
166
|
+
* passing non-function properties through unchanged.
|
|
167
|
+
*/
|
|
168
|
+
export function traceDurableObjectMethods(className, methods, options) {
|
|
169
|
+
const wrapped = {};
|
|
170
|
+
for (const [key, value] of Object.entries(methods)) {
|
|
171
|
+
wrapped[key] =
|
|
172
|
+
typeof value === "function"
|
|
173
|
+
? traceDurableObjectMethod(className, key, value, options)
|
|
174
|
+
: value;
|
|
175
|
+
}
|
|
176
|
+
return wrapped;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Runs a caller-side Durable Object invocation inside a semconv 1.41.0 CLIENT span named
|
|
180
|
+
* `"<className>/<method>"` with `rpc.system.name = "cloudflare_durable_object"`,
|
|
181
|
+
* `rpc.method = "<className>/<method>"`, and `o11y.durable_object.class = className`.
|
|
182
|
+
*/
|
|
183
|
+
export function traceDurableObjectCall(className, method, fn, options) {
|
|
184
|
+
const spec = durableObjectSpanSpec(className, method, SpanKind.CLIENT);
|
|
185
|
+
const links = mergeLinks(undefined, options?.links);
|
|
186
|
+
return runWithSpec(spec, fn, options, { links });
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Wraps a Cloudflare Durable Object stub in a Proxy that traces every method invocation with a
|
|
190
|
+
* semconv 1.41.0 CLIENT span. Calls to `stub.fetch(...)` also inject the active trace context
|
|
191
|
+
* into the forwarded Request headers so the Durable Object joins the caller's trace.
|
|
192
|
+
*
|
|
193
|
+
* Note: Promise-based RPC pipelining (such as `stub.getService().doWork()`) is not preserved
|
|
194
|
+
* through this Proxy wrapper because awaited calls are wrapped in standard Promises. For
|
|
195
|
+
* pipelined RPC calls, wrap the final awaited invocation with {@link traceDurableObjectCall}.
|
|
196
|
+
*/
|
|
197
|
+
export function traceDurableObjectStub(className, stub, options) {
|
|
198
|
+
const cache = new Map();
|
|
199
|
+
return new Proxy(stub, {
|
|
200
|
+
get(target, prop) {
|
|
201
|
+
const value = Reflect.get(target, prop, target);
|
|
202
|
+
if (typeof prop !== "string" || STUB_PASSTHROUGH_KEYS.has(prop) || typeof value !== "function") {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
const raw = value;
|
|
206
|
+
const cached = cache.get(prop);
|
|
207
|
+
if (cached !== undefined && cached.raw === raw)
|
|
208
|
+
return cached.wrapped;
|
|
209
|
+
const wrapped = prop === "fetch"
|
|
210
|
+
? (...args) => traceDurableObjectCall(className, "fetch", () => {
|
|
211
|
+
const first = args[0];
|
|
212
|
+
if (first instanceof Request || typeof first === "string" || first instanceof URL) {
|
|
213
|
+
const req = first instanceof Request
|
|
214
|
+
? new Request(first, args[1])
|
|
215
|
+
: new Request(String(first), args[1]);
|
|
216
|
+
try {
|
|
217
|
+
const propImpl = options?.telemetry?.propagator ?? propagation;
|
|
218
|
+
propImpl.inject(context.active(), req.headers, HEADERS_SETTER);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// Propagation failure never breaks the DO fetch call.
|
|
222
|
+
}
|
|
223
|
+
return raw.call(target, req);
|
|
224
|
+
}
|
|
225
|
+
return raw.apply(target, args);
|
|
226
|
+
}, options)
|
|
227
|
+
: (...args) => traceDurableObjectCall(className, prop, () => raw.apply(target, args), options);
|
|
228
|
+
cache.set(prop, { raw, wrapped });
|
|
229
|
+
return wrapped;
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Runs a Durable Object alarm handler inside a root SERVER span (`"<className> alarm <callbackName>"`)
|
|
235
|
+
* with `o11y.durable_object.class`, `faas.trigger = "timer"`, and deduplicated span links from any
|
|
236
|
+
* supplied detached Carriers.
|
|
237
|
+
*/
|
|
238
|
+
export function traceAlarm(className, callbackName, fn, options) {
|
|
239
|
+
const spec = alarmSpanSpec(className, callbackName);
|
|
240
|
+
const links = mergeLinks(options?.carriers, options?.links);
|
|
241
|
+
return runWithSpec(spec, fn, options, { root: true, links });
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Runs a Cloudflare Workers scheduled/cron handler inside a root SERVER span
|
|
245
|
+
* (`"scheduled <job|cron>"`) with `faas.trigger = "timer"`, `faas.cron`, and `faas.time`.
|
|
246
|
+
*/
|
|
247
|
+
export function traceScheduled(cron, fn, options) {
|
|
248
|
+
const cronExpr = typeof cron === "string" ? cron : cron.cron;
|
|
249
|
+
const scheduledTime = options?.scheduledTime ?? (typeof cron === "string" ? undefined : cron.scheduledTime);
|
|
250
|
+
const spec = scheduledSpanSpec(cronExpr, {
|
|
251
|
+
...(options?.job === undefined ? {} : { job: options.job }),
|
|
252
|
+
...(scheduledTime === undefined ? {} : { scheduledTime }),
|
|
253
|
+
});
|
|
254
|
+
const links = mergeLinks(undefined, options?.links);
|
|
255
|
+
return runWithSpec(spec, fn, options, { root: true, links });
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Runs a Cloudflare Queues batch consumer inside a root CONSUMER span (`"process <queue>"`)
|
|
259
|
+
* with semconv 1.41.0 messaging attributes, automatically extracting detached `_o11y` Carriers
|
|
260
|
+
* from batch messages and attaching deduplicated span links back to the originating operations.
|
|
261
|
+
*/
|
|
262
|
+
export function traceQueueBatch(batch, fn, options) {
|
|
263
|
+
const queue = typeof batch === "string" ? batch : batch.queue;
|
|
264
|
+
const messageCount = options?.messageCount ?? (typeof batch === "string" ? undefined : batch.messages.length);
|
|
265
|
+
const extracted = [];
|
|
266
|
+
if (typeof batch !== "string") {
|
|
267
|
+
for (const msg of batch.messages) {
|
|
268
|
+
const c = carrierFromQueueMessage(msg, options?.carrierField);
|
|
269
|
+
if (c !== undefined)
|
|
270
|
+
extracted.push(c);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (options?.carriers !== undefined) {
|
|
274
|
+
for (const c of options.carriers) {
|
|
275
|
+
if (c !== undefined && c !== null)
|
|
276
|
+
extracted.push(c);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const spec = queueBatchSpanSpec(queue, messageCount);
|
|
280
|
+
const links = mergeLinks(extracted, options?.links);
|
|
281
|
+
return runWithSpec(spec, fn, options, { root: true, links });
|
|
282
|
+
}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Workers context manager, over AsyncLocalStorage from node:async_hooks. workerd provides that
|
|
3
|
+
* module under the nodejs_compat compatibility flag, or under nodejs_als alone. It is the only Node
|
|
4
|
+
* builtin @libo11y/workers imports.
|
|
5
|
+
*
|
|
6
|
+
* Upstream's AsyncLocalStorageContextManager imports the unprefixed "async_hooks" and binds
|
|
7
|
+
* EventEmitters, which a Worker has no use for, so this is a small manager of its own with the same
|
|
8
|
+
* semantics for functions.
|
|
9
|
+
*/
|
|
10
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
|
+
import { ROOT_CONTEXT } from "@opentelemetry/api";
|
|
12
|
+
class WorkersContextManager {
|
|
13
|
+
storage = new AsyncLocalStorage();
|
|
14
|
+
enabled = false;
|
|
15
|
+
active() {
|
|
16
|
+
if (!this.enabled)
|
|
17
|
+
return ROOT_CONTEXT;
|
|
18
|
+
return this.storage.getStore() ?? ROOT_CONTEXT;
|
|
19
|
+
}
|
|
20
|
+
with(ctx, fn, thisArg, ...args) {
|
|
21
|
+
const call = () => fn.apply(thisArg, args);
|
|
22
|
+
if (!this.enabled)
|
|
23
|
+
return call();
|
|
24
|
+
return this.storage.run(ctx, call);
|
|
25
|
+
}
|
|
26
|
+
bind(ctx, target) {
|
|
27
|
+
if (typeof target !== "function")
|
|
28
|
+
return target;
|
|
29
|
+
const fn = target;
|
|
30
|
+
const manager = this;
|
|
31
|
+
const bound = function (...args) {
|
|
32
|
+
return manager.with(ctx, () => fn.apply(this, args));
|
|
33
|
+
};
|
|
34
|
+
return bound;
|
|
35
|
+
}
|
|
36
|
+
enable() {
|
|
37
|
+
this.enabled = true;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
disable() {
|
|
41
|
+
this.enabled = false;
|
|
42
|
+
// A fresh store, so a context entered before disable() is not active after a later enable().
|
|
43
|
+
this.storage = new AsyncLocalStorage();
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** A new AsyncLocalStorage context manager, not yet enabled. */
|
|
48
|
+
export function createContextManager() {
|
|
49
|
+
return new WorkersContextManager();
|
|
50
|
+
}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The OTEL_* variables, as core's Platform.env returns them. */
|
|
2
|
+
export type OtelEnv = Readonly<Record<string, string | undefined>>;
|
|
3
|
+
/** Picks the string-valued OTEL_* keys of a Worker's env bindings. Any other key is ignored. */
|
|
4
|
+
export declare function otelEnv(bindings: Readonly<Record<string, unknown>> | undefined): OtelEnv;
|
|
5
|
+
/**
|
|
6
|
+
* Parses a W3C-baggage-style key=value list, as OTEL_EXPORTER_OTLP_HEADERS and
|
|
7
|
+
* OTEL_RESOURCE_ATTRIBUTES use: comma-separated pairs, percent-decoded, whitespace trimmed. A
|
|
8
|
+
* malformed pair is skipped.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseKeyValues(raw: string | undefined): Record<string, string>;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OTEL_* variables of a Worker. A Worker has no process environment: its variables and secrets
|
|
3
|
+
* arrive as the env bindings object each handler receives, beside bindings that are not strings
|
|
4
|
+
* (KV namespaces, service bindings, Durable Object namespaces). The string-valued OTEL_* keys of
|
|
5
|
+
* that object are the standard variables; nothing else is read.
|
|
6
|
+
*/
|
|
7
|
+
import { parseOtlpHeaders } from "@libo11y/core/internal";
|
|
8
|
+
/** Picks the string-valued OTEL_* keys of a Worker's env bindings. Any other key is ignored. */
|
|
9
|
+
export function otelEnv(bindings) {
|
|
10
|
+
const out = {};
|
|
11
|
+
if (bindings === undefined || bindings === null || typeof bindings !== "object")
|
|
12
|
+
return out;
|
|
13
|
+
let keys;
|
|
14
|
+
try {
|
|
15
|
+
keys = Object.keys(bindings);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
for (const key of keys) {
|
|
21
|
+
if (!key.startsWith("OTEL_"))
|
|
22
|
+
continue;
|
|
23
|
+
try {
|
|
24
|
+
const value = bindings[key];
|
|
25
|
+
if (typeof value === "string")
|
|
26
|
+
out[key] = value;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// A binding whose getter throws contributes nothing.
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parses a W3C-baggage-style key=value list, as OTEL_EXPORTER_OTLP_HEADERS and
|
|
36
|
+
* OTEL_RESOURCE_ATTRIBUTES use: comma-separated pairs, percent-decoded, whitespace trimmed. A
|
|
37
|
+
* malformed pair is skipped.
|
|
38
|
+
*/
|
|
39
|
+
export function parseKeyValues(raw) {
|
|
40
|
+
return parseOtlpHeaders(raw, "standard");
|
|
41
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OTLP/HTTP export over fetch, for each signal core routes to otlp or console.
|
|
3
|
+
*
|
|
4
|
+
* Configuration resolution, bounded retry (up to 2 retries / 3 total attempts with 200-1000 ms
|
|
5
|
+
* equal jitter capped within the shared export and waitUntil flush deadline), gzip compression,
|
|
6
|
+
* and bounded 2xx partialSuccess inspection all live in @libo11y/core/internal. This module keeps
|
|
7
|
+
* only the thin fetch / Workers VPC binding sender racing against an AbortController timer.
|
|
8
|
+
*/
|
|
9
|
+
import { type ExporterRequest, type ExporterSet } from "@libo11y/core";
|
|
10
|
+
import { type OtlpSignal, type OtlpTemporalityPreference, type RetryTimers } from "@libo11y/core/internal";
|
|
11
|
+
import { type ExportResult } from "@opentelemetry/core";
|
|
12
|
+
import { type ISerializer } from "@opentelemetry/otlp-transformer";
|
|
13
|
+
import type { OtelEnv } from "./env.ts";
|
|
14
|
+
/** A fetch function: the global fetch, or a binding's, such as `(req) => env.OTLP.fetch(req)`. */
|
|
15
|
+
export type WorkersFetch = (request: Request) => Promise<Response>;
|
|
16
|
+
/** Where and how the exporters send. Each field beats its OTEL_EXPORTER_OTLP_* variable. */
|
|
17
|
+
export interface WorkersExporterOptions {
|
|
18
|
+
/**
|
|
19
|
+
* The base URL, as OTEL_EXPORTER_OTLP_ENDPOINT: each signal posts to it with v1/traces, v1/metrics
|
|
20
|
+
* or v1/logs appended. Default http://localhost:4318.
|
|
21
|
+
*/
|
|
22
|
+
readonly endpoint?: string;
|
|
23
|
+
/** The full URL for spans, as OTEL_EXPORTER_OTLP_TRACES_ENDPOINT. Beats endpoint. */
|
|
24
|
+
readonly tracesEndpoint?: string;
|
|
25
|
+
/** The full URL for metrics, as OTEL_EXPORTER_OTLP_METRICS_ENDPOINT. Beats endpoint. */
|
|
26
|
+
readonly metricsEndpoint?: string;
|
|
27
|
+
/** The full URL for log records, as OTEL_EXPORTER_OTLP_LOGS_ENDPOINT. Beats endpoint. */
|
|
28
|
+
readonly logsEndpoint?: string;
|
|
29
|
+
/** Headers sent with every export, on top of those OTEL_EXPORTER_OTLP_HEADERS names. */
|
|
30
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
31
|
+
/**
|
|
32
|
+
* The function every export goes through. Default the global fetch. Pass a binding's to export
|
|
33
|
+
* through a service binding or a Workers VPC binding: `(req) => env.OTLP.fetch(req)`.
|
|
34
|
+
*/
|
|
35
|
+
readonly fetch?: WorkersFetch;
|
|
36
|
+
/** gzip the bodies with CompressionStream. Default OTEL_EXPORTER_OTLP_COMPRESSION, else "none". */
|
|
37
|
+
readonly compression?: "gzip" | "none";
|
|
38
|
+
/** The deadline of one export in milliseconds. Default OTEL_EXPORTER_OTLP_TIMEOUT, else 10000. */
|
|
39
|
+
readonly timeoutMillis?: number;
|
|
40
|
+
}
|
|
41
|
+
/** The options buildExporters takes beside core's request. */
|
|
42
|
+
export interface WorkersExportConfig {
|
|
43
|
+
readonly env: OtelEnv;
|
|
44
|
+
readonly exporter?: WorkersExporterOptions | undefined;
|
|
45
|
+
/** The least time between two metric exports of one isolate, in milliseconds. */
|
|
46
|
+
readonly metricsFlushIntervalMillis: number;
|
|
47
|
+
}
|
|
48
|
+
/** One signal's resolved export settings. */
|
|
49
|
+
export interface SignalExport {
|
|
50
|
+
readonly url: string;
|
|
51
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
52
|
+
readonly compression: "gzip" | "none";
|
|
53
|
+
readonly timeoutMillis: number;
|
|
54
|
+
readonly temporalityPreference?: OtlpTemporalityPreference;
|
|
55
|
+
readonly fetch: WorkersFetch;
|
|
56
|
+
}
|
|
57
|
+
/** Appends a signal path to a base URL as the OTLP exporter specification says. */
|
|
58
|
+
export declare function signalUrl(base: string, signal: OtlpSignal): string;
|
|
59
|
+
/** Resolves one signal's export settings: option, then signal variable, then shared variable, then default. */
|
|
60
|
+
export declare function resolveSignalExport(signal: OtlpSignal, options: WorkersExporterOptions | undefined, env: OtelEnv): SignalExport;
|
|
61
|
+
/** An export that got an HTTP status outside 2xx. */
|
|
62
|
+
export declare class OtlpHttpError extends Error {
|
|
63
|
+
readonly status: number;
|
|
64
|
+
constructor(status: number);
|
|
65
|
+
}
|
|
66
|
+
/** An export that did not finish before its deadline, or was stopped by shutdown. */
|
|
67
|
+
export declare class OtlpTimeoutError extends Error {
|
|
68
|
+
constructor(message: string);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Posts one signal's serialized batches over fetch with bounded retry inside the shared export
|
|
72
|
+
* and flush deadline. shutdown() stops what is in flight and fails every later export. Nothing
|
|
73
|
+
* here throws to the caller.
|
|
74
|
+
*/
|
|
75
|
+
export declare class FetchOtlpExporter<Internal> {
|
|
76
|
+
private readonly signal;
|
|
77
|
+
private readonly settings;
|
|
78
|
+
private readonly contentType;
|
|
79
|
+
private readonly serializer;
|
|
80
|
+
private readonly timers;
|
|
81
|
+
private readonly inFlight;
|
|
82
|
+
private stopped;
|
|
83
|
+
constructor(settings: SignalExport, contentType: string, serializer: ISerializer<Internal, unknown>, signal?: OtlpSignal, timers?: RetryTimers);
|
|
84
|
+
export(items: Internal, resultCallback: (result: ExportResult) => void): void;
|
|
85
|
+
private send;
|
|
86
|
+
private attemptSend;
|
|
87
|
+
forceFlush(): Promise<void>;
|
|
88
|
+
shutdown(): Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
/** Builds the exporters for every signal the configuration routes to "otlp" or "console". */
|
|
91
|
+
export declare function buildExporters(req: ExporterRequest, config: WorkersExportConfig): ExporterSet;
|