@dyrected/core 2.12.0 → 2.13.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/dist/access-P6Z2XAFS.js +10 -0
- package/dist/ai-X3M2SE5A.js +17 -0
- package/dist/ai-tools-UPKJY3TX.js +14 -0
- package/dist/{app-config-C6hYM8WO.d.cts → app-D_ODsBWA.d.cts} +470 -2
- package/dist/{app-config-C6hYM8WO.d.ts → app-D_ODsBWA.d.ts} +470 -2
- package/dist/chunk-254THFDS.js +202 -0
- package/dist/chunk-27YRIGK5.js +1758 -0
- package/dist/chunk-4RAQJVJM.js +18 -0
- package/dist/{chunk-VOE2LOLF.js → chunk-DZMBVIMN.js} +265 -1760
- package/dist/chunk-HRRJ3RYT.js +70 -0
- package/dist/chunk-W7GKJM2X.js +1136 -0
- package/dist/chunk-ZYYDTHF3.js +635 -0
- package/dist/index.cjs +501 -525
- package/dist/index.d.cts +80 -4
- package/dist/index.d.ts +80 -4
- package/dist/index.js +41 -2
- package/dist/rag.service-YDGEYTXS.js +11 -0
- package/dist/server.cjs +4971 -1388
- package/dist/server.d.cts +358 -328
- package/dist/server.d.ts +358 -328
- package/dist/server.js +2035 -262
- package/package.json +12 -9
- package/LICENSE.md +0 -50
- package/dist/chunk-FRSJJO27.js +0 -519
- package/dist/observability-HQF4EUSV.js +0 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dyrected/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -20,7 +20,15 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "NODE_OPTIONS=--max-old-space-size=4096 tsup src/index.ts src/server.ts --format cjs,esm --dts",
|
|
25
|
+
"dev": "NODE_OPTIONS=--max-old-space-size=4096 tsup src/index.ts src/server.ts --format cjs,esm --watch --dts",
|
|
26
|
+
"lint": "eslint src/**/*.ts",
|
|
27
|
+
"test": "vitest"
|
|
28
|
+
},
|
|
23
29
|
"dependencies": {
|
|
30
|
+
"@ai-sdk/google": "^4.0.49",
|
|
31
|
+
"@ai-sdk/openai": "^4.0.50",
|
|
24
32
|
"@opentelemetry/api": "^1.9.1",
|
|
25
33
|
"@opentelemetry/exporter-metrics-otlp-http": "^0.220.0",
|
|
26
34
|
"@opentelemetry/exporter-prometheus": "^0.220.0",
|
|
@@ -29,6 +37,7 @@
|
|
|
29
37
|
"@opentelemetry/sdk-metrics": "^2.9.0",
|
|
30
38
|
"@opentelemetry/sdk-trace-base": "^2.9.0",
|
|
31
39
|
"@types/jexl": "^2.3.4",
|
|
40
|
+
"ai": "^7.0.76",
|
|
32
41
|
"hono": "^4.0.0",
|
|
33
42
|
"jexl": "^2.3.0",
|
|
34
43
|
"jose": "^6.2.3",
|
|
@@ -56,11 +65,5 @@
|
|
|
56
65
|
"publishConfig": {
|
|
57
66
|
"access": "public"
|
|
58
67
|
},
|
|
59
|
-
"description": "Core engine and schema handling for Dyrected CMS"
|
|
60
|
-
|
|
61
|
-
"build": "NODE_OPTIONS=--max-old-space-size=4096 tsup src/index.ts src/server.ts --format cjs,esm --dts",
|
|
62
|
-
"dev": "NODE_OPTIONS=--max-old-space-size=4096 tsup src/index.ts src/server.ts --format cjs,esm --watch --dts",
|
|
63
|
-
"lint": "eslint src/**/*.ts",
|
|
64
|
-
"test": "vitest"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
68
|
+
"description": "Core engine and schema handling for Dyrected CMS"
|
|
69
|
+
}
|
package/LICENSE.md
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
Business Source License 1.1
|
|
2
|
-
|
|
3
|
-
Parameters
|
|
4
|
-
|
|
5
|
-
Licensor: Dyrected
|
|
6
|
-
Licensed Work: Dyrected
|
|
7
|
-
Additional Use Grant: Production use and commercial use of the Licensed Work are permitted for your own internal business operations, your own websites and applications, and client websites or applications that you build or deploy on behalf of a client, provided that you do not, without a separate commercial license from the Licensor, (a) provide the Licensed Work as a hosted or managed service to third parties, (b) embed, white-label, sublicense, or otherwise redistribute the Licensed Work as part of a multi-tenant or customer-facing SaaS, platform, or developer service, or (c) enable third parties to create, manage, or operate independent projects, sites, workspaces, or environments using the Licensed Work as a material part of the service you provide.
|
|
8
|
-
Change Date: 2030-05-18
|
|
9
|
-
Change License: Apache License 2.0
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
Business Source License 1.1
|
|
14
|
-
|
|
15
|
-
License text copyright © 2024 MariaDB plc, All Rights Reserved.
|
|
16
|
-
“Business Source License” is a trademark of MariaDB plc.
|
|
17
|
-
|
|
18
|
-
### Terms
|
|
19
|
-
|
|
20
|
-
The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use.
|
|
21
|
-
|
|
22
|
-
Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate.
|
|
23
|
-
|
|
24
|
-
If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work.
|
|
25
|
-
|
|
26
|
-
All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor.
|
|
27
|
-
|
|
28
|
-
You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work.
|
|
29
|
-
|
|
30
|
-
Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work.
|
|
31
|
-
|
|
32
|
-
This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License).
|
|
33
|
-
|
|
34
|
-
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE.
|
|
35
|
-
|
|
36
|
-
MariaDB hereby grants you permission to use this License’s text to license your works, and to refer to it using the trademark “Business Source License”, as long as you comply with the Covenants of Licensor below.
|
|
37
|
-
|
|
38
|
-
### Covenants of Licensor
|
|
39
|
-
|
|
40
|
-
In consideration of the right to use this License’s text and the “Business Source License” name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor:
|
|
41
|
-
|
|
42
|
-
1. To specify as the Change License the GPL Version 2.0 or any later version, or a license that is compatible with GPL Version 2.0 or a later version, where “compatible” means that software provided under the Change License can be included in a program with software provided under GPL Version 2.0 or a later version. Licensor may specify additional Change Licenses without limitation.
|
|
43
|
-
|
|
44
|
-
2. To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text “None”.
|
|
45
|
-
|
|
46
|
-
3. Not to modify this License in any other way.
|
|
47
|
-
|
|
48
|
-
---
|
|
49
|
-
|
|
50
|
-
The Business Source License (this document, or the “License”) is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License.
|
package/dist/chunk-FRSJJO27.js
DELETED
|
@@ -1,519 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
attachRequestMetrics,
|
|
3
|
-
bindObservabilityRuntime,
|
|
4
|
-
buildRequestLogger,
|
|
5
|
-
captureRequestBody,
|
|
6
|
-
createObservabilityRuntime,
|
|
7
|
-
createRequestSpan,
|
|
8
|
-
endRequestSpan,
|
|
9
|
-
getConfigLogger,
|
|
10
|
-
getObservabilityRuntime,
|
|
11
|
-
getRequestLogger,
|
|
12
|
-
redactBody,
|
|
13
|
-
redactHeaders,
|
|
14
|
-
renderPrometheusMetrics,
|
|
15
|
-
resolveObservabilityConfig,
|
|
16
|
-
shouldSampleRequest
|
|
17
|
-
} from "./chunk-FRSJJO27.js";
|
|
18
|
-
import "./chunk-JSBRDJBE.js";
|
|
19
|
-
export {
|
|
20
|
-
attachRequestMetrics,
|
|
21
|
-
bindObservabilityRuntime,
|
|
22
|
-
buildRequestLogger,
|
|
23
|
-
captureRequestBody,
|
|
24
|
-
createObservabilityRuntime,
|
|
25
|
-
createRequestSpan,
|
|
26
|
-
endRequestSpan,
|
|
27
|
-
getConfigLogger,
|
|
28
|
-
getObservabilityRuntime,
|
|
29
|
-
getRequestLogger,
|
|
30
|
-
redactBody,
|
|
31
|
-
redactHeaders,
|
|
32
|
-
renderPrometheusMetrics,
|
|
33
|
-
resolveObservabilityConfig,
|
|
34
|
-
shouldSampleRequest
|
|
35
|
-
};
|