@mark1russell7/otel-ts 0.8.3 → 0.8.5
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 +42 -0
- package/dist/node-context.d.ts +51 -0
- package/dist/node-context.d.ts.map +1 -0
- package/dist/node-context.js +10 -0
- package/dist/node-context.js.map +1 -0
- package/dist/node-exporters.d.ts +0 -11
- package/dist/node-exporters.d.ts.map +1 -1
- package/dist/node-exporters.js +0 -25
- package/dist/node-exporters.js.map +1 -1
- package/dist/node-registration.d.ts +45 -0
- package/dist/node-registration.d.ts.map +1 -0
- package/dist/node-registration.js +88 -0
- package/dist/node-registration.js.map +1 -0
- package/dist/node.d.ts +3 -3
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +59 -45
- package/dist/node.js.map +1 -1
- package/dist/pull.d.ts +70 -0
- package/dist/pull.d.ts.map +1 -0
- package/dist/pull.js +93 -0
- package/dist/pull.js.map +1 -0
- package/package.json +11 -1
- package/src/node-context.ts +57 -0
- package/src/node-exporters.ts +0 -28
- package/src/node-registration.ts +116 -0
- package/src/node.ts +61 -47
- package/src/pull.ts +148 -0
package/README.md
CHANGED
|
@@ -320,6 +320,28 @@ A shorter interval sends more samples. Each series then costs more in Mimir and
|
|
|
320
320
|
|
|
321
321
|
By default, the exporter requests cumulative temporality. In its default configuration, Mimir rejects delta sums and delta histograms. Keep `metricsTemporality` at `"cumulative"` for Mimir and Prometheus.
|
|
322
322
|
|
|
323
|
+
## Read metrics when you ask
|
|
324
|
+
|
|
325
|
+
The entry `/pull` reads the metrics of a process at the times that the program chooses. A lab run, for example, reads them after the start and after each pass. The entry starts no SDK and sends nothing.
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
import { MeterProvider } from "@opentelemetry/sdk-metrics";
|
|
329
|
+
import { collectReadings, PullMetricReader } from "@mark1russell7/otel-ts/pull";
|
|
330
|
+
|
|
331
|
+
const reader = new PullMetricReader();
|
|
332
|
+
const provider = new MeterProvider({ readers: [reader] });
|
|
333
|
+
provider.getMeter("app").createCounter("requests").add(1, { route: "/a" });
|
|
334
|
+
|
|
335
|
+
const { readings, errors } = await collectReadings(reader, Date.now());
|
|
336
|
+
// readings: [{ series: 'requests{route="/a"}', at: "2026-11-24T12:00:00.000Z", value: 1 }]
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
- `PullMetricReader` has no timer and no export. Give it to a `MeterProvider`, or to the SDK as a metric reader.
|
|
340
|
+
- The option `temporality` is `"cumulative"` (the default) or `"delta"`. With `"delta"`, counters and histograms give the change after the last collection.
|
|
341
|
+
- Each set of attributes is a series. With `{ attributes: "sum" }`, each metric is one series, the sum over its attributes.
|
|
342
|
+
- A histogram gives the series `<name>.count` and `<name>.sum`.
|
|
343
|
+
- `errors` is the count of the callbacks that failed in the collection.
|
|
344
|
+
|
|
323
345
|
## Page lifecycle
|
|
324
346
|
|
|
325
347
|
The library follows the state of the page with [`page-lifecycle-tracker`](https://github.com/mark1russell7/page-lifecycle-tracker). It uses the tracker that the page shares (`getPageLifecycle()`), and it subscribes in the `export` phase:
|
|
@@ -445,6 +467,26 @@ By default, it stops the SDK and sends the last spans at `SIGTERM` and `SIGINT`.
|
|
|
445
467
|
|
|
446
468
|
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
469
|
|
|
470
|
+
### A service that exports nothing
|
|
471
|
+
|
|
472
|
+
The entry `/node` loads the SDK of `@opentelemetry/sdk-node` and each exporter, also with export off. In a small service, that is a large part of its memory. The entry `@mark1russell7/otel-ts/node/context` registers the same context with no SDK of `sdk-node`, no exporter and no instrumentation:
|
|
473
|
+
|
|
474
|
+
```ts
|
|
475
|
+
import { startNodeContext } from "@mark1russell7/otel-ts/node/context";
|
|
476
|
+
|
|
477
|
+
export const telemetry = startNodeContext({
|
|
478
|
+
serviceName: "my-service",
|
|
479
|
+
resourceAttributes: { "service.version": "1.2.3", "service.instance.id": "copy-1" },
|
|
480
|
+
});
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
- Each span gets a trace id, and a value of `withContext()` stays after `await`.
|
|
484
|
+
- The propagator is the propagator of `/node`: `traceparent` on each call, the context keys in `baggage` only on an internal call.
|
|
485
|
+
- No span leaves the process. A test gives `spanProcessors` with the exporter of its capture.
|
|
486
|
+
- It adds no listener for a signal. Call `telemetry.shutdown()` in the last stage of the service.
|
|
487
|
+
|
|
488
|
+
`startNodeContext()` and `startNodeTelemetry()` refuse each other: only one part of a program registers the context. To export, import `startNodeTelemetry()` from `/node` in its place.
|
|
489
|
+
|
|
448
490
|
### A service of `@mark1russell7/service`
|
|
449
491
|
|
|
450
492
|
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:
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@mark1russell7/otel-ts/node/context`: the context of a Node service that exports nothing, with no
|
|
3
|
+
* SDK of `@opentelemetry/sdk-node`.
|
|
4
|
+
*
|
|
5
|
+
* It registers the one context (docs/CONTEXT-DESIGN.md) as the entry `/node` does with export off:
|
|
6
|
+
* AsyncLocalStorage, the propagator of otel-ts (the trace context on each call, the baggage on internal calls
|
|
7
|
+
* only), and a tracer provider with the resource of the service. Each span gets a trace id, so the ids go
|
|
8
|
+
* through the service and into its own log lines.
|
|
9
|
+
*
|
|
10
|
+
* The entry `/node` loads `@opentelemetry/sdk-node` and each exporter also with export off. This entry loads
|
|
11
|
+
* none of them, and no instrumentation. Thus a small service pays only for the context. A service that exports
|
|
12
|
+
* imports `/node`.
|
|
13
|
+
*
|
|
14
|
+
* Only one part of a program registers the context: `startNodeContext()` and `startNodeTelemetry()` refuse each
|
|
15
|
+
* other until the `shutdown()` of the first.
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
import type { Attributes } from "@opentelemetry/api";
|
|
20
|
+
import type { SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
21
|
+
/** The configuration of `startNodeContext()` */
|
|
22
|
+
export interface NodeContextOptions {
|
|
23
|
+
/** The service name. Default: `OTEL_SERVICE_NAME`, else "node-service". */
|
|
24
|
+
serviceName?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The attributes of the resource of the service, for example `service.version` and `service.instance.id`
|
|
27
|
+
* (the copy of the service). They win over the detected attributes and over `OTEL_RESOURCE_ATTRIBUTES`.
|
|
28
|
+
*/
|
|
29
|
+
resourceAttributes?: Attributes;
|
|
30
|
+
/**
|
|
31
|
+
* The span processors. Default: none, and no span leaves the process. A test gives a processor with the
|
|
32
|
+
* exporter of its capture.
|
|
33
|
+
*/
|
|
34
|
+
spanProcessors?: readonly SpanProcessor[];
|
|
35
|
+
/** The environment of the service name and of `OTEL_NODE_RESOURCE_DETECTORS`. Default: `process.env`. */
|
|
36
|
+
env?: NodeJS.ProcessEnv;
|
|
37
|
+
}
|
|
38
|
+
/** The result of `startNodeContext()` */
|
|
39
|
+
export interface NodeContext {
|
|
40
|
+
/**
|
|
41
|
+
* Flush the span processors, stop the tracer provider, and remove the registration of the context. A second
|
|
42
|
+
* call does nothing. A service of `@mark1russell7/service` calls it in its last stage, `flush the output`.
|
|
43
|
+
*/
|
|
44
|
+
shutdown(): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Register the context of a Node service that exports nothing. A second call before the `shutdown()` of the
|
|
48
|
+
* first is an error. It adds no listener for a signal: the service calls `shutdown()` at its stop.
|
|
49
|
+
*/
|
|
50
|
+
export declare function startNodeContext(options?: NodeContextOptions): NodeContext;
|
|
51
|
+
//# sourceMappingURL=node-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-context.d.ts","sourceRoot":"","sources":["../src/node-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAG9D,gDAAgD;AAChD,MAAM,WAAW,kBAAkB;IACjC,2EAA2E;IAC3E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,UAAU,CAAC;IAChC;;;OAGG;IACH,cAAc,CAAC,EAAE,SAAS,aAAa,EAAE,CAAC;IAC1C,yGAAyG;IACzG,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CACzB;AAED,yCAAyC;AACzC,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,kBAAuB,GAAG,WAAW,CAG9E"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { registerNodeContext } from "./node-registration.js";
|
|
2
|
+
/**
|
|
3
|
+
* Register the context of a Node service that exports nothing. A second call before the `shutdown()` of the
|
|
4
|
+
* first is an error. It adds no listener for a signal: the service calls `shutdown()` at its stop.
|
|
5
|
+
*/
|
|
6
|
+
export function startNodeContext(options = {}) {
|
|
7
|
+
const registration = registerNodeContext("startNodeContext()", options);
|
|
8
|
+
return { shutdown: () => registration.stop() };
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=node-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-context.js","sourceRoot":"","sources":["../src/node-context.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AA6B7D;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAA8B,EAAE;IAC/D,MAAM,YAAY,GAAG,mBAAmB,CAAC,oBAAoB,EAAE,OAAO,CAAC,CAAC;IACxE,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,CAAC;AACjD,CAAC"}
|
package/dist/node-exporters.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
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";
|
|
4
2
|
import { type AggregationSelector } from "@opentelemetry/sdk-metrics";
|
|
5
3
|
import { type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
6
4
|
type Env = Readonly<Record<string, string | undefined>>;
|
|
@@ -26,15 +24,6 @@ export declare function otlpProtocol(signal: OtlpSignal, env: Env): OtlpProtocol
|
|
|
26
24
|
* Another value is an error.
|
|
27
25
|
*/
|
|
28
26
|
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[];
|
|
38
27
|
/**
|
|
39
28
|
* The span processors: the context processor, then a processor for `exporter`,
|
|
40
29
|
* 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,
|
|
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,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,EAAE,CASpG"}
|
package/dist/node-exporters.js
CHANGED
|
@@ -11,7 +11,6 @@ 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";
|
|
15
14
|
import { AggregationType, InstrumentType } from "@opentelemetry/sdk-metrics";
|
|
16
15
|
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace";
|
|
17
16
|
import { createContextLogRecordProcessor, createContextSpanProcessor, defaultAggregation } from "./context-processors.js";
|
|
@@ -60,30 +59,6 @@ export function histogramAggregation(env) {
|
|
|
60
59
|
}
|
|
61
60
|
throw new RangeError(`${variable} has "${value}": otel-ts makes "explicit_bucket_histogram" or "base2_exponential_bucket_histogram"`);
|
|
62
61
|
}
|
|
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
|
-
}
|
|
87
62
|
/**
|
|
88
63
|
* The span processors: the context processor, then a processor for `exporter`,
|
|
89
64
|
* 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,wBAAwB,EACxB,wBAAwB,GAGzB,MAAM,yBAAyB,CAAC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,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"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The registration of the context of a Node service with no SDK of `@opentelemetry/sdk-node`
|
|
3
|
+
* (docs/CONTEXT-DESIGN.md, section 4). The entry `/node/context` and the export off of the entry `/node`
|
|
4
|
+
* use it. Thus the two paths register the same context.
|
|
5
|
+
*
|
|
6
|
+
* This module imports no exporter, no instrumentation and no SDK of `@opentelemetry/sdk-node`.
|
|
7
|
+
* The entries test holds this rule.
|
|
8
|
+
*/
|
|
9
|
+
import { type Attributes } from "@opentelemetry/api";
|
|
10
|
+
import { type ResourceDetector } from "@opentelemetry/resources";
|
|
11
|
+
import { type SpanProcessor } from "@opentelemetry/sdk-trace";
|
|
12
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
13
|
+
/**
|
|
14
|
+
* The resource detectors that the NodeSDK uses: the names of `OTEL_NODE_RESOURCE_DETECTORS` (`host`, `os`,
|
|
15
|
+
* `serviceinstance`, `process`, `env`, `all` or `none`; default `all`), then a detector that gives
|
|
16
|
+
* `attributes`. That last detector wins over each other detector, also over `OTEL_RESOURCE_ATTRIBUTES` (the
|
|
17
|
+
* detector `env`), as the option `resourceAttributes` of `startNodeSDK()` of the SDK does. Without it, a
|
|
18
|
+
* `service.instance.id` of the service lost to the random id of the detector `serviceinstance`. The NodeSDK
|
|
19
|
+
* ignores a name that it does not know, with a warning. This function ignores it too.
|
|
20
|
+
*/
|
|
21
|
+
export declare function nodeResourceDetectors(env: Env, attributes: Attributes): ResourceDetector[];
|
|
22
|
+
/** The service name of a start: the option, else `OTEL_SERVICE_NAME`, else "node-service" */
|
|
23
|
+
export declare function nodeServiceName(serviceName: string | undefined, env: Env): string;
|
|
24
|
+
/** The configuration of the registration of the context */
|
|
25
|
+
export interface NodeContextRegistrationOptions {
|
|
26
|
+
readonly serviceName?: string | undefined;
|
|
27
|
+
readonly resourceAttributes?: Attributes | undefined;
|
|
28
|
+
readonly spanProcessors?: readonly SpanProcessor[] | undefined;
|
|
29
|
+
readonly env?: Env | undefined;
|
|
30
|
+
}
|
|
31
|
+
/** A registration of the context */
|
|
32
|
+
export interface NodeContextRegistration {
|
|
33
|
+
/** Stop the tracer provider, flush its processors, and remove the registration. A second call gives the same promise. */
|
|
34
|
+
stop(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Register the context for `caller`: the claim, AsyncLocalStorage as the context manager, the propagator of
|
|
38
|
+
* otel-ts, and a tracer provider with the resource of the service. A tracer provider of the SDK gives each span
|
|
39
|
+
* a trace id, also with no span processor.
|
|
40
|
+
*
|
|
41
|
+
* `prepare` runs after the claim and before the context. When it throws, the claim goes back.
|
|
42
|
+
*/
|
|
43
|
+
export declare function registerNodeContext(caller: string, options: NodeContextRegistrationOptions, prepare?: () => void): NodeContextRegistration;
|
|
44
|
+
export {};
|
|
45
|
+
//# sourceMappingURL=node-registration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-registration.d.ts","sourceRoot":"","sources":["../src/node-registration.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAA+B,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAElF,OAAO,EASL,KAAK,gBAAgB,EACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAkB,KAAK,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAI9E,KAAK,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAWxD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,UAAU,GAAG,gBAAgB,EAAE,CAO1F;AAED,6FAA6F;AAC7F,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,CAEjF;AAED,2DAA2D;AAC3D,MAAM,WAAW,8BAA8B;IAC7C,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C,QAAQ,CAAC,kBAAkB,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IACrD,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,aAAa,EAAE,GAAG,SAAS,CAAC;IAC/D,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,SAAS,CAAC;CAChC;AAED,oCAAoC;AACpC,MAAM,WAAW,uBAAuB;IACtC,yHAAyH;IACzH,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,8BAA8B,EAAE,OAAO,CAAC,EAAE,MAAM,IAAI,GAAG,uBAAuB,CAoC1I"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The registration of the context of a Node service with no SDK of `@opentelemetry/sdk-node`
|
|
3
|
+
* (docs/CONTEXT-DESIGN.md, section 4). The entry `/node/context` and the export off of the entry `/node`
|
|
4
|
+
* use it. Thus the two paths register the same context.
|
|
5
|
+
*
|
|
6
|
+
* This module imports no exporter, no instrumentation and no SDK of `@opentelemetry/sdk-node`.
|
|
7
|
+
* The entries test holds this rule.
|
|
8
|
+
*/
|
|
9
|
+
import { context, propagation, trace } from "@opentelemetry/api";
|
|
10
|
+
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
|
|
11
|
+
import { defaultResource, detectResources, envDetector, hostDetector, osDetector, processDetector, resourceFromAttributes, serviceInstanceIdDetector, } from "@opentelemetry/resources";
|
|
12
|
+
import { TracerProvider } from "@opentelemetry/sdk-trace";
|
|
13
|
+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
14
|
+
import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
|
|
15
|
+
/** The resource detectors of `OTEL_NODE_RESOURCE_DETECTORS`, with the names and the sequence of the NodeSDK. `env` is last. */
|
|
16
|
+
const RESOURCE_DETECTORS = new Map([
|
|
17
|
+
["host", hostDetector],
|
|
18
|
+
["os", osDetector],
|
|
19
|
+
["serviceinstance", serviceInstanceIdDetector],
|
|
20
|
+
["process", processDetector],
|
|
21
|
+
["env", envDetector],
|
|
22
|
+
]);
|
|
23
|
+
/**
|
|
24
|
+
* The resource detectors that the NodeSDK uses: the names of `OTEL_NODE_RESOURCE_DETECTORS` (`host`, `os`,
|
|
25
|
+
* `serviceinstance`, `process`, `env`, `all` or `none`; default `all`), then a detector that gives
|
|
26
|
+
* `attributes`. That last detector wins over each other detector, also over `OTEL_RESOURCE_ATTRIBUTES` (the
|
|
27
|
+
* detector `env`), as the option `resourceAttributes` of `startNodeSDK()` of the SDK does. Without it, a
|
|
28
|
+
* `service.instance.id` of the service lost to the random id of the detector `serviceinstance`. The NodeSDK
|
|
29
|
+
* ignores a name that it does not know, with a warning. This function ignores it too.
|
|
30
|
+
*/
|
|
31
|
+
export function nodeResourceDetectors(env, attributes) {
|
|
32
|
+
const names = (env["OTEL_NODE_RESOURCE_DETECTORS"] ?? "")
|
|
33
|
+
.split(",")
|
|
34
|
+
.map((name) => name.trim())
|
|
35
|
+
.filter((name) => name !== "");
|
|
36
|
+
const chosen = names.length === 0 || names.includes("all") ? [...RESOURCE_DETECTORS.values()] : names.includes("none") ? [] : names.flatMap((name) => RESOURCE_DETECTORS.get(name) ?? []);
|
|
37
|
+
return [...chosen, { detect: () => ({ attributes: { ...attributes } }) }];
|
|
38
|
+
}
|
|
39
|
+
/** The service name of a start: the option, else `OTEL_SERVICE_NAME`, else "node-service" */
|
|
40
|
+
export function nodeServiceName(serviceName, env) {
|
|
41
|
+
return serviceName ?? env["OTEL_SERVICE_NAME"] ?? "node-service";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register the context for `caller`: the claim, AsyncLocalStorage as the context manager, the propagator of
|
|
45
|
+
* otel-ts, and a tracer provider with the resource of the service. A tracer provider of the SDK gives each span
|
|
46
|
+
* a trace id, also with no span processor.
|
|
47
|
+
*
|
|
48
|
+
* `prepare` runs after the claim and before the context. When it throws, the claim goes back.
|
|
49
|
+
*/
|
|
50
|
+
export function registerNodeContext(caller, options, prepare) {
|
|
51
|
+
const release = claimContextRegistration(caller);
|
|
52
|
+
let tracerProvider;
|
|
53
|
+
try {
|
|
54
|
+
prepare?.();
|
|
55
|
+
const env = options.env ?? process.env;
|
|
56
|
+
// The sequence of the NodeSDK: the default resource, then the detectors, then the service name
|
|
57
|
+
const resource = defaultResource()
|
|
58
|
+
.merge(detectResources({ detectors: nodeResourceDetectors(env, options.resourceAttributes ?? {}) }))
|
|
59
|
+
.merge(resourceFromAttributes({ [ATTR_SERVICE_NAME]: nodeServiceName(options.serviceName, env) }));
|
|
60
|
+
tracerProvider = new TracerProvider({ resource, spanProcessors: [...(options.spanProcessors ?? [])] });
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
release();
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
// OpenTelemetry JS 3.x has no register() on the provider: set the three globals here. A set does not throw.
|
|
67
|
+
context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
|
|
68
|
+
propagation.setGlobalPropagator(new ContextPropagator());
|
|
69
|
+
trace.setGlobalTracerProvider(tracerProvider);
|
|
70
|
+
let stopping;
|
|
71
|
+
return {
|
|
72
|
+
stop: () =>
|
|
73
|
+
// The shutdown of the provider does not wait for an export that a SimpleSpanProcessor started. That export first
|
|
74
|
+
// waits for the async attributes of the resource. Thus the stop flushes first.
|
|
75
|
+
(stopping ??= tracerProvider
|
|
76
|
+
.forceFlush()
|
|
77
|
+
.then(() => tracerProvider.shutdown())
|
|
78
|
+
.catch(() => {
|
|
79
|
+
// a flush that fails at the stop loses its spans, and nothing else
|
|
80
|
+
})
|
|
81
|
+
.finally(() => {
|
|
82
|
+
for (const api of [trace, propagation, context])
|
|
83
|
+
api.disable();
|
|
84
|
+
release();
|
|
85
|
+
})),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=node-registration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-registration.js","sourceRoot":"","sources":["../src/node-registration.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAmB,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,+BAA+B,EAAE,MAAM,oCAAoC,CAAC;AACrF,OAAO,EACL,eAAe,EACf,eAAe,EACf,WAAW,EACX,YAAY,EACZ,UAAU,EACV,eAAe,EACf,sBAAsB,EACtB,yBAAyB,GAE1B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,cAAc,EAAsB,MAAM,0BAA0B,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAItF,+HAA+H;AAC/H,MAAM,kBAAkB,GAA0C,IAAI,GAAG,CAAC;IACxE,CAAC,MAAM,EAAE,YAAY,CAAC;IACtB,CAAC,IAAI,EAAE,UAAU,CAAC;IAClB,CAAC,iBAAiB,EAA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;aACD,OAAO,CAAC,GAAG,EAAE;YACZ,KAAK,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC;gBAAE,GAAG,CAAC,OAAO,EAAE,CAAC;YAC/D,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;KACR,CAAC;AACJ,CAAC"}
|
package/dist/node.d.ts
CHANGED
|
@@ -35,9 +35,9 @@ export interface NodeTelemetryOptions {
|
|
|
35
35
|
metricReaders?: NodeSDKConfiguration["metricReaders"];
|
|
36
36
|
/**
|
|
37
37
|
* Export on or off. Default: on when `OTEL_EXPORTER_OTLP_ENDPOINT` has a
|
|
38
|
-
* value. With export off,
|
|
39
|
-
*
|
|
40
|
-
* log lines.
|
|
38
|
+
* value. With export off, it starts no SDK: it registers the context as
|
|
39
|
+
* `startNodeContext()` of the entry `/node/context` does. Each span gets a
|
|
40
|
+
* trace id, so the ids go through the service and into its own log lines.
|
|
41
41
|
*/
|
|
42
42
|
enabled?: boolean;
|
|
43
43
|
/**
|
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":"AA4BA,OAAO,EAA8C,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEjG,OAAO,EAAW,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAqB,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAMhF,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;;;;;OAKG;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,CAsDpF"}
|
package/dist/node.js
CHANGED
|
@@ -20,15 +20,20 @@
|
|
|
20
20
|
* context keys to each span and each log record. Only one part of a program
|
|
21
21
|
* registers it.
|
|
22
22
|
*
|
|
23
|
+
* This entry loads the SDK and each exporter also with export off. A service
|
|
24
|
+
* that exports nothing imports the entry `/node/context`, which loads none of them.
|
|
25
|
+
*
|
|
23
26
|
* @packageDocumentation
|
|
24
27
|
*/
|
|
25
28
|
import { register } from "node:module";
|
|
26
29
|
import { context, logs, metrics, propagation, trace } from "@opentelemetry/api";
|
|
30
|
+
import { registerInstrumentations } from "@opentelemetry/instrumentation";
|
|
27
31
|
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
28
32
|
import { NoopSpanProcessor } from "@opentelemetry/sdk-trace";
|
|
29
33
|
import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
|
|
30
34
|
import { contextMetricViews } from "./context-processors.js";
|
|
31
|
-
import { histogramAggregation, nodeLogRecordProcessors,
|
|
35
|
+
import { histogramAggregation, nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
|
|
36
|
+
import { nodeResourceDetectors, nodeServiceName, registerNodeContext } from "./node-registration.js";
|
|
32
37
|
export { exporterNames, histogramAggregation, otlpProtocol } from "./node-exporters.js";
|
|
33
38
|
export { outgoingTraceparent, createTraceEcho, runInRemoteTrace } from "./trace-echo.js";
|
|
34
39
|
/** The signals that stop the SDK when `stopOnSignals` is true */
|
|
@@ -42,44 +47,23 @@ const OWN_LISTENERS = new Set();
|
|
|
42
47
|
export function startNodeTelemetry(options = {}) {
|
|
43
48
|
const env = options.env ?? process.env;
|
|
44
49
|
const enabled = options.enabled ?? !!env["OTEL_EXPORTER_OTLP_ENDPOINT"];
|
|
45
|
-
const release = claimContextRegistration("startNodeTelemetry()");
|
|
46
50
|
let esmHook = false;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// In the try too: a hook that does not resolve gives back the claim
|
|
51
|
+
// In the claim: a hook that does not resolve gives back the claim
|
|
52
|
+
const registerHook = () => {
|
|
50
53
|
if (options.importMetaUrl) {
|
|
51
54
|
register("@opentelemetry/instrumentation/hook.mjs", options.importMetaUrl);
|
|
52
55
|
esmHook = true;
|
|
53
56
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
...(options.metricReaders ? { metricReaders: options.metricReaders } : {}),
|
|
65
|
-
}
|
|
66
|
-
: { spanProcessors, logRecordProcessors: [], metricReaders: [] };
|
|
67
|
-
sdk = new NodeSDK({
|
|
68
|
-
serviceName: options.serviceName ?? env["OTEL_SERVICE_NAME"] ?? "node-service",
|
|
69
|
-
textMapPropagator: new ContextPropagator(),
|
|
70
|
-
// The aggregation of OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION:
|
|
71
|
-
// the views replace the preference of each reader
|
|
72
|
-
views: contextMetricViews(histogramAggregation(env)),
|
|
73
|
-
...exports,
|
|
74
|
-
...(options.resourceAttributes ? { resourceDetectors: nodeResourceDetectors(env, options.resourceAttributes) } : {}),
|
|
75
|
-
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
76
|
-
});
|
|
77
|
-
sdk.start();
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
release();
|
|
81
|
-
throw error;
|
|
82
|
-
}
|
|
57
|
+
};
|
|
58
|
+
// With export off: the registration of the entry /node/context, and no SDK. Thus the two paths register the
|
|
59
|
+
// same context, and nothing leaves the process. The instrumentations get the global tracer provider, as in the SDK.
|
|
60
|
+
const stop = enabled
|
|
61
|
+
? startSdk(options, env, registerHook)
|
|
62
|
+
: registerNodeContext("startNodeTelemetry()", { serviceName: options.serviceName, resourceAttributes: options.resourceAttributes, env }, () => {
|
|
63
|
+
registerHook();
|
|
64
|
+
if (options.instrumentations)
|
|
65
|
+
registerInstrumentations({ instrumentations: options.instrumentations });
|
|
66
|
+
}).stop;
|
|
83
67
|
// The signal listeners of this start, removed at the stop
|
|
84
68
|
const listeners = [];
|
|
85
69
|
// One stop: a second call waits for the flush of the first
|
|
@@ -94,17 +78,7 @@ export function startNodeTelemetry(options = {}) {
|
|
|
94
78
|
process.removeListener(signal, listener);
|
|
95
79
|
OWN_LISTENERS.delete(listener);
|
|
96
80
|
}
|
|
97
|
-
return (stopping =
|
|
98
|
-
.shutdown()
|
|
99
|
-
.catch(() => {
|
|
100
|
-
// a flush that fails at the stop loses its spans, and nothing else
|
|
101
|
-
})
|
|
102
|
-
.finally(() => {
|
|
103
|
-
// The SDK leaves its globals at the stop. Remove them, so a next start registers its own.
|
|
104
|
-
for (const api of [trace, metrics, logs, propagation, context])
|
|
105
|
-
api.disable();
|
|
106
|
-
release();
|
|
107
|
-
}));
|
|
81
|
+
return (stopping = stop());
|
|
108
82
|
};
|
|
109
83
|
if (options.stopOnSignals ?? true) {
|
|
110
84
|
for (const signal of STOP_SIGNALS) {
|
|
@@ -127,4 +101,44 @@ export function startNodeTelemetry(options = {}) {
|
|
|
127
101
|
}
|
|
128
102
|
return { enabled, esmHook, shutdown };
|
|
129
103
|
}
|
|
104
|
+
/** Start the SDK with export on, and give the function that stops it and removes the registration */
|
|
105
|
+
function startSdk(options, env, registerHook) {
|
|
106
|
+
const release = claimContextRegistration("startNodeTelemetry()");
|
|
107
|
+
let sdk;
|
|
108
|
+
try {
|
|
109
|
+
registerHook();
|
|
110
|
+
// The SDK registers its tracer provider only for a list of span processors that is not empty. Without the
|
|
111
|
+
// provider, each span is the span of the API with the trace id zero. A processor that does nothing keeps the
|
|
112
|
+
// provider, so each span gets a trace id, also with the traces exporter "none".
|
|
113
|
+
const exporting = nodeSpanProcessors(env, options.traceExporter);
|
|
114
|
+
sdk = new NodeSDK({
|
|
115
|
+
serviceName: nodeServiceName(options.serviceName, env),
|
|
116
|
+
textMapPropagator: new ContextPropagator(),
|
|
117
|
+
// The aggregation of OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION:
|
|
118
|
+
// the views replace the preference of each reader
|
|
119
|
+
views: contextMetricViews(histogramAggregation(env)),
|
|
120
|
+
spanProcessors: exporting.length > 0 ? exporting : [new NoopSpanProcessor()],
|
|
121
|
+
logRecordProcessors: nodeLogRecordProcessors(env, options.logExporter),
|
|
122
|
+
...(options.metricReaders ? { metricReaders: options.metricReaders } : {}),
|
|
123
|
+
...(options.resourceAttributes ? { resourceDetectors: nodeResourceDetectors(env, options.resourceAttributes) } : {}),
|
|
124
|
+
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
125
|
+
});
|
|
126
|
+
sdk.start();
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
release();
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
return () => sdk
|
|
133
|
+
.shutdown()
|
|
134
|
+
.catch(() => {
|
|
135
|
+
// a flush that fails at the stop loses its spans, and nothing else
|
|
136
|
+
})
|
|
137
|
+
.finally(() => {
|
|
138
|
+
// The SDK leaves its globals at the stop. Remove them, so a next start registers its own.
|
|
139
|
+
for (const api of [trace, metrics, logs, propagation, context])
|
|
140
|
+
api.disable();
|
|
141
|
+
release();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
130
144
|
//# 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;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;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,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,OAAO,EAA6B,MAAM,yBAAyB,CAAC;AAE7E,OAAO,EAAE,iBAAiB,EAAqB,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;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