@mark1russell7/otel-ts 0.8.0 → 0.8.2
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/README.md +34 -1
- package/dist/context.d.ts +6 -3
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +10 -4
- package/dist/context.js.map +1 -1
- package/dist/node-exporters.d.ts +11 -0
- package/dist/node-exporters.d.ts.map +1 -1
- package/dist/node-exporters.js +25 -0
- package/dist/node-exporters.js.map +1 -1
- package/dist/node.d.ts +13 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2 -1
- package/dist/node.js.map +1 -1
- package/package.json +1 -1
- package/src/context.ts +11 -5
- package/src/node-exporters.ts +28 -0
- package/src/node.ts +15 -3
package/README.md
CHANGED
|
@@ -232,7 +232,7 @@ The rules:
|
|
|
232
232
|
- `withInternalCall(fn)` marks the calls that `fn` starts as internal calls. `isInternalCall()` reads the mark.
|
|
233
233
|
- `incomingContext(headers, { trust })` gives the context of an incoming call, for its handler. It reads the headers of `node:http` or the `Headers` of a Fetch `Request`, from the root context. Start the handler in it: `context.with(incomingContext(req.headers, { trust }), handler)`. The option `trust` tells how much of the context of the caller stays:
|
|
234
234
|
- `"all"`: the trace and the baggage, for a caller that passed its check.
|
|
235
|
-
- `"trace"` (the default): the trace only. The baggage of the caller goes.
|
|
235
|
+
- `"trace"` (the default): the trace only. The baggage of the caller goes, and each key that a propagator of the program read from the headers goes too.
|
|
236
236
|
- `"none"`: nothing. The call starts a new trace, for a public edge.
|
|
237
237
|
|
|
238
238
|
In the browser, `init()` registers the context when `tracing` is on (the default):
|
|
@@ -443,6 +443,39 @@ With export on, the exporters come from `OTEL_TRACES_EXPORTER` and `OTEL_LOGS_EX
|
|
|
443
443
|
|
|
444
444
|
By default, it stops the SDK and sends the last spans at `SIGTERM` and `SIGINT`. Then, when the service has no listener of its own for the signal, the process ends with the signal, as it does without `startNodeTelemetry()`. When the service has a listener, that listener ends the process. Set `stopOnSignals: false` to add no listener.
|
|
445
445
|
|
|
446
|
+
The option `resourceAttributes` gives the attributes of the resource of the service, for example `service.version` and `service.instance.id` (the copy of the service). They win over the detected attributes and over `OTEL_RESOURCE_ATTRIBUTES`. `service.name` comes from `serviceName`. The detectors are those of the SDK, from `OTEL_NODE_RESOURCE_DETECTORS`.
|
|
447
|
+
|
|
448
|
+
### A service of `@mark1russell7/service`
|
|
449
|
+
|
|
450
|
+
A service of [`@mark1russell7/service`](https://github.com/mark1russell7/systems/tree/main/packages/service) has a staged shutdown of its own: after the signal, it drains its requests. The stop of telemetry must come after the drain. With `stopOnSignals: true`, the stop removes the context at the signal: the log lines of the drain lose their trace ids, and the spans of the drained requests go. So:
|
|
451
|
+
|
|
452
|
+
1. Start telemetry with `stopOnSignals: false`, in the module that the service loads first.
|
|
453
|
+
2. Give `telemetry.shutdown` to the last stage of the shutdown, `flush the output`.
|
|
454
|
+
3. Give `traceIds` to the logger and `callContext` to the transports and the clients, from `@mark1russell7/service/otel`.
|
|
455
|
+
|
|
456
|
+
```ts
|
|
457
|
+
// telemetry.ts: node --import ./src/telemetry.ts src/main.ts
|
|
458
|
+
import { startNodeTelemetry } from "@mark1russell7/otel-ts/node";
|
|
459
|
+
|
|
460
|
+
export const telemetry = startNodeTelemetry({
|
|
461
|
+
serviceName: "my-service",
|
|
462
|
+
resourceAttributes: { "service.version": "1.2.3", "service.instance.id": "copy-1" },
|
|
463
|
+
importMetaUrl: import.meta.url,
|
|
464
|
+
stopOnSignals: false,
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
// main.ts: the same module, thus the same telemetry
|
|
468
|
+
import { consoleLogger } from "@mark1russell7/service";
|
|
469
|
+
import { httpTransport } from "@mark1russell7/service/http";
|
|
470
|
+
import { callContext, traceIds } from "@mark1russell7/service/otel";
|
|
471
|
+
import { telemetry } from "./telemetry.ts";
|
|
472
|
+
|
|
473
|
+
svc.phase("flush the output", s(2));
|
|
474
|
+
svc.onPhase("flush the output", () => telemetry.shutdown());
|
|
475
|
+
const logger = consoleLogger({ traceIds });
|
|
476
|
+
const http = httpTransport(server, knobs, { listen, callContext });
|
|
477
|
+
```
|
|
478
|
+
|
|
446
479
|
The option `env` sets `enabled`, the service name, the exporter names and the protocols. An OTLP exporter and the sampler read their other settings (the endpoint, the headers, `OTEL_TRACES_SAMPLER`) from `process.env`. To send to another endpoint, give `traceExporter` and `logExporter`.
|
|
447
480
|
|
|
448
481
|
### The trace of a write, in its echo
|
package/dist/context.d.ts
CHANGED
|
@@ -94,7 +94,8 @@ export type IncomingHeaders = IncomingHeaderRecord | Headers;
|
|
|
94
94
|
/**
|
|
95
95
|
* How much of the context of a caller a server keeps:
|
|
96
96
|
* - "all": the trace and the baggage, for a caller that passed its check.
|
|
97
|
-
* - "trace": the trace only
|
|
97
|
+
* - "trace": the trace only, that is the span context of the caller (its trace id, span id, flags and trace state).
|
|
98
|
+
* Each other value that the propagator reads goes: the baggage, and each key of a propagator of the program.
|
|
98
99
|
* - "none": nothing. The call starts a new trace from the root context, for a public edge.
|
|
99
100
|
* The W3C trace context lets a server restart the trace of a caller that it does not trust.
|
|
100
101
|
*/
|
|
@@ -108,8 +109,10 @@ export interface IncomingContextOptions {
|
|
|
108
109
|
* The context of an incoming call, for its handler (docs/CONTEXT-DESIGN.md, section 5). The propagator that
|
|
109
110
|
* the program registers reads the trace context and the baggage from the headers. The read starts from the
|
|
110
111
|
* root context, thus the call gets no value of the context that runs the server. The option `trust` tells
|
|
111
|
-
* how much of the context of the caller stays: "all", "trace" (the default) or "none".
|
|
112
|
-
*
|
|
112
|
+
* how much of the context of the caller stays: "all", "trace" (the default) or "none". With "trace", the result
|
|
113
|
+
* holds only the span context that the propagator read. A propagator of the program that reads the baggage
|
|
114
|
+
* header, or another header, into a key of its own thus gives that key to a trusted call only. Without a
|
|
115
|
+
* registered propagator, the result is the root context.
|
|
113
116
|
*/
|
|
114
117
|
export declare function incomingContext(headers: IncomingHeaders, options?: IncomingContextOptions): Context;
|
|
115
118
|
//# sourceMappingURL=context.d.ts.map
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAA+D,KAAK,OAAO,EAAsB,MAAM,oBAAoB,CAAC;AAEnI,iGAAiG;AACjG,MAAM,MAAM,qBAAqB,GAAG,KAAK,GAAG,MAAM,CAAC;AAEnD,yGAAyG;AACzG,MAAM,MAAM,qBAAqB,GAAG,UAAU,GAAG,MAAM,CAAC;AAExD,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,WAAW,EAAE,qBAAqB,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAC1C,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,wDAAwD;AACxD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,WAAW,EAAE,qBAAqB,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAC1C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,gFAAgF;AAChF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IAAE,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;CAAE,CAAC;AAEzG,8EAA8E;AAC9E,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,MAAM;IAC3C,wCAAwC;IACxC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,C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qE;AACrE,wBAAgB,cAAc,CAAC,IAAI,GAAE,OAA0B,GAAG,OAAO,CAExE;AAED,qGAAqG;AACrG,MAAM,MAAM,oBAAoB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC;AAEpG,uGAAuG;AACvG,MAAM,MAAM,eAAe,GAAG,oBAAoB,GAAG,OAAO,CAAC;AAE7D;;;;;;;GAOG;AACH,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,CAAC;AAErD,uCAAuC;AACvC,MAAM,WAAW,sBAAsB;IACrC,qFAAqF;IACrF,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC;CAChC;AAoBD;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,eAAe,EAAE,OAAO,GAAE,sBAA2B,GAAG,OAAO,CAavG"}
|
package/dist/context.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @packageDocumentation
|
|
18
18
|
*/
|
|
19
|
-
import { context, createContextKey, propagation, ROOT_CONTEXT } from "@opentelemetry/api";
|
|
19
|
+
import { context, createContextKey, propagation, ROOT_CONTEXT, trace } from "@opentelemetry/api";
|
|
20
20
|
/** The default of `maxLength` */
|
|
21
21
|
export const DEFAULT_MAX_LENGTH = 256;
|
|
22
22
|
/*
|
|
@@ -185,8 +185,10 @@ const isFetchHeaders = (headers) => typeof headers.get === "function";
|
|
|
185
185
|
* The context of an incoming call, for its handler (docs/CONTEXT-DESIGN.md, section 5). The propagator that
|
|
186
186
|
* the program registers reads the trace context and the baggage from the headers. The read starts from the
|
|
187
187
|
* root context, thus the call gets no value of the context that runs the server. The option `trust` tells
|
|
188
|
-
* how much of the context of the caller stays: "all", "trace" (the default) or "none".
|
|
189
|
-
*
|
|
188
|
+
* how much of the context of the caller stays: "all", "trace" (the default) or "none". With "trace", the result
|
|
189
|
+
* holds only the span context that the propagator read. A propagator of the program that reads the baggage
|
|
190
|
+
* header, or another header, into a key of its own thus gives that key to a trusted call only. Without a
|
|
191
|
+
* registered propagator, the result is the root context.
|
|
190
192
|
*/
|
|
191
193
|
export function incomingContext(headers, options = {}) {
|
|
192
194
|
const trust = options.trust ?? "trace";
|
|
@@ -198,6 +200,10 @@ export function incomingContext(headers, options = {}) {
|
|
|
198
200
|
const read = isFetchHeaders(headers)
|
|
199
201
|
? propagation.extract(ROOT_CONTEXT, headers, fetchGetter)
|
|
200
202
|
: propagation.extract(ROOT_CONTEXT, headers, recordGetter);
|
|
201
|
-
|
|
203
|
+
if (trust === "all")
|
|
204
|
+
return read;
|
|
205
|
+
// Only the span context stays. Before, only the W3C baggage went, and each other key of the propagator stayed.
|
|
206
|
+
const span = trace.getSpanContext(read);
|
|
207
|
+
return span === undefined ? ROOT_CONTEXT : trace.setSpanContext(ROOT_CONTEXT, span);
|
|
202
208
|
}
|
|
203
209
|
//# sourceMappingURL=context.js.map
|
package/dist/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,YAAY,EAAoC,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,YAAY,EAAE,KAAK,EAAoC,MAAM,oBAAoB,CAAC;AAqCnI,iCAAiC;AACjC,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,GAAG,gCAAgC,CAAC;AAErD;;;GAGG;AACH,MAAM,QAAQ,GAAG,MAAM,C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E,OAAO,EAAE,YAAY,CAAC,CAAC;IAC7D,IAAI,KAAK,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IACjC,+GAA+G;IAC/G,MAAM,IAAI,GAAG,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IACxC,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,cAAc,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;AACtF,CAAC"}
|
package/dist/node-exporters.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type LogRecordExporter, type LogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
2
|
+
import type { Attributes } from "@opentelemetry/api";
|
|
3
|
+
import { type ResourceDetector } from "@opentelemetry/resources";
|
|
2
4
|
import { type AggregationSelector } from "@opentelemetry/sdk-metrics";
|
|
3
5
|
import { type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
4
6
|
type Env = Readonly<Record<string, string | undefined>>;
|
|
@@ -24,6 +26,15 @@ export declare function otlpProtocol(signal: OtlpSignal, env: Env): OtlpProtocol
|
|
|
24
26
|
* Another value is an error.
|
|
25
27
|
*/
|
|
26
28
|
export declare function histogramAggregation(env: Env): AggregationSelector;
|
|
29
|
+
/**
|
|
30
|
+
* The resource detectors that the NodeSDK uses: the names of `OTEL_NODE_RESOURCE_DETECTORS` (`host`, `os`,
|
|
31
|
+
* `serviceinstance`, `process`, `env`, `all` or `none`; default `all`), then a detector that gives
|
|
32
|
+
* `attributes`. That last detector wins over each other detector, also over `OTEL_RESOURCE_ATTRIBUTES` (the
|
|
33
|
+
* detector `env`), as the option `resourceAttributes` of `startNodeSDK()` of the SDK does. Without it, a
|
|
34
|
+
* `service.instance.id` of the service lost to the random id of the detector `serviceinstance`. The NodeSDK
|
|
35
|
+
* ignores a name that it does not know, with a warning. This function ignores it too.
|
|
36
|
+
*/
|
|
37
|
+
export declare function nodeResourceDetectors(env: Env, attributes: Attributes): ResourceDetector[];
|
|
27
38
|
/**
|
|
28
39
|
* The span processors: the context processor, then a processor for `exporter`,
|
|
29
40
|
* or for each exporter of the variables. No exporter gives no processor.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-exporters.d.ts","sourceRoot":"","sources":["../src/node-exporters.ts"],"names":[],"mappings":"AAYA,OAAO,EAIL,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAmC,KAAK,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACvG,OAAO,EAAgE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAG/I,KAAK,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAExD,mCAAmC;AACnC,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,MAAM,CAAC;AAC3C,6FAA6F;AAC7F,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,CAAC;AAC9C,0CAA0C;AAC1C,MAAM,MAAM,YAAY,GAAG,eAAe,GAAG,WAAW,CAAC;AAEzD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,GAAG,YAAY,EAAE,CAS1E;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,GAAG,YAAY,CAOvE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,GAAG,GAAG,mBAAmB,CAQlE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,YAAY,GAAG,aAAa,EAAE,CASrF;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ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EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,YAAY,GAAG,aAAa,EAAE,CASrF;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,EAAE,CASpG"}
|
package/dist/node-exporters.js
CHANGED
|
@@ -11,6 +11,7 @@ import { OTLPLogExporter as ProtobufLogExporter } from "@opentelemetry/exporter-
|
|
|
11
11
|
import { OTLPTraceExporter as JsonTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
12
12
|
import { OTLPTraceExporter as ProtobufTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
13
13
|
import { BatchLogRecordProcessor, ConsoleLogRecordExporter, SimpleLogRecordProcessor, } from "@opentelemetry/sdk-logs";
|
|
14
|
+
import { envDetector, hostDetector, osDetector, processDetector, serviceInstanceIdDetector } from "@opentelemetry/resources";
|
|
14
15
|
import { AggregationType, InstrumentType } from "@opentelemetry/sdk-metrics";
|
|
15
16
|
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace";
|
|
16
17
|
import { createContextLogRecordProcessor, createContextSpanProcessor, defaultAggregation } from "./context-processors.js";
|
|
@@ -59,6 +60,30 @@ export function histogramAggregation(env) {
|
|
|
59
60
|
}
|
|
60
61
|
throw new RangeError(`${variable} has "${value}": otel-ts makes "explicit_bucket_histogram" or "base2_exponential_bucket_histogram"`);
|
|
61
62
|
}
|
|
63
|
+
/** The resource detectors of `OTEL_NODE_RESOURCE_DETECTORS`, with the names and the sequence of the NodeSDK. `env` is last. */
|
|
64
|
+
const RESOURCE_DETECTORS = new Map([
|
|
65
|
+
["host", hostDetector],
|
|
66
|
+
["os", osDetector],
|
|
67
|
+
["serviceinstance", serviceInstanceIdDetector],
|
|
68
|
+
["process", processDetector],
|
|
69
|
+
["env", envDetector],
|
|
70
|
+
]);
|
|
71
|
+
/**
|
|
72
|
+
* The resource detectors that the NodeSDK uses: the names of `OTEL_NODE_RESOURCE_DETECTORS` (`host`, `os`,
|
|
73
|
+
* `serviceinstance`, `process`, `env`, `all` or `none`; default `all`), then a detector that gives
|
|
74
|
+
* `attributes`. That last detector wins over each other detector, also over `OTEL_RESOURCE_ATTRIBUTES` (the
|
|
75
|
+
* detector `env`), as the option `resourceAttributes` of `startNodeSDK()` of the SDK does. Without it, a
|
|
76
|
+
* `service.instance.id` of the service lost to the random id of the detector `serviceinstance`. The NodeSDK
|
|
77
|
+
* ignores a name that it does not know, with a warning. This function ignores it too.
|
|
78
|
+
*/
|
|
79
|
+
export function nodeResourceDetectors(env, attributes) {
|
|
80
|
+
const names = (env["OTEL_NODE_RESOURCE_DETECTORS"] ?? "")
|
|
81
|
+
.split(",")
|
|
82
|
+
.map((name) => name.trim())
|
|
83
|
+
.filter((name) => name !== "");
|
|
84
|
+
const chosen = names.length === 0 || names.includes("all") ? [...RESOURCE_DETECTORS.values()] : names.includes("none") ? [] : names.flatMap((name) => RESOURCE_DETECTORS.get(name) ?? []);
|
|
85
|
+
return [...chosen, { detect: () => ({ attributes: { ...attributes } }) }];
|
|
86
|
+
}
|
|
62
87
|
/**
|
|
63
88
|
* The span processors: the context processor, then a processor for `exporter`,
|
|
64
89
|
* or for each exporter of the variables. No exporter gives no processor.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-exporters.js","sourceRoot":"","sources":["../src/node-exporters.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,eAAe,IAAI,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAC5F,OAAO,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AACjG,OAAO,EAAE,iBAAiB,IAAI,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AACjG,OAAO,EAAE,iBAAiB,IAAI,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AACtG,OAAO,EACL,uBAAuB,EACvB,w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AAC,IAAI,EAAE,EAAE,CACtC,IAAI,KAAK,SAAS;YAChB,CAAC,CAAC,IAAI,wBAAwB,CAAC,EAAE,QAAQ,EAAE,IAAI,wBAAwB,EAAE,EAAE,CAAC;YAC5E,CAAC,CAAC,IAAI,uBAAuB,CAAC,EAAE,QAAQ,EAAE,YAAY,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,eAAe,EAAE,CAAC,CAAC,CAAC,IAAI,mBAAmB,EAAE,EAAE,CAAC,CAC7I,CAAC;IACN,OAAO,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,+BAA+B,EAAE,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACvF,CAAC"}
|
package/dist/node.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Attributes } from "@opentelemetry/api";
|
|
1
2
|
import { type NodeSDKConfiguration } from "@opentelemetry/sdk-node";
|
|
2
3
|
import type { LogRecordExporter } from "@opentelemetry/sdk-logs";
|
|
3
4
|
import type { SpanExporter } from "@opentelemetry/sdk-trace";
|
|
@@ -7,6 +8,12 @@ export { outgoingTraceparent, createTraceEcho, runInRemoteTrace, type RemoteTrac
|
|
|
7
8
|
export interface NodeTelemetryOptions {
|
|
8
9
|
/** The service name. Default: `OTEL_SERVICE_NAME`, else "node-service". */
|
|
9
10
|
serviceName?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The attributes of the resource of the service, for example `service.version` and `service.instance.id`
|
|
13
|
+
* (the copy of the service). They win over the detected attributes and over `OTEL_RESOURCE_ATTRIBUTES`.
|
|
14
|
+
* `service.name` comes from `serviceName`. Default: none, and the resource is the resource of the SDK.
|
|
15
|
+
*/
|
|
16
|
+
resourceAttributes?: Attributes;
|
|
10
17
|
/**
|
|
11
18
|
* The `import.meta.url` of the module that calls this function. The loader
|
|
12
19
|
* hook resolves from it, so it is the instance of the service's
|
|
@@ -35,7 +42,12 @@ export interface NodeTelemetryOptions {
|
|
|
35
42
|
/**
|
|
36
43
|
* Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
|
|
37
44
|
* service has no listener of its own for the signal, the process then ends
|
|
38
|
-
* with the signal, as it does without this option.
|
|
45
|
+
* with the signal, as it does without this option. A service with a staged
|
|
46
|
+
* shutdown of its own (for example `@mark1russell7/service`) gives false and
|
|
47
|
+
* calls `shutdown()` in its last stage. With true, the stop removes the
|
|
48
|
+
* context at the signal, while the service still drains its requests: the
|
|
49
|
+
* log lines of the drain lose their trace ids, and the spans of the drained
|
|
50
|
+
* requests go.
|
|
39
51
|
*/
|
|
40
52
|
stopOnSignals?: boolean;
|
|
41
53
|
/**
|
package/dist/node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAyBA,OAAO,EAA8C,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACjG,OAAO,EAAW,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAK7D,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAE/I,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAAE,KAAK,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAElI,kDAAkD;AAClD,MAAM,WAAW,oBAAoB;IACnC,2EAA2E;IAC3E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,UAAU,CAAC;IAChC;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iEAAiE;IACjE,gBAAgB,CAAC,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,CAAC;IAC5D;;;;;OAKG;IACH,aAAa,CAAC,EAAE,YAAY,CAAC;IAC7B,kHAAkH;IAClH,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,yCAAyC;IACzC,aAAa,CAAC,EAAE,oBAAoB,CAAC,eAAe,CAAC,CAAC;IACtD;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CACzB;AAQD,2CAA2C;AAC3C,MAAM,WAAW,aAAa;IAC5B,6BAA6B;IAC7B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,yHAAyH;IACzH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,oBAAyB,GAAG,aAAa,CAgFpF"}
|
package/dist/node.js
CHANGED
|
@@ -27,7 +27,7 @@ import { context, logs, metrics, propagation, trace } from "@opentelemetry/api";
|
|
|
27
27
|
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
28
28
|
import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
|
|
29
29
|
import { contextMetricViews } from "./context-processors.js";
|
|
30
|
-
import { histogramAggregation, nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
|
|
30
|
+
import { histogramAggregation, nodeLogRecordProcessors, nodeResourceDetectors, nodeSpanProcessors } from "./node-exporters.js";
|
|
31
31
|
export { exporterNames, histogramAggregation, otlpProtocol } from "./node-exporters.js";
|
|
32
32
|
export { outgoingTraceparent, createTraceEcho, runInRemoteTrace } from "./trace-echo.js";
|
|
33
33
|
/** The signals that stop the SDK when `stopOnSignals` is true */
|
|
@@ -65,6 +65,7 @@ export function startNodeTelemetry(options = {}) {
|
|
|
65
65
|
// the views replace the preference of each reader
|
|
66
66
|
views: contextMetricViews(histogramAggregation(env)),
|
|
67
67
|
...exports,
|
|
68
|
+
...(options.resourceAttributes ? { resourceDetectors: nodeResourceDetectors(env, options.resourceAttributes) } : {}),
|
|
68
69
|
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
69
70
|
});
|
|
70
71
|
sdk.start();
|
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAmB,MAAM,oBAAoB,CAAC;AACjG,OAAO,EAAE,OAAO,EAA6B,MAAM,yBAAyB,CAAC;AAG7E,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAE/H,OA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2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/otel-ts",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.2",
|
|
5
5
|
"description": "Frontend observability in one init() call — OpenTelemetry + Grafana Faro for any TypeScript web app",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Mark Russell (https://github.com/mark1russell7)",
|
package/src/context.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @packageDocumentation
|
|
18
18
|
*/
|
|
19
|
-
import { context, createContextKey, propagation, ROOT_CONTEXT, type Context, type TextMapGetter } from "@opentelemetry/api";
|
|
19
|
+
import { context, createContextKey, propagation, ROOT_CONTEXT, trace, type Context, type TextMapGetter } from "@opentelemetry/api";
|
|
20
20
|
|
|
21
21
|
/** How many distinct values a key has: "low" may become a metric attribute, "high" never does */
|
|
22
22
|
export type ContextKeyCardinality = "low" | "high";
|
|
@@ -215,7 +215,8 @@ export type IncomingHeaders = IncomingHeaderRecord | Headers;
|
|
|
215
215
|
/**
|
|
216
216
|
* How much of the context of a caller a server keeps:
|
|
217
217
|
* - "all": the trace and the baggage, for a caller that passed its check.
|
|
218
|
-
* - "trace": the trace only
|
|
218
|
+
* - "trace": the trace only, that is the span context of the caller (its trace id, span id, flags and trace state).
|
|
219
|
+
* Each other value that the propagator reads goes: the baggage, and each key of a propagator of the program.
|
|
219
220
|
* - "none": nothing. The call starts a new trace from the root context, for a public edge.
|
|
220
221
|
* The W3C trace context lets a server restart the trace of a caller that it does not trust.
|
|
221
222
|
*/
|
|
@@ -249,8 +250,10 @@ const isFetchHeaders = (headers: IncomingHeaders): headers is Headers => typeof
|
|
|
249
250
|
* The context of an incoming call, for its handler (docs/CONTEXT-DESIGN.md, section 5). The propagator that
|
|
250
251
|
* the program registers reads the trace context and the baggage from the headers. The read starts from the
|
|
251
252
|
* root context, thus the call gets no value of the context that runs the server. The option `trust` tells
|
|
252
|
-
* how much of the context of the caller stays: "all", "trace" (the default) or "none".
|
|
253
|
-
*
|
|
253
|
+
* how much of the context of the caller stays: "all", "trace" (the default) or "none". With "trace", the result
|
|
254
|
+
* holds only the span context that the propagator read. A propagator of the program that reads the baggage
|
|
255
|
+
* header, or another header, into a key of its own thus gives that key to a trusted call only. Without a
|
|
256
|
+
* registered propagator, the result is the root context.
|
|
254
257
|
*/
|
|
255
258
|
export function incomingContext(headers: IncomingHeaders, options: IncomingContextOptions = {}): Context {
|
|
256
259
|
const trust = options.trust ?? "trace";
|
|
@@ -261,5 +264,8 @@ export function incomingContext(headers: IncomingHeaders, options: IncomingConte
|
|
|
261
264
|
const read = isFetchHeaders(headers)
|
|
262
265
|
? propagation.extract(ROOT_CONTEXT, headers, fetchGetter)
|
|
263
266
|
: propagation.extract(ROOT_CONTEXT, headers, recordGetter);
|
|
264
|
-
|
|
267
|
+
if (trust === "all") return read;
|
|
268
|
+
// Only the span context stays. Before, only the W3C baggage went, and each other key of the propagator stayed.
|
|
269
|
+
const span = trace.getSpanContext(read);
|
|
270
|
+
return span === undefined ? ROOT_CONTEXT : trace.setSpanContext(ROOT_CONTEXT, span);
|
|
265
271
|
}
|
package/src/node-exporters.ts
CHANGED
|
@@ -17,6 +17,8 @@ import {
|
|
|
17
17
|
type LogRecordExporter,
|
|
18
18
|
type LogRecordProcessor,
|
|
19
19
|
} from "@opentelemetry/sdk-logs";
|
|
20
|
+
import type { Attributes } from "@opentelemetry/api";
|
|
21
|
+
import { envDetector, hostDetector, osDetector, processDetector, serviceInstanceIdDetector, type ResourceDetector } from "@opentelemetry/resources";
|
|
20
22
|
import { AggregationType, InstrumentType, type AggregationSelector } from "@opentelemetry/sdk-metrics";
|
|
21
23
|
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor, type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
22
24
|
import { createContextLogRecordProcessor, createContextSpanProcessor, defaultAggregation } from "./context-processors.js";
|
|
@@ -73,6 +75,32 @@ export function histogramAggregation(env: Env): AggregationSelector {
|
|
|
73
75
|
throw new RangeError(`${variable} has "${value}": otel-ts makes "explicit_bucket_histogram" or "base2_exponential_bucket_histogram"`);
|
|
74
76
|
}
|
|
75
77
|
|
|
78
|
+
/** The resource detectors of `OTEL_NODE_RESOURCE_DETECTORS`, with the names and the sequence of the NodeSDK. `env` is last. */
|
|
79
|
+
const RESOURCE_DETECTORS: ReadonlyMap<string, ResourceDetector> = new Map([
|
|
80
|
+
["host", hostDetector],
|
|
81
|
+
["os", osDetector],
|
|
82
|
+
["serviceinstance", serviceInstanceIdDetector],
|
|
83
|
+
["process", processDetector],
|
|
84
|
+
["env", envDetector],
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The resource detectors that the NodeSDK uses: the names of `OTEL_NODE_RESOURCE_DETECTORS` (`host`, `os`,
|
|
89
|
+
* `serviceinstance`, `process`, `env`, `all` or `none`; default `all`), then a detector that gives
|
|
90
|
+
* `attributes`. That last detector wins over each other detector, also over `OTEL_RESOURCE_ATTRIBUTES` (the
|
|
91
|
+
* detector `env`), as the option `resourceAttributes` of `startNodeSDK()` of the SDK does. Without it, a
|
|
92
|
+
* `service.instance.id` of the service lost to the random id of the detector `serviceinstance`. The NodeSDK
|
|
93
|
+
* ignores a name that it does not know, with a warning. This function ignores it too.
|
|
94
|
+
*/
|
|
95
|
+
export function nodeResourceDetectors(env: Env, attributes: Attributes): ResourceDetector[] {
|
|
96
|
+
const names = (env["OTEL_NODE_RESOURCE_DETECTORS"] ?? "")
|
|
97
|
+
.split(",")
|
|
98
|
+
.map((name) => name.trim())
|
|
99
|
+
.filter((name) => name !== "");
|
|
100
|
+
const chosen = names.length === 0 || names.includes("all") ? [...RESOURCE_DETECTORS.values()] : names.includes("none") ? [] : names.flatMap((name) => RESOURCE_DETECTORS.get(name) ?? []);
|
|
101
|
+
return [...chosen, { detect: () => ({ attributes: { ...attributes } }) }];
|
|
102
|
+
}
|
|
103
|
+
|
|
76
104
|
/**
|
|
77
105
|
* The span processors: the context processor, then a processor for `exporter`,
|
|
78
106
|
* or for each exporter of the variables. No exporter gives no processor.
|
package/src/node.ts
CHANGED
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
* @packageDocumentation
|
|
24
24
|
*/
|
|
25
25
|
import { register } from "node:module";
|
|
26
|
-
import { context, logs, metrics, propagation, trace } from "@opentelemetry/api";
|
|
26
|
+
import { context, logs, metrics, propagation, trace, type Attributes } from "@opentelemetry/api";
|
|
27
27
|
import { NodeSDK, type NodeSDKConfiguration } from "@opentelemetry/sdk-node";
|
|
28
28
|
import type { LogRecordExporter } from "@opentelemetry/sdk-logs";
|
|
29
29
|
import type { SpanExporter } from "@opentelemetry/sdk-trace";
|
|
30
30
|
import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
|
|
31
31
|
import { contextMetricViews } from "./context-processors.js";
|
|
32
|
-
import { histogramAggregation, nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
|
|
32
|
+
import { histogramAggregation, nodeLogRecordProcessors, nodeResourceDetectors, nodeSpanProcessors } from "./node-exporters.js";
|
|
33
33
|
|
|
34
34
|
export { exporterNames, histogramAggregation, otlpProtocol, type ExporterName, type OtlpProtocol, type OtlpSignal } from "./node-exporters.js";
|
|
35
35
|
|
|
@@ -39,6 +39,12 @@ export { outgoingTraceparent, createTraceEcho, runInRemoteTrace, type RemoteTrac
|
|
|
39
39
|
export interface NodeTelemetryOptions {
|
|
40
40
|
/** The service name. Default: `OTEL_SERVICE_NAME`, else "node-service". */
|
|
41
41
|
serviceName?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The attributes of the resource of the service, for example `service.version` and `service.instance.id`
|
|
44
|
+
* (the copy of the service). They win over the detected attributes and over `OTEL_RESOURCE_ATTRIBUTES`.
|
|
45
|
+
* `service.name` comes from `serviceName`. Default: none, and the resource is the resource of the SDK.
|
|
46
|
+
*/
|
|
47
|
+
resourceAttributes?: Attributes;
|
|
42
48
|
/**
|
|
43
49
|
* The `import.meta.url` of the module that calls this function. The loader
|
|
44
50
|
* hook resolves from it, so it is the instance of the service's
|
|
@@ -67,7 +73,12 @@ export interface NodeTelemetryOptions {
|
|
|
67
73
|
/**
|
|
68
74
|
* Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
|
|
69
75
|
* service has no listener of its own for the signal, the process then ends
|
|
70
|
-
* with the signal, as it does without this option.
|
|
76
|
+
* with the signal, as it does without this option. A service with a staged
|
|
77
|
+
* shutdown of its own (for example `@mark1russell7/service`) gives false and
|
|
78
|
+
* calls `shutdown()` in its last stage. With true, the stop removes the
|
|
79
|
+
* context at the signal, while the service still drains its requests: the
|
|
80
|
+
* log lines of the drain lose their trace ids, and the spans of the drained
|
|
81
|
+
* requests go.
|
|
71
82
|
*/
|
|
72
83
|
stopOnSignals?: boolean;
|
|
73
84
|
/**
|
|
@@ -128,6 +139,7 @@ export function startNodeTelemetry(options: NodeTelemetryOptions = {}): NodeTele
|
|
|
128
139
|
// the views replace the preference of each reader
|
|
129
140
|
views: contextMetricViews(histogramAggregation(env)),
|
|
130
141
|
...exports,
|
|
142
|
+
...(options.resourceAttributes ? { resourceDetectors: nodeResourceDetectors(env, options.resourceAttributes) } : {}),
|
|
131
143
|
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
132
144
|
});
|
|
133
145
|
sdk.start();
|