@mark1russell7/otel-ts 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/context-processors.d.ts +16 -6
- package/dist/context-processors.d.ts.map +1 -1
- package/dist/context-processors.js +22 -6
- package/dist/context-processors.js.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +13 -0
- package/dist/init.js.map +1 -1
- package/dist/logs.d.ts.map +1 -1
- package/dist/logs.js +3 -1
- package/dist/logs.js.map +1 -1
- package/dist/metrics.d.ts.map +1 -1
- package/dist/metrics.js +5 -2
- package/dist/metrics.js.map +1 -1
- package/dist/node-exporters.d.ts +7 -0
- package/dist/node-exporters.d.ts.map +1 -1
- package/dist/node-exporters.js +20 -4
- package/dist/node-exporters.js.map +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +6 -4
- package/dist/node.js.map +1 -1
- package/dist/tracing.d.ts.map +1 -1
- package/dist/tracing.js +7 -1
- package/dist/tracing.js.map +1 -1
- package/package.json +21 -20
- package/src/context-processors.ts +29 -7
- package/src/init.ts +13 -0
- package/src/logs.ts +3 -1
- package/src/metrics.ts +5 -2
- package/src/node-exporters.ts +20 -4
- package/src/node.ts +6 -4
- package/src/tracing.ts +7 -1
package/src/logs.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
createSessionLogRecordProcessor,
|
|
10
10
|
type SessionIdReader,
|
|
11
11
|
} from "./session-processors.js";
|
|
12
|
+
import { createContextLogRecordProcessor } from "./context-processors.js";
|
|
12
13
|
|
|
13
14
|
export function setupLogs(
|
|
14
15
|
resource: Resource,
|
|
@@ -24,7 +25,8 @@ export function setupLogs(
|
|
|
24
25
|
processors: [
|
|
25
26
|
// Before the exporting processor, so every exported record has it
|
|
26
27
|
createSessionLogRecordProcessor(getSessionId),
|
|
27
|
-
|
|
28
|
+
createContextLogRecordProcessor(),
|
|
29
|
+
new BatchLogRecordProcessor({ exporter }),
|
|
28
30
|
],
|
|
29
31
|
});
|
|
30
32
|
}
|
package/src/metrics.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { contextMetricViews } from "./context-processors.js";
|
|
1
2
|
import type { Resource } from "@opentelemetry/resources";
|
|
2
3
|
import {
|
|
3
4
|
AggregationType,
|
|
@@ -26,8 +27,8 @@ const TEMPORALITY_PREFERENCES: Record<
|
|
|
26
27
|
* instrument keeps its default aggregation. Used as the exporter's
|
|
27
28
|
* aggregation preference — the SDK's equivalent of
|
|
28
29
|
* OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION=base2_exponential_bucket_histogram —
|
|
29
|
-
*
|
|
30
|
-
*
|
|
30
|
+
* The context views (contextMetricViews) replace the preference of the
|
|
31
|
+
* exporter, so they take the same selector.
|
|
31
32
|
*/
|
|
32
33
|
const selectExponentialHistograms: AggregationSelector = (
|
|
33
34
|
instrumentType,
|
|
@@ -53,6 +54,8 @@ export function setupMetrics(
|
|
|
53
54
|
|
|
54
55
|
return new MeterProvider({
|
|
55
56
|
resource,
|
|
57
|
+
// Removes each context key of high cardinality from each metric
|
|
58
|
+
views: contextMetricViews(options.aggregationPreference),
|
|
56
59
|
readers: [
|
|
57
60
|
new PeriodicExportingMetricReader({
|
|
58
61
|
exporter,
|
package/src/node-exporters.ts
CHANGED
|
@@ -17,8 +17,9 @@ import {
|
|
|
17
17
|
type LogRecordExporter,
|
|
18
18
|
type LogRecordProcessor,
|
|
19
19
|
} from "@opentelemetry/sdk-logs";
|
|
20
|
+
import { AggregationType, InstrumentType, type AggregationSelector } from "@opentelemetry/sdk-metrics";
|
|
20
21
|
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor, type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
21
|
-
import { createContextLogRecordProcessor, createContextSpanProcessor } from "./context-processors.js";
|
|
22
|
+
import { createContextLogRecordProcessor, createContextSpanProcessor, defaultAggregation } from "./context-processors.js";
|
|
22
23
|
|
|
23
24
|
type Env = Readonly<Record<string, string | undefined>>;
|
|
24
25
|
|
|
@@ -55,6 +56,21 @@ export function otlpProtocol(signal: OtlpSignal, env: Env): OtlpProtocol {
|
|
|
55
56
|
throw new RangeError(`the OTLP protocol of ${signal} is "${value}": otel-ts sends "http/protobuf" or "http/json". For grpc, give the exporter in the options.`);
|
|
56
57
|
}
|
|
57
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The aggregation of `OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION`:
|
|
61
|
+
* `explicit_bucket_histogram` (default) or `base2_exponential_bucket_histogram`.
|
|
62
|
+
* Another value is an error.
|
|
63
|
+
*/
|
|
64
|
+
export function histogramAggregation(env: Env): AggregationSelector {
|
|
65
|
+
const variable = "OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION";
|
|
66
|
+
const value = (env[variable] ?? "").trim().toLowerCase();
|
|
67
|
+
if (value === "" || value === "explicit_bucket_histogram") return defaultAggregation;
|
|
68
|
+
if (value === "base2_exponential_bucket_histogram") {
|
|
69
|
+
return (type) => (type === InstrumentType.HISTOGRAM ? { type: AggregationType.EXPONENTIAL_HISTOGRAM } : { type: AggregationType.DEFAULT });
|
|
70
|
+
}
|
|
71
|
+
throw new RangeError(`${variable} has "${value}": otel-ts makes "explicit_bucket_histogram" or "base2_exponential_bucket_histogram"`);
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
/**
|
|
59
75
|
* The span processors: the context processor, then a processor for `exporter`,
|
|
60
76
|
* or for each exporter of the variables. No exporter gives no processor.
|
|
@@ -78,11 +94,11 @@ export function nodeSpanProcessors(env: Env, exporter?: SpanExporter): SpanProce
|
|
|
78
94
|
*/
|
|
79
95
|
export function nodeLogRecordProcessors(env: Env, exporter?: LogRecordExporter): LogRecordProcessor[] {
|
|
80
96
|
const exporting: LogRecordProcessor[] = exporter
|
|
81
|
-
? [new BatchLogRecordProcessor(exporter)]
|
|
97
|
+
? [new BatchLogRecordProcessor({ exporter })]
|
|
82
98
|
: exporterNames("logs", env).map((name) =>
|
|
83
99
|
name === "console"
|
|
84
|
-
? new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())
|
|
85
|
-
: new BatchLogRecordProcessor(otlpProtocol("logs", env) === "http/json" ? new JsonLogExporter() : new ProtobufLogExporter()),
|
|
100
|
+
? new SimpleLogRecordProcessor({ exporter: new ConsoleLogRecordExporter() })
|
|
101
|
+
: new BatchLogRecordProcessor({ exporter: otlpProtocol("logs", env) === "http/json" ? new JsonLogExporter() : new ProtobufLogExporter() }),
|
|
86
102
|
);
|
|
87
103
|
return exporting.length > 0 ? [createContextLogRecordProcessor(), ...exporting] : [];
|
|
88
104
|
}
|
package/src/node.ts
CHANGED
|
@@ -29,10 +29,10 @@ import { NodeSDK, type NodeSDKConfiguration } from "@opentelemetry/sdk-node";
|
|
|
29
29
|
import type { LogRecordExporter } from "@opentelemetry/sdk-logs";
|
|
30
30
|
import type { SpanExporter } from "@opentelemetry/sdk-trace-base";
|
|
31
31
|
import { ContextPropagator, claimContextRegistration } from "./context-propagator.js";
|
|
32
|
-
import {
|
|
33
|
-
import { nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
|
|
32
|
+
import { contextMetricViews } from "./context-processors.js";
|
|
33
|
+
import { histogramAggregation, nodeLogRecordProcessors, nodeSpanProcessors } from "./node-exporters.js";
|
|
34
34
|
|
|
35
|
-
export { exporterNames, otlpProtocol, type ExporterName, type OtlpProtocol, type OtlpSignal } from "./node-exporters.js";
|
|
35
|
+
export { exporterNames, histogramAggregation, otlpProtocol, type ExporterName, type OtlpProtocol, type OtlpSignal } from "./node-exporters.js";
|
|
36
36
|
|
|
37
37
|
export { outgoingTraceparent, createTraceEcho, runInRemoteTrace, type RemoteTraceOptions, type TraceEcho } from "./trace-echo.js";
|
|
38
38
|
|
|
@@ -121,7 +121,9 @@ export function startNodeTelemetry(options: NodeTelemetryOptions = {}): NodeTele
|
|
|
121
121
|
sdk = new NodeSDK({
|
|
122
122
|
serviceName: options.serviceName ?? env["OTEL_SERVICE_NAME"] ?? "node-service",
|
|
123
123
|
textMapPropagator: new ContextPropagator(),
|
|
124
|
-
|
|
124
|
+
// The aggregation of OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION:
|
|
125
|
+
// the views replace the preference of each reader
|
|
126
|
+
views: contextMetricViews(histogramAggregation(env)),
|
|
125
127
|
...exports,
|
|
126
128
|
...(options.instrumentations ? { instrumentations: options.instrumentations } : {}),
|
|
127
129
|
});
|
package/src/tracing.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { LongTaskInstrumentation } from "@opentelemetry/instrumentation-long-tas
|
|
|
8
8
|
import { registerInstrumentations } from "@opentelemetry/instrumentation";
|
|
9
9
|
import type { ResolvedConfig } from "./types.js";
|
|
10
10
|
import { hasDom } from "./env.js";
|
|
11
|
+
import { createContextSpanProcessor } from "./context-processors.js";
|
|
12
|
+
import { ContextPropagator } from "./context-propagator.js";
|
|
11
13
|
import {
|
|
12
14
|
createSessionSpanProcessor,
|
|
13
15
|
type SessionIdReader,
|
|
@@ -30,11 +32,15 @@ export function setupTracing(
|
|
|
30
32
|
resource,
|
|
31
33
|
spanProcessors: [
|
|
32
34
|
createSessionSpanProcessor(getSessionId),
|
|
35
|
+
// The context keys (docs/CONTEXT-DESIGN.md), before the export too
|
|
36
|
+
createContextSpanProcessor(),
|
|
33
37
|
new BatchSpanProcessor(exporter),
|
|
34
38
|
],
|
|
35
39
|
});
|
|
36
40
|
|
|
37
|
-
|
|
41
|
+
// The propagator of otel-ts: `traceparent` on each call that the
|
|
42
|
+
// instrumentations permit, `baggage` only on an internal call
|
|
43
|
+
provider.register({ contextManager, propagator: new ContextPropagator() });
|
|
38
44
|
|
|
39
45
|
registerInstrumentations({
|
|
40
46
|
instrumentations: [
|