franchising 0.0.0-stage → 0.1.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/LICENSE +61 -0
- package/README.md +88 -2
- package/dist/accounts-B4bnTXUd.js +242 -0
- package/dist/accounts-B4bnTXUd.js.map +1 -0
- package/dist/accounts-TyMPSHM0.cjs +313 -0
- package/dist/accounts-TyMPSHM0.cjs.map +1 -0
- package/dist/adapters-DM59AJfR.d.cts +216 -0
- package/dist/adapters-DM59AJfR.d.ts +216 -0
- package/dist/cli/template-assets/api/src/errors/codes.ts +17 -0
- package/dist/cli/template-assets/api/src/errors/index.ts +16 -0
- package/dist/cli/template-assets/api/src/filters/exception.ts +53 -0
- package/dist/cli/template-assets/api/src/main.ts +189 -0
- package/dist/cli/template-assets/api/src/modules/health/controller.ts +23 -0
- package/dist/cli/template-assets/api/src/modules/health/dto/response.dto.ts +23 -0
- package/dist/cli/template-assets/api/src/modules/health/module.ts +12 -0
- package/dist/cli/template-assets/api/src/modules/health/service.ts +54 -0
- package/dist/cli/template-assets/api/src/modules/messaging/module.ts +7 -0
- package/dist/cli/template-assets/api/src/modules/messaging/service.ts +74 -0
- package/dist/cli/template-assets/api/src/shared/exceptions.ts +40 -0
- package/dist/cli/template-assets/api/src/shared/response.ts +31 -0
- package/dist/cli/template-assets/api/src/telemetry.ts +59 -0
- package/dist/cli/template-assets/api/src/types.ts +8 -0
- package/dist/cli/template-assets/cloudflare/cache.service.ts +23 -0
- package/dist/cli/template-assets/cloudflare/queue.service.ts +22 -0
- package/dist/cli/template-assets/cloudflare/worker.ts +33 -0
- package/dist/cli/template-assets/packages/jobs/src/index.ts +49 -0
- package/dist/cli/template-assets/packages/queue-runtime/src/index.ts +54 -0
- package/dist/cli/template-assets/root/dot-dockerignore +11 -0
- package/dist/cli/template-assets/root/dot-editorconfig +13 -0
- package/dist/cli/template-assets/root/dot-oxfmtrc.json +9 -0
- package/dist/cli/template-assets/root/dot-oxlintrc.json +47 -0
- package/dist/cli/template-assets/root/lefthook.yml +18 -0
- package/dist/cli/template-assets/web/postcss.config.mjs +9 -0
- package/dist/cli/template-assets/web/src/app/(app)/dashboard/page.tsx +5 -0
- package/dist/cli/template-assets/web/src/app/(app)/layout.tsx +25 -0
- package/dist/cli/template-assets/web/src/app/(auth)/login/page.tsx +75 -0
- package/dist/cli/template-assets/web/src/app/globals.css +48 -0
- package/dist/cli/template-assets/web/src/app/layout.tsx +30 -0
- package/dist/cli/template-assets/web/src/app/page.tsx +20 -0
- package/dist/cli/template-assets/web/src/components/overview/overview-panel.tsx +27 -0
- package/dist/cli/template-assets/web/src/components/shell/app-shell.tsx +37 -0
- package/dist/cli/template-assets/web/src/components/shell/providers.tsx +22 -0
- package/dist/cli/template-assets/web/src/lib/api-client.ts +57 -0
- package/dist/cli/template-assets/web/src/lib/auth-client.ts +15 -0
- package/dist/cli/template-assets/web/src/lib/server-auth.ts +38 -0
- package/dist/cli/template-assets/web/src/types/api.ts +18 -0
- package/dist/cli/template-assets/worker/src/app.module.ts +5 -0
- package/dist/cli/template-assets/worker/src/env.ts +8 -0
- package/dist/cli/template-assets/worker/src/main.ts +25 -0
- package/dist/cli/template-assets/worker/src/modules/queue/config.ts +16 -0
- package/dist/cli/template-assets/worker/src/modules/queue/module.ts +11 -0
- package/dist/cli/template-assets/worker/src/modules/queue/processor.ts +105 -0
- package/dist/cli/template-assets/worker/src/modules/queue/repository.ts +16 -0
- package/dist/cli/template-assets/worker/src/modules/queue/retry.ts +17 -0
- package/dist/cli/template-assets/worker/test/retry.test.ts.tpl +12 -0
- package/dist/cli/template-assets/worker-messaging/jobs-messaging.ts +12 -0
- package/dist/cli/template-assets/worker-messaging/messaging-api-service.ts +39 -0
- package/dist/cli/template-assets/worker-messaging/src/env.ts +14 -0
- package/dist/cli/template-assets/worker-messaging/src/modules/messaging/service.ts +59 -0
- package/dist/cli/template-assets/worker-messaging/src/modules/queue/module.ts +8 -0
- package/dist/cli/template-assets/worker-messaging/src/modules/queue/processor.ts +113 -0
- package/dist/cli.js +1218 -0
- package/dist/cn-BVOnrSza.js +1 -0
- package/dist/constants-C4iP0--i.js +38 -0
- package/dist/constants-C4iP0--i.js.map +1 -0
- package/dist/constants-rCR-LNSN.cjs +91 -0
- package/dist/constants-rCR-LNSN.cjs.map +1 -0
- package/dist/decorate-BR5FVX5x.js +14 -0
- package/dist/decorate-R-oqksAh.cjs +25 -0
- package/dist/decorateParam-BFJPEGKJ.cjs +13 -0
- package/dist/decorateParam-CKE7_J1s.js +8 -0
- package/dist/define-adapter-Cc0QrfKt.cjs +16 -0
- package/dist/define-adapter-Cc0QrfKt.cjs.map +1 -0
- package/dist/define-adapter-DRcXno3j.js +11 -0
- package/dist/define-adapter-DRcXno3j.js.map +1 -0
- package/dist/dispatcher-BsD7cO2w.cjs +84 -0
- package/dist/dispatcher-BsD7cO2w.cjs.map +1 -0
- package/dist/dispatcher-DA_l1Vkv.js +73 -0
- package/dist/dispatcher-DA_l1Vkv.js.map +1 -0
- package/dist/errors-DxHoXrLd.d.cts +46 -0
- package/dist/errors-DxHoXrLd.d.ts +46 -0
- package/dist/http/index.cjs +440 -0
- package/dist/http/index.cjs.map +1 -0
- package/dist/http/index.d.cts +257 -0
- package/dist/http/index.d.ts +257 -0
- package/dist/http/index.js +400 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/swagger.cjs +122 -0
- package/dist/http/swagger.cjs.map +1 -0
- package/dist/http/swagger.d.cts +59 -0
- package/dist/http/swagger.d.ts +59 -0
- package/dist/http/swagger.js +117 -0
- package/dist/http/swagger.js.map +1 -0
- package/dist/index-B3UOHsA9.d.ts +679 -0
- package/dist/index-Bdl0AiNM.d.ts +1262 -0
- package/dist/index-By8b4ksT.d.ts +25 -0
- package/dist/index-C1KmT8dQ.d.ts +174 -0
- package/dist/index-CYT1PFzo.d.cts +679 -0
- package/dist/index-Cj-10Q5F.d.cts +174 -0
- package/dist/index-DDSaLjXV.d.cts +25 -0
- package/dist/index-FTrYdHfq.d.cts +1262 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1 -0
- package/dist/label-BqmJoVE-.cjs +1 -0
- package/dist/label-XeChe1Bu.js +1 -0
- package/dist/messaging/index.cjs +1 -0
- package/dist/messaging/index.d.cts +1775 -0
- package/dist/messaging/index.d.ts +1775 -0
- package/dist/messaging/index.js +1 -0
- package/dist/misc-BpM_lMTx.js +1 -0
- package/dist/misc-DkqJOm1V.cjs +1 -0
- package/dist/operational-CnxdBwVG.d.cts +13 -0
- package/dist/operational-CnxdBwVG.d.ts +13 -0
- package/dist/redaction-DhuHTlHA.js +124 -0
- package/dist/redaction-DhuHTlHA.js.map +1 -0
- package/dist/redaction-uRIQbTwZ.cjs +171 -0
- package/dist/redaction-uRIQbTwZ.cjs.map +1 -0
- package/dist/rolldown-runtime-auK_WOaK.cjs +1 -0
- package/dist/rolldown-runtime-xuxB8TGW.cjs +32 -0
- package/dist/runtime-C95UYYLb.cjs +111 -0
- package/dist/runtime-C95UYYLb.cjs.map +1 -0
- package/dist/runtime-DG2oBFYw.js +88 -0
- package/dist/runtime-DG2oBFYw.js.map +1 -0
- package/dist/skills-C1uwr9Pp.js +4 -0
- package/dist/skills-DuE-XnBp.js +1 -0
- package/dist/sso/client.cjs +37 -0
- package/dist/sso/client.cjs.map +1 -0
- package/dist/sso/client.d.cts +90 -0
- package/dist/sso/client.d.ts +90 -0
- package/dist/sso/client.js +36 -0
- package/dist/sso/client.js.map +1 -0
- package/dist/sso/index.cjs +833 -0
- package/dist/sso/index.cjs.map +1 -0
- package/dist/sso/index.d.cts +3 -0
- package/dist/sso/index.d.ts +3 -0
- package/dist/sso/index.js +821 -0
- package/dist/sso/index.js.map +1 -0
- package/dist/sso/nestjs.cjs +169 -0
- package/dist/sso/nestjs.cjs.map +1 -0
- package/dist/sso/nestjs.d.cts +92 -0
- package/dist/sso/nestjs.d.ts +92 -0
- package/dist/sso/nestjs.js +151 -0
- package/dist/sso/nestjs.js.map +1 -0
- package/dist/sso/resource.cjs +124 -0
- package/dist/sso/resource.cjs.map +1 -0
- package/dist/sso/resource.d.cts +3 -0
- package/dist/sso/resource.d.ts +3 -0
- package/dist/sso/resource.js +114 -0
- package/dist/sso/resource.js.map +1 -0
- package/dist/status-CRvlFPIs.js +13 -0
- package/dist/status-CRvlFPIs.js.map +1 -0
- package/dist/status-DtK6OHtj.cjs +24 -0
- package/dist/status-DtK6OHtj.cjs.map +1 -0
- package/dist/team-CJVAg4HH.d.cts +157 -0
- package/dist/team-CJVAg4HH.d.ts +157 -0
- package/dist/telemetry/adapters.cjs +6 -0
- package/dist/telemetry/adapters.d.cts +3 -0
- package/dist/telemetry/adapters.d.ts +3 -0
- package/dist/telemetry/adapters.js +4 -0
- package/dist/telemetry/betterstack.cjs +74 -0
- package/dist/telemetry/betterstack.cjs.map +1 -0
- package/dist/telemetry/betterstack.d.cts +12 -0
- package/dist/telemetry/betterstack.d.ts +12 -0
- package/dist/telemetry/betterstack.js +71 -0
- package/dist/telemetry/betterstack.js.map +1 -0
- package/dist/telemetry/index.cjs +1237 -0
- package/dist/telemetry/index.cjs.map +1 -0
- package/dist/telemetry/index.d.cts +114 -0
- package/dist/telemetry/index.d.ts +114 -0
- package/dist/telemetry/index.js +1205 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/telemetry/posthog.cjs +72 -0
- package/dist/telemetry/posthog.cjs.map +1 -0
- package/dist/telemetry/posthog.d.cts +10 -0
- package/dist/telemetry/posthog.d.ts +10 -0
- package/dist/telemetry/posthog.js +71 -0
- package/dist/telemetry/posthog.js.map +1 -0
- package/dist/telemetry/register.cjs +6 -0
- package/dist/telemetry/register.d.cts +14 -0
- package/dist/telemetry/register.d.ts +14 -0
- package/dist/telemetry/register.js +3 -0
- package/dist/telemetry/testing.cjs +54 -0
- package/dist/telemetry/testing.cjs.map +1 -0
- package/dist/telemetry/testing.d.cts +15 -0
- package/dist/telemetry/testing.d.ts +15 -0
- package/dist/telemetry/testing.js +52 -0
- package/dist/telemetry/testing.js.map +1 -0
- package/dist/telemetry.mock-BSsx7L6S.cjs +45 -0
- package/dist/telemetry.mock-BSsx7L6S.cjs.map +1 -0
- package/dist/telemetry.mock-BZRpy63c.js +40 -0
- package/dist/telemetry.mock-BZRpy63c.js.map +1 -0
- package/dist/telemetry.mock-C1wXuf-M.d.cts +4 -0
- package/dist/telemetry.mock-CdaxofJ7.d.ts +4 -0
- package/dist/ui/chart.cjs +6 -0
- package/dist/ui/chart.d.cts +34 -0
- package/dist/ui/chart.d.ts +34 -0
- package/dist/ui/chart.js +6 -0
- package/dist/ui/form.cjs +1 -0
- package/dist/ui/form.d.cts +23 -0
- package/dist/ui/form.d.ts +23 -0
- package/dist/ui/form.js +1 -0
- package/dist/ui/hooks.cjs +1 -0
- package/dist/ui/hooks.d.cts +66 -0
- package/dist/ui/hooks.d.ts +66 -0
- package/dist/ui/hooks.js +1 -0
- package/dist/ui/index.cjs +1 -0
- package/dist/ui/index.d.cts +3 -0
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/index.js +1 -0
- package/dist/ui/styles.css +2 -0
- package/dist/ui/utils.cjs +1 -0
- package/dist/ui/utils.d.cts +199 -0
- package/dist/ui/utils.d.ts +199 -0
- package/dist/ui/utils.js +1 -0
- package/dist/use-mobile-BbeIMRgE.cjs +1 -0
- package/dist/use-mobile-Cdd4Lp7p.js +1 -0
- package/dist/use-mobile-ecpyXWz3.d.cts +10 -0
- package/dist/use-mobile-ecpyXWz3.d.ts +10 -0
- package/dist/verifier-BIBtPCVi.cjs +119 -0
- package/dist/verifier-BIBtPCVi.cjs.map +1 -0
- package/dist/verifier-DNdy5Ynw.js +90 -0
- package/dist/verifier-DNdy5Ynw.js.map +1 -0
- package/dist/version-CPunTYCY.js +1 -0
- package/dist/version-CfUi3iAV.cjs +1 -0
- package/package.json +523 -5
|
@@ -0,0 +1,1205 @@
|
|
|
1
|
+
import { n as TelemetryError, t as TelemetryAdapterDispatcher } from "../dispatcher-DA_l1Vkv.js";
|
|
2
|
+
import { i as readEnv, r as shutdownTelemetry, t as isTelemetryRegistered } from "../runtime-DG2oBFYw.js";
|
|
3
|
+
import { n as __decorateMetadata, t as __decorate } from "../decorate-BR5FVX5x.js";
|
|
4
|
+
import { t as __decorateParam } from "../decorateParam-CKE7_J1s.js";
|
|
5
|
+
import { a as sanitize, c as sanitizeText, i as isSensitiveKey, n as containsSensitiveText, o as sanitizeAttributes, r as isLikelyIdentifier, s as sanitizeError, t as DEFAULT_REDACT_PATHS } from "../redaction-DhuHTlHA.js";
|
|
6
|
+
import { t as createTelemetryMock } from "../telemetry.mock-BZRpy63c.js";
|
|
7
|
+
import { Catch, HttpException, Inject, Injectable, Module, Optional, RequestMethod } from "@nestjs/common";
|
|
8
|
+
import { APP_FILTER, APP_INTERCEPTOR, BaseExceptionFilter } from "@nestjs/core";
|
|
9
|
+
import pino from "pino";
|
|
10
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
|
+
import { SpanStatusCode, context, metrics, propagation, trace } from "@opentelemetry/api";
|
|
12
|
+
import { LoggerModule, PinoLogger } from "nestjs-pino";
|
|
13
|
+
import { SeverityNumber, logs } from "@opentelemetry/api-logs";
|
|
14
|
+
import { LogController } from "fastify";
|
|
15
|
+
import { Observable } from "rxjs";
|
|
16
|
+
import { finalize, tap } from "rxjs/operators";
|
|
17
|
+
|
|
18
|
+
// src/modules/telemetry/shared/context/context.ts
|
|
19
|
+
const storage = new AsyncLocalStorage();
|
|
20
|
+
function baseContext() {
|
|
21
|
+
const span = trace.getActiveSpan()?.spanContext();
|
|
22
|
+
return {
|
|
23
|
+
service: readEnv("OTEL_SERVICE_NAME", "SERVICE_NAME") ?? "unknown-service",
|
|
24
|
+
product: readEnv("TELEMETRY_PRODUCT"),
|
|
25
|
+
environment: readEnv("NODE_ENV") ?? "development",
|
|
26
|
+
release: readEnv("OTEL_SERVICE_VERSION", "RELEASE", "RAILWAY_DEPLOYMENT_ID", "GIT_SHA"),
|
|
27
|
+
traceId: span?.traceId,
|
|
28
|
+
spanId: span?.spanId,
|
|
29
|
+
...storage.getStore()
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
let TelemetryContext = class TelemetryContext {
|
|
33
|
+
logger;
|
|
34
|
+
constructor(logger) {
|
|
35
|
+
this.logger = logger;
|
|
36
|
+
}
|
|
37
|
+
get() {
|
|
38
|
+
return baseContext();
|
|
39
|
+
}
|
|
40
|
+
assign(context) {
|
|
41
|
+
const current = storage.getStore();
|
|
42
|
+
if (current) Object.assign(current, context);
|
|
43
|
+
try {
|
|
44
|
+
this.logger?.assign({
|
|
45
|
+
actor_id: context.actorId,
|
|
46
|
+
organization_id: context.organizationId,
|
|
47
|
+
job_id: context.jobId,
|
|
48
|
+
correlation_id: context.correlationId
|
|
49
|
+
});
|
|
50
|
+
} catch {}
|
|
51
|
+
}
|
|
52
|
+
identify(actorId) {
|
|
53
|
+
this.assign({ actorId });
|
|
54
|
+
}
|
|
55
|
+
organization(organizationId) {
|
|
56
|
+
this.assign({ organizationId });
|
|
57
|
+
}
|
|
58
|
+
run(context, fn) {
|
|
59
|
+
return storage.run({
|
|
60
|
+
...storage.getStore(),
|
|
61
|
+
...context
|
|
62
|
+
}, fn);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
TelemetryContext = __decorate([
|
|
66
|
+
Injectable(),
|
|
67
|
+
__decorateParam(0, Optional()),
|
|
68
|
+
__decorateParam(0, Inject(PinoLogger)),
|
|
69
|
+
__decorateMetadata("design:paramtypes", [Object])
|
|
70
|
+
], TelemetryContext);
|
|
71
|
+
|
|
72
|
+
// src/modules/telemetry/shared/logging/otel.ts
|
|
73
|
+
const otelLogger = logs.getLogger("@rockdev/telemetry", void 0, { includeTraceContext: true });
|
|
74
|
+
function severity(level) {
|
|
75
|
+
if (level >= 60) return {
|
|
76
|
+
text: "fatal",
|
|
77
|
+
number: SeverityNumber.FATAL
|
|
78
|
+
};
|
|
79
|
+
if (level >= 50) return {
|
|
80
|
+
text: "error",
|
|
81
|
+
number: SeverityNumber.ERROR
|
|
82
|
+
};
|
|
83
|
+
if (level >= 40) return {
|
|
84
|
+
text: "warn",
|
|
85
|
+
number: SeverityNumber.WARN
|
|
86
|
+
};
|
|
87
|
+
if (level >= 30) return {
|
|
88
|
+
text: "info",
|
|
89
|
+
number: SeverityNumber.INFO
|
|
90
|
+
};
|
|
91
|
+
if (level >= 20) return {
|
|
92
|
+
text: "debug",
|
|
93
|
+
number: SeverityNumber.DEBUG
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
text: "trace",
|
|
97
|
+
number: SeverityNumber.TRACE
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function toAnyValue(value) {
|
|
101
|
+
if (value instanceof Error) {
|
|
102
|
+
const safeError = sanitizeError(value);
|
|
103
|
+
return toAnyValue({
|
|
104
|
+
name: safeError.name,
|
|
105
|
+
message: safeError.message,
|
|
106
|
+
...safeError.stack ? { stack: safeError.stack } : {}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (typeof value === "string") return sanitizeText(value);
|
|
110
|
+
if (Array.isArray(value)) return value.map(toAnyValue);
|
|
111
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, isSensitiveKey(key) ? "[REDACTED]" : toAnyValue(child)]));
|
|
112
|
+
if (typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) return value;
|
|
113
|
+
return String(value);
|
|
114
|
+
}
|
|
115
|
+
function toLogRecord(args, level) {
|
|
116
|
+
const [first, second, ...rest] = args;
|
|
117
|
+
const body = {};
|
|
118
|
+
if (first && typeof first === "object" && !Array.isArray(first)) {
|
|
119
|
+
const safe = toAnyValue(first);
|
|
120
|
+
if (safe && typeof safe === "object" && !Array.isArray(safe)) Object.assign(body, safe);
|
|
121
|
+
if (second !== void 0 && typeof second !== "string") body.arguments = toAnyValue(second);
|
|
122
|
+
} else if (first !== void 0) body.message = typeof first === "string" ? sanitizeText(first) : toAnyValue(first);
|
|
123
|
+
if (typeof second === "string") body.message = sanitizeText(second);
|
|
124
|
+
if (rest.length > 0) body.arguments = toAnyValue(rest);
|
|
125
|
+
const currentSeverity = severity(level);
|
|
126
|
+
return {
|
|
127
|
+
body,
|
|
128
|
+
severityNumber: currentSeverity.number,
|
|
129
|
+
severityText: currentSeverity.text,
|
|
130
|
+
...typeof body.event === "string" ? { eventName: body.event } : {}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function emitPinoLog(args, level) {
|
|
134
|
+
if (process.env.TELEMETRY_ENABLED === "false" || process.env.TELEMETRY_LOGS_ENABLED === "false") return;
|
|
135
|
+
try {
|
|
136
|
+
otelLogger.emit(toLogRecord(args, level));
|
|
137
|
+
} catch {}
|
|
138
|
+
}
|
|
139
|
+
function createTelemetryPinoHooks() {
|
|
140
|
+
return { logMethod(args, method, level) {
|
|
141
|
+
emitPinoLog(args, level);
|
|
142
|
+
method.apply(this, args);
|
|
143
|
+
} };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// src/modules/telemetry/shared/state/operational.ts
|
|
147
|
+
let TelemetryOperationalState = class TelemetryOperationalState {
|
|
148
|
+
signals = new Map([
|
|
149
|
+
"errors",
|
|
150
|
+
"events",
|
|
151
|
+
"logs",
|
|
152
|
+
"metrics",
|
|
153
|
+
"tracing"
|
|
154
|
+
].map((signal) => [signal, {
|
|
155
|
+
failed: false,
|
|
156
|
+
failureCount: 0
|
|
157
|
+
}]));
|
|
158
|
+
recordFailure(signal) {
|
|
159
|
+
const current = this.signals.get(signal);
|
|
160
|
+
if (!current) return;
|
|
161
|
+
current.failed = true;
|
|
162
|
+
current.failureCount += 1;
|
|
163
|
+
current.lastFailureAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
164
|
+
}
|
|
165
|
+
status(signal) {
|
|
166
|
+
return { ...this.signals.get(signal) ?? {
|
|
167
|
+
failed: false,
|
|
168
|
+
failureCount: 0
|
|
169
|
+
} };
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
TelemetryOperationalState = __decorate([Injectable()], TelemetryOperationalState);
|
|
173
|
+
|
|
174
|
+
// src/modules/telemetry/types/telemetry.ts
|
|
175
|
+
var Telemetry = class {};
|
|
176
|
+
|
|
177
|
+
// src/modules/telemetry/core/checked-telemetry.ts
|
|
178
|
+
var CheckedTelemetry = class extends Telemetry {
|
|
179
|
+
target;
|
|
180
|
+
adapters;
|
|
181
|
+
options;
|
|
182
|
+
constructor(target, adapters, options = {}) {
|
|
183
|
+
super();
|
|
184
|
+
this.target = target;
|
|
185
|
+
this.adapters = adapters;
|
|
186
|
+
this.options = options;
|
|
187
|
+
}
|
|
188
|
+
debug(event, attributes) {
|
|
189
|
+
this.require("logs", "telemetry.debug()");
|
|
190
|
+
this.target.debug(event, attributes);
|
|
191
|
+
}
|
|
192
|
+
info(event, attributes) {
|
|
193
|
+
this.require("logs", "telemetry.info()");
|
|
194
|
+
this.target.info(event, attributes);
|
|
195
|
+
}
|
|
196
|
+
warn(event, attributes) {
|
|
197
|
+
this.require("logs", "telemetry.warn()");
|
|
198
|
+
this.target.warn(event, attributes);
|
|
199
|
+
}
|
|
200
|
+
error(event, error, attributes) {
|
|
201
|
+
this.require("logs", "telemetry.error()");
|
|
202
|
+
this.target.error(event, error, attributes);
|
|
203
|
+
}
|
|
204
|
+
event(event, attributes) {
|
|
205
|
+
this.require("events", "telemetry.event()");
|
|
206
|
+
this.target.event(event, attributes);
|
|
207
|
+
}
|
|
208
|
+
captureException(error, attributes, options) {
|
|
209
|
+
this.require("errors", "telemetry.captureException()");
|
|
210
|
+
this.target.captureException(error, attributes, options);
|
|
211
|
+
}
|
|
212
|
+
async span(name, fn, attributes) {
|
|
213
|
+
this.require("tracing", "telemetry.span()");
|
|
214
|
+
return this.target.span(name, fn, attributes);
|
|
215
|
+
}
|
|
216
|
+
increment(name, value, attributes) {
|
|
217
|
+
this.require("metrics", "telemetry.increment()");
|
|
218
|
+
this.target.increment(name, value, attributes);
|
|
219
|
+
}
|
|
220
|
+
histogram(name, value, attributes) {
|
|
221
|
+
this.require("metrics", "telemetry.histogram()");
|
|
222
|
+
this.target.histogram(name, value, attributes);
|
|
223
|
+
}
|
|
224
|
+
async trackDependency(options, fn) {
|
|
225
|
+
this.requireDependency("telemetry.trackDependency()");
|
|
226
|
+
return this.target.trackDependency(options, fn);
|
|
227
|
+
}
|
|
228
|
+
runJob(options, fn) {
|
|
229
|
+
try {
|
|
230
|
+
this.requireDependency("telemetry.runJob()");
|
|
231
|
+
} catch (error) {
|
|
232
|
+
return Promise.reject(error);
|
|
233
|
+
}
|
|
234
|
+
return this.target.runJob(options, fn);
|
|
235
|
+
}
|
|
236
|
+
async fetch(input, init, options) {
|
|
237
|
+
this.requireDependency("telemetry.fetch()");
|
|
238
|
+
return this.target.fetch(input, init, options);
|
|
239
|
+
}
|
|
240
|
+
traceparent() {
|
|
241
|
+
return this.target.traceparent();
|
|
242
|
+
}
|
|
243
|
+
getContext() {
|
|
244
|
+
return this.target.getContext();
|
|
245
|
+
}
|
|
246
|
+
shutdown() {
|
|
247
|
+
return this.target.shutdown();
|
|
248
|
+
}
|
|
249
|
+
disabled(capability) {
|
|
250
|
+
if (process.env.TELEMETRY_ENABLED === "false") return true;
|
|
251
|
+
if (capability === "metrics") return process.env.TELEMETRY_METRICS_ENABLED === "false";
|
|
252
|
+
if (capability === "tracing") return this.options.tracing === false || process.env.TELEMETRY_TRACING_ENABLED === "false";
|
|
253
|
+
if (capability === "errors") return this.options.errors === false;
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
require(capability, operation) {
|
|
257
|
+
if (this.disabled(capability)) return;
|
|
258
|
+
this.adapters.assertSupports(capability, operation);
|
|
259
|
+
}
|
|
260
|
+
requireDependency(operation) {
|
|
261
|
+
this.require("tracing", operation);
|
|
262
|
+
this.require("metrics", operation);
|
|
263
|
+
this.require("errors", operation);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// src/modules/telemetry/shared/context/adapter.ts
|
|
268
|
+
function adapterContext(context) {
|
|
269
|
+
return sanitizeAttributes({ ...context });
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// src/modules/telemetry/shared/di/tokens.ts
|
|
273
|
+
const TELEMETRY_OPTIONS = Symbol("TELEMETRY_OPTIONS");
|
|
274
|
+
|
|
275
|
+
// src/modules/telemetry/shared/utils/case.ts
|
|
276
|
+
function snakeCase(value) {
|
|
277
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[.-]/g, "_").toLowerCase();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/modules/telemetry/shared/logging/attributes.ts
|
|
281
|
+
function logAttributes(context, attributes) {
|
|
282
|
+
const record = {};
|
|
283
|
+
for (const [key, value] of Object.entries({
|
|
284
|
+
requestId: context.requestId,
|
|
285
|
+
traceId: context.traceId,
|
|
286
|
+
spanId: context.spanId,
|
|
287
|
+
service: context.service,
|
|
288
|
+
product: context.product,
|
|
289
|
+
environment: context.environment,
|
|
290
|
+
release: context.release,
|
|
291
|
+
actorId: context.actorId,
|
|
292
|
+
organizationId: context.organizationId,
|
|
293
|
+
jobId: context.jobId,
|
|
294
|
+
correlationId: context.correlationId,
|
|
295
|
+
...attributes
|
|
296
|
+
})) if (value !== void 0) record[snakeCase(key)] = value;
|
|
297
|
+
return sanitizeAttributes(record);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/modules/telemetry/shared/metrics/instruments.ts
|
|
301
|
+
const meter = metrics.getMeter("@rockdev/telemetry");
|
|
302
|
+
const counters = /* @__PURE__ */ new Map();
|
|
303
|
+
const histograms = /* @__PURE__ */ new Map();
|
|
304
|
+
|
|
305
|
+
// src/modules/telemetry/shared/metrics/metrics.ts
|
|
306
|
+
const ALLOWED_METRIC_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
307
|
+
"service",
|
|
308
|
+
"route",
|
|
309
|
+
"method",
|
|
310
|
+
"status",
|
|
311
|
+
"statusCode",
|
|
312
|
+
"statusClass",
|
|
313
|
+
"result",
|
|
314
|
+
"dependency",
|
|
315
|
+
"operation",
|
|
316
|
+
"queue",
|
|
317
|
+
"job",
|
|
318
|
+
"product",
|
|
319
|
+
"channel",
|
|
320
|
+
"provider",
|
|
321
|
+
"priority",
|
|
322
|
+
"reason"
|
|
323
|
+
]);
|
|
324
|
+
function validValue(key, value) {
|
|
325
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 100) return false;
|
|
326
|
+
if (containsSensitiveText(value) || isLikelyIdentifier(value)) return false;
|
|
327
|
+
if (key === "route") return (value === "unmatched" || value.startsWith("/")) && !value.includes("?") && !value.includes("://") && !value.split("/").some((segment) => /^\d+$/.test(segment));
|
|
328
|
+
if (key === "method") return /^[A-Z]+$/.test(value);
|
|
329
|
+
if (key === "statusCode") return /^[1-5]\d\d$/.test(value);
|
|
330
|
+
if (key === "statusClass") return /^[1-5]xx$/.test(value);
|
|
331
|
+
if (key === "result") return value === "success" || value === "error";
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
function sanitizeMetricAttributes(attributes) {
|
|
335
|
+
if (!attributes) return {
|
|
336
|
+
attributes: void 0,
|
|
337
|
+
rejectedKeys: []
|
|
338
|
+
};
|
|
339
|
+
const valid = {};
|
|
340
|
+
const rejectedKeys = [];
|
|
341
|
+
for (const [rawKey, value] of Object.entries(attributes)) {
|
|
342
|
+
const key = rawKey;
|
|
343
|
+
if (!ALLOWED_METRIC_ATTRIBUTES.has(key) || isSensitiveKey(rawKey) || !validValue(key, value)) {
|
|
344
|
+
rejectedKeys.push(rawKey);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
valid[key] = value;
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
attributes: valid,
|
|
351
|
+
rejectedKeys
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/modules/telemetry/core/telemetry.service.ts
|
|
356
|
+
let TelemetryService = class TelemetryService extends Telemetry {
|
|
357
|
+
logger;
|
|
358
|
+
context;
|
|
359
|
+
operationalState;
|
|
360
|
+
options;
|
|
361
|
+
adapters;
|
|
362
|
+
constructor(logger, context, operationalState, options = {}, adapters) {
|
|
363
|
+
super();
|
|
364
|
+
this.logger = logger;
|
|
365
|
+
this.context = context;
|
|
366
|
+
this.operationalState = operationalState;
|
|
367
|
+
this.options = options;
|
|
368
|
+
this.adapters = adapters;
|
|
369
|
+
}
|
|
370
|
+
debug(event, attributes) {
|
|
371
|
+
this.write("debug", event, attributes);
|
|
372
|
+
}
|
|
373
|
+
info(event, attributes) {
|
|
374
|
+
this.write("info", event, attributes);
|
|
375
|
+
}
|
|
376
|
+
warn(event, attributes) {
|
|
377
|
+
this.write("warn", event, attributes);
|
|
378
|
+
}
|
|
379
|
+
error(event, error, attributes) {
|
|
380
|
+
this.writeError(event, error, attributes);
|
|
381
|
+
}
|
|
382
|
+
event(event, attributes) {
|
|
383
|
+
try {
|
|
384
|
+
const context = this.getContext();
|
|
385
|
+
const safeAttributes = logAttributes(context, attributes);
|
|
386
|
+
this.logger.info({
|
|
387
|
+
event,
|
|
388
|
+
event_kind: "product",
|
|
389
|
+
...safeAttributes
|
|
390
|
+
});
|
|
391
|
+
this.adapters?.captureEvent?.({
|
|
392
|
+
event,
|
|
393
|
+
attributes: safeAttributes,
|
|
394
|
+
context: adapterContext(context)
|
|
395
|
+
});
|
|
396
|
+
} catch {
|
|
397
|
+
this.operationalState.recordFailure("events");
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
captureException(error, attributes, options) {
|
|
401
|
+
this.error("exception.captured", error, attributes);
|
|
402
|
+
if (this.options.errors === false) return;
|
|
403
|
+
const reportError = sanitizeError(error, process.env.TELEMETRY_ERROR_STACK_ENABLED !== "false");
|
|
404
|
+
const reportContext = {
|
|
405
|
+
request: sanitizeAttributes({ ...this.getContext() }),
|
|
406
|
+
attributes: sanitizeAttributes(attributes)
|
|
407
|
+
};
|
|
408
|
+
this.adapters?.captureException?.({
|
|
409
|
+
error: reportError,
|
|
410
|
+
context: reportContext,
|
|
411
|
+
...options?.fingerprint ? { fingerprint: options.fingerprint } : {}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
async span(name, fn, attributes = {}) {
|
|
415
|
+
if (this.options.tracing === false || process.env.TELEMETRY_TRACING_ENABLED === "false") return fn({
|
|
416
|
+
setAttributes() {},
|
|
417
|
+
addEvent() {}
|
|
418
|
+
});
|
|
419
|
+
try {
|
|
420
|
+
return trace.getTracer(this.getContext().service).startActiveSpan(name, { attributes: sanitizeAttributes(attributes) }, async (span) => {
|
|
421
|
+
const startedAt = performance.now();
|
|
422
|
+
const events = [];
|
|
423
|
+
let spanError;
|
|
424
|
+
let spanAttributes = sanitizeAttributes(attributes);
|
|
425
|
+
const facade = {
|
|
426
|
+
setAttributes: (values) => {
|
|
427
|
+
const safe = sanitizeAttributes(values);
|
|
428
|
+
spanAttributes = {
|
|
429
|
+
...spanAttributes,
|
|
430
|
+
...safe
|
|
431
|
+
};
|
|
432
|
+
span.setAttributes(safe);
|
|
433
|
+
},
|
|
434
|
+
addEvent: (event, values) => {
|
|
435
|
+
const safe = sanitizeAttributes(values);
|
|
436
|
+
events.push({
|
|
437
|
+
name: event,
|
|
438
|
+
attributes: safe
|
|
439
|
+
});
|
|
440
|
+
span.addEvent(event, safe);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
try {
|
|
444
|
+
return await fn(facade);
|
|
445
|
+
} catch (error) {
|
|
446
|
+
spanError = sanitizeError(error);
|
|
447
|
+
try {
|
|
448
|
+
span.recordException(sanitizeError(error));
|
|
449
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
450
|
+
} catch {
|
|
451
|
+
this.operationalState.recordFailure("tracing");
|
|
452
|
+
}
|
|
453
|
+
throw error;
|
|
454
|
+
} finally {
|
|
455
|
+
this.adapters?.captureSpan?.({
|
|
456
|
+
name,
|
|
457
|
+
durationMs: performance.now() - startedAt,
|
|
458
|
+
attributes: spanAttributes,
|
|
459
|
+
events,
|
|
460
|
+
context: adapterContext(this.getContext()),
|
|
461
|
+
...spanError ? { error: spanError } : {}
|
|
462
|
+
});
|
|
463
|
+
try {
|
|
464
|
+
span.end();
|
|
465
|
+
} catch {
|
|
466
|
+
this.operationalState.recordFailure("tracing");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
} catch {
|
|
471
|
+
this.operationalState.recordFailure("tracing");
|
|
472
|
+
return fn({
|
|
473
|
+
setAttributes() {},
|
|
474
|
+
addEvent() {}
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
increment(name, value = 1, attributes) {
|
|
479
|
+
if (process.env.TELEMETRY_METRICS_ENABLED === "false") return;
|
|
480
|
+
const sanitized = sanitizeMetricAttributes(attributes);
|
|
481
|
+
this.warnRejectedMetricAttributes(name, sanitized.rejectedKeys);
|
|
482
|
+
try {
|
|
483
|
+
const instrument = counters.get(name) ?? meter.createCounter(name);
|
|
484
|
+
counters.set(name, instrument);
|
|
485
|
+
instrument.add(value, sanitized.attributes);
|
|
486
|
+
this.adapters?.captureMetric?.({
|
|
487
|
+
type: "counter",
|
|
488
|
+
name,
|
|
489
|
+
value,
|
|
490
|
+
attributes: sanitized.attributes ?? {},
|
|
491
|
+
context: adapterContext(this.getContext())
|
|
492
|
+
});
|
|
493
|
+
} catch {
|
|
494
|
+
this.operationalState.recordFailure("metrics");
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
histogram(name, value, attributes) {
|
|
498
|
+
if (process.env.TELEMETRY_METRICS_ENABLED === "false") return;
|
|
499
|
+
const sanitized = sanitizeMetricAttributes(attributes);
|
|
500
|
+
this.warnRejectedMetricAttributes(name, sanitized.rejectedKeys);
|
|
501
|
+
try {
|
|
502
|
+
const instrument = histograms.get(name) ?? meter.createHistogram(name, { unit: "ms" });
|
|
503
|
+
histograms.set(name, instrument);
|
|
504
|
+
instrument.record(value, sanitized.attributes);
|
|
505
|
+
this.adapters?.captureMetric?.({
|
|
506
|
+
type: "histogram",
|
|
507
|
+
name,
|
|
508
|
+
value,
|
|
509
|
+
attributes: sanitized.attributes ?? {},
|
|
510
|
+
context: adapterContext(this.getContext())
|
|
511
|
+
});
|
|
512
|
+
} catch {
|
|
513
|
+
this.operationalState.recordFailure("metrics");
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
async trackDependency(options, fn) {
|
|
517
|
+
const startedAt = performance.now();
|
|
518
|
+
try {
|
|
519
|
+
const result = await this.span(`dependency.${options.name}.${options.operation}`, async () => fn(), {
|
|
520
|
+
"dependency.name": options.name,
|
|
521
|
+
"operation.name": options.operation
|
|
522
|
+
});
|
|
523
|
+
this.increment("rock_dependency_requests_total", 1, {
|
|
524
|
+
dependency: options.name,
|
|
525
|
+
operation: options.operation,
|
|
526
|
+
result: "success"
|
|
527
|
+
});
|
|
528
|
+
return result;
|
|
529
|
+
} catch (error) {
|
|
530
|
+
this.increment("rock_dependency_requests_total", 1, {
|
|
531
|
+
dependency: options.name,
|
|
532
|
+
operation: options.operation,
|
|
533
|
+
result: "error"
|
|
534
|
+
});
|
|
535
|
+
this.captureException(error, {
|
|
536
|
+
dependency: options.name,
|
|
537
|
+
operation: options.operation
|
|
538
|
+
});
|
|
539
|
+
throw error;
|
|
540
|
+
} finally {
|
|
541
|
+
this.histogram("rock_dependency_request_duration_ms", performance.now() - startedAt, {
|
|
542
|
+
dependency: options.name,
|
|
543
|
+
operation: options.operation
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
runJob(options, fn) {
|
|
548
|
+
const run = () => this.context.run({
|
|
549
|
+
...options.jobId ? { jobId: options.jobId } : {},
|
|
550
|
+
...options.correlationId ? { correlationId: options.correlationId } : {}
|
|
551
|
+
}, () => this.trackDependency({
|
|
552
|
+
name: `queue.${options.queue}`,
|
|
553
|
+
operation: options.job
|
|
554
|
+
}, fn));
|
|
555
|
+
if (!options.traceparent) return run();
|
|
556
|
+
const parent = propagation.extract(context.active(), { traceparent: options.traceparent });
|
|
557
|
+
return context.with(parent, run);
|
|
558
|
+
}
|
|
559
|
+
async fetch(input, init, options) {
|
|
560
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
561
|
+
let host = "unknown";
|
|
562
|
+
let path = url;
|
|
563
|
+
try {
|
|
564
|
+
const parsed = new URL(url);
|
|
565
|
+
host = parsed.host;
|
|
566
|
+
path = parsed.pathname;
|
|
567
|
+
} catch {}
|
|
568
|
+
const method = (init?.method ?? (input instanceof Request ? input.method : void 0) ?? "GET").toUpperCase();
|
|
569
|
+
const dependency = options?.dependency ?? host;
|
|
570
|
+
const operation = options?.operation ?? `${method} ${path}`;
|
|
571
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : {}));
|
|
572
|
+
propagation.inject(context.active(), headers, { set: (carrier, key, value) => carrier.set(key, String(value)) });
|
|
573
|
+
return this.trackDependency({
|
|
574
|
+
name: dependency,
|
|
575
|
+
operation
|
|
576
|
+
}, () => this.span(`http.client.${dependency}`, async (span) => {
|
|
577
|
+
const response = await fetch(input, {
|
|
578
|
+
...init,
|
|
579
|
+
headers
|
|
580
|
+
});
|
|
581
|
+
span.setAttributes({ "http.response.status_code": response.status });
|
|
582
|
+
return response;
|
|
583
|
+
}, {
|
|
584
|
+
"http.request.method": method,
|
|
585
|
+
"server.address": host,
|
|
586
|
+
"url.path": path,
|
|
587
|
+
...options?.attributes
|
|
588
|
+
}));
|
|
589
|
+
}
|
|
590
|
+
traceparent() {
|
|
591
|
+
const carrier = {};
|
|
592
|
+
try {
|
|
593
|
+
propagation.inject(context.active(), carrier);
|
|
594
|
+
} catch {
|
|
595
|
+
this.operationalState.recordFailure("tracing");
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
return carrier.traceparent;
|
|
599
|
+
}
|
|
600
|
+
getContext() {
|
|
601
|
+
return this.context.get();
|
|
602
|
+
}
|
|
603
|
+
async shutdown() {
|
|
604
|
+
await Promise.allSettled([this.adapters?.shutdown() ?? Promise.resolve(), shutdownTelemetry().catch(() => {
|
|
605
|
+
this.operationalState.recordFailure("logs");
|
|
606
|
+
this.operationalState.recordFailure("metrics");
|
|
607
|
+
this.operationalState.recordFailure("tracing");
|
|
608
|
+
})]);
|
|
609
|
+
}
|
|
610
|
+
write(level, event, attributes) {
|
|
611
|
+
try {
|
|
612
|
+
const context = this.getContext();
|
|
613
|
+
const safeAttributes = logAttributes(context, attributes);
|
|
614
|
+
this.logger[level]({
|
|
615
|
+
event,
|
|
616
|
+
...safeAttributes
|
|
617
|
+
});
|
|
618
|
+
this.adapters?.captureLog?.({
|
|
619
|
+
level,
|
|
620
|
+
event,
|
|
621
|
+
attributes: safeAttributes,
|
|
622
|
+
context: adapterContext(context)
|
|
623
|
+
});
|
|
624
|
+
} catch {
|
|
625
|
+
this.operationalState.recordFailure("logs");
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
writeError(event, error, attributes) {
|
|
629
|
+
try {
|
|
630
|
+
const context = this.getContext();
|
|
631
|
+
const safeAttributes = logAttributes(context, attributes);
|
|
632
|
+
this.logger.error({
|
|
633
|
+
event,
|
|
634
|
+
err: sanitizeError(error),
|
|
635
|
+
...safeAttributes
|
|
636
|
+
});
|
|
637
|
+
this.adapters?.captureLog?.({
|
|
638
|
+
level: "error",
|
|
639
|
+
event,
|
|
640
|
+
attributes: safeAttributes,
|
|
641
|
+
context: adapterContext(context)
|
|
642
|
+
});
|
|
643
|
+
} catch {
|
|
644
|
+
this.operationalState.recordFailure("logs");
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
warnRejectedMetricAttributes(metricName, rejectedKeys) {
|
|
648
|
+
if (rejectedKeys.length === 0) return;
|
|
649
|
+
this.write("warn", "telemetry.metric_attributes_rejected", {
|
|
650
|
+
metricName,
|
|
651
|
+
rejectedAttributeKeys: rejectedKeys
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
TelemetryService = __decorate([
|
|
656
|
+
Injectable(),
|
|
657
|
+
__decorateParam(0, Inject(PinoLogger)),
|
|
658
|
+
__decorateParam(1, Inject(TelemetryContext)),
|
|
659
|
+
__decorateParam(2, Inject(TelemetryOperationalState)),
|
|
660
|
+
__decorateParam(3, Inject(TELEMETRY_OPTIONS)),
|
|
661
|
+
__decorateParam(4, Inject(TelemetryAdapterDispatcher)),
|
|
662
|
+
__decorateMetadata("design:paramtypes", [
|
|
663
|
+
Object,
|
|
664
|
+
typeof TelemetryContext === "undefined" ? Object : TelemetryContext,
|
|
665
|
+
typeof TelemetryOperationalState === "undefined" ? Object : TelemetryOperationalState,
|
|
666
|
+
Object,
|
|
667
|
+
typeof TelemetryAdapterDispatcher === "undefined" ? Object : TelemetryAdapterDispatcher
|
|
668
|
+
])
|
|
669
|
+
], TelemetryService);
|
|
670
|
+
|
|
671
|
+
// src/modules/telemetry/core/create-telemetry.ts
|
|
672
|
+
function createTelemetry(options = {}) {
|
|
673
|
+
const context = new TelemetryContext();
|
|
674
|
+
const operationalState = new TelemetryOperationalState();
|
|
675
|
+
const logger = pino({
|
|
676
|
+
hooks: createTelemetryPinoHooks(),
|
|
677
|
+
level: process.env.LOG_LEVEL ?? "info",
|
|
678
|
+
redact: {
|
|
679
|
+
paths: DEFAULT_REDACT_PATHS,
|
|
680
|
+
censor: "[REDACTED]"
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
const adapters = new TelemetryAdapterDispatcher(options.adapters ?? [], operationalState);
|
|
684
|
+
return new CheckedTelemetry(new TelemetryService(logger, context, operationalState, options, adapters), adapters, options);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// src/modules/telemetry/shared/tracing/span.ts
|
|
688
|
+
function recordError(span, error) {
|
|
689
|
+
try {
|
|
690
|
+
span.recordException(sanitizeError(error));
|
|
691
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
692
|
+
} catch {}
|
|
693
|
+
}
|
|
694
|
+
function endSpan(span) {
|
|
695
|
+
try {
|
|
696
|
+
span.end();
|
|
697
|
+
} catch {}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// src/modules/telemetry/shared/utils/promise.ts
|
|
701
|
+
function isPromiseLike(value) {
|
|
702
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// src/modules/telemetry/core/trace.decorator.ts
|
|
706
|
+
function Trace(name, attributes = {}) {
|
|
707
|
+
return (_target, _propertyKey, descriptor) => {
|
|
708
|
+
const typedDescriptor = descriptor;
|
|
709
|
+
const original = typedDescriptor.value;
|
|
710
|
+
if (!original) return;
|
|
711
|
+
typedDescriptor.value = function traced(...args) {
|
|
712
|
+
const invoke = () => original.apply(this, args);
|
|
713
|
+
if (process.env.TELEMETRY_TRACING_ENABLED === "false") return invoke();
|
|
714
|
+
try {
|
|
715
|
+
return trace.getTracer(readEnv("OTEL_SERVICE_NAME") ?? "unknown-service").startActiveSpan(name, { attributes: sanitizeAttributes(attributes) }, (span) => {
|
|
716
|
+
try {
|
|
717
|
+
const result = invoke();
|
|
718
|
+
if (!isPromiseLike(result)) {
|
|
719
|
+
endSpan(span);
|
|
720
|
+
return result;
|
|
721
|
+
}
|
|
722
|
+
return result.then((value) => {
|
|
723
|
+
endSpan(span);
|
|
724
|
+
return value;
|
|
725
|
+
}, (error) => {
|
|
726
|
+
recordError(span, error);
|
|
727
|
+
endSpan(span);
|
|
728
|
+
throw error;
|
|
729
|
+
});
|
|
730
|
+
} catch (error) {
|
|
731
|
+
recordError(span, error);
|
|
732
|
+
endSpan(span);
|
|
733
|
+
throw error;
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
} catch {
|
|
737
|
+
return invoke();
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// src/modules/telemetry/nest/health/indicator.ts
|
|
744
|
+
let TelemetryHealthIndicator = class TelemetryHealthIndicator {
|
|
745
|
+
operationalState;
|
|
746
|
+
options = {};
|
|
747
|
+
status() {
|
|
748
|
+
const enabled = process.env.TELEMETRY_ENABLED !== "false";
|
|
749
|
+
return { telemetry: {
|
|
750
|
+
status: enabled ? "up" : "disabled",
|
|
751
|
+
errors: enabled && this.options.errors !== false && process.env.TELEMETRY_ERRORS_ENABLED !== "false",
|
|
752
|
+
events: enabled,
|
|
753
|
+
metrics: enabled && process.env.TELEMETRY_METRICS_ENABLED !== "false",
|
|
754
|
+
tracing: enabled && this.options.tracing !== false && process.env.TELEMETRY_TRACING_ENABLED !== "false",
|
|
755
|
+
otlpConfigured: Boolean(this.options.adapters?.some((adapter) => adapter.otlp?.())),
|
|
756
|
+
adapters: (this.options.adapters ?? []).map((adapter) => adapter.name),
|
|
757
|
+
signals: {
|
|
758
|
+
errors: this.operationalState.status("errors"),
|
|
759
|
+
events: this.operationalState.status("events"),
|
|
760
|
+
logs: this.operationalState.status("logs"),
|
|
761
|
+
metrics: this.operationalState.status("metrics"),
|
|
762
|
+
tracing: this.operationalState.status("tracing")
|
|
763
|
+
}
|
|
764
|
+
} };
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
__decorate([Inject(TelemetryOperationalState), __decorateMetadata("design:type", typeof TelemetryOperationalState === "undefined" ? Object : TelemetryOperationalState)], TelemetryHealthIndicator.prototype, "operationalState", void 0);
|
|
768
|
+
__decorate([Inject(TELEMETRY_OPTIONS), __decorateMetadata("design:type", Object)], TelemetryHealthIndicator.prototype, "options", void 0);
|
|
769
|
+
TelemetryHealthIndicator = __decorate([Injectable()], TelemetryHealthIndicator);
|
|
770
|
+
|
|
771
|
+
// src/modules/telemetry/shared/http/log.ts
|
|
772
|
+
function resolveHttpSuccessLogsEnabled(logging) {
|
|
773
|
+
if (logging?.httpSuccessLogs !== void 0) return logging.httpSuccessLogs;
|
|
774
|
+
if (process.env.TELEMETRY_HTTP_SUCCESS_LOGS_ENABLED === "false") return false;
|
|
775
|
+
if (process.env.TELEMETRY_HTTP_SUCCESS_LOGS_ENABLED === "true") return true;
|
|
776
|
+
return true;
|
|
777
|
+
}
|
|
778
|
+
function positiveFiniteNumber(value) {
|
|
779
|
+
return Number.isFinite(value) && value > 0 ? value : void 0;
|
|
780
|
+
}
|
|
781
|
+
function resolveHttpSlowRequestMs(logging) {
|
|
782
|
+
if (logging?.httpSlowRequestMs !== void 0) return positiveFiniteNumber(logging.httpSlowRequestMs);
|
|
783
|
+
const configured = process.env.TELEMETRY_HTTP_SLOW_REQUEST_MS;
|
|
784
|
+
if (configured === void 0 || configured.trim() === "") return void 0;
|
|
785
|
+
return positiveFiniteNumber(Number(configured));
|
|
786
|
+
}
|
|
787
|
+
function httpLogLevel(response, error, successLogsEnabled) {
|
|
788
|
+
const statusCode = response.statusCode ?? 500;
|
|
789
|
+
if (!error && !successLogsEnabled && statusCode < 400) return "silent";
|
|
790
|
+
if (error || statusCode >= 500) return "error";
|
|
791
|
+
if (statusCode >= 400) return "warn";
|
|
792
|
+
return "info";
|
|
793
|
+
}
|
|
794
|
+
function requestRoute(request) {
|
|
795
|
+
return request.route?.path ?? request.routerPath ?? request.routeOptions?.url ?? "unmatched";
|
|
796
|
+
}
|
|
797
|
+
function clientIp(request) {
|
|
798
|
+
return request.ip ?? request.socket?.remoteAddress;
|
|
799
|
+
}
|
|
800
|
+
function httpRequestLog(request) {
|
|
801
|
+
return {
|
|
802
|
+
request_id: typeof request.id === "string" ? request.id : void 0,
|
|
803
|
+
method: request.method,
|
|
804
|
+
route: requestRoute(request),
|
|
805
|
+
client_ip: clientIp(request)
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
function httpResponseLog(response) {
|
|
809
|
+
return { status_code: response.statusCode };
|
|
810
|
+
}
|
|
811
|
+
function httpCompletionLog(request, response, value, error = false) {
|
|
812
|
+
return {
|
|
813
|
+
event: error ? "request.failed" : "request.completed",
|
|
814
|
+
request_id: typeof request.id === "string" ? request.id : void 0,
|
|
815
|
+
method: request.method,
|
|
816
|
+
route: requestRoute(request),
|
|
817
|
+
client_ip: clientIp(request),
|
|
818
|
+
status_code: response.statusCode,
|
|
819
|
+
duration_ms: value?.responseTime
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// src/modules/telemetry/shared/http/request.ts
|
|
824
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
825
|
+
function resolveRequestId(headers, header = "x-request-id") {
|
|
826
|
+
const incoming = headers?.[header.toLowerCase()];
|
|
827
|
+
const value = Array.isArray(incoming) ? incoming[0] : incoming;
|
|
828
|
+
return typeof value === "string" && UUID_PATTERN.test(value) ? value : crypto.randomUUID();
|
|
829
|
+
}
|
|
830
|
+
function isRequestId(value) {
|
|
831
|
+
return typeof value === "string" && UUID_PATTERN.test(value);
|
|
832
|
+
}
|
|
833
|
+
function createTelemetryFastifyOptions(options = {}) {
|
|
834
|
+
const requestIdHeader = options.requestId?.header ?? "x-request-id";
|
|
835
|
+
const excludedLogPaths = new Set(options.logging?.excludePaths ?? []);
|
|
836
|
+
const httpSuccessLogsEnabled = resolveHttpSuccessLogsEnabled(options.logging);
|
|
837
|
+
return {
|
|
838
|
+
requestIdHeader,
|
|
839
|
+
logController: new TelemetryFastifyLogController({
|
|
840
|
+
disableRequestLogging: (request) => excludedLogPaths.has((request.url ?? "").split("?")[0] ?? ""),
|
|
841
|
+
requestIdLogLabel: "request_id"
|
|
842
|
+
}, httpSuccessLogsEnabled),
|
|
843
|
+
genReqId: (request) => isRequestId(request.id) ? request.id : resolveRequestId(request.headers, requestIdHeader),
|
|
844
|
+
logger: {
|
|
845
|
+
level: options.logging?.level ?? process.env.LOG_LEVEL ?? "info",
|
|
846
|
+
redact: {
|
|
847
|
+
paths: [...DEFAULT_REDACT_PATHS, ...options.logging?.redact ?? []],
|
|
848
|
+
censor: "[REDACTED]"
|
|
849
|
+
},
|
|
850
|
+
serializers: {
|
|
851
|
+
req: httpRequestLog,
|
|
852
|
+
res: httpResponseLog
|
|
853
|
+
},
|
|
854
|
+
customSuccessObject: httpCompletionLog,
|
|
855
|
+
customErrorObject: (request, response, value) => httpCompletionLog(request, response, value, true)
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
var TelemetryFastifyLogController = class extends LogController {
|
|
860
|
+
httpSuccessLogsEnabled;
|
|
861
|
+
constructor(options, httpSuccessLogsEnabled) {
|
|
862
|
+
super(options);
|
|
863
|
+
this.httpSuccessLogsEnabled = httpSuccessLogsEnabled;
|
|
864
|
+
}
|
|
865
|
+
incomingRequest(request) {
|
|
866
|
+
if (this.isLogDisabled(request) || !this.httpSuccessLogsEnabled) return;
|
|
867
|
+
request.log.info({ req: httpRequestLog(request) }, "incoming request");
|
|
868
|
+
}
|
|
869
|
+
requestCompleted(error, request, reply) {
|
|
870
|
+
if (this.isLogDisabled(request)) return;
|
|
871
|
+
const fields = httpCompletionLog(request, reply, { responseTime: reply.elapsedTime }, Boolean(error));
|
|
872
|
+
if (error) reply.log.error({
|
|
873
|
+
...fields,
|
|
874
|
+
err: sanitizeError(error)
|
|
875
|
+
}, "request errored");
|
|
876
|
+
else {
|
|
877
|
+
const level = httpLogLevel(reply, error, this.httpSuccessLogsEnabled);
|
|
878
|
+
if (level === "silent") return;
|
|
879
|
+
if (level === "error") reply.log.error(fields, "request completed");
|
|
880
|
+
else if (level === "warn") reply.log.warn(fields, "request completed");
|
|
881
|
+
else reply.log.info(fields, "request completed");
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
defaultErrorLog(error, request, reply) {
|
|
885
|
+
if (this.isLogDisabled(request)) return;
|
|
886
|
+
reply.log.error({
|
|
887
|
+
...httpCompletionLog(request, reply, void 0, true),
|
|
888
|
+
err: sanitizeError(error)
|
|
889
|
+
}, "request failed");
|
|
890
|
+
}
|
|
891
|
+
routeNotFound(request, reply) {
|
|
892
|
+
if (this.isLogDisabled(request)) return;
|
|
893
|
+
reply.log.info(httpCompletionLog(request, reply), "route not found");
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
// src/modules/telemetry/shared/http/error-response.ts
|
|
898
|
+
function defaultCode(statusCode) {
|
|
899
|
+
return {
|
|
900
|
+
400: "BAD_REQUEST",
|
|
901
|
+
401: "UNAUTHORIZED",
|
|
902
|
+
403: "FORBIDDEN",
|
|
903
|
+
404: "NOT_FOUND",
|
|
904
|
+
409: "CONFLICT",
|
|
905
|
+
422: "UNPROCESSABLE_ENTITY",
|
|
906
|
+
429: "RATE_LIMITED",
|
|
907
|
+
502: "UPSTREAM_ERROR"
|
|
908
|
+
}[statusCode] ?? (statusCode >= 500 ? "INTERNAL_ERROR" : "REQUEST_FAILED");
|
|
909
|
+
}
|
|
910
|
+
function defaultMessage(exception, statusCode) {
|
|
911
|
+
if (statusCode >= 500) return "Internal server error";
|
|
912
|
+
if (!(exception instanceof HttpException)) return "Request failed";
|
|
913
|
+
const response = exception.getResponse();
|
|
914
|
+
if (typeof response === "string") return response;
|
|
915
|
+
if (response && typeof response === "object" && "message" in response) {
|
|
916
|
+
const message = response.message;
|
|
917
|
+
return Array.isArray(message) ? message.join(". ") : String(message);
|
|
918
|
+
}
|
|
919
|
+
return exception.message;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
// src/modules/telemetry/nest/filters/exception.ts
|
|
923
|
+
let TelemetryExceptionFilter = class TelemetryExceptionFilter extends BaseExceptionFilter {
|
|
924
|
+
telemetry;
|
|
925
|
+
options;
|
|
926
|
+
constructor(telemetry, options) {
|
|
927
|
+
super();
|
|
928
|
+
this.telemetry = telemetry;
|
|
929
|
+
this.options = options;
|
|
930
|
+
}
|
|
931
|
+
catch(exception, host) {
|
|
932
|
+
const status = exception instanceof HttpException ? exception.getStatus() : 500;
|
|
933
|
+
if (status >= 500 || status === 429) this.telemetry.captureException(exception, { statusCode: status });
|
|
934
|
+
const requestId = this.telemetry.getContext().requestId;
|
|
935
|
+
const formatted = this.options.http?.mapException?.(exception, {
|
|
936
|
+
statusCode: status,
|
|
937
|
+
requestId
|
|
938
|
+
}) ?? {
|
|
939
|
+
code: defaultCode(status),
|
|
940
|
+
message: defaultMessage(exception, status)
|
|
941
|
+
};
|
|
942
|
+
const response = host.switchToHttp().getResponse();
|
|
943
|
+
const reply = response.status?.(status) ?? response.code?.(status) ?? response;
|
|
944
|
+
const body = { error: {
|
|
945
|
+
...formatted,
|
|
946
|
+
requestId
|
|
947
|
+
} };
|
|
948
|
+
if ("json" in response && typeof response.json === "function") response.json(body);
|
|
949
|
+
else if ("send" in response && typeof response.send === "function") response.send(body);
|
|
950
|
+
else if (typeof reply.send === "function") reply.send(body);
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
TelemetryExceptionFilter = __decorate([
|
|
954
|
+
Catch(),
|
|
955
|
+
__decorateParam(0, Inject(TelemetryService)),
|
|
956
|
+
__decorateParam(1, Inject(TELEMETRY_OPTIONS)),
|
|
957
|
+
__decorateMetadata("design:paramtypes", [typeof TelemetryService === "undefined" ? Object : TelemetryService, Object])
|
|
958
|
+
], TelemetryExceptionFilter);
|
|
959
|
+
|
|
960
|
+
// src/modules/telemetry/nest/interceptor.ts
|
|
961
|
+
let TelemetryHttpInterceptor = class TelemetryHttpInterceptor {
|
|
962
|
+
telemetry;
|
|
963
|
+
context;
|
|
964
|
+
options;
|
|
965
|
+
constructor(telemetry, context, options) {
|
|
966
|
+
this.telemetry = telemetry;
|
|
967
|
+
this.context = context;
|
|
968
|
+
this.options = options;
|
|
969
|
+
}
|
|
970
|
+
intercept(executionContext, next) {
|
|
971
|
+
if (executionContext.getType() !== "http") return next.handle();
|
|
972
|
+
const http = executionContext.switchToHttp();
|
|
973
|
+
const request = http.getRequest();
|
|
974
|
+
const response = http.getResponse();
|
|
975
|
+
const requestId = request.id;
|
|
976
|
+
const requestIdHeader = this.options.requestId?.header ?? "x-request-id";
|
|
977
|
+
const responseHeaders = response;
|
|
978
|
+
if (requestId) {
|
|
979
|
+
responseHeaders.setHeader?.(requestIdHeader, requestId);
|
|
980
|
+
responseHeaders.header?.(requestIdHeader, requestId);
|
|
981
|
+
}
|
|
982
|
+
const route = requestRoute(request);
|
|
983
|
+
const startedAt = performance.now();
|
|
984
|
+
const extracted = this.options.context?.extract?.(request);
|
|
985
|
+
if (extracted) this.context.assign(extracted);
|
|
986
|
+
const httpMetricsEnabled = this.options.metrics?.http !== false;
|
|
987
|
+
const httpSuccessLogsEnabled = resolveHttpSuccessLogsEnabled(this.options.logging);
|
|
988
|
+
const httpSlowRequestMs = resolveHttpSlowRequestMs(this.options.logging);
|
|
989
|
+
return new Observable((subscriber) => {
|
|
990
|
+
let streamFailed = false;
|
|
991
|
+
if (httpMetricsEnabled) this.telemetry.increment("rock_http_requests_active", 1, {
|
|
992
|
+
method: request.method,
|
|
993
|
+
route
|
|
994
|
+
});
|
|
995
|
+
return next.handle().pipe(tap({ error: () => streamFailed = true }), finalize(() => {
|
|
996
|
+
const statusCode = response.statusCode ?? 500;
|
|
997
|
+
const durationMs = performance.now() - startedAt;
|
|
998
|
+
const attributes = {
|
|
999
|
+
method: request.method,
|
|
1000
|
+
route,
|
|
1001
|
+
statusCode: String(statusCode),
|
|
1002
|
+
statusClass: `${Math.floor(statusCode / 100)}xx`
|
|
1003
|
+
};
|
|
1004
|
+
if (httpMetricsEnabled) {
|
|
1005
|
+
this.telemetry.increment("rock_http_requests_total", 1, attributes);
|
|
1006
|
+
this.telemetry.increment("rock_http_requests_active", -1, {
|
|
1007
|
+
method: request.method,
|
|
1008
|
+
route
|
|
1009
|
+
});
|
|
1010
|
+
this.telemetry.histogram("rock_http_request_duration_ms", durationMs, attributes);
|
|
1011
|
+
}
|
|
1012
|
+
if (!streamFailed && !httpSuccessLogsEnabled && httpSlowRequestMs !== void 0 && statusCode < 400 && durationMs >= httpSlowRequestMs) this.telemetry.warn("http.request_slow", {
|
|
1013
|
+
method: request.method,
|
|
1014
|
+
route,
|
|
1015
|
+
statusCode,
|
|
1016
|
+
durationMs,
|
|
1017
|
+
thresholdMs: httpSlowRequestMs
|
|
1018
|
+
});
|
|
1019
|
+
})).subscribe(subscriber);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
TelemetryHttpInterceptor = __decorate([Injectable(), __decorateMetadata("design:paramtypes", [
|
|
1024
|
+
Object,
|
|
1025
|
+
Object,
|
|
1026
|
+
Object
|
|
1027
|
+
])], TelemetryHttpInterceptor);
|
|
1028
|
+
|
|
1029
|
+
// src/modules/telemetry/nest/loggers/nest.ts
|
|
1030
|
+
let TelemetryNestLogger = class TelemetryNestLogger {
|
|
1031
|
+
logger;
|
|
1032
|
+
constructor(logger) {
|
|
1033
|
+
this.logger = logger;
|
|
1034
|
+
}
|
|
1035
|
+
log(message, ...optionalParams) {
|
|
1036
|
+
this.write("info", message, optionalParams);
|
|
1037
|
+
}
|
|
1038
|
+
error(message, ...optionalParams) {
|
|
1039
|
+
this.write("error", message, optionalParams);
|
|
1040
|
+
}
|
|
1041
|
+
warn(message, ...optionalParams) {
|
|
1042
|
+
this.write("warn", message, optionalParams);
|
|
1043
|
+
}
|
|
1044
|
+
debug(message, ...optionalParams) {
|
|
1045
|
+
this.write("debug", message, optionalParams);
|
|
1046
|
+
}
|
|
1047
|
+
verbose(message, ...optionalParams) {
|
|
1048
|
+
this.write("trace", message, optionalParams);
|
|
1049
|
+
}
|
|
1050
|
+
fatal(message, ...optionalParams) {
|
|
1051
|
+
this.write("fatal", message, optionalParams);
|
|
1052
|
+
}
|
|
1053
|
+
write(level, message, optionalParams) {
|
|
1054
|
+
this.logger[level].call(this.logger, this.sanitizeLogValue(message), ...optionalParams.map((value) => this.sanitizeLogValue(value)));
|
|
1055
|
+
}
|
|
1056
|
+
sanitizeLogValue(value) {
|
|
1057
|
+
if (value instanceof Error) return sanitizeError(value);
|
|
1058
|
+
if (typeof value === "string") return sanitizeText(value);
|
|
1059
|
+
return sanitize(value);
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
1062
|
+
TelemetryNestLogger = __decorate([
|
|
1063
|
+
Injectable(),
|
|
1064
|
+
__decorateParam(0, Inject(PinoLogger)),
|
|
1065
|
+
__decorateMetadata("design:paramtypes", [Object])
|
|
1066
|
+
], TelemetryNestLogger);
|
|
1067
|
+
|
|
1068
|
+
// src/modules/telemetry/nest/middlewares/context.ts
|
|
1069
|
+
let TelemetryContextMiddleware = class TelemetryContextMiddleware {
|
|
1070
|
+
context;
|
|
1071
|
+
options;
|
|
1072
|
+
constructor(context, options) {
|
|
1073
|
+
this.context = context;
|
|
1074
|
+
this.options = options;
|
|
1075
|
+
}
|
|
1076
|
+
use(request, response, next) {
|
|
1077
|
+
const requestIdHeader = this.options.requestId?.header ?? "x-request-id";
|
|
1078
|
+
const requestId = isRequestId(request.id) ? request.id : resolveRequestId(request.headers, requestIdHeader);
|
|
1079
|
+
request.id = requestId;
|
|
1080
|
+
response.setHeader?.(requestIdHeader, requestId);
|
|
1081
|
+
response.header?.(requestIdHeader, requestId);
|
|
1082
|
+
this.context.run({
|
|
1083
|
+
requestId,
|
|
1084
|
+
...this.options.context?.extract?.(request)
|
|
1085
|
+
}, next);
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
TelemetryContextMiddleware = __decorate([
|
|
1089
|
+
Injectable(),
|
|
1090
|
+
__decorateParam(0, Inject(TelemetryContext)),
|
|
1091
|
+
__decorateParam(1, Inject(TELEMETRY_OPTIONS)),
|
|
1092
|
+
__decorateMetadata("design:paramtypes", [typeof TelemetryContext === "undefined" ? Object : TelemetryContext, Object])
|
|
1093
|
+
], TelemetryContextMiddleware);
|
|
1094
|
+
|
|
1095
|
+
// src/modules/telemetry/nest/shutdown.ts
|
|
1096
|
+
let TelemetryShutdownService = class TelemetryShutdownService {
|
|
1097
|
+
telemetry;
|
|
1098
|
+
constructor(telemetry) {
|
|
1099
|
+
this.telemetry = telemetry;
|
|
1100
|
+
}
|
|
1101
|
+
onApplicationBootstrap() {
|
|
1102
|
+
if (isTelemetryRegistered() || process.env.TELEMETRY_ENABLED === "false") return;
|
|
1103
|
+
this.telemetry.warn("telemetry.sdk_not_registered", { hint: "call registerTelemetry({ adapters }) as the first statement of the process entrypoint" });
|
|
1104
|
+
}
|
|
1105
|
+
async onApplicationShutdown() {
|
|
1106
|
+
await this.telemetry.shutdown();
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
TelemetryShutdownService = __decorate([
|
|
1110
|
+
Injectable(),
|
|
1111
|
+
__decorateParam(0, Inject(TelemetryService)),
|
|
1112
|
+
__decorateMetadata("design:paramtypes", [typeof TelemetryService === "undefined" ? Object : TelemetryService])
|
|
1113
|
+
], TelemetryShutdownService);
|
|
1114
|
+
|
|
1115
|
+
// src/modules/telemetry/nest/module.ts
|
|
1116
|
+
var _TelemetryModule;
|
|
1117
|
+
let TelemetryModule = _TelemetryModule = class TelemetryModule {
|
|
1118
|
+
_options;
|
|
1119
|
+
constructor(_options) {
|
|
1120
|
+
this._options = _options;
|
|
1121
|
+
}
|
|
1122
|
+
configure(consumer) {
|
|
1123
|
+
consumer.apply(TelemetryContextMiddleware).forRoutes({
|
|
1124
|
+
path: "*",
|
|
1125
|
+
method: RequestMethod.ALL
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
static forRoot(options = {}) {
|
|
1129
|
+
const requestIdHeader = options.requestId?.header ?? "x-request-id";
|
|
1130
|
+
const excludedLogPaths = new Set(options.logging?.excludePaths ?? []);
|
|
1131
|
+
const httpSuccessLogsEnabled = resolveHttpSuccessLogsEnabled(options.logging);
|
|
1132
|
+
return {
|
|
1133
|
+
global: true,
|
|
1134
|
+
module: _TelemetryModule,
|
|
1135
|
+
imports: [LoggerModule.forRoot({ pinoHttp: {
|
|
1136
|
+
hooks: createTelemetryPinoHooks(),
|
|
1137
|
+
level: options.logging?.level ?? process.env.LOG_LEVEL ?? "info",
|
|
1138
|
+
customLogLevel: (_request, response, error) => httpLogLevel(response, error, httpSuccessLogsEnabled),
|
|
1139
|
+
redact: {
|
|
1140
|
+
paths: [...DEFAULT_REDACT_PATHS, ...options.logging?.redact ?? []],
|
|
1141
|
+
censor: "[REDACTED]"
|
|
1142
|
+
},
|
|
1143
|
+
genReqId(request, response) {
|
|
1144
|
+
const id = isRequestId(request.id) ? request.id : resolveRequestId(request.headers, requestIdHeader);
|
|
1145
|
+
response.setHeader?.(requestIdHeader, id);
|
|
1146
|
+
return id;
|
|
1147
|
+
},
|
|
1148
|
+
serializers: {
|
|
1149
|
+
req: httpRequestLog,
|
|
1150
|
+
res: httpResponseLog
|
|
1151
|
+
},
|
|
1152
|
+
customSuccessObject: (request, response, value) => httpCompletionLog(request, response, value),
|
|
1153
|
+
customErrorObject: (request, response) => httpCompletionLog(request, response, void 0, true),
|
|
1154
|
+
autoLogging: { ignore: (request) => excludedLogPaths.has((request.url ?? "").split("?")[0] ?? "") }
|
|
1155
|
+
} })],
|
|
1156
|
+
providers: [
|
|
1157
|
+
{
|
|
1158
|
+
provide: TELEMETRY_OPTIONS,
|
|
1159
|
+
useValue: options
|
|
1160
|
+
},
|
|
1161
|
+
TelemetryContext,
|
|
1162
|
+
TelemetryContextMiddleware,
|
|
1163
|
+
TelemetryOperationalState,
|
|
1164
|
+
{
|
|
1165
|
+
provide: TelemetryAdapterDispatcher,
|
|
1166
|
+
useFactory: (state) => new TelemetryAdapterDispatcher(options.adapters ?? [], state),
|
|
1167
|
+
inject: [TelemetryOperationalState]
|
|
1168
|
+
},
|
|
1169
|
+
TelemetryService,
|
|
1170
|
+
TelemetryExceptionFilter,
|
|
1171
|
+
TelemetryHealthIndicator,
|
|
1172
|
+
TelemetryShutdownService,
|
|
1173
|
+
TelemetryNestLogger,
|
|
1174
|
+
{
|
|
1175
|
+
provide: Telemetry,
|
|
1176
|
+
useFactory: (service, dispatcher) => new CheckedTelemetry(service, dispatcher, options),
|
|
1177
|
+
inject: [TelemetryService, TelemetryAdapterDispatcher]
|
|
1178
|
+
},
|
|
1179
|
+
...options.http?.filter === false ? [] : [{
|
|
1180
|
+
provide: APP_FILTER,
|
|
1181
|
+
useExisting: TelemetryExceptionFilter
|
|
1182
|
+
}],
|
|
1183
|
+
{
|
|
1184
|
+
provide: APP_INTERCEPTOR,
|
|
1185
|
+
useFactory: (telemetry, context) => new TelemetryHttpInterceptor(telemetry, context, options),
|
|
1186
|
+
inject: [TelemetryService, TelemetryContext]
|
|
1187
|
+
}
|
|
1188
|
+
],
|
|
1189
|
+
exports: [
|
|
1190
|
+
Telemetry,
|
|
1191
|
+
TelemetryContext,
|
|
1192
|
+
TelemetryHealthIndicator,
|
|
1193
|
+
TelemetryNestLogger
|
|
1194
|
+
]
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
TelemetryModule = _TelemetryModule = __decorate([
|
|
1199
|
+
Module({}),
|
|
1200
|
+
__decorateParam(0, Inject(TELEMETRY_OPTIONS)),
|
|
1201
|
+
__decorateMetadata("design:paramtypes", [Object])
|
|
1202
|
+
], TelemetryModule);
|
|
1203
|
+
|
|
1204
|
+
export { Telemetry, TelemetryContext, TelemetryError, TelemetryHealthIndicator, TelemetryModule, TelemetryNestLogger, Trace, createTelemetry, createTelemetryFastifyOptions, createTelemetryMock };
|
|
1205
|
+
//# sourceMappingURL=index.js.map
|