@watchtowerobservability/node 0.3.0 → 0.5.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 +5 -1
- package/dist/cjs/buffer.d.ts +2 -3
- package/dist/cjs/buffer.js +6 -6
- package/dist/cjs/iast.d.ts +27 -0
- package/dist/cjs/iast.js +138 -0
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.js +35 -19
- package/dist/cjs/load.d.ts +2 -0
- package/dist/cjs/load.js +18 -0
- package/dist/cjs/metrics.d.ts +1 -2
- package/dist/cjs/metrics.js +6 -6
- package/dist/esm/buffer.d.ts +2 -3
- package/dist/esm/buffer.js +6 -6
- package/dist/esm/iast.d.ts +27 -0
- package/dist/esm/iast.js +130 -0
- package/dist/esm/index.d.ts +5 -3
- package/dist/esm/index.js +34 -18
- package/dist/esm/load.d.ts +2 -0
- package/dist/esm/metrics.d.ts +1 -2
- package/dist/esm/metrics.js +5 -5
- package/package.json +64 -13
package/README.md
CHANGED
|
@@ -7,9 +7,13 @@
|
|
|
7
7
|
GitHub Packages needs a token with `read:packages` even for public packages:
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
npm install @watchtowerobservability/node
|
|
10
|
+
npm install @watchtowerobservability/node @opentelemetry/context-async-hooks @opentelemetry/core \
|
|
11
|
+
@opentelemetry/exporter-logs-otlp-http @opentelemetry/exporter-metrics-otlp-http @opentelemetry/exporter-trace-otlp-http \
|
|
12
|
+
@opentelemetry/otlp-transformer @opentelemetry/resources @opentelemetry/sdk-logs @opentelemetry/sdk-metrics @opentelemetry/sdk-trace
|
|
11
13
|
```
|
|
12
14
|
|
|
15
|
+
`@opentelemetry/api` is the only hard dependency; the OpenTelemetry SDK packages above are optional peers (so your app controls their versions) and are loaded by `init`, which throws an error naming the missing ones.
|
|
16
|
+
|
|
13
17
|
Or install the attested tarball from a release without a token:
|
|
14
18
|
`npm install https://github.com/watchtowerobservability/watchtower-node/releases/download/v0.2.0/watchtowerobservability-node-0.2.0.tgz`.
|
|
15
19
|
|
package/dist/cjs/buffer.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace
|
|
1
|
+
import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace';
|
|
2
2
|
import type { LogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs';
|
|
3
|
-
import { AggregationTemporality } from '@opentelemetry/sdk-metrics';
|
|
4
|
-
import type { InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
|
|
3
|
+
import type { AggregationTemporality, InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
|
|
5
4
|
type Done = Parameters<SpanExporter['export']>[1];
|
|
6
5
|
/** What the spool wraps: a span, log record or metric exporter; `B` is one export batch. */
|
|
7
6
|
export interface BatchExporter<B> {
|
package/dist/cjs/buffer.js
CHANGED
|
@@ -40,14 +40,14 @@ exports.MetricSpool = exports.spoolLogs = exports.SpoolExporter = exports.encode
|
|
|
40
40
|
const fs = __importStar(require("node:fs"));
|
|
41
41
|
const path = __importStar(require("node:path"));
|
|
42
42
|
const crypto = __importStar(require("node:crypto"));
|
|
43
|
-
const
|
|
44
|
-
const
|
|
43
|
+
const load_js_1 = require("./load.js");
|
|
44
|
+
const ser = () => (0, load_js_1.need)('@opentelemetry/otlp-transformer');
|
|
45
45
|
/** OTLP/JSON encoders for the three signals. */
|
|
46
|
-
const encodeSpans = (spans) => Buffer.from(
|
|
46
|
+
const encodeSpans = (spans) => Buffer.from(ser().JsonTraceSerializer.serializeRequest(spans));
|
|
47
47
|
exports.encodeSpans = encodeSpans;
|
|
48
|
-
const encodeLogs = (logs) => Buffer.from(
|
|
48
|
+
const encodeLogs = (logs) => Buffer.from(ser().JsonLogsSerializer.serializeRequest(logs));
|
|
49
49
|
exports.encodeLogs = encodeLogs;
|
|
50
|
-
const encodeMetrics = (rm) => Buffer.from(
|
|
50
|
+
const encodeMetrics = (rm) => Buffer.from(ser().JsonMetricsSerializer.serializeRequest(rm));
|
|
51
51
|
exports.encodeMetrics = encodeMetrics;
|
|
52
52
|
class SpoolExporter {
|
|
53
53
|
inner;
|
|
@@ -125,7 +125,7 @@ class MetricSpool extends SpoolExporter {
|
|
|
125
125
|
}
|
|
126
126
|
selectAggregationTemporality(t) {
|
|
127
127
|
const pick = this.metricInner.selectAggregationTemporality;
|
|
128
|
-
return pick ? pick.call(this.metricInner, t) :
|
|
128
|
+
return pick ? pick.call(this.metricInner, t) : 1; // CUMULATIVE, the SDK default (enum value, so sdk-metrics is not imported eagerly)
|
|
129
129
|
}
|
|
130
130
|
forceFlush() {
|
|
131
131
|
return this.metricInner.forceFlush();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface IastOptions {
|
|
2
|
+
/** API base URL, e.g. `https://api.example.com`. */
|
|
3
|
+
apiUrl: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
project: string;
|
|
6
|
+
service: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
/** Flush delay in ms (default 2000). */
|
|
9
|
+
flushMs?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface TaintFinding {
|
|
12
|
+
vuln_type: string;
|
|
13
|
+
trace_id: string;
|
|
14
|
+
code_location: string;
|
|
15
|
+
severity: string;
|
|
16
|
+
title: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function startIast(o: IastOptions): void;
|
|
19
|
+
export declare function stopIast(): void;
|
|
20
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
21
|
+
export declare function iastMiddleware(): (req: any, _res: unknown, next: (e?: unknown) => void) => void;
|
|
22
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
23
|
+
export declare function checkSink(vulnType: string, text: unknown, severity?: string): boolean;
|
|
24
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
25
|
+
export declare function guardSink<A extends unknown[], R>(fn: (...a: A) => R, vulnType: string): (...a: A) => R;
|
|
26
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
27
|
+
export declare function flushIast(): Promise<void>;
|
package/dist/cjs/iast.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startIast = startIast;
|
|
4
|
+
exports.stopIast = stopIast;
|
|
5
|
+
exports.iastMiddleware = iastMiddleware;
|
|
6
|
+
exports.checkSink = checkSink;
|
|
7
|
+
exports.guardSink = guardSink;
|
|
8
|
+
exports.flushIast = flushIast;
|
|
9
|
+
// IAST (opt-in): notices at runtime when request-derived input reaches a sensitive sink and reports
|
|
10
|
+
// it to `POST /v1/projects/{prj}/code-security/runtime` (`{service, findings:[{vuln_type, trace_id,
|
|
11
|
+
// code_location, severity, title}]}`). Off until `startIast` is called; until then `iastMiddleware`
|
|
12
|
+
// is a pass-through and `guardSink` calls the original function untouched.
|
|
13
|
+
//
|
|
14
|
+
// How: `iastMiddleware()` stores the request's input strings (query, params, body, cookies, header
|
|
15
|
+
// values) in an AsyncLocalStorage for the life of the request. `guardSink(fn, vulnType)` wraps a
|
|
16
|
+
// sink (`pool.query`, `child_process.exec`, `fs.readFile`, ...); before the call it checks whether
|
|
17
|
+
// any input string appears inside the sink's first argument. Parameterised queries keep the input
|
|
18
|
+
// in the values array, not the text, so they never match. A hit is queued with the `file:line` of
|
|
19
|
+
// the caller (first stack frame outside node_modules and this file) and the active trace id, and
|
|
20
|
+
// is flushed in the background. Each (type, location) is reported once per process.
|
|
21
|
+
// shortcut: taint is substring matching of raw input values (>= 4 chars) in the sink text, not
|
|
22
|
+
// byte-level propagation; input that is transformed (encoded, split) before use is missed.
|
|
23
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
24
|
+
const api_1 = require("@opentelemetry/api");
|
|
25
|
+
const MIN_LEN = 4;
|
|
26
|
+
const MAX_INPUTS = 256;
|
|
27
|
+
const store = new node_async_hooks_1.AsyncLocalStorage();
|
|
28
|
+
let opts = null;
|
|
29
|
+
let queue = [];
|
|
30
|
+
let seen = new Set();
|
|
31
|
+
let timer = null;
|
|
32
|
+
function startIast(o) {
|
|
33
|
+
opts = o;
|
|
34
|
+
}
|
|
35
|
+
function stopIast() {
|
|
36
|
+
opts = null;
|
|
37
|
+
queue = [];
|
|
38
|
+
seen = new Set();
|
|
39
|
+
if (timer)
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
timer = null;
|
|
42
|
+
}
|
|
43
|
+
function collect(v, out, depth = 0) {
|
|
44
|
+
if (out.length >= MAX_INPUTS || depth > 4 || v == null)
|
|
45
|
+
return;
|
|
46
|
+
if (typeof v === 'string') {
|
|
47
|
+
if (v.length >= MIN_LEN)
|
|
48
|
+
out.push(v);
|
|
49
|
+
}
|
|
50
|
+
else if (Array.isArray(v)) {
|
|
51
|
+
for (const x of v)
|
|
52
|
+
collect(x, out, depth + 1);
|
|
53
|
+
}
|
|
54
|
+
else if (typeof v === 'object') {
|
|
55
|
+
for (const x of Object.values(v))
|
|
56
|
+
collect(x, out, depth + 1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
60
|
+
function iastMiddleware() {
|
|
61
|
+
return (req, _res, next) => {
|
|
62
|
+
if (!opts)
|
|
63
|
+
return next();
|
|
64
|
+
// Resolved at sink time: body parsers and routers run after this middleware.
|
|
65
|
+
const inputsFor = () => {
|
|
66
|
+
const inputs = [];
|
|
67
|
+
for (const k of ['query', 'params', 'body', 'cookies', 'headers'])
|
|
68
|
+
collect(req[k], inputs);
|
|
69
|
+
return inputs;
|
|
70
|
+
};
|
|
71
|
+
store.run(inputsFor, next);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function callerLocation() {
|
|
75
|
+
const frames = (new Error().stack || '').split('\n').slice(2);
|
|
76
|
+
for (const f of frames) {
|
|
77
|
+
if (f.includes('node_modules') || f.includes('node:internal') || /iast\.[jt]s/.test(f))
|
|
78
|
+
continue;
|
|
79
|
+
const m = /\(?([^()\s]+:\d+)(?::\d+)?\)?\s*$/.exec(f);
|
|
80
|
+
if (m)
|
|
81
|
+
return m[1].replace(/^file:\/\//, '');
|
|
82
|
+
}
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
86
|
+
function checkSink(vulnType, text, severity = 'high') {
|
|
87
|
+
const inputsFor = store.getStore();
|
|
88
|
+
if (!opts || !inputsFor || typeof text !== 'string')
|
|
89
|
+
return false;
|
|
90
|
+
if (!inputsFor().some((v) => text.includes(v)))
|
|
91
|
+
return false;
|
|
92
|
+
const loc = callerLocation();
|
|
93
|
+
const key = `${vulnType}|${loc}`;
|
|
94
|
+
if (seen.has(key))
|
|
95
|
+
return true;
|
|
96
|
+
seen.add(key);
|
|
97
|
+
queue.push({
|
|
98
|
+
vuln_type: vulnType,
|
|
99
|
+
trace_id: api_1.trace.getActiveSpan()?.spanContext().traceId || '',
|
|
100
|
+
code_location: loc,
|
|
101
|
+
severity,
|
|
102
|
+
title: `Request input reaches ${vulnType.replace(/_/g, ' ')} sink`,
|
|
103
|
+
});
|
|
104
|
+
if (!timer) {
|
|
105
|
+
timer = setTimeout(() => void flushIast(), opts.flushMs ?? 2000);
|
|
106
|
+
timer.unref?.();
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
111
|
+
function guardSink(fn, vulnType) {
|
|
112
|
+
return function (...a) {
|
|
113
|
+
const first = a[0];
|
|
114
|
+
checkSink(vulnType, typeof first === 'string' ? first : first?.text);
|
|
115
|
+
return fn.apply(this, a);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
119
|
+
async function flushIast() {
|
|
120
|
+
if (timer)
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
timer = null;
|
|
123
|
+
const o = opts;
|
|
124
|
+
const findings = queue;
|
|
125
|
+
queue = [];
|
|
126
|
+
if (!o || findings.length === 0)
|
|
127
|
+
return;
|
|
128
|
+
try {
|
|
129
|
+
await (o.fetch || globalThis.fetch)(`${o.apiUrl.replace(/\/$/, '')}/v1/projects/${encodeURIComponent(o.project)}/code-security/runtime`, {
|
|
130
|
+
method: 'POST',
|
|
131
|
+
headers: { Authorization: `Bearer ${o.apiKey}`, 'Content-Type': 'application/json' },
|
|
132
|
+
body: JSON.stringify({ service: o.service, findings }),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (_e) {
|
|
136
|
+
/* best effort */
|
|
137
|
+
}
|
|
138
|
+
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
2
|
import { metrics, trace } from '@opentelemetry/api';
|
|
3
3
|
import type { Tracer } from '@opentelemetry/api';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import type { Resource } from '@opentelemetry/resources';
|
|
5
|
+
import type { TracerProvider } from '@opentelemetry/sdk-trace';
|
|
6
|
+
import type { LoggerProvider } from '@opentelemetry/sdk-logs';
|
|
6
7
|
import type { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
7
8
|
import { captureException } from './errors.js';
|
|
8
9
|
import type { CaptureContext } from './errors.js';
|
|
@@ -79,7 +80,8 @@ export declare class Client {
|
|
|
79
80
|
};
|
|
80
81
|
readonly fetch: typeof fetch;
|
|
81
82
|
readonly redact: InitOptions['redact'];
|
|
82
|
-
readonly provider:
|
|
83
|
+
readonly provider: TracerProvider;
|
|
84
|
+
readonly resource: Resource;
|
|
83
85
|
readonly tracer: Tracer;
|
|
84
86
|
readonly loggerProvider: LoggerProvider;
|
|
85
87
|
readonly logger: ReturnType<LoggerProvider['getLogger']>;
|
package/dist/cjs/index.js
CHANGED
|
@@ -42,12 +42,7 @@ const node_util_1 = require("node:util");
|
|
|
42
42
|
const api_1 = require("@opentelemetry/api");
|
|
43
43
|
Object.defineProperty(exports, "metrics", { enumerable: true, get: function () { return api_1.metrics; } });
|
|
44
44
|
Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return api_1.trace; } });
|
|
45
|
-
const
|
|
46
|
-
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
47
|
-
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
48
|
-
const sdk_logs_1 = require("@opentelemetry/sdk-logs");
|
|
49
|
-
const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
|
|
50
|
-
const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
|
|
45
|
+
const load_js_1 = require("./load.js");
|
|
51
46
|
const buffer_js_1 = require("./buffer.js");
|
|
52
47
|
const metrics_js_1 = require("./metrics.js");
|
|
53
48
|
const nodePath = __importStar(require("node:path"));
|
|
@@ -73,6 +68,7 @@ class Client {
|
|
|
73
68
|
fetch;
|
|
74
69
|
redact;
|
|
75
70
|
provider;
|
|
71
|
+
resource;
|
|
76
72
|
tracer;
|
|
77
73
|
loggerProvider;
|
|
78
74
|
logger;
|
|
@@ -95,38 +91,48 @@ class Client {
|
|
|
95
91
|
this.fetch = o.fetch || globalThis.fetch;
|
|
96
92
|
const environment = o.environment || env.WT_ENVIRONMENT;
|
|
97
93
|
const release = o.release || env.WT_RELEASE;
|
|
98
|
-
|
|
94
|
+
// the OTel SDK packages are optional peers, loaded here (not at import time) with a clear error when missing
|
|
95
|
+
const { resourceFromAttributes } = (0, load_js_1.need)('@opentelemetry/resources');
|
|
96
|
+
const resource = resourceFromAttributes({
|
|
99
97
|
'service.name': o.serviceName || env.WT_SERVICE || 'app',
|
|
100
98
|
...(environment ? { 'deployment.environment.name': environment } : {}),
|
|
101
99
|
...(release ? { 'service.version': release } : {}),
|
|
102
100
|
...safeResource(o.resource),
|
|
103
101
|
});
|
|
102
|
+
this.resource = resource;
|
|
104
103
|
this.redact = o.redact;
|
|
105
|
-
this.provider = new sdk_trace_node_1.NodeTracerProvider({ resource });
|
|
106
|
-
if (o.redact)
|
|
107
|
-
this.provider.addSpanProcessor(redactor(o.redact));
|
|
108
104
|
let exporter = o.exporter;
|
|
109
105
|
if (!exporter) {
|
|
110
106
|
const url = `${this.endpoint}/v1/traces`;
|
|
111
|
-
|
|
107
|
+
const { OTLPTraceExporter } = (0, load_js_1.need)('@opentelemetry/exporter-trace-otlp-http');
|
|
108
|
+
exporter = new OTLPTraceExporter({ url, headers: this.headers });
|
|
112
109
|
// spans and log records spool to their own subdirectories
|
|
113
110
|
if (o.bufferDir)
|
|
114
111
|
exporter = new buffer_js_1.SpoolExporter(exporter, nodePath.join(o.bufferDir, 'traces'), url, this.headers, this.fetch);
|
|
115
112
|
}
|
|
116
|
-
|
|
113
|
+
// the redactor runs first, so the batch processor exports redacted attributes
|
|
114
|
+
const { BatchSpanProcessor, TracerProvider: TP } = (0, load_js_1.need)('@opentelemetry/sdk-trace');
|
|
115
|
+
this.provider = new TP({
|
|
116
|
+
resource,
|
|
117
|
+
spanProcessors: [...(o.redact ? [redactor(o.redact)] : []), new BatchSpanProcessor({ exporter })],
|
|
118
|
+
});
|
|
117
119
|
this.tracer = this.provider.getTracer('watchtower');
|
|
118
|
-
this.loggerProvider = new sdk_logs_1.LoggerProvider({ resource });
|
|
119
120
|
const logsOn = (0, metrics_js_1.switchOn)('WATCHTOWER_LOGS', 'OTEL_LOGS_EXPORTER', o.logs);
|
|
120
121
|
let logExporter = logsOn ? o.logExporter : undefined;
|
|
121
122
|
if (logsOn && !logExporter && !o.exporter) {
|
|
122
123
|
const url = `${this.endpoint}/v1/logs`;
|
|
123
|
-
|
|
124
|
+
const { OTLPLogExporter } = (0, load_js_1.need)('@opentelemetry/exporter-logs-otlp-http');
|
|
125
|
+
logExporter = new OTLPLogExporter({ url, headers: this.headers });
|
|
124
126
|
if (o.bufferDir)
|
|
125
127
|
logExporter = (0, buffer_js_1.spoolLogs)(logExporter, nodePath.join(o.bufferDir, 'logs'), url, this.headers, this.fetch);
|
|
126
128
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
const { LoggerProvider: LP, SimpleLogRecordProcessor, BatchLogRecordProcessor } = (0, load_js_1.need)('@opentelemetry/sdk-logs');
|
|
130
|
+
this.loggerProvider = new LP({
|
|
131
|
+
resource,
|
|
132
|
+
processors: !logExporter ? [] : [o.logExporter
|
|
133
|
+
? new SimpleLogRecordProcessor({ exporter: logExporter })
|
|
134
|
+
: new BatchLogRecordProcessor({ exporter: logExporter })],
|
|
135
|
+
});
|
|
130
136
|
this.logger = this.loggerProvider.getLogger('watchtower');
|
|
131
137
|
// with a test span exporter only an explicit metric exporter is used, as for logs
|
|
132
138
|
if ((0, metrics_js_1.switchOn)('WATCHTOWER_METRICS', 'OTEL_METRICS_EXPORTER', o.metrics) && (o.metricExporter || !o.exporter)) {
|
|
@@ -273,13 +279,23 @@ function init(opts) {
|
|
|
273
279
|
void current?.profiler?.then((p) => p?.stop());
|
|
274
280
|
const c = new Client(opts);
|
|
275
281
|
current = c;
|
|
276
|
-
|
|
282
|
+
// Global tracer + async context so trace.getActiveSpan() works; a re-init replaces the previous client's.
|
|
283
|
+
api_1.trace.disable();
|
|
284
|
+
api_1.context.disable();
|
|
285
|
+
api_1.propagation.disable();
|
|
286
|
+
const { AsyncLocalStorageContextManager } = (0, load_js_1.need)('@opentelemetry/context-async-hooks');
|
|
287
|
+
const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = (0, load_js_1.need)('@opentelemetry/core');
|
|
288
|
+
api_1.context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
|
|
289
|
+
api_1.propagation.setGlobalPropagator(new CompositePropagator({
|
|
290
|
+
propagators: [new W3CTraceContextPropagator(), new W3CBaggagePropagator()],
|
|
291
|
+
}));
|
|
292
|
+
api_1.trace.setGlobalTracerProvider(c.provider);
|
|
277
293
|
if (c.meterProvider) {
|
|
278
294
|
api_1.metrics.disable(); // a re-init replaces the previous client's provider
|
|
279
295
|
api_1.metrics.setGlobalMeterProvider(c.meterProvider);
|
|
280
296
|
}
|
|
281
297
|
if (process.env.WT_PROFILING === '1') {
|
|
282
|
-
const attrs = c.
|
|
298
|
+
const attrs = c.resource.attributes;
|
|
283
299
|
c.profiler = (0, profiling_js_1.startProfiling)({
|
|
284
300
|
apiKey: c.headers.Authorization.slice('Bearer '.length),
|
|
285
301
|
endpoint: c.endpoint,
|
package/dist/cjs/load.d.ts
CHANGED
package/dist/cjs/load.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.load = load;
|
|
4
|
+
exports.need = need;
|
|
4
5
|
// Optional-peer loader (winston-transport). This is the CommonJS form; scripts/build.mjs swaps in an ESM twin
|
|
5
6
|
// (createRequire(import.meta.url)) for dist/esm. The cwd fallback only matters when neither exists (test runners).
|
|
6
7
|
const node_module_1 = require("node:module");
|
|
@@ -9,3 +10,20 @@ function load(id) {
|
|
|
9
10
|
return require(id);
|
|
10
11
|
return (0, node_module_1.createRequire)(`${process.cwd()}/`)(id);
|
|
11
12
|
}
|
|
13
|
+
const SDK_PEERS = [
|
|
14
|
+
'@opentelemetry/context-async-hooks', '@opentelemetry/core', '@opentelemetry/exporter-logs-otlp-http',
|
|
15
|
+
'@opentelemetry/exporter-metrics-otlp-http', '@opentelemetry/exporter-trace-otlp-http', '@opentelemetry/otlp-transformer',
|
|
16
|
+
'@opentelemetry/resources', '@opentelemetry/sdk-logs', '@opentelemetry/sdk-metrics', '@opentelemetry/sdk-trace',
|
|
17
|
+
];
|
|
18
|
+
/** Load an OpenTelemetry SDK package (an optional peer); a missing one throws an error naming what to install. */
|
|
19
|
+
function need(id) {
|
|
20
|
+
try {
|
|
21
|
+
return load(id);
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
if (e?.code === 'MODULE_NOT_FOUND' && String(e.message).includes(id)) {
|
|
25
|
+
throw new Error(`@watchtowerobservability/node needs the OpenTelemetry SDK, which is not installed (missing ${id}). Run: npm install ${SDK_PEERS.join(' ')}`);
|
|
26
|
+
}
|
|
27
|
+
throw e;
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/cjs/metrics.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Resource } from '@opentelemetry/resources';
|
|
2
|
-
import { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
3
|
-
import type { PushMetricExporter } from '@opentelemetry/sdk-metrics';
|
|
2
|
+
import type { MeterProvider, PushMetricExporter } from '@opentelemetry/sdk-metrics';
|
|
4
3
|
/** Explicit flag, else `WATCHTOWER_LOGS` / `WATCHTOWER_METRICS` (default on); `OTEL_*_EXPORTER=none` turns it off. */
|
|
5
4
|
export declare function switchOn(name: string, otelExporterEnv: string, flag?: boolean): boolean;
|
|
6
5
|
export interface Metrics {
|
package/dist/cjs/metrics.js
CHANGED
|
@@ -40,8 +40,6 @@ exports.startMetrics = startMetrics;
|
|
|
40
40
|
// metrics (event loop delay and utilization, GC, heap) come from the optional peer
|
|
41
41
|
// @opentelemetry/instrumentation-runtime-node when it is installed.
|
|
42
42
|
const nodePath = __importStar(require("node:path"));
|
|
43
|
-
const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
|
|
44
|
-
const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
|
|
45
43
|
const buffer_js_1 = require("./buffer.js");
|
|
46
44
|
const load_js_1 = require("./load.js");
|
|
47
45
|
/** Explicit flag, else `WATCHTOWER_LOGS` / `WATCHTOWER_METRICS` (default on); `OTEL_*_EXPORTER=none` turns it off. */
|
|
@@ -54,20 +52,22 @@ function switchOn(name, otelExporterEnv, flag) {
|
|
|
54
52
|
return !['off', 'false', '0', 'no'].includes((env[name] || 'on').trim().toLowerCase());
|
|
55
53
|
}
|
|
56
54
|
function startMetrics(endpoint, headers, resource, bufferDir, fetch, exporter) {
|
|
55
|
+
const sdk = (0, load_js_1.need)('@opentelemetry/sdk-metrics');
|
|
57
56
|
const url = `${endpoint}/v1/metrics`;
|
|
58
57
|
let exp = exporter;
|
|
59
58
|
if (!exp) {
|
|
59
|
+
const { AggregationTemporalityPreference, OTLPMetricExporter } = (0, load_js_1.need)('@opentelemetry/exporter-metrics-otlp-http');
|
|
60
60
|
const delta = process.env.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
|
|
61
|
-
? {} : { temporalityPreference:
|
|
62
|
-
exp = new
|
|
61
|
+
? {} : { temporalityPreference: AggregationTemporalityPreference.DELTA };
|
|
62
|
+
exp = new OTLPMetricExporter({ url, headers, ...delta });
|
|
63
63
|
if (bufferDir)
|
|
64
64
|
exp = new buffer_js_1.MetricSpool(exp, nodePath.join(bufferDir, 'metrics'), url, headers, fetch);
|
|
65
65
|
}
|
|
66
66
|
const interval = Number(process.env.OTEL_METRIC_EXPORT_INTERVAL) || 60000;
|
|
67
|
-
const reader = new
|
|
67
|
+
const reader = new sdk.PeriodicExportingMetricReader({
|
|
68
68
|
exporter: exp, exportIntervalMillis: interval, exportTimeoutMillis: Math.min(30000, interval),
|
|
69
69
|
});
|
|
70
|
-
const provider = new
|
|
70
|
+
const provider = new sdk.MeterProvider({ resource, readers: [reader] });
|
|
71
71
|
let runtime;
|
|
72
72
|
try {
|
|
73
73
|
const mod = (0, load_js_1.load)('@opentelemetry/instrumentation-runtime-node');
|
package/dist/esm/buffer.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace
|
|
1
|
+
import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace';
|
|
2
2
|
import type { LogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs';
|
|
3
|
-
import { AggregationTemporality } from '@opentelemetry/sdk-metrics';
|
|
4
|
-
import type { InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
|
|
3
|
+
import type { AggregationTemporality, InstrumentType, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics';
|
|
5
4
|
type Done = Parameters<SpanExporter['export']>[1];
|
|
6
5
|
/** What the spool wraps: a span, log record or metric exporter; `B` is one export batch. */
|
|
7
6
|
export interface BatchExporter<B> {
|
package/dist/esm/buffer.js
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
import * as fs from 'node:fs';
|
|
5
5
|
import * as path from 'node:path';
|
|
6
6
|
import * as crypto from 'node:crypto';
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import { need } from './load.js';
|
|
8
|
+
const ser = () => need('@opentelemetry/otlp-transformer');
|
|
9
9
|
/** OTLP/JSON encoders for the three signals. */
|
|
10
|
-
export const encodeSpans = (spans) => Buffer.from(JsonTraceSerializer.serializeRequest(spans));
|
|
11
|
-
export const encodeLogs = (logs) => Buffer.from(JsonLogsSerializer.serializeRequest(logs));
|
|
12
|
-
export const encodeMetrics = (rm) => Buffer.from(JsonMetricsSerializer.serializeRequest(rm));
|
|
10
|
+
export const encodeSpans = (spans) => Buffer.from(ser().JsonTraceSerializer.serializeRequest(spans));
|
|
11
|
+
export const encodeLogs = (logs) => Buffer.from(ser().JsonLogsSerializer.serializeRequest(logs));
|
|
12
|
+
export const encodeMetrics = (rm) => Buffer.from(ser().JsonMetricsSerializer.serializeRequest(rm));
|
|
13
13
|
export class SpoolExporter {
|
|
14
14
|
inner;
|
|
15
15
|
dir;
|
|
@@ -84,7 +84,7 @@ export class MetricSpool extends SpoolExporter {
|
|
|
84
84
|
}
|
|
85
85
|
selectAggregationTemporality(t) {
|
|
86
86
|
const pick = this.metricInner.selectAggregationTemporality;
|
|
87
|
-
return pick ? pick.call(this.metricInner, t) :
|
|
87
|
+
return pick ? pick.call(this.metricInner, t) : 1; // CUMULATIVE, the SDK default (enum value, so sdk-metrics is not imported eagerly)
|
|
88
88
|
}
|
|
89
89
|
forceFlush() {
|
|
90
90
|
return this.metricInner.forceFlush();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface IastOptions {
|
|
2
|
+
/** API base URL, e.g. `https://api.example.com`. */
|
|
3
|
+
apiUrl: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
project: string;
|
|
6
|
+
service: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
/** Flush delay in ms (default 2000). */
|
|
9
|
+
flushMs?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface TaintFinding {
|
|
12
|
+
vuln_type: string;
|
|
13
|
+
trace_id: string;
|
|
14
|
+
code_location: string;
|
|
15
|
+
severity: string;
|
|
16
|
+
title: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function startIast(o: IastOptions): void;
|
|
19
|
+
export declare function stopIast(): void;
|
|
20
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
21
|
+
export declare function iastMiddleware(): (req: any, _res: unknown, next: (e?: unknown) => void) => void;
|
|
22
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
23
|
+
export declare function checkSink(vulnType: string, text: unknown, severity?: string): boolean;
|
|
24
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
25
|
+
export declare function guardSink<A extends unknown[], R>(fn: (...a: A) => R, vulnType: string): (...a: A) => R;
|
|
26
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
27
|
+
export declare function flushIast(): Promise<void>;
|
package/dist/esm/iast.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// IAST (opt-in): notices at runtime when request-derived input reaches a sensitive sink and reports
|
|
2
|
+
// it to `POST /v1/projects/{prj}/code-security/runtime` (`{service, findings:[{vuln_type, trace_id,
|
|
3
|
+
// code_location, severity, title}]}`). Off until `startIast` is called; until then `iastMiddleware`
|
|
4
|
+
// is a pass-through and `guardSink` calls the original function untouched.
|
|
5
|
+
//
|
|
6
|
+
// How: `iastMiddleware()` stores the request's input strings (query, params, body, cookies, header
|
|
7
|
+
// values) in an AsyncLocalStorage for the life of the request. `guardSink(fn, vulnType)` wraps a
|
|
8
|
+
// sink (`pool.query`, `child_process.exec`, `fs.readFile`, ...); before the call it checks whether
|
|
9
|
+
// any input string appears inside the sink's first argument. Parameterised queries keep the input
|
|
10
|
+
// in the values array, not the text, so they never match. A hit is queued with the `file:line` of
|
|
11
|
+
// the caller (first stack frame outside node_modules and this file) and the active trace id, and
|
|
12
|
+
// is flushed in the background. Each (type, location) is reported once per process.
|
|
13
|
+
// shortcut: taint is substring matching of raw input values (>= 4 chars) in the sink text, not
|
|
14
|
+
// byte-level propagation; input that is transformed (encoded, split) before use is missed.
|
|
15
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
16
|
+
import { trace } from '@opentelemetry/api';
|
|
17
|
+
const MIN_LEN = 4;
|
|
18
|
+
const MAX_INPUTS = 256;
|
|
19
|
+
const store = new AsyncLocalStorage();
|
|
20
|
+
let opts = null;
|
|
21
|
+
let queue = [];
|
|
22
|
+
let seen = new Set();
|
|
23
|
+
let timer = null;
|
|
24
|
+
export function startIast(o) {
|
|
25
|
+
opts = o;
|
|
26
|
+
}
|
|
27
|
+
export function stopIast() {
|
|
28
|
+
opts = null;
|
|
29
|
+
queue = [];
|
|
30
|
+
seen = new Set();
|
|
31
|
+
if (timer)
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
timer = null;
|
|
34
|
+
}
|
|
35
|
+
function collect(v, out, depth = 0) {
|
|
36
|
+
if (out.length >= MAX_INPUTS || depth > 4 || v == null)
|
|
37
|
+
return;
|
|
38
|
+
if (typeof v === 'string') {
|
|
39
|
+
if (v.length >= MIN_LEN)
|
|
40
|
+
out.push(v);
|
|
41
|
+
}
|
|
42
|
+
else if (Array.isArray(v)) {
|
|
43
|
+
for (const x of v)
|
|
44
|
+
collect(x, out, depth + 1);
|
|
45
|
+
}
|
|
46
|
+
else if (typeof v === 'object') {
|
|
47
|
+
for (const x of Object.values(v))
|
|
48
|
+
collect(x, out, depth + 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
52
|
+
export function iastMiddleware() {
|
|
53
|
+
return (req, _res, next) => {
|
|
54
|
+
if (!opts)
|
|
55
|
+
return next();
|
|
56
|
+
// Resolved at sink time: body parsers and routers run after this middleware.
|
|
57
|
+
const inputsFor = () => {
|
|
58
|
+
const inputs = [];
|
|
59
|
+
for (const k of ['query', 'params', 'body', 'cookies', 'headers'])
|
|
60
|
+
collect(req[k], inputs);
|
|
61
|
+
return inputs;
|
|
62
|
+
};
|
|
63
|
+
store.run(inputsFor, next);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function callerLocation() {
|
|
67
|
+
const frames = (new Error().stack || '').split('\n').slice(2);
|
|
68
|
+
for (const f of frames) {
|
|
69
|
+
if (f.includes('node_modules') || f.includes('node:internal') || /iast\.[jt]s/.test(f))
|
|
70
|
+
continue;
|
|
71
|
+
const m = /\(?([^()\s]+:\d+)(?::\d+)?\)?\s*$/.exec(f);
|
|
72
|
+
if (m)
|
|
73
|
+
return m[1].replace(/^file:\/\//, '');
|
|
74
|
+
}
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
78
|
+
export function checkSink(vulnType, text, severity = 'high') {
|
|
79
|
+
const inputsFor = store.getStore();
|
|
80
|
+
if (!opts || !inputsFor || typeof text !== 'string')
|
|
81
|
+
return false;
|
|
82
|
+
if (!inputsFor().some((v) => text.includes(v)))
|
|
83
|
+
return false;
|
|
84
|
+
const loc = callerLocation();
|
|
85
|
+
const key = `${vulnType}|${loc}`;
|
|
86
|
+
if (seen.has(key))
|
|
87
|
+
return true;
|
|
88
|
+
seen.add(key);
|
|
89
|
+
queue.push({
|
|
90
|
+
vuln_type: vulnType,
|
|
91
|
+
trace_id: trace.getActiveSpan()?.spanContext().traceId || '',
|
|
92
|
+
code_location: loc,
|
|
93
|
+
severity,
|
|
94
|
+
title: `Request input reaches ${vulnType.replace(/_/g, ' ')} sink`,
|
|
95
|
+
});
|
|
96
|
+
if (!timer) {
|
|
97
|
+
timer = setTimeout(() => void flushIast(), opts.flushMs ?? 2000);
|
|
98
|
+
timer.unref?.();
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
103
|
+
export function guardSink(fn, vulnType) {
|
|
104
|
+
return function (...a) {
|
|
105
|
+
const first = a[0];
|
|
106
|
+
checkSink(vulnType, typeof first === 'string' ? first : first?.text);
|
|
107
|
+
return fn.apply(this, a);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
111
|
+
export async function flushIast() {
|
|
112
|
+
if (timer)
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
timer = null;
|
|
115
|
+
const o = opts;
|
|
116
|
+
const findings = queue;
|
|
117
|
+
queue = [];
|
|
118
|
+
if (!o || findings.length === 0)
|
|
119
|
+
return;
|
|
120
|
+
try {
|
|
121
|
+
await (o.fetch || globalThis.fetch)(`${o.apiUrl.replace(/\/$/, '')}/v1/projects/${encodeURIComponent(o.project)}/code-security/runtime`, {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: { Authorization: `Bearer ${o.apiKey}`, 'Content-Type': 'application/json' },
|
|
124
|
+
body: JSON.stringify({ service: o.service, findings }),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
catch (_e) {
|
|
128
|
+
/* best effort */
|
|
129
|
+
}
|
|
130
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
2
|
import { metrics, trace } from '@opentelemetry/api';
|
|
3
3
|
import type { Tracer } from '@opentelemetry/api';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import type { Resource } from '@opentelemetry/resources';
|
|
5
|
+
import type { TracerProvider } from '@opentelemetry/sdk-trace';
|
|
6
|
+
import type { LoggerProvider } from '@opentelemetry/sdk-logs';
|
|
6
7
|
import type { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
7
8
|
import { captureException } from './errors.js';
|
|
8
9
|
import type { CaptureContext } from './errors.js';
|
|
@@ -79,7 +80,8 @@ export declare class Client {
|
|
|
79
80
|
};
|
|
80
81
|
readonly fetch: typeof fetch;
|
|
81
82
|
readonly redact: InitOptions['redact'];
|
|
82
|
-
readonly provider:
|
|
83
|
+
readonly provider: TracerProvider;
|
|
84
|
+
readonly resource: Resource;
|
|
83
85
|
readonly tracer: Tracer;
|
|
84
86
|
readonly loggerProvider: LoggerProvider;
|
|
85
87
|
readonly logger: ReturnType<LoggerProvider['getLogger']>;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
// Thin Watchtower layer over the OpenTelemetry Node SDK: key header, endpoint default,
|
|
2
2
|
// offline buffer, wt.*-safe resource attributes, redaction hook, getPrompt/score/evaluate/observe.
|
|
3
3
|
import { inspect } from 'node:util';
|
|
4
|
-
import { metrics, trace } from '@opentelemetry/api';
|
|
5
|
-
import {
|
|
6
|
-
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
7
|
-
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
8
|
-
import { LoggerProvider, SimpleLogRecordProcessor, BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';
|
|
9
|
-
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
10
|
-
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
|
|
4
|
+
import { context, metrics, propagation, trace } from '@opentelemetry/api';
|
|
5
|
+
import { need as needPeer } from './load.js';
|
|
11
6
|
import { SpoolExporter, spoolLogs } from './buffer.js';
|
|
12
7
|
import { startMetrics, switchOn } from './metrics.js';
|
|
13
8
|
import * as nodePath from 'node:path';
|
|
@@ -31,6 +26,7 @@ export class Client {
|
|
|
31
26
|
fetch;
|
|
32
27
|
redact;
|
|
33
28
|
provider;
|
|
29
|
+
resource;
|
|
34
30
|
tracer;
|
|
35
31
|
loggerProvider;
|
|
36
32
|
logger;
|
|
@@ -53,38 +49,48 @@ export class Client {
|
|
|
53
49
|
this.fetch = o.fetch || globalThis.fetch;
|
|
54
50
|
const environment = o.environment || env.WT_ENVIRONMENT;
|
|
55
51
|
const release = o.release || env.WT_RELEASE;
|
|
56
|
-
|
|
52
|
+
// the OTel SDK packages are optional peers, loaded here (not at import time) with a clear error when missing
|
|
53
|
+
const { resourceFromAttributes } = needPeer('@opentelemetry/resources');
|
|
54
|
+
const resource = resourceFromAttributes({
|
|
57
55
|
'service.name': o.serviceName || env.WT_SERVICE || 'app',
|
|
58
56
|
...(environment ? { 'deployment.environment.name': environment } : {}),
|
|
59
57
|
...(release ? { 'service.version': release } : {}),
|
|
60
58
|
...safeResource(o.resource),
|
|
61
59
|
});
|
|
60
|
+
this.resource = resource;
|
|
62
61
|
this.redact = o.redact;
|
|
63
|
-
this.provider = new NodeTracerProvider({ resource });
|
|
64
|
-
if (o.redact)
|
|
65
|
-
this.provider.addSpanProcessor(redactor(o.redact));
|
|
66
62
|
let exporter = o.exporter;
|
|
67
63
|
if (!exporter) {
|
|
68
64
|
const url = `${this.endpoint}/v1/traces`;
|
|
65
|
+
const { OTLPTraceExporter } = needPeer('@opentelemetry/exporter-trace-otlp-http');
|
|
69
66
|
exporter = new OTLPTraceExporter({ url, headers: this.headers });
|
|
70
67
|
// spans and log records spool to their own subdirectories
|
|
71
68
|
if (o.bufferDir)
|
|
72
69
|
exporter = new SpoolExporter(exporter, nodePath.join(o.bufferDir, 'traces'), url, this.headers, this.fetch);
|
|
73
70
|
}
|
|
74
|
-
|
|
71
|
+
// the redactor runs first, so the batch processor exports redacted attributes
|
|
72
|
+
const { BatchSpanProcessor, TracerProvider: TP } = needPeer('@opentelemetry/sdk-trace');
|
|
73
|
+
this.provider = new TP({
|
|
74
|
+
resource,
|
|
75
|
+
spanProcessors: [...(o.redact ? [redactor(o.redact)] : []), new BatchSpanProcessor({ exporter })],
|
|
76
|
+
});
|
|
75
77
|
this.tracer = this.provider.getTracer('watchtower');
|
|
76
|
-
this.loggerProvider = new LoggerProvider({ resource });
|
|
77
78
|
const logsOn = switchOn('WATCHTOWER_LOGS', 'OTEL_LOGS_EXPORTER', o.logs);
|
|
78
79
|
let logExporter = logsOn ? o.logExporter : undefined;
|
|
79
80
|
if (logsOn && !logExporter && !o.exporter) {
|
|
80
81
|
const url = `${this.endpoint}/v1/logs`;
|
|
82
|
+
const { OTLPLogExporter } = needPeer('@opentelemetry/exporter-logs-otlp-http');
|
|
81
83
|
logExporter = new OTLPLogExporter({ url, headers: this.headers });
|
|
82
84
|
if (o.bufferDir)
|
|
83
85
|
logExporter = spoolLogs(logExporter, nodePath.join(o.bufferDir, 'logs'), url, this.headers, this.fetch);
|
|
84
86
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
const { LoggerProvider: LP, SimpleLogRecordProcessor, BatchLogRecordProcessor } = needPeer('@opentelemetry/sdk-logs');
|
|
88
|
+
this.loggerProvider = new LP({
|
|
89
|
+
resource,
|
|
90
|
+
processors: !logExporter ? [] : [o.logExporter
|
|
91
|
+
? new SimpleLogRecordProcessor({ exporter: logExporter })
|
|
92
|
+
: new BatchLogRecordProcessor({ exporter: logExporter })],
|
|
93
|
+
});
|
|
88
94
|
this.logger = this.loggerProvider.getLogger('watchtower');
|
|
89
95
|
// with a test span exporter only an explicit metric exporter is used, as for logs
|
|
90
96
|
if (switchOn('WATCHTOWER_METRICS', 'OTEL_METRICS_EXPORTER', o.metrics) && (o.metricExporter || !o.exporter)) {
|
|
@@ -230,13 +236,23 @@ export function init(opts) {
|
|
|
230
236
|
void current?.profiler?.then((p) => p?.stop());
|
|
231
237
|
const c = new Client(opts);
|
|
232
238
|
current = c;
|
|
233
|
-
|
|
239
|
+
// Global tracer + async context so trace.getActiveSpan() works; a re-init replaces the previous client's.
|
|
240
|
+
trace.disable();
|
|
241
|
+
context.disable();
|
|
242
|
+
propagation.disable();
|
|
243
|
+
const { AsyncLocalStorageContextManager } = needPeer('@opentelemetry/context-async-hooks');
|
|
244
|
+
const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = needPeer('@opentelemetry/core');
|
|
245
|
+
context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
|
|
246
|
+
propagation.setGlobalPropagator(new CompositePropagator({
|
|
247
|
+
propagators: [new W3CTraceContextPropagator(), new W3CBaggagePropagator()],
|
|
248
|
+
}));
|
|
249
|
+
trace.setGlobalTracerProvider(c.provider);
|
|
234
250
|
if (c.meterProvider) {
|
|
235
251
|
metrics.disable(); // a re-init replaces the previous client's provider
|
|
236
252
|
metrics.setGlobalMeterProvider(c.meterProvider);
|
|
237
253
|
}
|
|
238
254
|
if (process.env.WT_PROFILING === '1') {
|
|
239
|
-
const attrs = c.
|
|
255
|
+
const attrs = c.resource.attributes;
|
|
240
256
|
c.profiler = startProfiling({
|
|
241
257
|
apiKey: c.headers.Authorization.slice('Bearer '.length),
|
|
242
258
|
endpoint: c.endpoint,
|
package/dist/esm/load.d.ts
CHANGED
package/dist/esm/metrics.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Resource } from '@opentelemetry/resources';
|
|
2
|
-
import { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
3
|
-
import type { PushMetricExporter } from '@opentelemetry/sdk-metrics';
|
|
2
|
+
import type { MeterProvider, PushMetricExporter } from '@opentelemetry/sdk-metrics';
|
|
4
3
|
/** Explicit flag, else `WATCHTOWER_LOGS` / `WATCHTOWER_METRICS` (default on); `OTEL_*_EXPORTER=none` turns it off. */
|
|
5
4
|
export declare function switchOn(name: string, otelExporterEnv: string, flag?: boolean): boolean;
|
|
6
5
|
export interface Metrics {
|
package/dist/esm/metrics.js
CHANGED
|
@@ -3,10 +3,8 @@
|
|
|
3
3
|
// metrics (event loop delay and utilization, GC, heap) come from the optional peer
|
|
4
4
|
// @opentelemetry/instrumentation-runtime-node when it is installed.
|
|
5
5
|
import * as nodePath from 'node:path';
|
|
6
|
-
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
|
|
7
|
-
import { AggregationTemporalityPreference, OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
|
|
8
6
|
import { MetricSpool } from './buffer.js';
|
|
9
|
-
import { load } from './load.js';
|
|
7
|
+
import { load, need } from './load.js';
|
|
10
8
|
/** Explicit flag, else `WATCHTOWER_LOGS` / `WATCHTOWER_METRICS` (default on); `OTEL_*_EXPORTER=none` turns it off. */
|
|
11
9
|
export function switchOn(name, otelExporterEnv, flag) {
|
|
12
10
|
if (flag !== undefined)
|
|
@@ -17,9 +15,11 @@ export function switchOn(name, otelExporterEnv, flag) {
|
|
|
17
15
|
return !['off', 'false', '0', 'no'].includes((env[name] || 'on').trim().toLowerCase());
|
|
18
16
|
}
|
|
19
17
|
export function startMetrics(endpoint, headers, resource, bufferDir, fetch, exporter) {
|
|
18
|
+
const sdk = need('@opentelemetry/sdk-metrics');
|
|
20
19
|
const url = `${endpoint}/v1/metrics`;
|
|
21
20
|
let exp = exporter;
|
|
22
21
|
if (!exp) {
|
|
22
|
+
const { AggregationTemporalityPreference, OTLPMetricExporter } = need('@opentelemetry/exporter-metrics-otlp-http');
|
|
23
23
|
const delta = process.env.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
|
|
24
24
|
? {} : { temporalityPreference: AggregationTemporalityPreference.DELTA };
|
|
25
25
|
exp = new OTLPMetricExporter({ url, headers, ...delta });
|
|
@@ -27,10 +27,10 @@ export function startMetrics(endpoint, headers, resource, bufferDir, fetch, expo
|
|
|
27
27
|
exp = new MetricSpool(exp, nodePath.join(bufferDir, 'metrics'), url, headers, fetch);
|
|
28
28
|
}
|
|
29
29
|
const interval = Number(process.env.OTEL_METRIC_EXPORT_INTERVAL) || 60000;
|
|
30
|
-
const reader = new PeriodicExportingMetricReader({
|
|
30
|
+
const reader = new sdk.PeriodicExportingMetricReader({
|
|
31
31
|
exporter: exp, exportIntervalMillis: interval, exportTimeoutMillis: Math.min(30000, interval),
|
|
32
32
|
});
|
|
33
|
-
const provider = new MeterProvider({ resource, readers: [reader] });
|
|
33
|
+
const provider = new sdk.MeterProvider({ resource, readers: [reader] });
|
|
34
34
|
let runtime;
|
|
35
35
|
try {
|
|
36
36
|
const mod = load('@opentelemetry/instrumentation-runtime-node');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@watchtowerobservability/node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/watchtowerobservability/watchtower-node.git"
|
|
@@ -37,6 +37,16 @@
|
|
|
37
37
|
"default": "./dist/cjs/appsec.js"
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
|
+
"./iast": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/esm/iast.d.ts",
|
|
43
|
+
"default": "./dist/esm/iast.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/cjs/iast.d.ts",
|
|
47
|
+
"default": "./dist/cjs/iast.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
40
50
|
"./probes": {
|
|
41
51
|
"import": {
|
|
42
52
|
"types": "./dist/esm/probes.d.ts",
|
|
@@ -340,7 +350,7 @@
|
|
|
340
350
|
},
|
|
341
351
|
"license": "Apache-2.0",
|
|
342
352
|
"engines": {
|
|
343
|
-
"node": ">=
|
|
353
|
+
"node": ">=22.15"
|
|
344
354
|
},
|
|
345
355
|
"scripts": {
|
|
346
356
|
"build": "node scripts/build.mjs",
|
|
@@ -349,30 +359,71 @@
|
|
|
349
359
|
"prepack": "npm run build"
|
|
350
360
|
},
|
|
351
361
|
"dependencies": {
|
|
352
|
-
"@opentelemetry/api": "^1.
|
|
353
|
-
"@opentelemetry/exporter-logs-otlp-http": "^0.57.0",
|
|
354
|
-
"@opentelemetry/exporter-metrics-otlp-http": "^0.57.0",
|
|
355
|
-
"@opentelemetry/exporter-trace-otlp-http": "^0.57.0",
|
|
356
|
-
"@opentelemetry/otlp-transformer": "^0.57.0",
|
|
357
|
-
"@opentelemetry/resources": "^1.30.0",
|
|
358
|
-
"@opentelemetry/sdk-logs": "^0.57.0",
|
|
359
|
-
"@opentelemetry/sdk-metrics": "^1.30.0",
|
|
360
|
-
"@opentelemetry/sdk-trace-base": "^1.30.0",
|
|
361
|
-
"@opentelemetry/sdk-trace-node": "^1.30.0"
|
|
362
|
+
"@opentelemetry/api": "^1.10.0"
|
|
362
363
|
},
|
|
363
364
|
"peerDependencies": {
|
|
364
|
-
"@opentelemetry/
|
|
365
|
+
"@opentelemetry/context-async-hooks": "^3.1.0",
|
|
366
|
+
"@opentelemetry/core": "^3.1.0",
|
|
367
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.301.0",
|
|
368
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.301.0",
|
|
369
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.301.0",
|
|
370
|
+
"@opentelemetry/instrumentation-runtime-node": ">=0.38.0",
|
|
371
|
+
"@opentelemetry/otlp-transformer": "^0.301.0",
|
|
372
|
+
"@opentelemetry/resources": "^3.1.0",
|
|
373
|
+
"@opentelemetry/sdk-logs": "^3.1.0",
|
|
374
|
+
"@opentelemetry/sdk-metrics": "^3.1.0",
|
|
375
|
+
"@opentelemetry/sdk-trace": "^3.1.0",
|
|
365
376
|
"winston-transport": "^4.0.0"
|
|
366
377
|
},
|
|
367
378
|
"peerDependenciesMeta": {
|
|
379
|
+
"@opentelemetry/context-async-hooks": {
|
|
380
|
+
"optional": true
|
|
381
|
+
},
|
|
382
|
+
"@opentelemetry/core": {
|
|
383
|
+
"optional": true
|
|
384
|
+
},
|
|
385
|
+
"@opentelemetry/exporter-logs-otlp-http": {
|
|
386
|
+
"optional": true
|
|
387
|
+
},
|
|
388
|
+
"@opentelemetry/exporter-metrics-otlp-http": {
|
|
389
|
+
"optional": true
|
|
390
|
+
},
|
|
391
|
+
"@opentelemetry/exporter-trace-otlp-http": {
|
|
392
|
+
"optional": true
|
|
393
|
+
},
|
|
368
394
|
"@opentelemetry/instrumentation-runtime-node": {
|
|
369
395
|
"optional": true
|
|
370
396
|
},
|
|
397
|
+
"@opentelemetry/otlp-transformer": {
|
|
398
|
+
"optional": true
|
|
399
|
+
},
|
|
400
|
+
"@opentelemetry/resources": {
|
|
401
|
+
"optional": true
|
|
402
|
+
},
|
|
403
|
+
"@opentelemetry/sdk-logs": {
|
|
404
|
+
"optional": true
|
|
405
|
+
},
|
|
406
|
+
"@opentelemetry/sdk-metrics": {
|
|
407
|
+
"optional": true
|
|
408
|
+
},
|
|
409
|
+
"@opentelemetry/sdk-trace": {
|
|
410
|
+
"optional": true
|
|
411
|
+
},
|
|
371
412
|
"winston-transport": {
|
|
372
413
|
"optional": true
|
|
373
414
|
}
|
|
374
415
|
},
|
|
375
416
|
"devDependencies": {
|
|
417
|
+
"@opentelemetry/context-async-hooks": "^3.1.0",
|
|
418
|
+
"@opentelemetry/core": "^3.1.0",
|
|
419
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.301.0",
|
|
420
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.301.0",
|
|
421
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.301.0",
|
|
422
|
+
"@opentelemetry/otlp-transformer": "^0.301.0",
|
|
423
|
+
"@opentelemetry/resources": "^3.1.0",
|
|
424
|
+
"@opentelemetry/sdk-logs": "^3.1.0",
|
|
425
|
+
"@opentelemetry/sdk-metrics": "^3.1.0",
|
|
426
|
+
"@opentelemetry/sdk-trace": "^3.1.0",
|
|
376
427
|
"@types/node": "22.15.0",
|
|
377
428
|
"typescript": "5.7.2",
|
|
378
429
|
"vitest": "5.0.5",
|