@mark1russell7/otel-ts 0.1.2 → 0.1.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 +21 -0
- package/README.md +7 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/node.d.ts +11 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +21 -11
- package/dist/node.js.map +1 -1
- package/package.json +3 -1
- package/src/node.ts +32 -11
package/dist/node.d.ts
CHANGED
|
@@ -18,9 +18,18 @@ export interface NodeTelemetryOptions {
|
|
|
18
18
|
metricReaders?: NodeSDKConfiguration["metricReaders"];
|
|
19
19
|
/** On or off. Default: on when `OTEL_EXPORTER_OTLP_ENDPOINT` has a value. */
|
|
20
20
|
enabled?: boolean;
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
|
|
23
|
+
* service has no listener of its own for the signal, the process then ends
|
|
24
|
+
* with the signal, as it does without this option.
|
|
25
|
+
*/
|
|
22
26
|
stopOnSignals?: boolean;
|
|
23
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* The environment that sets `enabled` and the service name. Default:
|
|
29
|
+
* `process.env`. The SDK reads its exporter settings (`OTEL_EXPORTER_OTLP_*`)
|
|
30
|
+
* from `process.env`, not from this object. To send the spans to an
|
|
31
|
+
* endpoint of this object, give `traceExporter`.
|
|
32
|
+
*/
|
|
24
33
|
env?: NodeJS.ProcessEnv;
|
|
25
34
|
}
|
|
26
35
|
/** The result of `startNodeTelemetry()` */
|
package/dist/node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAW,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAE7E,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAAE,KAAK,kBA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AAC,UAAU,CAAC;CACzB;AAKD,2CAA2C;AAC3C,MAAM,WAAW,aAAa;IAC5B,gCAAgC;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,gGAAgG;IAChG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED,mFAAmF;AACnF,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,oBAAyB,GAAG,aAAa,CAwCpF"}
|
package/dist/node.js
CHANGED
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
import { register } from "node:module";
|
|
19
19
|
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
20
20
|
export { outgoingTraceparent, createTraceEcho, runInRemoteTrace } from "./trace-echo.js";
|
|
21
|
+
/** The signals that stop the SDK when `stopOnSignals` is true */
|
|
22
|
+
const STOP_SIGNALS = ["SIGTERM", "SIGINT"];
|
|
21
23
|
/** Start OpenTelemetry for a Node service: the loader hook first, then the SDK. */
|
|
22
24
|
export function startNodeTelemetry(options = {}) {
|
|
23
25
|
const env = options.env ?? process.env;
|
|
@@ -36,18 +38,26 @@ export function startNodeTelemetry(options = {}) {
|
|
|
36
38
|
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
37
39
|
});
|
|
38
40
|
sdk.start();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
await sdk.shutdown().catch(() => {
|
|
45
|
-
// a flush that fails at the stop loses its spans, and nothing else
|
|
46
|
-
});
|
|
47
|
-
};
|
|
41
|
+
// One stop: a second call waits for the flush of the first
|
|
42
|
+
let stopping;
|
|
43
|
+
const shutdown = () => (stopping ??= sdk.shutdown().catch(() => {
|
|
44
|
+
// a flush that fails at the stop loses its spans, and nothing else
|
|
45
|
+
}));
|
|
48
46
|
if (options.stopOnSignals ?? true) {
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
for (const signal of STOP_SIGNALS) {
|
|
48
|
+
// First in the list, so it sees each listener of the service before that listener runs
|
|
49
|
+
process.prependOnceListener(signal, () => {
|
|
50
|
+
// A listener for a signal removes the default action of Node.js, which ends the
|
|
51
|
+
// process. This listener is gone now. When the service has no listener of its own,
|
|
52
|
+
// raise the signal again after the flush: the default action then ends the process.
|
|
53
|
+
// Before, a service without a listener continued to run after SIGTERM or SIGINT.
|
|
54
|
+
const serviceListens = process.listenerCount(signal) > 0;
|
|
55
|
+
void shutdown().finally(() => {
|
|
56
|
+
if (!serviceListens)
|
|
57
|
+
process.kill(process.pid, signal);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
51
61
|
}
|
|
52
62
|
return { enabled: true, esmHook, shutdown };
|
|
53
63
|
}
|
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,OAAO,EAA6B,MAAM,yBAAyB,CAAC;AAE7E,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAA2C,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,OAAO,EAA6B,MAAM,yBAAyB,CAAC;AAE7E,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAA2C,MAAM,iBAAiB,CAAC;AAmClI,iEAAiE;AACjE,MAAM,YAAY,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAU,CAAC;AAYpD,mFAAmF;AACnF,MAAM,UAAU,kBAAkB,CAAC,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AE;QACtC,mEAAmE;IACrE,CAAC,CAAC,CAAC,CAAC;IACN,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,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;AAC9C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/otel-ts",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"description": "Frontend observability in one init() call — OpenTelemetry + Grafana Faro for any TypeScript web app",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Mark Russell (https://github.com/mark1russell7)",
|
|
6
8
|
"homepage": "https://github.com/mark1russell7/otel-ts#readme",
|
|
7
9
|
"bugs": {
|
|
8
10
|
"url": "https://github.com/mark1russell7/otel-ts/issues"
|
package/src/node.ts
CHANGED
|
@@ -38,12 +38,24 @@ export interface NodeTelemetryOptions {
|
|
|
38
38
|
metricReaders?: NodeSDKConfiguration["metricReaders"];
|
|
39
39
|
/** On or off. Default: on when `OTEL_EXPORTER_OTLP_ENDPOINT` has a value. */
|
|
40
40
|
enabled?: boolean;
|
|
41
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
|
|
43
|
+
* service has no listener of its own for the signal, the process then ends
|
|
44
|
+
* with the signal, as it does without this option.
|
|
45
|
+
*/
|
|
42
46
|
stopOnSignals?: boolean;
|
|
43
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* The environment that sets `enabled` and the service name. Default:
|
|
49
|
+
* `process.env`. The SDK reads its exporter settings (`OTEL_EXPORTER_OTLP_*`)
|
|
50
|
+
* from `process.env`, not from this object. To send the spans to an
|
|
51
|
+
* endpoint of this object, give `traceExporter`.
|
|
52
|
+
*/
|
|
44
53
|
env?: NodeJS.ProcessEnv;
|
|
45
54
|
}
|
|
46
55
|
|
|
56
|
+
/** The signals that stop the SDK when `stopOnSignals` is true */
|
|
57
|
+
const STOP_SIGNALS = ["SIGTERM", "SIGINT"] as const;
|
|
58
|
+
|
|
47
59
|
/** The result of `startNodeTelemetry()` */
|
|
48
60
|
export interface NodeTelemetry {
|
|
49
61
|
/** True when the SDK started */
|
|
@@ -73,17 +85,26 @@ export function startNodeTelemetry(options: NodeTelemetryOptions = {}): NodeTele
|
|
|
73
85
|
});
|
|
74
86
|
sdk.start();
|
|
75
87
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
await sdk.shutdown().catch(() => {
|
|
88
|
+
// One stop: a second call waits for the flush of the first
|
|
89
|
+
let stopping: Promise<void> | undefined;
|
|
90
|
+
const shutdown = (): Promise<void> =>
|
|
91
|
+
(stopping ??= sdk.shutdown().catch(() => {
|
|
81
92
|
// a flush that fails at the stop loses its spans, and nothing else
|
|
82
|
-
});
|
|
83
|
-
};
|
|
93
|
+
}));
|
|
84
94
|
if (options.stopOnSignals ?? true) {
|
|
85
|
-
|
|
86
|
-
|
|
95
|
+
for (const signal of STOP_SIGNALS) {
|
|
96
|
+
// First in the list, so it sees each listener of the service before that listener runs
|
|
97
|
+
process.prependOnceListener(signal, () => {
|
|
98
|
+
// A listener for a signal removes the default action of Node.js, which ends the
|
|
99
|
+
// process. This listener is gone now. When the service has no listener of its own,
|
|
100
|
+
// raise the signal again after the flush: the default action then ends the process.
|
|
101
|
+
// Before, a service without a listener continued to run after SIGTERM or SIGINT.
|
|
102
|
+
const serviceListens = process.listenerCount(signal) > 0;
|
|
103
|
+
void shutdown().finally(() => {
|
|
104
|
+
if (!serviceListens) process.kill(process.pid, signal);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
87
108
|
}
|
|
88
109
|
return { enabled: true, esmHook, shutdown };
|
|
89
110
|
}
|