@dyrected/core 2.7.0 → 2.7.1
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/dist/chunk-2UXLA4YN.js +3139 -0
- package/dist/chunk-5U2WRCNH.js +3140 -0
- package/dist/chunk-AKYYGMFS.js +3365 -0
- package/dist/chunk-EJG6AMYB.js +3537 -0
- package/dist/chunk-FRSJJO27.js +519 -0
- package/dist/chunk-G43REXPX.js +3147 -0
- package/dist/chunk-GFKBPBJJ.js +3637 -0
- package/dist/chunk-LFGSRQLX.js +3568 -0
- package/dist/chunk-NNZRYF3U.js +3537 -0
- package/dist/chunk-PYE2JSY4.js +3568 -0
- package/dist/chunk-QCXLDKZO.js +3272 -0
- package/dist/chunk-UKUEKTAO.js +513 -0
- package/dist/chunk-VM43G3IG.js +506 -0
- package/dist/chunk-VWJKFZQU.js +3139 -0
- package/dist/chunk-YSSHLGQ6.js +3139 -0
- package/dist/chunk-Z7CFUYVP.js +3653 -0
- package/dist/index.cjs +984 -82
- package/dist/index.d.cts +197 -1
- package/dist/index.d.ts +197 -1
- package/dist/index.js +67 -1
- package/dist/observability-CNM4QFE4.js +34 -0
- package/dist/observability-KCMTV7GO.js +34 -0
- package/dist/observability-L5BORBB2.js +34 -0
- package/dist/server.cjs +1268 -888
- package/dist/server.d.cts +28 -3
- package/dist/server.d.ts +28 -3
- package/dist/server.js +38 -33
- package/package.json +1 -1
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
// src/observability.ts
|
|
2
|
+
import {
|
|
3
|
+
TraceFlags,
|
|
4
|
+
context as otelContext,
|
|
5
|
+
trace
|
|
6
|
+
} from "@opentelemetry/api";
|
|
7
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
8
|
+
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
|
|
9
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
10
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
11
|
+
import {
|
|
12
|
+
MeterProvider,
|
|
13
|
+
PeriodicExportingMetricReader
|
|
14
|
+
} from "@opentelemetry/sdk-metrics";
|
|
15
|
+
import {
|
|
16
|
+
BasicTracerProvider,
|
|
17
|
+
BatchSpanProcessor,
|
|
18
|
+
ConsoleSpanExporter
|
|
19
|
+
} from "@opentelemetry/sdk-trace-base";
|
|
20
|
+
import pino, {
|
|
21
|
+
destination as pinoDestination,
|
|
22
|
+
multistream,
|
|
23
|
+
stdTimeFunctions
|
|
24
|
+
} from "pino";
|
|
25
|
+
import { Writable } from "stream";
|
|
26
|
+
var DEFAULT_REDACT_HEADERS = ["authorization", "cookie", "set-cookie", "x-api-key"];
|
|
27
|
+
var DEFAULT_REDACT_PATHS = [
|
|
28
|
+
"password",
|
|
29
|
+
"currentPassword",
|
|
30
|
+
"newPassword",
|
|
31
|
+
"confirmPassword",
|
|
32
|
+
"token",
|
|
33
|
+
"refreshToken",
|
|
34
|
+
"accessToken",
|
|
35
|
+
"secret",
|
|
36
|
+
"apiKey",
|
|
37
|
+
"inviteToken",
|
|
38
|
+
"resetToken"
|
|
39
|
+
];
|
|
40
|
+
var DEFAULT_MAX_BODY_BYTES = 8192;
|
|
41
|
+
var DEFAULT_SUCCESS_SAMPLE_RATE = 0.1;
|
|
42
|
+
var DEFAULT_BODY_SAMPLE_RATE = 0.02;
|
|
43
|
+
var REDACTED_VALUE = "[REDACTED]";
|
|
44
|
+
var runtimeByConfig = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
var fallbackLogger = pino({
|
|
46
|
+
name: "dyrected",
|
|
47
|
+
enabled: process.env.DISABLE_LOGGING !== "true",
|
|
48
|
+
timestamp: stdTimeFunctions.isoTime
|
|
49
|
+
});
|
|
50
|
+
function resolveObservabilityConfig(config) {
|
|
51
|
+
const requestLogging = config?.requestLogging;
|
|
52
|
+
const sampling = config?.sampling;
|
|
53
|
+
const tracing = config?.tracing;
|
|
54
|
+
const metrics = config?.metrics;
|
|
55
|
+
return {
|
|
56
|
+
requestLogging: {
|
|
57
|
+
enabled: requestLogging?.enabled ?? true,
|
|
58
|
+
logBodies: requestLogging?.logBodies ?? false,
|
|
59
|
+
maxBodyBytes: requestLogging?.maxBodyBytes ?? DEFAULT_MAX_BODY_BYTES,
|
|
60
|
+
redactPaths: uniqueValues([
|
|
61
|
+
...DEFAULT_REDACT_PATHS,
|
|
62
|
+
...requestLogging?.redactPaths ?? []
|
|
63
|
+
]),
|
|
64
|
+
includeHeaders: normalizeHeaderNames(requestLogging?.includeHeaders ?? []),
|
|
65
|
+
redactHeaders: uniqueValues(
|
|
66
|
+
normalizeHeaderNames([
|
|
67
|
+
...DEFAULT_REDACT_HEADERS,
|
|
68
|
+
...requestLogging?.redactHeaders ?? []
|
|
69
|
+
])
|
|
70
|
+
)
|
|
71
|
+
},
|
|
72
|
+
sampling: {
|
|
73
|
+
successRate: clampRate(sampling?.successRate ?? DEFAULT_SUCCESS_SAMPLE_RATE),
|
|
74
|
+
traceSuccessRate: clampRate(
|
|
75
|
+
sampling?.traceSuccessRate ?? sampling?.successRate ?? DEFAULT_SUCCESS_SAMPLE_RATE
|
|
76
|
+
),
|
|
77
|
+
bodySuccessRate: clampRate(
|
|
78
|
+
sampling?.bodySuccessRate ?? Math.min(DEFAULT_BODY_SAMPLE_RATE, sampling?.successRate ?? DEFAULT_SUCCESS_SAMPLE_RATE)
|
|
79
|
+
),
|
|
80
|
+
alwaysKeep4xx: sampling?.alwaysKeep4xx ?? true,
|
|
81
|
+
alwaysKeep5xx: sampling?.alwaysKeep5xx ?? true
|
|
82
|
+
},
|
|
83
|
+
tracing: {
|
|
84
|
+
enabled: tracing?.enabled ?? false,
|
|
85
|
+
serviceName: tracing?.serviceName ?? "dyrected",
|
|
86
|
+
exporter: tracing?.exporter ?? "none",
|
|
87
|
+
headers: tracing?.headers ?? {},
|
|
88
|
+
endpoint: tracing?.endpoint
|
|
89
|
+
},
|
|
90
|
+
metrics: {
|
|
91
|
+
enabled: metrics?.enabled ?? false,
|
|
92
|
+
exporter: metrics?.exporter ?? "none",
|
|
93
|
+
endpoint: metrics?.endpoint,
|
|
94
|
+
path: metrics?.path ?? "/metrics"
|
|
95
|
+
},
|
|
96
|
+
transports: {
|
|
97
|
+
targets: config?.transports?.targets && config.transports.targets.length > 0 ? config.transports.targets : [{ type: "stdout" }]
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function createObservabilityRuntime(config) {
|
|
102
|
+
const resolved = resolveObservabilityConfig(config.observability);
|
|
103
|
+
const logger = createRootLogger(config.logger, resolved);
|
|
104
|
+
const shutdownTasks = [];
|
|
105
|
+
const meterRuntime = resolved.metrics.enabled || resolved.metrics.exporter === "prometheus" ? createMeterProvider(resolved) : void 0;
|
|
106
|
+
const tracerProvider = resolved.tracing.enabled ? createTracerProvider(resolved) : void 0;
|
|
107
|
+
const tracer = tracerProvider?.getTracer("dyrected");
|
|
108
|
+
const meter = meterRuntime?.provider.getMeter("dyrected");
|
|
109
|
+
const metrics = meter ? {
|
|
110
|
+
requestCount: meter.createCounter("dyrected_http_requests_total", {
|
|
111
|
+
description: "Total HTTP requests served by Dyrected"
|
|
112
|
+
}),
|
|
113
|
+
authFailureCount: meter.createCounter("dyrected_auth_failures_total", {
|
|
114
|
+
description: "Authentication failures"
|
|
115
|
+
}),
|
|
116
|
+
uncaughtErrorCount: meter.createCounter("dyrected_uncaught_errors_total", {
|
|
117
|
+
description: "Uncaught application errors"
|
|
118
|
+
}),
|
|
119
|
+
auditWriteFailureCount: meter.createCounter("dyrected_audit_write_failures_total", {
|
|
120
|
+
description: "Audit log persistence failures"
|
|
121
|
+
}),
|
|
122
|
+
emailSendFailureCount: meter.createCounter("dyrected_email_send_failures_total", {
|
|
123
|
+
description: "Transactional email delivery failures"
|
|
124
|
+
}),
|
|
125
|
+
workflowHookFailureCount: meter.createCounter("dyrected_workflow_hook_failures_total", {
|
|
126
|
+
description: "Workflow hook failures isolated after successful writes"
|
|
127
|
+
}),
|
|
128
|
+
requestDuration: meter.createHistogram("dyrected_http_request_duration_ms", {
|
|
129
|
+
description: "HTTP request duration in milliseconds",
|
|
130
|
+
unit: "ms"
|
|
131
|
+
})
|
|
132
|
+
} : void 0;
|
|
133
|
+
if (meterRuntime) {
|
|
134
|
+
shutdownTasks.push(() => meterRuntime.provider.shutdown());
|
|
135
|
+
}
|
|
136
|
+
if (tracerProvider) {
|
|
137
|
+
shutdownTasks.push(() => tracerProvider.shutdown());
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
logger,
|
|
141
|
+
config: resolved,
|
|
142
|
+
tracer,
|
|
143
|
+
metrics,
|
|
144
|
+
prometheusExporter: meterRuntime?.prometheusExporter,
|
|
145
|
+
shutdown: async () => {
|
|
146
|
+
for (const task of shutdownTasks) {
|
|
147
|
+
await task();
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
recordAuthFailure: (attributes) => metrics?.authFailureCount.add(1, attributes),
|
|
151
|
+
recordUncaughtError: (attributes) => metrics?.uncaughtErrorCount.add(1, attributes),
|
|
152
|
+
recordAuditWriteFailure: (attributes) => metrics?.auditWriteFailureCount.add(1, attributes),
|
|
153
|
+
recordEmailSendFailure: (attributes) => metrics?.emailSendFailureCount.add(1, attributes),
|
|
154
|
+
recordWorkflowHookFailure: (attributes) => metrics?.workflowHookFailureCount.add(1, attributes)
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function bindObservabilityRuntime(config, runtime) {
|
|
158
|
+
runtimeByConfig.set(config, runtime);
|
|
159
|
+
}
|
|
160
|
+
function getObservabilityRuntime(config) {
|
|
161
|
+
if (!config || typeof config !== "object") return void 0;
|
|
162
|
+
return runtimeByConfig.get(config);
|
|
163
|
+
}
|
|
164
|
+
function createRootLogger(loggerConfig, observability) {
|
|
165
|
+
if (loggerConfig && !("options" in loggerConfig)) {
|
|
166
|
+
return loggerConfig;
|
|
167
|
+
}
|
|
168
|
+
const options = loggerConfig?.options ?? {};
|
|
169
|
+
const enabled = options.enabled ?? process.env.DISABLE_LOGGING !== "true";
|
|
170
|
+
const name = options.name ?? "dyrected";
|
|
171
|
+
if (loggerConfig?.destination) {
|
|
172
|
+
return pino(
|
|
173
|
+
{
|
|
174
|
+
...options,
|
|
175
|
+
enabled,
|
|
176
|
+
name,
|
|
177
|
+
timestamp: options.timestamp ?? stdTimeFunctions.isoTime
|
|
178
|
+
},
|
|
179
|
+
loggerConfig.destination
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
const streams = buildTransportStreams(observability);
|
|
183
|
+
const destination = streams.length === 1 ? streams[0] : multistream(streams.map((stream) => ({ stream })));
|
|
184
|
+
return pino(
|
|
185
|
+
{
|
|
186
|
+
...options,
|
|
187
|
+
enabled,
|
|
188
|
+
name,
|
|
189
|
+
timestamp: options.timestamp ?? stdTimeFunctions.isoTime
|
|
190
|
+
},
|
|
191
|
+
destination
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
function buildTransportStreams(observability) {
|
|
195
|
+
const targets = observability.transports.targets;
|
|
196
|
+
if (targets.length === 0) {
|
|
197
|
+
return [defaultLoggerDestination()];
|
|
198
|
+
}
|
|
199
|
+
return targets.map((target) => {
|
|
200
|
+
switch (target.type) {
|
|
201
|
+
case "stdout":
|
|
202
|
+
return defaultLoggerDestination();
|
|
203
|
+
case "stderr":
|
|
204
|
+
return pinoDestination(2);
|
|
205
|
+
case "file":
|
|
206
|
+
return pinoDestination(target.path);
|
|
207
|
+
case "otlp":
|
|
208
|
+
return new OtlpLogWritable(target.endpoint, target.headers);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function defaultLoggerDestination() {
|
|
213
|
+
if (process.env.NODE_ENV !== "production" && typeof window === "undefined") {
|
|
214
|
+
try {
|
|
215
|
+
const req = globalThis.require;
|
|
216
|
+
if (req) {
|
|
217
|
+
const pinoPretty = req("pino-pretty");
|
|
218
|
+
return pinoPretty({
|
|
219
|
+
colorize: true,
|
|
220
|
+
ignore: "pid,hostname",
|
|
221
|
+
translateTime: "SYS:HH:MM:ss",
|
|
222
|
+
destination: 1,
|
|
223
|
+
sync: true
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
} catch {
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return pinoDestination(1);
|
|
230
|
+
}
|
|
231
|
+
var OtlpLogWritable = class extends Writable {
|
|
232
|
+
constructor(endpoint, headers = {}) {
|
|
233
|
+
super();
|
|
234
|
+
this.endpoint = endpoint;
|
|
235
|
+
this.headers = headers;
|
|
236
|
+
}
|
|
237
|
+
endpoint;
|
|
238
|
+
headers;
|
|
239
|
+
_write(chunk, _encoding, callback) {
|
|
240
|
+
const payload = chunk.toString();
|
|
241
|
+
void fetch(this.endpoint, {
|
|
242
|
+
method: "POST",
|
|
243
|
+
headers: {
|
|
244
|
+
"content-type": "application/json",
|
|
245
|
+
...this.headers
|
|
246
|
+
},
|
|
247
|
+
body: JSON.stringify({ logs: payload.trim().split("\n").filter(Boolean) })
|
|
248
|
+
}).then(() => callback()).catch(
|
|
249
|
+
(error) => callback(error instanceof Error ? error : new Error(String(error)))
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
function createTracerProvider(observability) {
|
|
254
|
+
const exporter = createTraceExporter(observability);
|
|
255
|
+
const provider = new BasicTracerProvider({
|
|
256
|
+
resource: resourceFromAttributes({
|
|
257
|
+
"service.name": observability.tracing.serviceName
|
|
258
|
+
}),
|
|
259
|
+
spanProcessors: exporter ? [new BatchSpanProcessor(exporter)] : []
|
|
260
|
+
});
|
|
261
|
+
trace.setGlobalTracerProvider(provider);
|
|
262
|
+
return provider;
|
|
263
|
+
}
|
|
264
|
+
function createTraceExporter(observability) {
|
|
265
|
+
switch (observability.tracing.exporter) {
|
|
266
|
+
case "console":
|
|
267
|
+
return new ConsoleSpanExporter();
|
|
268
|
+
case "otlp":
|
|
269
|
+
return new OTLPTraceExporter({
|
|
270
|
+
url: observability.tracing.endpoint,
|
|
271
|
+
headers: observability.tracing.headers
|
|
272
|
+
});
|
|
273
|
+
default:
|
|
274
|
+
return void 0;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function createMeterProvider(observability) {
|
|
278
|
+
const readers = [];
|
|
279
|
+
let prometheusExporter;
|
|
280
|
+
switch (observability.metrics.exporter) {
|
|
281
|
+
case "otlp":
|
|
282
|
+
readers.push(
|
|
283
|
+
new PeriodicExportingMetricReader({
|
|
284
|
+
exporter: new OTLPMetricExporter({
|
|
285
|
+
url: observability.metrics.endpoint
|
|
286
|
+
})
|
|
287
|
+
})
|
|
288
|
+
);
|
|
289
|
+
break;
|
|
290
|
+
case "prometheus":
|
|
291
|
+
prometheusExporter = new PrometheusExporter({
|
|
292
|
+
endpoint: observability.metrics.path,
|
|
293
|
+
preventServerStart: true
|
|
294
|
+
});
|
|
295
|
+
readers.push(prometheusExporter);
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
const provider = new MeterProvider({
|
|
299
|
+
resource: resourceFromAttributes({
|
|
300
|
+
"service.name": observability.tracing.serviceName
|
|
301
|
+
}),
|
|
302
|
+
readers
|
|
303
|
+
});
|
|
304
|
+
return { provider, prometheusExporter };
|
|
305
|
+
}
|
|
306
|
+
function getConfigLogger(config, component) {
|
|
307
|
+
const base = config?.logger && !("options" in config.logger) ? config.logger : fallbackLogger;
|
|
308
|
+
return base.child({ component });
|
|
309
|
+
}
|
|
310
|
+
function getRequestLogger(c, component) {
|
|
311
|
+
const base = c.get("logger") ?? fallbackLogger;
|
|
312
|
+
return component ? base.child({ component }) : base;
|
|
313
|
+
}
|
|
314
|
+
function buildRequestLogger(runtime, vars, requestId) {
|
|
315
|
+
const trace2 = vars.requestTrace;
|
|
316
|
+
return runtime.logger.child({
|
|
317
|
+
requestId,
|
|
318
|
+
siteId: vars.siteId,
|
|
319
|
+
workspaceId: vars.workspaceId,
|
|
320
|
+
traceId: trace2?.traceId,
|
|
321
|
+
spanId: trace2?.spanId
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
function shouldSampleRequest(requestId, statusCode, sampling, kind) {
|
|
325
|
+
if (statusCode >= 500 && sampling.alwaysKeep5xx) return true;
|
|
326
|
+
if (statusCode >= 400 && sampling.alwaysKeep4xx) return true;
|
|
327
|
+
const rate = kind === "trace" ? sampling.traceSuccessRate : kind === "body" ? sampling.bodySuccessRate : sampling.successRate;
|
|
328
|
+
return deterministicSample(requestId ?? "no-request-id", rate);
|
|
329
|
+
}
|
|
330
|
+
function redactHeaders(headers, observability) {
|
|
331
|
+
const includeHeaders = new Set(observability.requestLogging.includeHeaders);
|
|
332
|
+
const redactHeaders2 = new Set(observability.requestLogging.redactHeaders);
|
|
333
|
+
const output = {};
|
|
334
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
335
|
+
const normalizedKey = key.toLowerCase();
|
|
336
|
+
if (includeHeaders.size > 0 && !includeHeaders.has(normalizedKey)) continue;
|
|
337
|
+
output[normalizedKey] = redactHeaders2.has(normalizedKey) ? REDACTED_VALUE : value;
|
|
338
|
+
}
|
|
339
|
+
return output;
|
|
340
|
+
}
|
|
341
|
+
async function captureRequestBody(request, observability) {
|
|
342
|
+
const contentType = request.headers.get("content-type") ?? void 0;
|
|
343
|
+
const contentLength = toNumber(request.headers.get("content-length"));
|
|
344
|
+
const jsonBody = contentType?.includes("application/json");
|
|
345
|
+
if (!jsonBody) {
|
|
346
|
+
return {
|
|
347
|
+
attempted: false,
|
|
348
|
+
contentType,
|
|
349
|
+
contentLength
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
try {
|
|
353
|
+
const text = await request.clone().text();
|
|
354
|
+
if (!text) {
|
|
355
|
+
return { attempted: true, contentType, contentLength };
|
|
356
|
+
}
|
|
357
|
+
const truncated = text.length > observability.requestLogging.maxBodyBytes;
|
|
358
|
+
const raw = truncated ? text.slice(0, observability.requestLogging.maxBodyBytes) : text;
|
|
359
|
+
try {
|
|
360
|
+
const parsed = JSON.parse(raw);
|
|
361
|
+
return {
|
|
362
|
+
attempted: true,
|
|
363
|
+
contentType,
|
|
364
|
+
contentLength: contentLength ?? text.length,
|
|
365
|
+
truncated,
|
|
366
|
+
body: redactBody(parsed, observability.requestLogging.redactPaths)
|
|
367
|
+
};
|
|
368
|
+
} catch {
|
|
369
|
+
return {
|
|
370
|
+
attempted: true,
|
|
371
|
+
contentType,
|
|
372
|
+
contentLength: contentLength ?? text.length,
|
|
373
|
+
truncated,
|
|
374
|
+
parseFailed: true
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
} catch {
|
|
378
|
+
return {
|
|
379
|
+
attempted: true,
|
|
380
|
+
contentType,
|
|
381
|
+
contentLength,
|
|
382
|
+
parseFailed: true
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function redactBody(value, redactPaths) {
|
|
387
|
+
const cloned = deepClone(value);
|
|
388
|
+
for (const path of redactPaths) {
|
|
389
|
+
applyRedaction(cloned, path.split("."));
|
|
390
|
+
}
|
|
391
|
+
return cloned;
|
|
392
|
+
}
|
|
393
|
+
function applyRedaction(target, segments) {
|
|
394
|
+
if (!target || segments.length === 0) return;
|
|
395
|
+
const [segment, ...rest] = segments;
|
|
396
|
+
if (Array.isArray(target)) {
|
|
397
|
+
for (const item of target) {
|
|
398
|
+
if (segment === "*") {
|
|
399
|
+
applyRedaction(item, rest);
|
|
400
|
+
} else {
|
|
401
|
+
applyRedaction(item, segments);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (typeof target !== "object") return;
|
|
407
|
+
const record = target;
|
|
408
|
+
if (segment === "*") {
|
|
409
|
+
for (const value of Object.values(record)) {
|
|
410
|
+
applyRedaction(value, rest);
|
|
411
|
+
}
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (!(segment in record)) return;
|
|
415
|
+
if (rest.length === 0) {
|
|
416
|
+
record[segment] = REDACTED_VALUE;
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
applyRedaction(record[segment], rest);
|
|
420
|
+
}
|
|
421
|
+
function deepClone(value) {
|
|
422
|
+
if (typeof structuredClone === "function") {
|
|
423
|
+
return structuredClone(value);
|
|
424
|
+
}
|
|
425
|
+
return JSON.parse(JSON.stringify(value));
|
|
426
|
+
}
|
|
427
|
+
function createRequestSpan(runtime, name, attributes) {
|
|
428
|
+
if (!runtime.tracer) return void 0;
|
|
429
|
+
const span = runtime.tracer.startSpan(name, { attributes }, otelContext.active());
|
|
430
|
+
const spanContext = span.spanContext();
|
|
431
|
+
return {
|
|
432
|
+
span,
|
|
433
|
+
traceId: spanContext.traceId,
|
|
434
|
+
spanId: spanContext.spanId,
|
|
435
|
+
sampled: spanContext.traceFlags === TraceFlags.SAMPLED
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function endRequestSpan(requestTrace, statusCode, error) {
|
|
439
|
+
if (!requestTrace) return;
|
|
440
|
+
requestTrace.span.setAttribute("http.status_code", statusCode);
|
|
441
|
+
if (error) {
|
|
442
|
+
requestTrace.span.recordException(error instanceof Error ? error : new Error(String(error)));
|
|
443
|
+
}
|
|
444
|
+
requestTrace.span.end();
|
|
445
|
+
}
|
|
446
|
+
function attachRequestMetrics(runtime, args) {
|
|
447
|
+
runtime.metrics?.requestCount.add(1, {
|
|
448
|
+
method: args.method,
|
|
449
|
+
route: args.route,
|
|
450
|
+
status_class: `${Math.floor(args.statusCode / 100)}xx`
|
|
451
|
+
});
|
|
452
|
+
runtime.metrics?.requestDuration.record(args.durationMs, {
|
|
453
|
+
method: args.method,
|
|
454
|
+
route: args.route,
|
|
455
|
+
status_class: `${Math.floor(args.statusCode / 100)}xx`
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
async function renderPrometheusMetrics(exporter) {
|
|
459
|
+
let statusCode = 200;
|
|
460
|
+
const headers = {};
|
|
461
|
+
let body = "";
|
|
462
|
+
await new Promise((resolve) => {
|
|
463
|
+
const response = {
|
|
464
|
+
statusCode,
|
|
465
|
+
setHeader(name, value) {
|
|
466
|
+
headers[name.toLowerCase()] = value;
|
|
467
|
+
},
|
|
468
|
+
end(chunk) {
|
|
469
|
+
if (chunk) body += chunk;
|
|
470
|
+
statusCode = this.statusCode;
|
|
471
|
+
resolve();
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
exporter.getMetricsRequestHandler({}, response);
|
|
475
|
+
});
|
|
476
|
+
return { body, headers, statusCode };
|
|
477
|
+
}
|
|
478
|
+
function normalizeHeaderNames(headers) {
|
|
479
|
+
return headers.map((value) => value.toLowerCase());
|
|
480
|
+
}
|
|
481
|
+
function uniqueValues(values) {
|
|
482
|
+
return [...new Set(values)];
|
|
483
|
+
}
|
|
484
|
+
function clampRate(value) {
|
|
485
|
+
if (Number.isNaN(value)) return 0;
|
|
486
|
+
return Math.max(0, Math.min(1, value));
|
|
487
|
+
}
|
|
488
|
+
function deterministicSample(seed, rate) {
|
|
489
|
+
if (rate >= 1) return true;
|
|
490
|
+
if (rate <= 0) return false;
|
|
491
|
+
let hash = 0;
|
|
492
|
+
for (let index = 0; index < seed.length; index += 1) {
|
|
493
|
+
hash = hash * 31 + seed.charCodeAt(index) >>> 0;
|
|
494
|
+
}
|
|
495
|
+
return hash / 4294967295 <= rate;
|
|
496
|
+
}
|
|
497
|
+
function toNumber(value) {
|
|
498
|
+
if (!value) return void 0;
|
|
499
|
+
const parsed = Number(value);
|
|
500
|
+
return Number.isFinite(parsed) ? parsed : void 0;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export {
|
|
504
|
+
resolveObservabilityConfig,
|
|
505
|
+
createObservabilityRuntime,
|
|
506
|
+
bindObservabilityRuntime,
|
|
507
|
+
getObservabilityRuntime,
|
|
508
|
+
getConfigLogger,
|
|
509
|
+
getRequestLogger,
|
|
510
|
+
buildRequestLogger,
|
|
511
|
+
shouldSampleRequest,
|
|
512
|
+
redactHeaders,
|
|
513
|
+
captureRequestBody,
|
|
514
|
+
redactBody,
|
|
515
|
+
createRequestSpan,
|
|
516
|
+
endRequestSpan,
|
|
517
|
+
attachRequestMetrics,
|
|
518
|
+
renderPrometheusMetrics
|
|
519
|
+
};
|