@mark1russell7/otel-ts 0.8.1 → 0.8.3

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 CHANGED
@@ -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
@@ -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,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,EAAE,CASpG"}
1
+ {"version":3,"file":"node-exporters.d.ts","sourceRoot":"","sources":["../src/node-exporters.ts"],"names":[],"mappings":"AAYA,O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CAAC,EAAE,iBAAiB,GAAG,kBAAkB,EAAE,CASpG"}
@@ -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,wBAAwB,EACxB,wBAAwB,GAGzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,eAAe,EAAE,cAAc,EAA4B,MAAM,4BAA4B,CAAC;AACvG,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,mBAAmB,EAAyC,MAAM,0BAA0B,CAAC;AAC/I,OAAO,EAAE,+BAA+B,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAW1H;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,MAAkB,EAAE,GAAQ;IACxD,MAAM,QAAQ,GAAG,QAAQ,MAAM,CAAC,WAAW,EAAE,WAAW,CAAC;IACzD,MAAM,KAAK,GAAG,CAA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,CAAC,EAAE,CAAC;AACvF,CAAC"}
package/dist/node.d.ts CHANGED
@@ -1,12 +1,19 @@
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
- import type { SpanExporter } from "@opentelemetry/sdk-trace";
4
+ import { type SpanExporter } from "@opentelemetry/sdk-trace";
4
5
  export { exporterNames, histogramAggregation, otlpProtocol, type ExporterName, type OtlpProtocol, type OtlpSignal } from "./node-exporters.js";
5
6
  export { outgoingTraceparent, createTraceEcho, runInRemoteTrace, type RemoteTraceOptions, type TraceEcho } from "./trace-echo.js";
6
7
  /** The configuration of `startNodeTelemetry()` */
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
@@ -28,14 +35,20 @@ export interface NodeTelemetryOptions {
28
35
  metricReaders?: NodeSDKConfiguration["metricReaders"];
29
36
  /**
30
37
  * 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.
38
+ * value. With export off, the context is registered all the same, and each
39
+ * span gets a trace id. So the ids go through the service and into its own
40
+ * log lines.
33
41
  */
34
42
  enabled?: boolean;
35
43
  /**
36
44
  * Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
37
45
  * service has no listener of its own for the signal, the process then ends
38
- * with the signal, as it does without this option.
46
+ * with the signal, as it does without this option. A service with a staged
47
+ * shutdown of its own (for example `@mark1russell7/service`) gives false and
48
+ * calls `shutdown()` in its last stage. With true, the stop removes the
49
+ * context at the signal, while the service still drains its requests: the
50
+ * log lines of the drain lose their trace ids, and the spans of the drained
51
+ * requests go.
39
52
  */
40
53
  stopOnSignals?: boolean;
41
54
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AA0BA,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,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,E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7B,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,CAqFpF"}
package/dist/node.js CHANGED
@@ -25,9 +25,10 @@
25
25
  import { register } from "node:module";
26
26
  import { context, logs, metrics, propagation, trace } from "@opentelemetry/api";
27
27
  import { NodeSDK } from "@opentelemetry/sdk-node";
28
+ import { NoopSpanProcessor } from "@opentelemetry/sdk-trace";
28
29
  import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
29
30
  import { contextMetricViews } from "./context-processors.js";
30
- import { histogramAggregation, nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
31
+ import { histogramAggregation, nodeLogRecordProcessors, nodeResourceDetectors, nodeSpanProcessors } from "./node-exporters.js";
31
32
  export { exporterNames, histogramAggregation, otlpProtocol } from "./node-exporters.js";
32
33
  export { outgoingTraceparent, createTraceEcho, runInRemoteTrace } from "./trace-echo.js";
33
34
  /** The signals that stop the SDK when `stopOnSignals` is true */
@@ -50,14 +51,19 @@ export function startNodeTelemetry(options = {}) {
50
51
  register("@opentelemetry/instrumentation/hook.mjs", options.importMetaUrl);
51
52
  esmHook = true;
52
53
  }
53
- // With export off: no processor and no reader, so nothing leaves the process
54
+ // With export off: no exporter and no reader, so nothing leaves the process.
55
+ // The SDK registers its tracer provider only for a list of span processors that is not empty. Without the
56
+ // provider, each span is the span of the API with the trace id zero. A processor that does nothing keeps the
57
+ // provider, so each span gets a trace id, also with export off or with the traces exporter "none".
58
+ const exporting = enabled ? nodeSpanProcessors(env, options.traceExporter) : [];
59
+ const spanProcessors = exporting.length > 0 ? exporting : [new NoopSpanProcessor()];
54
60
  const exports = enabled
55
61
  ? {
56
- spanProcessors: nodeSpanProcessors(env, options.traceExporter),
62
+ spanProcessors,
57
63
  logRecordProcessors: nodeLogRecordProcessors(env, options.logExporter),
58
64
  ...(options.metricReaders ? { metricReaders: options.metricReaders } : {}),
59
65
  }
60
- : { spanProcessors: [], logRecordProcessors: [], metricReaders: [] };
66
+ : { spanProcessors, logRecordProcessors: [], metricReaders: [] };
61
67
  sdk = new NodeSDK({
62
68
  serviceName: options.serviceName ?? env["OTEL_SERVICE_NAME"] ?? "node-service",
63
69
  textMapPropagator: new ContextPropagator(),
@@ -65,6 +71,7 @@ export function startNodeTelemetry(options = {}) {
65
71
  // the views replace the preference of each reader
66
72
  views: contextMetricViews(histogramAggregation(env)),
67
73
  ...exports,
74
+ ...(options.resourceAttributes ? { resourceDetectors: nodeResourceDetectors(env, options.resourceAttributes) } : {}),
68
75
  ...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
69
76
  });
70
77
  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,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,OAAO,E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EAAE,CAAC;YAClC,MAAM,QAAQ,GAAG,GAAS,EAAE;gBAC1B,gFAAgF;gBAChF,mFAAmF;gBACnF,oFAAoF;gBACpF,iFAAiF;gBACjF,MAAM,cAAc,GAAG,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;gBAC5F,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;YACF,SAAS,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;YACnC,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAC5B,uFAAuF;YACvF,OAAO,CAAC,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;AACxC,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.8.1",
4
+ "version": "0.8.3",
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)",
@@ -91,7 +91,7 @@
91
91
  "devDependencies": {
92
92
  "@mark1russell7/grafana-infra": "github:mark1russell7/grafana-infra",
93
93
  "@mark1russell7/lag": "^0.5.0",
94
- "@mark1russell7/rigor-testkit": "^0.1.2",
94
+ "@mark1russell7/rigor-testkit": "^0.2.0",
95
95
  "@opentelemetry/sdk-node": "~0.301.2",
96
96
  "@types/node": "^26.6.5",
97
97
  "@vitest/coverage-v8": "^4.1.12",
@@ -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
- import type { SpanExporter } from "@opentelemetry/sdk-trace";
29
+ import { NoopSpanProcessor, 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
@@ -60,14 +66,20 @@ export interface NodeTelemetryOptions {
60
66
  metricReaders?: NodeSDKConfiguration["metricReaders"];
61
67
  /**
62
68
  * Export on or off. Default: on when `OTEL_EXPORTER_OTLP_ENDPOINT` has a
63
- * value. With export off, the context is registered all the same, so the ids
64
- * go through the service and into its own log lines.
69
+ * value. With export off, the context is registered all the same, and each
70
+ * span gets a trace id. So the ids go through the service and into its own
71
+ * log lines.
65
72
  */
66
73
  enabled?: boolean;
67
74
  /**
68
75
  * Stop the SDK (and flush) at SIGTERM and SIGINT. Default: true. When the
69
76
  * service has no listener of its own for the signal, the process then ends
70
- * with the signal, as it does without this option.
77
+ * with the signal, as it does without this option. A service with a staged
78
+ * shutdown of its own (for example `@mark1russell7/service`) gives false and
79
+ * calls `shutdown()` in its last stage. With true, the stop removes the
80
+ * context at the signal, while the service still drains its requests: the
81
+ * log lines of the drain lose their trace ids, and the spans of the drained
82
+ * requests go.
71
83
  */
72
84
  stopOnSignals?: boolean;
73
85
  /**
@@ -113,14 +125,19 @@ export function startNodeTelemetry(options: NodeTelemetryOptions = {}): NodeTele
113
125
  register("@opentelemetry/instrumentation/hook.mjs", options.importMetaUrl);
114
126
  esmHook = true;
115
127
  }
116
- // With export off: no processor and no reader, so nothing leaves the process
128
+ // With export off: no exporter and no reader, so nothing leaves the process.
129
+ // The SDK registers its tracer provider only for a list of span processors that is not empty. Without the
130
+ // provider, each span is the span of the API with the trace id zero. A processor that does nothing keeps the
131
+ // provider, so each span gets a trace id, also with export off or with the traces exporter "none".
132
+ const exporting = enabled ? nodeSpanProcessors(env, options.traceExporter) : [];
133
+ const spanProcessors = exporting.length > 0 ? exporting : [new NoopSpanProcessor()];
117
134
  const exports: Partial<NodeSDKConfiguration> = enabled
118
135
  ? {
119
- spanProcessors: nodeSpanProcessors(env, options.traceExporter),
136
+ spanProcessors,
120
137
  logRecordProcessors: nodeLogRecordProcessors(env, options.logExporter),
121
138
  ...(options.metricReaders ? { metricReaders: options.metricReaders } : {}),
122
139
  }
123
- : { spanProcessors: [], logRecordProcessors: [], metricReaders: [] };
140
+ : { spanProcessors, logRecordProcessors: [], metricReaders: [] };
124
141
  sdk = new NodeSDK({
125
142
  serviceName: options.serviceName ?? env["OTEL_SERVICE_NAME"] ?? "node-service",
126
143
  textMapPropagator: new ContextPropagator(),
@@ -128,6 +145,7 @@ export function startNodeTelemetry(options: NodeTelemetryOptions = {}): NodeTele
128
145
  // the views replace the preference of each reader
129
146
  views: contextMetricViews(histogramAggregation(env)),
130
147
  ...exports,
148
+ ...(options.resourceAttributes ? { resourceDetectors: nodeResourceDetectors(env, options.resourceAttributes) } : {}),
131
149
  ...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
132
150
  });
133
151
  sdk.start();