@devopsplaybook.io/otel-utils 1.11.0-beta.43.27704ef → 1.12.0-beta.44.7e03cb8
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 +73 -1
- package/dist/src/StandardLogger.js +8 -10
- package/dist/src/StandardMeter.js +7 -10
- package/dist/src/StandardTracer.js +9 -10
- package/dist/src/models/ConfigOTelInterface.d.ts +9 -0
- package/dist/src/utils/configValidation.d.ts +5 -1
- package/dist/src/utils/configValidation.js +52 -0
- package/dist/src/utils/exporterConfig.d.ts +9 -0
- package/dist/src/utils/exporterConfig.js +30 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -23,19 +23,50 @@ All classes accept a `ConfigOTelInterface` object. The fields mirror environment
|
|
|
23
23
|
| `OPENTELEMETRY_COLLECTOR_HTTP_LOGS` | `string?` | — | OTLP HTTP endpoint for logs |
|
|
24
24
|
| `OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_INTERVAL_SECONDS` | `number?` | `60` | Log export interval in seconds (clamped to ≥ 1) |
|
|
25
25
|
| `OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS` | `number?` | `60` | Metrics export interval in seconds (clamped to ≥ 1) |
|
|
26
|
+
| `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_INTERVAL_SECONDS` | `number?` | `5` | Trace export interval in seconds (clamped to ≥ 1) |
|
|
26
27
|
| `OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_MAX_QUEUE_SIZE` | `number?` | `2048` | Max log records buffered before export |
|
|
27
28
|
| `OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_MAX_BATCH_SIZE` | `number?` | `512` | Max log records per export batch |
|
|
28
29
|
| `OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_TIMEOUT_MILLIS` | `number?` | `30000` | Log export timeout in milliseconds |
|
|
30
|
+
| `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_QUEUE_SIZE` | `number?` | `2048` | Max spans buffered before export |
|
|
31
|
+
| `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_BATCH_SIZE` | `number?` | `512` | Max spans per export batch |
|
|
32
|
+
| `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_TIMEOUT_MILLIS` | `number?` | `30000` | Trace export timeout in milliseconds |
|
|
33
|
+
| `OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_TIMEOUT_MILLIS` | `number?` | `30000` | Metrics reader export timeout (clamped to the interval) |
|
|
34
|
+
| `OPENTELEMETRY_COLLECTOR_HTTP_HEADERS` | `Record<string, string>?` | `{}` | Extra OTLP HTTP headers for all signals |
|
|
35
|
+
| `OPENTELEMETRY_COLLECTOR_HTTP_TIMEOUT_MILLIS` | `number?` | `10000` | Per-request OTLP HTTP exporter timeout |
|
|
36
|
+
| `OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION` | `"gzip" \| "none"` | `"none"` | OTLP HTTP request compression |
|
|
29
37
|
| `OPENTELEMETRY_CONSOLE_LOG_MODE` | `"always" \| "fallback" \| "never"` | `"always"` | Console mirroring mode for `ModuleLogger` |
|
|
30
38
|
| `OPENTELEMETRY_LOG_LEVEL` | `"trace" \| "debug" \| "info" \| "warn" \| "error"` | `"info"` | Minimum level emitted to OTel and mirrored to the console |
|
|
31
39
|
| `OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER` | `string?` | — | Bearer token for collector auth |
|
|
32
40
|
|
|
33
41
|
`SERVICE_ID` and `VERSION` must be non-empty strings. `StandardTracer`/`StandardMeter` constructors and `StandardLogger.initOTel` fail fast with `[otel-utils] SERVICE_ID must be a non-empty string` / `[otel-utils] VERSION must be a non-empty string` otherwise. A failed re-initialisation of `StandardLogger` keeps the previous provider in place.
|
|
34
42
|
|
|
35
|
-
Numeric export intervals below 1 s (including `0` and negative values from an empty environment variable) are clamped to 1 s; `NaN`/`Infinity` fall back to the 60 s
|
|
43
|
+
Numeric export intervals below 1 s (including `0` and negative values from an empty environment variable) are clamped to 1 s; `NaN`/`Infinity` fall back to the signal's default (60 s for logs and metrics, 5 s for traces). Non-positive or non-integer batch values fall back to their defaults. The metrics export timeout is clamped to the export interval when it is larger, with one `console.warn` — see [StandardMeter](#standardmeter). An invalid `OPENTELEMETRY_LOG_LEVEL` logs one `console.warn` and falls back to `"info"`; an invalid `OPENTELEMETRY_CONSOLE_LOG_MODE` logs one `console.warn` and falls back to `"always"`; an invalid `OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION` logs one `console.warn` and falls back to `"none"`. Invalid collector URLs and header maps throw an `Error` naming the offending field (see [Exporter HTTP options](#exporter-http-options-all-signals)).
|
|
36
44
|
|
|
37
45
|
When a collector endpoint is not provided, the corresponding signal (traces, logs, or metrics) is initialized with no export: traces and metrics still have a local provider but no data is sent, and `StandardLogger` creates no provider (its `getLogger()` returns `undefined`).
|
|
38
46
|
|
|
47
|
+
## Exporter HTTP options (all signals)
|
|
48
|
+
|
|
49
|
+
Traces, logs and metrics share the same OTLP HTTP exporter options:
|
|
50
|
+
|
|
51
|
+
| Option | Default | Behaviour |
|
|
52
|
+
| --------------------------------------------- | -------- | -------------------------------------------------------------------------- |
|
|
53
|
+
| `OPENTELEMETRY_COLLECTOR_HTTP_HEADERS` | `{}` | Extra request headers (`Record<string, string>`) sent to the collector. |
|
|
54
|
+
| `OPENTELEMETRY_COLLECTOR_HTTP_TIMEOUT_MILLIS` | `10000` | Per-request timeout of the OTLP exporter (not the batch export timeout). |
|
|
55
|
+
| `OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION` | `"none"` | `"gzip"` compresses OTLP request bodies; `"none"` sends them uncompressed. |
|
|
56
|
+
|
|
57
|
+
**Header precedence:** generic headers from `OPENTELEMETRY_COLLECTOR_HTTP_HEADERS` are applied first; when `OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER` is set, a bearer `Authorization` header is applied last and wins. Any generic `Authorization`/`authorization` entry is removed first (case-insensitive), so exactly one authorization header is sent. Invalid header maps (not an object, or a non-string value) throw an `Error` naming `OPENTELEMETRY_COLLECTOR_HTTP_HEADERS` and the offending key. An invalid compression value logs one `console.warn` and falls back to `"none"`.
|
|
58
|
+
|
|
59
|
+
**Endpoint safety:** every configured `OPENTELEMETRY_COLLECTOR_HTTP_*` endpoint is validated when the provider is constructed (for `StandardLogger`, during `initOTel` before the previous provider is released). Malformed URLs and non-`http(s)` protocols (for example `ftp://`) throw `[otel-utils] <VARIABLE> must be a valid http(s) URL, got "..."`, naming the field, instead of failing later at export time. A `http://` endpoint combined with `OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER` logs exactly one `console.warn` per provider initialization explaining that the bearer token is sent in clear text; it does not fail, since plain HTTP may be intentional in a homelab. The token itself is never logged.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const tracer = new StandardTracer({
|
|
63
|
+
...config,
|
|
64
|
+
OPENTELEMETRY_COLLECTOR_HTTP_HEADERS: { "X-Tenant-Id": "acme" },
|
|
65
|
+
OPENTELEMETRY_COLLECTOR_HTTP_TIMEOUT_MILLIS: 5000,
|
|
66
|
+
OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION: "gzip",
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
39
70
|
## Exported Classes
|
|
40
71
|
|
|
41
72
|
### StandardTracer
|
|
@@ -65,6 +96,29 @@ span.end();
|
|
|
65
96
|
- `shutdown()` — Flushes buffered spans and shuts the tracer provider down. Idempotent.
|
|
66
97
|
- `forceFlush()` — Flushes buffered spans to the collector without shutting the provider down; resolves immediately when no collector is configured. Use it at checkpoints in short-lived jobs and serverless handlers.
|
|
67
98
|
|
|
99
|
+
**Trace batch tunables and loss window**
|
|
100
|
+
|
|
101
|
+
When a traces endpoint is configured, spans flow through a `BatchSpanProcessor`. It buffers spans in memory and exports them periodically:
|
|
102
|
+
|
|
103
|
+
| Option | Config field | Default |
|
|
104
|
+
| ---------------------- | -------------------------------------------------------- | -------- |
|
|
105
|
+
| `scheduledDelayMillis` | `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_INTERVAL_SECONDS` | 5000 ms |
|
|
106
|
+
| `maxQueueSize` | `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_QUEUE_SIZE` | 2048 |
|
|
107
|
+
| `maxExportBatchSize` | `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_BATCH_SIZE` | 512 |
|
|
108
|
+
| `exportTimeoutMillis` | `OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_TIMEOUT_MILLIS` | 30000 ms |
|
|
109
|
+
|
|
110
|
+
A span waits up to `scheduledDelayMillis` (5 s by default) before it is exported; each export sends at most `maxExportBatchSize` (512) spans with a 30 s timeout, and at most `maxQueueSize` (2048) spans are buffered. Spans that arrive while the queue is full are **dropped silently** — `BatchSpanProcessor` reports drops only through the OpenTelemetry `diag` logger, which is disabled by default. A crash or restart loses every span buffered since the last export; call `shutdown()` or use `registerShutdownHooks` (see [Graceful shutdown](#graceful-shutdown)) to flush them. Keep `maxExportBatchSize` ≤ `maxQueueSize` (the SDK clamps it otherwise).
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const tracer = new StandardTracer({
|
|
114
|
+
...config,
|
|
115
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_INTERVAL_SECONDS: 2,
|
|
116
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_QUEUE_SIZE: 4096,
|
|
117
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_BATCH_SIZE: 1024,
|
|
118
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_TIMEOUT_MILLIS: 10000,
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
68
122
|
### StandardLogger
|
|
69
123
|
|
|
70
124
|
Initializes a `LoggerProvider` with an OTLP log exporter and a `BatchLogRecordProcessor` (`maxQueueSize: 2048`, `maxExportBatchSize: 512`, `exportTimeoutMillis: 30000`, `scheduledDelayMillis: 60000` by default). When `OPENTELEMETRY_COLLECTOR_HTTP_LOGS` is set, the provider is also registered as the global OTel logger provider (first instance only), so `logs.getLogger(...)` from `@opentelemetry/api` returns loggers wired to the configured exporter. Each instance always logs through its own provider (`logger.createModuleLogger(...)`), so a second instance never mis-routes its records to the first one. Without that endpoint, `initOTel` still records the service identity but creates no provider and `getLogger()` returns `undefined`.
|
|
@@ -157,6 +211,23 @@ When a `Span` is passed as `context`, the log record includes `span.id` and `tra
|
|
|
157
211
|
|
|
158
212
|
Creates a `MeterProvider` with an optional `PeriodicExportingMetricReader` (`concurrencyLimit: 5`). Provides convenience factories for common metric types. When `OPENTELEMETRY_COLLECTOR_HTTP_METRICS` is set, the provider is registered as the global meter provider (first instance only), so metrics emitted through `metrics.getMeter(...)` reach the configured exporter. Unconfigured meters leave the global API untouched (Noop).
|
|
159
213
|
|
|
214
|
+
**Metric reader tunables**
|
|
215
|
+
|
|
216
|
+
| Option | Config field | Default |
|
|
217
|
+
| ---------------------- | --------------------------------------------------------- | -------- |
|
|
218
|
+
| `exportIntervalMillis` | `OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS` | 60000 ms |
|
|
219
|
+
| `exportTimeoutMillis` | `OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_TIMEOUT_MILLIS` | 30000 ms |
|
|
220
|
+
|
|
221
|
+
Each export is cancelled after `exportTimeoutMillis`. When the configured timeout exceeds the export interval, it is clamped to the interval and one `console.warn` explains the effective value (`PeriodicExportingMetricReader` requires timeout ≤ interval). For example, a 5 s interval with the default 30 s timeout exports with a 5 s timeout and warns once at startup. The default 60 s interval and 30 s timeout are compatible and warning-free.
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const meter = new StandardMeter({
|
|
225
|
+
...config,
|
|
226
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS: 15,
|
|
227
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_TIMEOUT_MILLIS: 10000,
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
160
231
|
```typescript
|
|
161
232
|
import { StandardMeter } from "@devopsplaybook.io/otel-utils";
|
|
162
233
|
|
|
@@ -318,6 +389,7 @@ All three signals share the same service identity (name + version) and resource
|
|
|
318
389
|
- `@opentelemetry/sdk-logs` — Logging SDK
|
|
319
390
|
- `@opentelemetry/exporter-trace-otlp-http` / `@opentelemetry/exporter-metrics-otlp-http` / `@opentelemetry/exporter-logs-otlp-http` — OTLP HTTP exporters
|
|
320
391
|
- `@opentelemetry/id-generator-aws-xray` — AWS X-Ray trace ID format
|
|
392
|
+
- `@opentelemetry/otlp-exporter-base` — OTLP exporter compression option
|
|
321
393
|
- `@opentelemetry/context-async-hooks` — Async context management
|
|
322
394
|
- `@opentelemetry/core` — W3C trace context propagation
|
|
323
395
|
- `@opentelemetry/resources` / `@opentelemetry/semantic-conventions` — Resource attributes
|
|
@@ -7,6 +7,7 @@ const sdk_logs_1 = require("@opentelemetry/sdk-logs");
|
|
|
7
7
|
const createResource_1 = require("./utils/createResource");
|
|
8
8
|
const configValidation_1 = require("./utils/configValidation");
|
|
9
9
|
const ModuleLogger_1 = require("./ModuleLogger");
|
|
10
|
+
const exporterConfig_1 = require("./utils/exporterConfig");
|
|
10
11
|
let globalLoggerProvider;
|
|
11
12
|
const ALREADY_REGISTERED_WARNING = "[otel-utils] A global logger provider is already registered; StandardLogger will not replace it. This instance still logs through its own provider, but logs.getLogger() keeps using the existing provider.";
|
|
12
13
|
class StandardLogger {
|
|
@@ -50,24 +51,21 @@ class StandardLogger {
|
|
|
50
51
|
(0, configValidation_1.validateServiceIdentity)(config);
|
|
51
52
|
this.consoleMode = (0, configValidation_1.normalizeConsoleLogMode)(config.OPENTELEMETRY_CONSOLE_LOG_MODE);
|
|
52
53
|
this.logLevel = (0, configValidation_1.normalizeLogLevel)(config.OPENTELEMETRY_LOG_LEVEL);
|
|
54
|
+
// Build the exporter options before releasing the previous provider: a bad
|
|
55
|
+
// re-init must keep the working provider in place.
|
|
56
|
+
const exporterOptions = config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS
|
|
57
|
+
? (0, exporterConfig_1.buildCollectorExporterOptions)(config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS, config, "OPENTELEMETRY_COLLECTOR_HTTP_LOGS")
|
|
58
|
+
: undefined;
|
|
53
59
|
// Re-initialisation flushes and unregisters the previous provider first.
|
|
54
60
|
if (this.loggerProvider) {
|
|
55
61
|
await this.releaseProvider();
|
|
56
62
|
}
|
|
57
63
|
this.serviceName = config.SERVICE_ID;
|
|
58
64
|
this.serviceVersion = config.VERSION;
|
|
59
|
-
if (!
|
|
65
|
+
if (!exporterOptions) {
|
|
60
66
|
return;
|
|
61
67
|
}
|
|
62
|
-
const
|
|
63
|
-
if (config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER) {
|
|
64
|
-
exporterHeaders["Authorization"] =
|
|
65
|
-
`Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
66
|
-
}
|
|
67
|
-
const exporter = new exporter_logs_otlp_http_1.OTLPLogExporter({
|
|
68
|
-
url: config.OPENTELEMETRY_COLLECTOR_HTTP_LOGS,
|
|
69
|
-
headers: exporterHeaders,
|
|
70
|
-
});
|
|
68
|
+
const exporter = new exporter_logs_otlp_http_1.OTLPLogExporter(exporterOptions);
|
|
71
69
|
this.loggerProvider = new sdk_logs_1.LoggerProvider({
|
|
72
70
|
processors: [
|
|
73
71
|
new sdk_logs_1.BatchLogRecordProcessor({
|
|
@@ -6,6 +6,7 @@ const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-ot
|
|
|
6
6
|
const createResource_1 = require("./utils/createResource");
|
|
7
7
|
const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
|
|
8
8
|
const configValidation_1 = require("./utils/configValidation");
|
|
9
|
+
const exporterConfig_1 = require("./utils/exporterConfig");
|
|
9
10
|
let globalMeterProvider;
|
|
10
11
|
const ALREADY_REGISTERED_WARNING = "[otel-utils] A global meter provider is already registered; StandardMeter will not replace it. This instance still records metrics through its own provider, but metrics.getMeter() keeps using the existing provider.";
|
|
11
12
|
class StandardMeter {
|
|
@@ -17,22 +18,18 @@ class StandardMeter {
|
|
|
17
18
|
let hasCollector = false;
|
|
18
19
|
if (config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS) {
|
|
19
20
|
hasCollector = true;
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
headers: {},
|
|
21
|
+
const metricExporter = new exporter_metrics_otlp_http_1.OTLPMetricExporter({
|
|
22
|
+
...(0, exporterConfig_1.buildCollectorExporterOptions)(config.OPENTELEMETRY_COLLECTOR_HTTP_METRICS, config, "OPENTELEMETRY_COLLECTOR_HTTP_METRICS"),
|
|
23
23
|
concurrencyLimit: 5,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
collectorOptions.headers["Authorization"] =
|
|
27
|
-
`Bearer ${config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER}`;
|
|
28
|
-
}
|
|
29
|
-
const metricExporter = new exporter_metrics_otlp_http_1.OTLPMetricExporter(collectorOptions);
|
|
24
|
+
});
|
|
25
|
+
const exportIntervalMillis = (0, configValidation_1.normalizeExportIntervalSeconds)(config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_INTERVAL_SECONDS, 60) * 1000;
|
|
30
26
|
this.meterProvider = new sdk_metrics_1.MeterProvider({
|
|
31
27
|
resource: (0, createResource_1.createOTelResource)(this.serviceName, this.serviceVersion),
|
|
32
28
|
readers: [
|
|
33
29
|
new sdk_metrics_1.PeriodicExportingMetricReader({
|
|
34
30
|
exporter: metricExporter,
|
|
35
|
-
exportIntervalMillis
|
|
31
|
+
exportIntervalMillis,
|
|
32
|
+
exportTimeoutMillis: (0, configValidation_1.resolveMetricExportTimeout)((0, configValidation_1.normalizePositiveInteger)(config.OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_TIMEOUT_MILLIS, 30000), exportIntervalMillis),
|
|
36
33
|
}),
|
|
37
34
|
],
|
|
38
35
|
});
|
|
@@ -10,6 +10,7 @@ const sdk_trace_1 = require("@opentelemetry/sdk-trace");
|
|
|
10
10
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
11
11
|
const configValidation_1 = require("./utils/configValidation");
|
|
12
12
|
const createResource_1 = require("./utils/createResource");
|
|
13
|
+
const exporterConfig_1 = require("./utils/exporterConfig");
|
|
13
14
|
let globalTracerProvider;
|
|
14
15
|
let contextManagerInitialized = false;
|
|
15
16
|
function getRegisteredTracerProvider() {
|
|
@@ -27,16 +28,14 @@ class StandardTracer {
|
|
|
27
28
|
this.serviceVersion = config.VERSION;
|
|
28
29
|
const spanProcessors = [];
|
|
29
30
|
if (config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES) {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
39
|
-
spanProcessors.push(new sdk_trace_1.BatchSpanProcessor({ exporter }));
|
|
31
|
+
const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter((0, exporterConfig_1.buildCollectorExporterOptions)(config.OPENTELEMETRY_COLLECTOR_HTTP_TRACES, config, "OPENTELEMETRY_COLLECTOR_HTTP_TRACES"));
|
|
32
|
+
spanProcessors.push(new sdk_trace_1.BatchSpanProcessor({
|
|
33
|
+
exporter,
|
|
34
|
+
scheduledDelayMillis: (0, configValidation_1.normalizeExportIntervalSeconds)(config.OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_INTERVAL_SECONDS, 5) * 1000,
|
|
35
|
+
maxQueueSize: (0, configValidation_1.normalizePositiveInteger)(config.OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_QUEUE_SIZE, 2048),
|
|
36
|
+
maxExportBatchSize: (0, configValidation_1.normalizePositiveInteger)(config.OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_BATCH_SIZE, 512),
|
|
37
|
+
exportTimeoutMillis: (0, configValidation_1.normalizePositiveInteger)(config.OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_TIMEOUT_MILLIS, 30000),
|
|
38
|
+
}));
|
|
40
39
|
}
|
|
41
40
|
this.traceProvider = new sdk_trace_1.TracerProvider({
|
|
42
41
|
idGenerator: new id_generator_aws_xray_1.AWSXRayIdGenerator(),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type ConsoleLogMode = "always" | "fallback" | "never";
|
|
2
2
|
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error";
|
|
3
|
+
export type Compression = "gzip" | "none";
|
|
3
4
|
export interface ConfigOTelInterface {
|
|
4
5
|
SERVICE_ID: string;
|
|
5
6
|
VERSION: string;
|
|
@@ -11,6 +12,14 @@ export interface ConfigOTelInterface {
|
|
|
11
12
|
OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_MAX_QUEUE_SIZE?: number;
|
|
12
13
|
OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_MAX_BATCH_SIZE?: number;
|
|
13
14
|
OPENTELEMETRY_COLLECTOR_EXPORT_LOGS_TIMEOUT_MILLIS?: number;
|
|
15
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_INTERVAL_SECONDS?: number;
|
|
16
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_QUEUE_SIZE?: number;
|
|
17
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_MAX_BATCH_SIZE?: number;
|
|
18
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_TRACES_TIMEOUT_MILLIS?: number;
|
|
19
|
+
OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_TIMEOUT_MILLIS?: number;
|
|
20
|
+
OPENTELEMETRY_COLLECTOR_HTTP_HEADERS?: Record<string, string>;
|
|
21
|
+
OPENTELEMETRY_COLLECTOR_HTTP_TIMEOUT_MILLIS?: number;
|
|
22
|
+
OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION?: Compression;
|
|
14
23
|
OPENTELEMETRY_CONSOLE_LOG_MODE?: ConsoleLogMode;
|
|
15
24
|
OPENTELEMETRY_LOG_LEVEL?: LogLevel;
|
|
16
25
|
OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER?: string;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import type { ConfigOTelInterface, ConsoleLogMode, LogLevel } from "../models/ConfigOTelInterface";
|
|
1
|
+
import type { Compression, ConfigOTelInterface, ConsoleLogMode, LogLevel } from "../models/ConfigOTelInterface";
|
|
2
2
|
export declare function validateServiceIdentity(config: Pick<ConfigOTelInterface, "SERVICE_ID" | "VERSION">): void;
|
|
3
3
|
export declare function normalizeExportIntervalSeconds(value: number | undefined, defaultSeconds: number): number;
|
|
4
4
|
export declare function normalizePositiveInteger(value: number | undefined, defaultValue: number): number;
|
|
5
5
|
export declare function normalizeConsoleLogMode(value: unknown): ConsoleLogMode;
|
|
6
|
+
export declare function validateCollectorUrl(value: unknown, variableName: string): void;
|
|
7
|
+
export declare function normalizeHeaders(value: unknown): Record<string, string>;
|
|
8
|
+
export declare function normalizeCompression(value: unknown): Compression;
|
|
9
|
+
export declare function resolveMetricExportTimeout(timeoutMillis: number, intervalMillis: number): number;
|
|
6
10
|
export declare function normalizeLogLevel(value: unknown): LogLevel;
|
|
@@ -4,6 +4,10 @@ exports.validateServiceIdentity = validateServiceIdentity;
|
|
|
4
4
|
exports.normalizeExportIntervalSeconds = normalizeExportIntervalSeconds;
|
|
5
5
|
exports.normalizePositiveInteger = normalizePositiveInteger;
|
|
6
6
|
exports.normalizeConsoleLogMode = normalizeConsoleLogMode;
|
|
7
|
+
exports.validateCollectorUrl = validateCollectorUrl;
|
|
8
|
+
exports.normalizeHeaders = normalizeHeaders;
|
|
9
|
+
exports.normalizeCompression = normalizeCompression;
|
|
10
|
+
exports.resolveMetricExportTimeout = resolveMetricExportTimeout;
|
|
7
11
|
exports.normalizeLogLevel = normalizeLogLevel;
|
|
8
12
|
function validateServiceIdentity(config) {
|
|
9
13
|
if (typeof config.SERVICE_ID !== "string" ||
|
|
@@ -35,6 +39,54 @@ function normalizeConsoleLogMode(value) {
|
|
|
35
39
|
}
|
|
36
40
|
return "always";
|
|
37
41
|
}
|
|
42
|
+
function validateCollectorUrl(value, variableName) {
|
|
43
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
44
|
+
throw new Error(`[otel-utils] ${variableName} must be a non-empty valid http(s) URL, got "${String(value)}"`);
|
|
45
|
+
}
|
|
46
|
+
let url;
|
|
47
|
+
try {
|
|
48
|
+
url = new URL(value);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new Error(`[otel-utils] ${variableName} must be a valid http(s) URL, got "${value}"`);
|
|
52
|
+
}
|
|
53
|
+
if ((url.protocol !== "http:" && url.protocol !== "https:") ||
|
|
54
|
+
url.hostname === "") {
|
|
55
|
+
throw new Error(`[otel-utils] ${variableName} must be a valid http(s) URL, got "${value}"`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function normalizeHeaders(value) {
|
|
59
|
+
if (value === undefined) {
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
63
|
+
throw new Error("[otel-utils] OPENTELEMETRY_COLLECTOR_HTTP_HEADERS must be an object mapping header names to strings");
|
|
64
|
+
}
|
|
65
|
+
const headers = {};
|
|
66
|
+
for (const [name, headerValue] of Object.entries(value)) {
|
|
67
|
+
if (typeof headerValue !== "string") {
|
|
68
|
+
throw new Error(`[otel-utils] OPENTELEMETRY_COLLECTOR_HTTP_HEADERS["${name}"] must be a string`);
|
|
69
|
+
}
|
|
70
|
+
headers[name] = headerValue;
|
|
71
|
+
}
|
|
72
|
+
return headers;
|
|
73
|
+
}
|
|
74
|
+
function normalizeCompression(value) {
|
|
75
|
+
if (value === "gzip" || value === "none") {
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
if (value !== undefined) {
|
|
79
|
+
console.warn(`[otel-utils] Invalid OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION value "${String(value)}"; expected "gzip" or "none". Defaulting to "none".`);
|
|
80
|
+
}
|
|
81
|
+
return "none";
|
|
82
|
+
}
|
|
83
|
+
function resolveMetricExportTimeout(timeoutMillis, intervalMillis) {
|
|
84
|
+
if (timeoutMillis <= intervalMillis) {
|
|
85
|
+
return timeoutMillis;
|
|
86
|
+
}
|
|
87
|
+
console.warn(`[otel-utils] OPENTELEMETRY_COLLECTOR_EXPORT_METRICS_TIMEOUT_MILLIS (${timeoutMillis} ms) exceeds the metrics export interval (${intervalMillis} ms); clamping the export timeout to ${intervalMillis} ms.`);
|
|
88
|
+
return intervalMillis;
|
|
89
|
+
}
|
|
38
90
|
function normalizeLogLevel(value) {
|
|
39
91
|
if (value === "trace" ||
|
|
40
92
|
value === "debug" ||
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
|
|
2
|
+
import type { ConfigOTelInterface } from "../models/ConfigOTelInterface";
|
|
3
|
+
export interface CollectorExporterOptions {
|
|
4
|
+
url: string;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
timeoutMillis: number;
|
|
7
|
+
compression: CompressionAlgorithm;
|
|
8
|
+
}
|
|
9
|
+
export declare function buildCollectorExporterOptions(url: string, config: ConfigOTelInterface, variableName: string): CollectorExporterOptions;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildCollectorExporterOptions = buildCollectorExporterOptions;
|
|
4
|
+
const otlp_exporter_base_1 = require("@opentelemetry/otlp-exporter-base");
|
|
5
|
+
const configValidation_1 = require("./configValidation");
|
|
6
|
+
function buildCollectorExporterOptions(url, config, variableName) {
|
|
7
|
+
(0, configValidation_1.validateCollectorUrl)(url, variableName);
|
|
8
|
+
const authorization = config.OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER;
|
|
9
|
+
if (authorization && new URL(url).protocol === "http:") {
|
|
10
|
+
console.warn(`[otel-utils] ${variableName} uses plain HTTP; the bearer token from OPENTELEMETRY_COLLECT_AUTHORIZATION_HEADER is sent unencrypted. Prefer an https:// endpoint.`);
|
|
11
|
+
}
|
|
12
|
+
const headers = (0, configValidation_1.normalizeHeaders)(config.OPENTELEMETRY_COLLECTOR_HTTP_HEADERS);
|
|
13
|
+
if (authorization) {
|
|
14
|
+
for (const name of Object.keys(headers)) {
|
|
15
|
+
if (name.toLowerCase() === "authorization") {
|
|
16
|
+
delete headers[name];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
headers["Authorization"] = `Bearer ${authorization}`;
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
url,
|
|
23
|
+
headers,
|
|
24
|
+
timeoutMillis: (0, configValidation_1.normalizePositiveInteger)(config.OPENTELEMETRY_COLLECTOR_HTTP_TIMEOUT_MILLIS, 10000),
|
|
25
|
+
compression: (0, configValidation_1.normalizeCompression)(config.OPENTELEMETRY_COLLECTOR_HTTP_COMPRESSION) ===
|
|
26
|
+
"gzip"
|
|
27
|
+
? otlp_exporter_base_1.CompressionAlgorithm.GZIP
|
|
28
|
+
: otlp_exporter_base_1.CompressionAlgorithm.NONE,
|
|
29
|
+
};
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devopsplaybook.io/otel-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0-beta.44.7e03cb8",
|
|
4
4
|
"description": "Utility to simplify integration with Open Telemetry",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Open",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"@opentelemetry/exporter-metrics-otlp-http": "^0.300.0",
|
|
42
42
|
"@opentelemetry/exporter-trace-otlp-http": "^0.300.0",
|
|
43
43
|
"@opentelemetry/id-generator-aws-xray": "^3.0.0",
|
|
44
|
+
"@opentelemetry/otlp-exporter-base": "^0.300.0",
|
|
44
45
|
"@opentelemetry/resources": "^3.0.0",
|
|
45
46
|
"@opentelemetry/sdk-logs": "^3.0.0",
|
|
46
47
|
"@opentelemetry/sdk-metrics": "^3.0.0",
|