@mark1russell7/otel-ts 0.3.0 → 0.4.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/README.md +50 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/context-processors.d.ts +38 -0
- package/dist/context-processors.d.ts.map +1 -0
- package/dist/context-processors.js +76 -0
- package/dist/context-processors.js.map +1 -0
- package/dist/context-propagator.d.ts +39 -0
- package/dist/context-propagator.d.ts.map +1 -0
- package/dist/context-propagator.js +88 -0
- package/dist/context-propagator.js.map +1 -0
- package/dist/context.d.ts +90 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +169 -0
- package/dist/context.js.map +1 -0
- package/dist/node-exporters.d.ts +33 -0
- package/dist/node-exporters.d.ts.map +1 -0
- package/dist/node-exporters.js +70 -0
- package/dist/node-exporters.js.map +1 -0
- package/dist/node.d.ts +28 -10
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +51 -12
- package/dist/node.js.map +1 -1
- package/dist/session-processors.d.ts.map +1 -1
- package/dist/session-processors.js +6 -19
- package/dist/session-processors.js.map +1 -1
- package/package.json +7 -1
- package/src/context-processors.ts +84 -0
- package/src/context-propagator.ts +96 -0
- package/src/context.ts +207 -0
- package/src/node-exporters.ts +88 -0
- package/src/node.ts +75 -22
- package/src/session-processors.ts +6 -19
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The span and log processors of a Node service (docs/CONTEXT-DESIGN.md, section 4).
|
|
3
|
+
*
|
|
4
|
+
* The NodeSDK makes its processors from the `OTEL_*` variables, but it has no
|
|
5
|
+
* public way to add a processor to them. So otel-ts makes them: the context
|
|
6
|
+
* processor of C3 first, then one processor for each exporter that the
|
|
7
|
+
* variables name. The sampler and the metric readers stay those of the SDK.
|
|
8
|
+
*/
|
|
9
|
+
import { OTLPLogExporter as JsonLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
10
|
+
import { OTLPLogExporter as ProtobufLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
|
|
11
|
+
import { OTLPTraceExporter as JsonTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
12
|
+
import { OTLPTraceExporter as ProtobufTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
13
|
+
import { BatchLogRecordProcessor, ConsoleLogRecordExporter, SimpleLogRecordProcessor, } from "@opentelemetry/sdk-logs";
|
|
14
|
+
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
15
|
+
import { createContextLogRecordProcessor, createContextSpanProcessor } from "./context-processors.js";
|
|
16
|
+
/**
|
|
17
|
+
* The exporters of `OTEL_<SIGNAL>_EXPORTER`, a list with commas. Default:
|
|
18
|
+
* `otlp`. With `none` in the list: no exporter. Another name is an error.
|
|
19
|
+
*/
|
|
20
|
+
export function exporterNames(signal, env) {
|
|
21
|
+
const variable = `OTEL_${signal.toUpperCase()}_EXPORTER`;
|
|
22
|
+
const names = [...new Set((env[variable] ?? "").split(",").map((name) => name.trim().toLowerCase()).filter(Boolean))];
|
|
23
|
+
if (names.length === 0)
|
|
24
|
+
return ["otlp"];
|
|
25
|
+
if (names.includes("none"))
|
|
26
|
+
return [];
|
|
27
|
+
for (const name of names) {
|
|
28
|
+
if (name !== "otlp" && name !== "console")
|
|
29
|
+
throw new RangeError(`${variable} has "${name}": otel-ts makes "otlp", "console" or "none"`);
|
|
30
|
+
}
|
|
31
|
+
return names;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The protocol of `OTEL_EXPORTER_OTLP_<SIGNAL>_PROTOCOL`, else of
|
|
35
|
+
* `OTEL_EXPORTER_OTLP_PROTOCOL`. Default: `http/protobuf`, as the SDK.
|
|
36
|
+
*/
|
|
37
|
+
export function otlpProtocol(signal, env) {
|
|
38
|
+
const variable = `OTEL_EXPORTER_OTLP_${signal.toUpperCase()}_PROTOCOL`;
|
|
39
|
+
const value = (env[variable] ?? env["OTEL_EXPORTER_OTLP_PROTOCOL"] ?? "http/protobuf").trim();
|
|
40
|
+
if (value === "http/protobuf" || value === "http/json")
|
|
41
|
+
return value;
|
|
42
|
+
throw new RangeError(`the OTLP protocol of ${signal} is "${value}": otel-ts sends "http/protobuf" or "http/json". For grpc, give the exporter in the options.`);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The span processors: the context processor, then a processor for `exporter`,
|
|
46
|
+
* or for each exporter of the variables. No exporter gives no processor.
|
|
47
|
+
* An OTLP exporter reads its endpoint, headers and timeout from `process.env`.
|
|
48
|
+
*/
|
|
49
|
+
export function nodeSpanProcessors(env, exporter) {
|
|
50
|
+
const exporting = exporter
|
|
51
|
+
? [new BatchSpanProcessor(exporter)]
|
|
52
|
+
: exporterNames("traces", env).map((name) => name === "console"
|
|
53
|
+
? new SimpleSpanProcessor(new ConsoleSpanExporter())
|
|
54
|
+
: new BatchSpanProcessor(otlpProtocol("traces", env) === "http/json" ? new JsonTraceExporter() : new ProtobufTraceExporter()));
|
|
55
|
+
return exporting.length > 0 ? [createContextSpanProcessor(), ...exporting] : [];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The log record processors: the context processor, then a processor for
|
|
59
|
+
* `exporter`, or for each exporter of the variables. No exporter gives no processor.
|
|
60
|
+
* An OTLP exporter reads its endpoint, headers and timeout from `process.env`.
|
|
61
|
+
*/
|
|
62
|
+
export function nodeLogRecordProcessors(env, exporter) {
|
|
63
|
+
const exporting = exporter
|
|
64
|
+
? [new BatchLogRecordProcessor(exporter)]
|
|
65
|
+
: exporterNames("logs", env).map((name) => name === "console"
|
|
66
|
+
? new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())
|
|
67
|
+
: new BatchLogRecordProcessor(otlpProtocol("logs", env) === "http/json" ? new JsonLogExporter() : new ProtobufLogExporter()));
|
|
68
|
+
return exporting.length > 0 ? [createContextLogRecordProcessor(), ...exporting] : [];
|
|
69
|
+
}
|
|
70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AC,0BAA0B,EAAE,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAClF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAQ,EAAE,QAA4B;IAC5E,MAAM,SAAS,GAAyB,QAAQ;QAC9C,CAAC,CAAC,CAAC,IAAI,uBAAuB,CAAC,QAAQ,CAAC,CAAC;QACzC,CAAC,CAAC,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CACtC,IAAI,KAAK,SAAS;YAChB,CAAC,CAAC,IAAI,wBAAwB,CAAC,IAAI,wBAAwB,EAAE,CAAC;YAC9D,CAAC,CAAC,IAAI,uBAAuB,CAAC,YAAY,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,eAAe,EAAE,CAAC,CAAC,CAAC,IAAI,mBAAmB,EAAE,CAAC,CAC/H,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,4 +1,7 @@
|
|
|
1
1
|
import { type NodeSDKConfiguration } from "@opentelemetry/sdk-node";
|
|
2
|
+
import type { LogRecordExporter } from "@opentelemetry/sdk-logs";
|
|
3
|
+
import type { SpanExporter } from "@opentelemetry/sdk-trace-base";
|
|
4
|
+
export { exporterNames, otlpProtocol, type ExporterName, type OtlpProtocol, type OtlpSignal } from "./node-exporters.js";
|
|
2
5
|
export { outgoingTraceparent, createTraceEcho, runInRemoteTrace, type RemoteTraceOptions, type TraceEcho } from "./trace-echo.js";
|
|
3
6
|
/** The configuration of `startNodeTelemetry()` */
|
|
4
7
|
export interface NodeTelemetryOptions {
|
|
@@ -12,11 +15,22 @@ export interface NodeTelemetryOptions {
|
|
|
12
15
|
importMetaUrl?: string;
|
|
13
16
|
/** The instrumentations, from the dependencies of the service */
|
|
14
17
|
instrumentations?: NodeSDKConfiguration["instrumentations"];
|
|
15
|
-
/**
|
|
16
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The trace exporter. Default: the exporters of `OTEL_TRACES_EXPORTER`
|
|
20
|
+
* (`otlp`, `console` or `none`; default `otlp`), with the protocol of
|
|
21
|
+
* `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` or `OTEL_EXPORTER_OTLP_PROTOCOL`
|
|
22
|
+
* (`http/protobuf` or `http/json`; default `http/protobuf`).
|
|
23
|
+
*/
|
|
24
|
+
traceExporter?: SpanExporter;
|
|
25
|
+
/** The log record exporter. Default: the exporters of `OTEL_LOGS_EXPORTER`, with the same rules as the traces. */
|
|
26
|
+
logExporter?: LogRecordExporter;
|
|
17
27
|
/** The metric readers. Default: none. */
|
|
18
28
|
metricReaders?: NodeSDKConfiguration["metricReaders"];
|
|
19
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Export on or off. Default: on when `OTEL_EXPORTER_OTLP_ENDPOINT` has a
|
|
31
|
+
* value. With export off, the context is registered all the same, so the ids
|
|
32
|
+
* go through the service and into its own log lines.
|
|
33
|
+
*/
|
|
20
34
|
enabled?: boolean;
|
|
21
35
|
/**
|
|
22
36
|
* Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
|
|
@@ -25,22 +39,26 @@ export interface NodeTelemetryOptions {
|
|
|
25
39
|
*/
|
|
26
40
|
stopOnSignals?: boolean;
|
|
27
41
|
/**
|
|
28
|
-
* The environment that sets `enabled
|
|
29
|
-
* `process.env`.
|
|
30
|
-
*
|
|
31
|
-
*
|
|
42
|
+
* The environment that sets `enabled`, the service name, the exporter
|
|
43
|
+
* names and the protocols. Default: `process.env`. An OTLP exporter and the
|
|
44
|
+
* sampler of the SDK read their settings (endpoint, headers,
|
|
45
|
+
* `OTEL_TRACES_SAMPLER`) from `process.env`, not from this object. To send
|
|
46
|
+
* to an endpoint of this object, give `traceExporter` and `logExporter`.
|
|
32
47
|
*/
|
|
33
48
|
env?: NodeJS.ProcessEnv;
|
|
34
49
|
}
|
|
35
50
|
/** The result of `startNodeTelemetry()` */
|
|
36
51
|
export interface NodeTelemetry {
|
|
37
|
-
/** True when
|
|
52
|
+
/** True when export is on */
|
|
38
53
|
readonly enabled: boolean;
|
|
39
54
|
/** True when the loader hook was registered */
|
|
40
55
|
readonly esmHook: boolean;
|
|
41
|
-
/** Stop the SDK
|
|
56
|
+
/** Stop the SDK, flush the spans and metrics, and remove the registration of the context. A second call does nothing. */
|
|
42
57
|
shutdown(): Promise<void>;
|
|
43
58
|
}
|
|
44
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* Start OpenTelemetry for a Node service: the loader hook first, then the SDK.
|
|
61
|
+
* A second call before the `shutdown()` of the first is an error.
|
|
62
|
+
*/
|
|
45
63
|
export declare function startNodeTelemetry(options?: NodeTelemetryOptions): NodeTelemetry;
|
|
46
64
|
//# sourceMappingURL=node.d.ts.map
|
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":"AA2BA,OAAO,EAAW,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAKlE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEzH,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,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;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CACzB;AAKD,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,CA8DpF"}
|
package/dist/node.js
CHANGED
|
@@ -13,35 +13,74 @@
|
|
|
13
13
|
* of the service (the OpenTelemetry packages must have one version line), so
|
|
14
14
|
* `@opentelemetry/sdk-node` is a peer dependency of this entry.
|
|
15
15
|
*
|
|
16
|
+
* It registers the one context (docs/CONTEXT-DESIGN.md) also with export
|
|
17
|
+
* off: AsyncLocalStorage, the propagator of otel-ts (the trace context on each
|
|
18
|
+
* call, the baggage on internal calls only), and the metric view that removes
|
|
19
|
+
* the keys of high cardinality. With export on, the context processors add the
|
|
20
|
+
* context keys to each span and each log record. Only one part of a program
|
|
21
|
+
* registers it.
|
|
22
|
+
*
|
|
16
23
|
* @packageDocumentation
|
|
17
24
|
*/
|
|
18
25
|
import { register } from "node:module";
|
|
26
|
+
import { context, metrics, propagation, trace } from "@opentelemetry/api";
|
|
27
|
+
import { logs } from "@opentelemetry/api-logs";
|
|
19
28
|
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
29
|
+
import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
|
|
30
|
+
import { contextMetricView } from "./context-processors.js";
|
|
31
|
+
import { nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
|
|
32
|
+
export { exporterNames, otlpProtocol } from "./node-exporters.js";
|
|
20
33
|
export { outgoingTraceparent, createTraceEcho, runInRemoteTrace } from "./trace-echo.js";
|
|
21
34
|
/** The signals that stop the SDK when `stopOnSignals` is true */
|
|
22
35
|
const STOP_SIGNALS = ["SIGTERM", "SIGINT"];
|
|
23
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* Start OpenTelemetry for a Node service: the loader hook first, then the SDK.
|
|
38
|
+
* A second call before the `shutdown()` of the first is an error.
|
|
39
|
+
*/
|
|
24
40
|
export function startNodeTelemetry(options = {}) {
|
|
25
41
|
const env = options.env ?? process.env;
|
|
26
42
|
const enabled = options.enabled ?? !!env["OTEL_EXPORTER_OTLP_ENDPOINT"];
|
|
27
|
-
|
|
28
|
-
return { enabled: false, esmHook: false, shutdown: async () => { } };
|
|
43
|
+
const release = claimContextRegistration("startNodeTelemetry()");
|
|
29
44
|
let esmHook = false;
|
|
30
45
|
if (options.importMetaUrl) {
|
|
31
46
|
register("@opentelemetry/instrumentation/hook.mjs", options.importMetaUrl);
|
|
32
47
|
esmHook = true;
|
|
33
48
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
49
|
+
let sdk;
|
|
50
|
+
try {
|
|
51
|
+
// With export off: no processor and no reader, so nothing leaves the process
|
|
52
|
+
const exports = enabled
|
|
53
|
+
? {
|
|
54
|
+
spanProcessors: nodeSpanProcessors(env, options.traceExporter),
|
|
55
|
+
logRecordProcessors: nodeLogRecordProcessors(env, options.logExporter),
|
|
56
|
+
...(options.metricReaders ? { metricReaders: options.metricReaders } : {}),
|
|
57
|
+
}
|
|
58
|
+
: { spanProcessors: [], logRecordProcessors: [], metricReaders: [] };
|
|
59
|
+
sdk = new NodeSDK({
|
|
60
|
+
serviceName: options.serviceName ?? env["OTEL_SERVICE_NAME"] ?? "node-service",
|
|
61
|
+
textMapPropagator: new ContextPropagator(),
|
|
62
|
+
views: [contextMetricView()],
|
|
63
|
+
...exports,
|
|
64
|
+
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
65
|
+
});
|
|
66
|
+
sdk.start();
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
release();
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
41
72
|
// One stop: a second call waits for the flush of the first
|
|
42
73
|
let stopping;
|
|
43
|
-
const shutdown = () => (stopping ??= sdk
|
|
74
|
+
const shutdown = () => (stopping ??= sdk
|
|
75
|
+
.shutdown()
|
|
76
|
+
.catch(() => {
|
|
44
77
|
// a flush that fails at the stop loses its spans, and nothing else
|
|
78
|
+
})
|
|
79
|
+
.finally(() => {
|
|
80
|
+
// The SDK leaves its globals at the stop. Remove them, so a next start registers its own.
|
|
81
|
+
for (const api of [trace, metrics, logs, propagation, context])
|
|
82
|
+
api.disable();
|
|
83
|
+
release();
|
|
45
84
|
}));
|
|
46
85
|
if (options.stopOnSignals ?? true) {
|
|
47
86
|
for (const signal of STOP_SIGNALS) {
|
|
@@ -59,6 +98,6 @@ export function startNodeTelemetry(options = {}) {
|
|
|
59
98
|
});
|
|
60
99
|
}
|
|
61
100
|
}
|
|
62
|
-
return { enabled
|
|
101
|
+
return { enabled, esmHook, shutdown };
|
|
63
102
|
}
|
|
64
103
|
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA
|
|
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,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAA6B,MAAM,yBAAyB,CAAC;AAG7E,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACtF,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAElF,OAAO,EAAE,aAAa,EAAE,YAAY,EAAyD,MAAM,qBAAqB,CAAC;AAEzH,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAA2C,MAAM,iBAAiB,CAAC;AA+ClI,iEAAiE;AACjE,MAAM,YAAY,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAU,CAAC;AAYpD;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAAgC,EAAE;IACnE,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;IACxE,MAAM,OAAO,GAAG,wBAAwB,CAAC,sBAAsB,CAAC,CAAC;IAEjE,IAAI,OAAO,GAAG,KAAK,CAAC;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QAAQ,KAAK,GAAG;SACd,QAAQ,EAAE;SACV,KAAK,CAAC,GAAG,EAAE;QACV,mEAAmE;IACrE,CAAC,CAAC;SACD,OAAO,CAAC,GAAG,EAAE;QACZ,0FAA0F;QAC1F,KAAK,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,CAAC;YAAE,GAAG,CAAC,OAAO,EAAE,CAAC;QAC9E,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC,CAAC;IACR,IAAI,OAAO,CAAC,aAAa,IAAI,IAAI,EAAE,CAAC;QAClC,KAAK,MAAM,MAAM,IAAI,YAAY,EAAE,CAAC;YAClC,uFAAuF;YACvF,OAAO,CAAC,mBAAmB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACvC,gFAAgF;gBAChF,mFAAmF;gBACnF,oFAAoF;gBACpF,iFAAiF;gBACjF,MAAM,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACzD,KAAK,QAAQ,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE;oBAC3B,IAAI,CAAC,cAAc;wBAAE,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;gBACzD,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;AACxC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-processors.d.ts","sourceRoot":"","sources":["../src/session-processors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"session-processors.d.ts","sourceRoot":"","sources":["../src/session-processors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAInE,yEAAyE;AACzE,MAAM,MAAM,eAAe,GAAG,MAAM,MAAM,CAAC;AAgB3C,4DAA4D;AAC5D,wBAAgB,+BAA+B,CAC7C,YAAY,EAAE,eAAe,GAC5B,kBAAkB,CAEpB;AAED,kDAAkD;AAClD,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,eAAe,GAC5B,aAAa,CAEf"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ATTR_SESSION_ID } from "./constants.js";
|
|
2
|
+
import { createAttributesLogRecordProcessor, createAttributesSpanProcessor } from "./context-processors.js";
|
|
2
3
|
/*
|
|
3
4
|
* The OpenTelemetry Browser SDK's session-processor pattern: `session.id` is
|
|
4
5
|
* an attribute of each log record and span, read as the record is created,
|
|
@@ -8,30 +9,16 @@ import { ATTR_SESSION_ID } from "./constants.js";
|
|
|
8
9
|
*
|
|
9
10
|
* A `session.id` the caller set explicitly (e.g. on a `session.end` event
|
|
10
11
|
* for the previous session) is kept.
|
|
12
|
+
*
|
|
13
|
+
* These are a case of the processors of the context keys
|
|
14
|
+
* (context-processors.ts), with the session as the source of the value.
|
|
11
15
|
*/
|
|
12
16
|
/** Sets `session.id` on each log record as it is emitted */
|
|
13
17
|
export function createSessionLogRecordProcessor(getSessionId) {
|
|
14
|
-
return {
|
|
15
|
-
onEmit(logRecord) {
|
|
16
|
-
if (logRecord.attributes[ATTR_SESSION_ID] === undefined) {
|
|
17
|
-
logRecord.setAttribute(ATTR_SESSION_ID, getSessionId());
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
forceFlush: () => Promise.resolve(),
|
|
21
|
-
shutdown: () => Promise.resolve(),
|
|
22
|
-
};
|
|
18
|
+
return createAttributesLogRecordProcessor(() => ({ [ATTR_SESSION_ID]: getSessionId() }));
|
|
23
19
|
}
|
|
24
20
|
/** Sets `session.id` on each span as it starts */
|
|
25
21
|
export function createSessionSpanProcessor(getSessionId) {
|
|
26
|
-
return {
|
|
27
|
-
onStart(span) {
|
|
28
|
-
if (span.attributes[ATTR_SESSION_ID] === undefined) {
|
|
29
|
-
span.setAttribute(ATTR_SESSION_ID, getSessionId());
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
onEnd() { },
|
|
33
|
-
forceFlush: () => Promise.resolve(),
|
|
34
|
-
shutdown: () => Promise.resolve(),
|
|
35
|
-
};
|
|
22
|
+
return createAttributesSpanProcessor(() => ({ [ATTR_SESSION_ID]: getSessionId() }));
|
|
36
23
|
}
|
|
37
24
|
//# sourceMappingURL=session-processors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-processors.js","sourceRoot":"","sources":["../src/session-processors.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"session-processors.js","sourceRoot":"","sources":["../src/session-processors.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,kCAAkC,EAAE,6BAA6B,EAAE,MAAM,yBAAyB,CAAC;AAK5G;;;;;;;;;;;;GAYG;AAEH,4DAA4D;AAC5D,MAAM,UAAU,+BAA+B,CAC7C,YAA6B;IAE7B,OAAO,kCAAkC,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3F,CAAC;AAED,kDAAkD;AAClD,MAAM,UAAU,0BAA0B,CACxC,YAA6B;IAE7B,OAAO,6BAA6B,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC;AACtF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/otel-ts",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
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)",
|
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
"types": "./dist/core.d.ts",
|
|
30
30
|
"import": "./dist/core.js"
|
|
31
31
|
},
|
|
32
|
+
"./context": {
|
|
33
|
+
"types": "./dist/context.d.ts",
|
|
34
|
+
"import": "./dist/context.js"
|
|
35
|
+
},
|
|
32
36
|
"./node": {
|
|
33
37
|
"types": "./dist/node.d.ts",
|
|
34
38
|
"import": "./dist/node.js"
|
|
@@ -52,8 +56,10 @@
|
|
|
52
56
|
"@opentelemetry/context-zone": "^2.2.0",
|
|
53
57
|
"@opentelemetry/core": "^2.6.0",
|
|
54
58
|
"@opentelemetry/exporter-logs-otlp-http": "^0.213.0",
|
|
59
|
+
"@opentelemetry/exporter-logs-otlp-proto": "^0.213.0",
|
|
55
60
|
"@opentelemetry/exporter-metrics-otlp-http": "^0.213.0",
|
|
56
61
|
"@opentelemetry/exporter-trace-otlp-http": "^0.213.0",
|
|
62
|
+
"@opentelemetry/exporter-trace-otlp-proto": "^0.213.0",
|
|
57
63
|
"@opentelemetry/instrumentation": "^0.213.0",
|
|
58
64
|
"@opentelemetry/instrumentation-long-task": "^0.58.0",
|
|
59
65
|
"@opentelemetry/resources": "^2.6.0",
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The processors of the context keys (docs/CONTEXT-DESIGN.md, section 4).
|
|
3
|
+
*
|
|
4
|
+
* - Each span and each log record gets the value of each defined context key
|
|
5
|
+
* of its context, as an attribute.
|
|
6
|
+
* - A metric gets no attribute of a key with `cardinality: "high"`: one time
|
|
7
|
+
* series for each user id would fill the store.
|
|
8
|
+
*
|
|
9
|
+
* The `session.id` processors (session-processors.ts) are a case of the same
|
|
10
|
+
* two processors. This file imports only types of the SDK packages.
|
|
11
|
+
*/
|
|
12
|
+
import { context, type Attributes, type Context } from "@opentelemetry/api";
|
|
13
|
+
import type { LogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
14
|
+
import type { IAttributesProcessor, ViewOptions } from "@opentelemetry/sdk-metrics";
|
|
15
|
+
import type { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
16
|
+
import { contextKeyRule, contextValues } from "./context.js";
|
|
17
|
+
|
|
18
|
+
/** The attributes to add to a record that starts in the context `from` */
|
|
19
|
+
export type ContextAttributesReader = (from: Context) => Readonly<Record<string, string>>;
|
|
20
|
+
|
|
21
|
+
/** Sets the attributes of `read` on each span as it starts. An attribute that the caller set is kept. */
|
|
22
|
+
export function createAttributesSpanProcessor(read: ContextAttributesReader): SpanProcessor {
|
|
23
|
+
return {
|
|
24
|
+
onStart(span, parentContext) {
|
|
25
|
+
for (const [key, value] of Object.entries(read(parentContext))) {
|
|
26
|
+
if (span.attributes[key] === undefined) span.setAttribute(key, value);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
onEnd() {},
|
|
30
|
+
forceFlush: () => Promise.resolve(),
|
|
31
|
+
shutdown: () => Promise.resolve(),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Sets the attributes of `read` on each log record as it is emitted. An attribute that the caller set is kept. */
|
|
36
|
+
export function createAttributesLogRecordProcessor(read: ContextAttributesReader): LogRecordProcessor {
|
|
37
|
+
return {
|
|
38
|
+
onEmit(logRecord, from) {
|
|
39
|
+
for (const [key, value] of Object.entries(read(from ?? context.active()))) {
|
|
40
|
+
if (logRecord.attributes[key] === undefined) logRecord.setAttribute(key, value);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
forceFlush: () => Promise.resolve(),
|
|
44
|
+
shutdown: () => Promise.resolve(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Sets the value of each defined context key on each span */
|
|
49
|
+
export function createContextSpanProcessor(): SpanProcessor {
|
|
50
|
+
return createAttributesSpanProcessor(contextValues);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Sets the value of each defined context key on each log record */
|
|
54
|
+
export function createContextLogRecordProcessor(): LogRecordProcessor {
|
|
55
|
+
return createAttributesLogRecordProcessor(contextValues);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Removes each attribute of a defined key with `cardinality: "high"` from a
|
|
60
|
+
* measurement. It reads the rules at each measurement, so a key defined after
|
|
61
|
+
* the start of the meter provider is removed too.
|
|
62
|
+
*/
|
|
63
|
+
export function createContextAttributesProcessor(): IAttributesProcessor {
|
|
64
|
+
return {
|
|
65
|
+
process(incoming: Attributes): Attributes {
|
|
66
|
+
let result: Attributes | undefined;
|
|
67
|
+
for (const key of Object.keys(incoming)) {
|
|
68
|
+
if (contextKeyRule(key)?.cardinality !== "high") continue;
|
|
69
|
+
result ??= { ...incoming };
|
|
70
|
+
delete result[key];
|
|
71
|
+
}
|
|
72
|
+
return result ?? incoming;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The view for each instrument that removes the keys of high cardinality.
|
|
79
|
+
* Give it as the only view of the meter provider: a second view that matches
|
|
80
|
+
* an instrument makes a second stream of it.
|
|
81
|
+
*/
|
|
82
|
+
export function contextMetricView(): ViewOptions {
|
|
83
|
+
return { instrumentName: "*", attributesProcessors: [createContextAttributesProcessor()] };
|
|
84
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The propagator of the one context (docs/CONTEXT-DESIGN.md, section 5), and
|
|
3
|
+
* the guard that lets only one part of a program register the context.
|
|
4
|
+
*
|
|
5
|
+
* - The W3C trace context (`traceparent`, `tracestate`) goes on each call. It
|
|
6
|
+
* holds only random ids.
|
|
7
|
+
* - The W3C baggage goes only on a call that starts in a context with the
|
|
8
|
+
* internal mark (`withInternalCall` of `/context`), and only with the keys
|
|
9
|
+
* that `defineContextKeys` defined with `propagate: "internal"`. A value
|
|
10
|
+
* longer than the `maxLength` of its key is dropped, not cut.
|
|
11
|
+
* - On extract, the baggage keeps only those keys too, so an entry from
|
|
12
|
+
* outside does not travel on. The W3C limits (180 entries, 8192 bytes) are
|
|
13
|
+
* the limits of the W3C baggage propagator of `@opentelemetry/core`.
|
|
14
|
+
*
|
|
15
|
+
* A server that does not trust its caller removes the extracted baggage with
|
|
16
|
+
* `propagation.deleteBaggage(context)`, and keeps the trace context.
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
propagation,
|
|
20
|
+
type Baggage,
|
|
21
|
+
type Context,
|
|
22
|
+
type TextMapGetter,
|
|
23
|
+
type TextMapPropagator,
|
|
24
|
+
type TextMapSetter,
|
|
25
|
+
} from "@opentelemetry/api";
|
|
26
|
+
import { W3CBaggagePropagator, W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
27
|
+
import { contextKeyRule, isInternalCall } from "./context.js";
|
|
28
|
+
|
|
29
|
+
/** The baggage with only the defined internal keys, each value in its maxLength; undefined when no entry stays */
|
|
30
|
+
export function allowedBaggage(baggage: Baggage): Baggage | undefined {
|
|
31
|
+
let kept = baggage;
|
|
32
|
+
for (const [key, entry] of baggage.getAllEntries()) {
|
|
33
|
+
const rule = contextKeyRule(key);
|
|
34
|
+
if (rule?.propagate !== "internal" || entry.value.length > rule.maxLength) kept = kept.removeEntry(key);
|
|
35
|
+
}
|
|
36
|
+
return kept.getAllEntries().length > 0 ? kept : undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The W3C trace context on each call, and the allowed baggage on internal calls only */
|
|
40
|
+
export class ContextPropagator implements TextMapPropagator {
|
|
41
|
+
private readonly traceContext = new W3CTraceContextPropagator();
|
|
42
|
+
private readonly baggage = new W3CBaggagePropagator();
|
|
43
|
+
|
|
44
|
+
inject(context: Context, carrier: unknown, setter: TextMapSetter): void {
|
|
45
|
+
this.traceContext.inject(context, carrier, setter);
|
|
46
|
+
if (!isInternalCall(context)) return;
|
|
47
|
+
const baggage = propagation.getBaggage(context);
|
|
48
|
+
const allowed = baggage && allowedBaggage(baggage);
|
|
49
|
+
if (allowed) this.baggage.inject(propagation.setBaggage(context, allowed), carrier, setter);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
extract(context: Context, carrier: unknown, getter: TextMapGetter): Context {
|
|
53
|
+
const next = this.baggage.extract(this.traceContext.extract(context, carrier, getter), carrier, getter);
|
|
54
|
+
const baggage = propagation.getBaggage(next);
|
|
55
|
+
if (!baggage) return next;
|
|
56
|
+
const allowed = allowedBaggage(baggage);
|
|
57
|
+
return allowed ? propagation.setBaggage(next, allowed) : propagation.deleteBaggage(next);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fields(): string[] {
|
|
61
|
+
return [...this.traceContext.fields(), ...this.baggage.fields()];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* The owner of the registration is on globalThis, so two copies of otel-ts in
|
|
67
|
+
* one program also refuse each other.
|
|
68
|
+
*/
|
|
69
|
+
const OWNER = Symbol.for("@mark1russell7/otel-ts/context owner");
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Take the registration of the context for `caller` (for example "init()" or
|
|
73
|
+
* "startNodeTelemetry()"), and give the function that gives it back. A second
|
|
74
|
+
* registration while the first holds is an error that names the first caller.
|
|
75
|
+
* The OpenTelemetry API itself keeps the first registration and only writes a
|
|
76
|
+
* diagnostic message.
|
|
77
|
+
*/
|
|
78
|
+
export function claimContextRegistration(caller: string): () => void {
|
|
79
|
+
const holder = globalThis as { [OWNER]?: string };
|
|
80
|
+
const owner = holder[OWNER];
|
|
81
|
+
if (owner !== undefined) {
|
|
82
|
+
throw new Error(`The context is registered already by ${owner}. Only one part of a program registers it: call the shutdown() of ${owner} first.`);
|
|
83
|
+
}
|
|
84
|
+
holder[OWNER] = caller;
|
|
85
|
+
let released = false;
|
|
86
|
+
return () => {
|
|
87
|
+
if (released) return;
|
|
88
|
+
released = true;
|
|
89
|
+
if (holder[OWNER] === caller) delete holder[OWNER];
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The caller that holds the registration of the context, or undefined */
|
|
94
|
+
export function contextRegistrationOwner(): string | undefined {
|
|
95
|
+
return (globalThis as { [OWNER]?: string })[OWNER];
|
|
96
|
+
}
|