franchising 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -1
- package/dist/cli.js +109 -109
- package/dist/core-BidJDmSN.cjs +759 -0
- package/dist/core-BidJDmSN.cjs.map +1 -0
- package/dist/core-C-EFrEd6.js +710 -0
- package/dist/core-C-EFrEd6.js.map +1 -0
- package/dist/{index-Cj-10Q5F.d.cts → index--EJQTnHy.d.cts} +2 -2
- package/dist/index-B3aVdDMV.d.cts +51 -0
- package/dist/{index-CYT1PFzo.d.cts → index-BDYdEWl2.d.cts} +157 -3
- package/dist/{index-B3UOHsA9.d.ts → index-BDYdEWl2.d.ts} +157 -3
- package/dist/index-ByS2IC02.d.ts +51 -0
- package/dist/{index-C1KmT8dQ.d.ts → index-u3ArUXO0.d.ts} +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/messaging/index.cjs +1 -1
- package/dist/messaging/index.d.cts +120 -5
- package/dist/messaging/index.d.ts +120 -5
- package/dist/messaging/index.js +1 -1
- package/dist/sdk-B2Lp3JCz.cjs +45 -0
- package/dist/sdk-B2Lp3JCz.cjs.map +1 -0
- package/dist/sdk-IGOHpAeG.js +22 -0
- package/dist/sdk-IGOHpAeG.js.map +1 -0
- package/dist/sso/client.d.cts +1 -2
- package/dist/sso/client.d.ts +1 -2
- package/dist/sso/index.d.cts +1 -2
- package/dist/sso/index.d.ts +1 -2
- package/dist/sso/nestjs.d.cts +2 -2
- package/dist/sso/nestjs.d.ts +2 -2
- package/dist/sso/resource.d.cts +2 -2
- package/dist/sso/resource.d.ts +2 -2
- package/dist/telemetry/core.cjs +11 -0
- package/dist/telemetry/core.d.cts +4 -0
- package/dist/telemetry/core.d.ts +4 -0
- package/dist/telemetry/core.js +5 -0
- package/dist/telemetry/index.cjs +86 -799
- package/dist/telemetry/index.cjs.map +1 -1
- package/dist/telemetry/index.d.cts +9 -55
- package/dist/telemetry/index.d.ts +9 -55
- package/dist/telemetry/index.js +67 -773
- package/dist/telemetry/index.js.map +1 -1
- package/dist/telemetry/register.cjs +86 -4
- package/dist/telemetry/register.cjs.map +1 -0
- package/dist/telemetry/register.d.cts +4 -4
- package/dist/telemetry/register.d.ts +4 -4
- package/dist/telemetry/register.js +84 -2
- package/dist/telemetry/register.js.map +1 -0
- package/dist/version-BNLcT13l.js +1 -0
- package/dist/{version-CfUi3iAV.cjs → version-BrUpZnZ7.cjs} +1 -1
- package/package.json +18 -2
- package/dist/runtime-C95UYYLb.cjs +0 -111
- package/dist/runtime-C95UYYLb.cjs.map +0 -1
- package/dist/runtime-DG2oBFYw.js +0 -88
- package/dist/runtime-DG2oBFYw.js.map +0 -1
- package/dist/team-CJVAg4HH.d.cts +0 -157
- package/dist/team-CJVAg4HH.d.ts +0 -157
- package/dist/version-CPunTYCY.js +0 -1
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
import { t as TelemetryAdapterDispatcher } from "./dispatcher-DA_l1Vkv.js";
|
|
2
|
+
import { i as readEnv, n as shutdownTelemetry } from "./sdk-IGOHpAeG.js";
|
|
3
|
+
import { 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";
|
|
4
|
+
import pino from "pino";
|
|
5
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
+
import { SpanStatusCode, context, metrics, propagation, trace } from "@opentelemetry/api";
|
|
7
|
+
import { SeverityNumber, logs } from "@opentelemetry/api-logs";
|
|
8
|
+
|
|
9
|
+
// src/modules/telemetry/shared/context/context.ts
|
|
10
|
+
const storage = new AsyncLocalStorage();
|
|
11
|
+
function baseContext() {
|
|
12
|
+
const span = trace.getActiveSpan()?.spanContext();
|
|
13
|
+
return {
|
|
14
|
+
service: readEnv("OTEL_SERVICE_NAME", "SERVICE_NAME") ?? "unknown-service",
|
|
15
|
+
product: readEnv("TELEMETRY_PRODUCT"),
|
|
16
|
+
environment: readEnv("NODE_ENV") ?? "development",
|
|
17
|
+
release: readEnv("OTEL_SERVICE_VERSION", "RELEASE", "RAILWAY_DEPLOYMENT_ID", "GIT_SHA"),
|
|
18
|
+
traceId: span?.traceId,
|
|
19
|
+
spanId: span?.spanId,
|
|
20
|
+
...storage.getStore()
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
var TelemetryContext = class {
|
|
24
|
+
logger;
|
|
25
|
+
constructor(logger) {
|
|
26
|
+
this.logger = logger;
|
|
27
|
+
}
|
|
28
|
+
get() {
|
|
29
|
+
return baseContext();
|
|
30
|
+
}
|
|
31
|
+
assign(context) {
|
|
32
|
+
const current = storage.getStore();
|
|
33
|
+
if (current) Object.assign(current, context);
|
|
34
|
+
try {
|
|
35
|
+
this.logger?.assign({
|
|
36
|
+
actor_id: context.actorId,
|
|
37
|
+
organization_id: context.organizationId,
|
|
38
|
+
job_id: context.jobId,
|
|
39
|
+
correlation_id: context.correlationId
|
|
40
|
+
});
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
identify(actorId) {
|
|
44
|
+
this.assign({ actorId });
|
|
45
|
+
}
|
|
46
|
+
organization(organizationId) {
|
|
47
|
+
this.assign({ organizationId });
|
|
48
|
+
}
|
|
49
|
+
run(context, fn) {
|
|
50
|
+
return storage.run({
|
|
51
|
+
...storage.getStore(),
|
|
52
|
+
...context
|
|
53
|
+
}, fn);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/modules/telemetry/shared/logging/otel.ts
|
|
58
|
+
const otelLogger = logs.getLogger("@rockdev/telemetry", void 0, { includeTraceContext: true });
|
|
59
|
+
function severity(level) {
|
|
60
|
+
if (level >= 60) return {
|
|
61
|
+
text: "fatal",
|
|
62
|
+
number: SeverityNumber.FATAL
|
|
63
|
+
};
|
|
64
|
+
if (level >= 50) return {
|
|
65
|
+
text: "error",
|
|
66
|
+
number: SeverityNumber.ERROR
|
|
67
|
+
};
|
|
68
|
+
if (level >= 40) return {
|
|
69
|
+
text: "warn",
|
|
70
|
+
number: SeverityNumber.WARN
|
|
71
|
+
};
|
|
72
|
+
if (level >= 30) return {
|
|
73
|
+
text: "info",
|
|
74
|
+
number: SeverityNumber.INFO
|
|
75
|
+
};
|
|
76
|
+
if (level >= 20) return {
|
|
77
|
+
text: "debug",
|
|
78
|
+
number: SeverityNumber.DEBUG
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
text: "trace",
|
|
82
|
+
number: SeverityNumber.TRACE
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function toAnyValue(value) {
|
|
86
|
+
if (value instanceof Error) {
|
|
87
|
+
const safeError = sanitizeError(value);
|
|
88
|
+
return toAnyValue({
|
|
89
|
+
name: safeError.name,
|
|
90
|
+
message: safeError.message,
|
|
91
|
+
...safeError.stack ? { stack: safeError.stack } : {}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (typeof value === "string") return sanitizeText(value);
|
|
95
|
+
if (Array.isArray(value)) return value.map(toAnyValue);
|
|
96
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, isSensitiveKey(key) ? "[REDACTED]" : toAnyValue(child)]));
|
|
97
|
+
if (typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) return value;
|
|
98
|
+
return String(value);
|
|
99
|
+
}
|
|
100
|
+
function toLogRecord(args, level) {
|
|
101
|
+
const [first, second, ...rest] = args;
|
|
102
|
+
const body = {};
|
|
103
|
+
if (first && typeof first === "object" && !Array.isArray(first)) {
|
|
104
|
+
const safe = toAnyValue(first);
|
|
105
|
+
if (safe && typeof safe === "object" && !Array.isArray(safe)) Object.assign(body, safe);
|
|
106
|
+
if (second !== void 0 && typeof second !== "string") body.arguments = toAnyValue(second);
|
|
107
|
+
} else if (first !== void 0) body.message = typeof first === "string" ? sanitizeText(first) : toAnyValue(first);
|
|
108
|
+
if (typeof second === "string") body.message = sanitizeText(second);
|
|
109
|
+
if (rest.length > 0) body.arguments = toAnyValue(rest);
|
|
110
|
+
const currentSeverity = severity(level);
|
|
111
|
+
return {
|
|
112
|
+
body,
|
|
113
|
+
severityNumber: currentSeverity.number,
|
|
114
|
+
severityText: currentSeverity.text,
|
|
115
|
+
...typeof body.event === "string" ? { eventName: body.event } : {}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function emitPinoLog(args, level) {
|
|
119
|
+
if (process.env.TELEMETRY_ENABLED === "false" || process.env.TELEMETRY_LOGS_ENABLED === "false") return;
|
|
120
|
+
try {
|
|
121
|
+
otelLogger.emit(toLogRecord(args, level));
|
|
122
|
+
} catch {}
|
|
123
|
+
}
|
|
124
|
+
function createTelemetryPinoHooks() {
|
|
125
|
+
return { logMethod(args, method, level) {
|
|
126
|
+
emitPinoLog(args, level);
|
|
127
|
+
method.apply(this, args);
|
|
128
|
+
} };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/modules/telemetry/shared/state/operational.ts
|
|
132
|
+
var TelemetryOperationalState = class {
|
|
133
|
+
signals = new Map([
|
|
134
|
+
"errors",
|
|
135
|
+
"events",
|
|
136
|
+
"logs",
|
|
137
|
+
"metrics",
|
|
138
|
+
"tracing"
|
|
139
|
+
].map((signal) => [signal, {
|
|
140
|
+
failed: false,
|
|
141
|
+
failureCount: 0
|
|
142
|
+
}]));
|
|
143
|
+
recordFailure(signal) {
|
|
144
|
+
const current = this.signals.get(signal);
|
|
145
|
+
if (!current) return;
|
|
146
|
+
current.failed = true;
|
|
147
|
+
current.failureCount += 1;
|
|
148
|
+
current.lastFailureAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
149
|
+
}
|
|
150
|
+
status(signal) {
|
|
151
|
+
return { ...this.signals.get(signal) ?? {
|
|
152
|
+
failed: false,
|
|
153
|
+
failureCount: 0
|
|
154
|
+
} };
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// src/modules/telemetry/types/telemetry.ts
|
|
159
|
+
var Telemetry = class {};
|
|
160
|
+
|
|
161
|
+
// src/modules/telemetry/core/checked-telemetry.ts
|
|
162
|
+
var CheckedTelemetry = class extends Telemetry {
|
|
163
|
+
target;
|
|
164
|
+
adapters;
|
|
165
|
+
options;
|
|
166
|
+
constructor(target, adapters, options = {}) {
|
|
167
|
+
super();
|
|
168
|
+
this.target = target;
|
|
169
|
+
this.adapters = adapters;
|
|
170
|
+
this.options = options;
|
|
171
|
+
}
|
|
172
|
+
debug(event, attributes) {
|
|
173
|
+
this.require("logs", "telemetry.debug()");
|
|
174
|
+
this.target.debug(event, attributes);
|
|
175
|
+
}
|
|
176
|
+
info(event, attributes) {
|
|
177
|
+
this.require("logs", "telemetry.info()");
|
|
178
|
+
this.target.info(event, attributes);
|
|
179
|
+
}
|
|
180
|
+
warn(event, attributes) {
|
|
181
|
+
this.require("logs", "telemetry.warn()");
|
|
182
|
+
this.target.warn(event, attributes);
|
|
183
|
+
}
|
|
184
|
+
error(event, error, attributes) {
|
|
185
|
+
this.require("logs", "telemetry.error()");
|
|
186
|
+
this.target.error(event, error, attributes);
|
|
187
|
+
}
|
|
188
|
+
event(event, attributes) {
|
|
189
|
+
this.require("events", "telemetry.event()");
|
|
190
|
+
this.target.event(event, attributes);
|
|
191
|
+
}
|
|
192
|
+
captureException(error, attributes, options) {
|
|
193
|
+
this.require("errors", "telemetry.captureException()");
|
|
194
|
+
this.target.captureException(error, attributes, options);
|
|
195
|
+
}
|
|
196
|
+
async span(name, fn, attributes) {
|
|
197
|
+
this.require("tracing", "telemetry.span()");
|
|
198
|
+
return this.target.span(name, fn, attributes);
|
|
199
|
+
}
|
|
200
|
+
increment(name, value, attributes) {
|
|
201
|
+
this.require("metrics", "telemetry.increment()");
|
|
202
|
+
this.target.increment(name, value, attributes);
|
|
203
|
+
}
|
|
204
|
+
histogram(name, value, attributes) {
|
|
205
|
+
this.require("metrics", "telemetry.histogram()");
|
|
206
|
+
this.target.histogram(name, value, attributes);
|
|
207
|
+
}
|
|
208
|
+
async trackDependency(options, fn) {
|
|
209
|
+
this.requireDependency("telemetry.trackDependency()");
|
|
210
|
+
return this.target.trackDependency(options, fn);
|
|
211
|
+
}
|
|
212
|
+
runJob(options, fn) {
|
|
213
|
+
try {
|
|
214
|
+
this.requireDependency("telemetry.runJob()");
|
|
215
|
+
} catch (error) {
|
|
216
|
+
return Promise.reject(error);
|
|
217
|
+
}
|
|
218
|
+
return this.target.runJob(options, fn);
|
|
219
|
+
}
|
|
220
|
+
async fetch(input, init, options) {
|
|
221
|
+
this.requireDependency("telemetry.fetch()");
|
|
222
|
+
return this.target.fetch(input, init, options);
|
|
223
|
+
}
|
|
224
|
+
traceparent() {
|
|
225
|
+
return this.target.traceparent();
|
|
226
|
+
}
|
|
227
|
+
getContext() {
|
|
228
|
+
return this.target.getContext();
|
|
229
|
+
}
|
|
230
|
+
shutdown() {
|
|
231
|
+
return this.target.shutdown();
|
|
232
|
+
}
|
|
233
|
+
disabled(capability) {
|
|
234
|
+
if (process.env.TELEMETRY_ENABLED === "false") return true;
|
|
235
|
+
if (capability === "metrics") return process.env.TELEMETRY_METRICS_ENABLED === "false";
|
|
236
|
+
if (capability === "tracing") return this.options.tracing === false || process.env.TELEMETRY_TRACING_ENABLED === "false";
|
|
237
|
+
if (capability === "errors") return this.options.errors === false;
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
require(capability, operation) {
|
|
241
|
+
if (this.disabled(capability)) return;
|
|
242
|
+
this.adapters.assertSupports(capability, operation);
|
|
243
|
+
}
|
|
244
|
+
requireDependency(operation) {
|
|
245
|
+
this.require("tracing", operation);
|
|
246
|
+
this.require("metrics", operation);
|
|
247
|
+
this.require("errors", operation);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// src/modules/telemetry/shared/context/adapter.ts
|
|
252
|
+
function adapterContext(context) {
|
|
253
|
+
return sanitizeAttributes({ ...context });
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/modules/telemetry/shared/utils/case.ts
|
|
257
|
+
function snakeCase(value) {
|
|
258
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[.-]/g, "_").toLowerCase();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/modules/telemetry/shared/logging/attributes.ts
|
|
262
|
+
function logAttributes(context, attributes) {
|
|
263
|
+
const record = {};
|
|
264
|
+
for (const [key, value] of Object.entries({
|
|
265
|
+
requestId: context.requestId,
|
|
266
|
+
traceId: context.traceId,
|
|
267
|
+
spanId: context.spanId,
|
|
268
|
+
service: context.service,
|
|
269
|
+
product: context.product,
|
|
270
|
+
environment: context.environment,
|
|
271
|
+
release: context.release,
|
|
272
|
+
actorId: context.actorId,
|
|
273
|
+
organizationId: context.organizationId,
|
|
274
|
+
jobId: context.jobId,
|
|
275
|
+
correlationId: context.correlationId,
|
|
276
|
+
...attributes
|
|
277
|
+
})) if (value !== void 0) record[snakeCase(key)] = value;
|
|
278
|
+
return sanitizeAttributes(record);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/modules/telemetry/shared/metrics/instruments.ts
|
|
282
|
+
const meter = metrics.getMeter("@rockdev/telemetry");
|
|
283
|
+
const counters = /* @__PURE__ */ new Map();
|
|
284
|
+
const histograms = /* @__PURE__ */ new Map();
|
|
285
|
+
|
|
286
|
+
// src/modules/telemetry/shared/metrics/metrics.ts
|
|
287
|
+
const ALLOWED_METRIC_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
288
|
+
"service",
|
|
289
|
+
"route",
|
|
290
|
+
"method",
|
|
291
|
+
"status",
|
|
292
|
+
"statusCode",
|
|
293
|
+
"statusClass",
|
|
294
|
+
"result",
|
|
295
|
+
"dependency",
|
|
296
|
+
"operation",
|
|
297
|
+
"queue",
|
|
298
|
+
"job",
|
|
299
|
+
"product",
|
|
300
|
+
"channel",
|
|
301
|
+
"provider",
|
|
302
|
+
"priority",
|
|
303
|
+
"reason"
|
|
304
|
+
]);
|
|
305
|
+
function validValue(key, value) {
|
|
306
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 100) return false;
|
|
307
|
+
if (containsSensitiveText(value) || isLikelyIdentifier(value)) return false;
|
|
308
|
+
if (key === "route") return (value === "unmatched" || value.startsWith("/")) && !value.includes("?") && !value.includes("://") && !value.split("/").some((segment) => /^\d+$/.test(segment));
|
|
309
|
+
if (key === "method") return /^[A-Z]+$/.test(value);
|
|
310
|
+
if (key === "statusCode") return /^[1-5]\d\d$/.test(value);
|
|
311
|
+
if (key === "statusClass") return /^[1-5]xx$/.test(value);
|
|
312
|
+
if (key === "result") return value === "success" || value === "error";
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
function sanitizeMetricAttributes(attributes) {
|
|
316
|
+
if (!attributes) return {
|
|
317
|
+
attributes: void 0,
|
|
318
|
+
rejectedKeys: []
|
|
319
|
+
};
|
|
320
|
+
const valid = {};
|
|
321
|
+
const rejectedKeys = [];
|
|
322
|
+
for (const [rawKey, value] of Object.entries(attributes)) {
|
|
323
|
+
const key = rawKey;
|
|
324
|
+
if (!ALLOWED_METRIC_ATTRIBUTES.has(key) || isSensitiveKey(rawKey) || !validValue(key, value)) {
|
|
325
|
+
rejectedKeys.push(rawKey);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
valid[key] = value;
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
attributes: valid,
|
|
332
|
+
rejectedKeys
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// src/modules/telemetry/core/telemetry.service.ts
|
|
337
|
+
var TelemetryService = class extends Telemetry {
|
|
338
|
+
logger;
|
|
339
|
+
context;
|
|
340
|
+
operationalState;
|
|
341
|
+
options;
|
|
342
|
+
adapters;
|
|
343
|
+
constructor(logger, context, operationalState, options = {}, adapters) {
|
|
344
|
+
super();
|
|
345
|
+
this.logger = logger;
|
|
346
|
+
this.context = context;
|
|
347
|
+
this.operationalState = operationalState;
|
|
348
|
+
this.options = options;
|
|
349
|
+
this.adapters = adapters;
|
|
350
|
+
}
|
|
351
|
+
debug(event, attributes) {
|
|
352
|
+
this.write("debug", event, attributes);
|
|
353
|
+
}
|
|
354
|
+
info(event, attributes) {
|
|
355
|
+
this.write("info", event, attributes);
|
|
356
|
+
}
|
|
357
|
+
warn(event, attributes) {
|
|
358
|
+
this.write("warn", event, attributes);
|
|
359
|
+
}
|
|
360
|
+
error(event, error, attributes) {
|
|
361
|
+
this.writeError(event, error, attributes);
|
|
362
|
+
}
|
|
363
|
+
event(event, attributes) {
|
|
364
|
+
try {
|
|
365
|
+
const context = this.getContext();
|
|
366
|
+
const safeAttributes = logAttributes(context, attributes);
|
|
367
|
+
this.logger.info({
|
|
368
|
+
event,
|
|
369
|
+
event_kind: "product",
|
|
370
|
+
...safeAttributes
|
|
371
|
+
});
|
|
372
|
+
this.adapters?.captureEvent?.({
|
|
373
|
+
event,
|
|
374
|
+
attributes: safeAttributes,
|
|
375
|
+
context: adapterContext(context)
|
|
376
|
+
});
|
|
377
|
+
} catch {
|
|
378
|
+
this.operationalState.recordFailure("events");
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
captureException(error, attributes, options) {
|
|
382
|
+
this.error("exception.captured", error, attributes);
|
|
383
|
+
if (this.options.errors === false) return;
|
|
384
|
+
const reportError = sanitizeError(error, process.env.TELEMETRY_ERROR_STACK_ENABLED !== "false");
|
|
385
|
+
const reportContext = {
|
|
386
|
+
request: sanitizeAttributes({ ...this.getContext() }),
|
|
387
|
+
attributes: sanitizeAttributes(attributes)
|
|
388
|
+
};
|
|
389
|
+
this.adapters?.captureException?.({
|
|
390
|
+
error: reportError,
|
|
391
|
+
context: reportContext,
|
|
392
|
+
...options?.fingerprint ? { fingerprint: options.fingerprint } : {}
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
async span(name, fn, attributes = {}) {
|
|
396
|
+
if (this.options.tracing === false || process.env.TELEMETRY_TRACING_ENABLED === "false") return fn({
|
|
397
|
+
setAttributes() {},
|
|
398
|
+
addEvent() {}
|
|
399
|
+
});
|
|
400
|
+
try {
|
|
401
|
+
return trace.getTracer(this.getContext().service).startActiveSpan(name, { attributes: sanitizeAttributes(attributes) }, async (span) => {
|
|
402
|
+
const startedAt = performance.now();
|
|
403
|
+
const events = [];
|
|
404
|
+
let spanError;
|
|
405
|
+
let spanAttributes = sanitizeAttributes(attributes);
|
|
406
|
+
const facade = {
|
|
407
|
+
setAttributes: (values) => {
|
|
408
|
+
const safe = sanitizeAttributes(values);
|
|
409
|
+
spanAttributes = {
|
|
410
|
+
...spanAttributes,
|
|
411
|
+
...safe
|
|
412
|
+
};
|
|
413
|
+
span.setAttributes(safe);
|
|
414
|
+
},
|
|
415
|
+
addEvent: (event, values) => {
|
|
416
|
+
const safe = sanitizeAttributes(values);
|
|
417
|
+
events.push({
|
|
418
|
+
name: event,
|
|
419
|
+
attributes: safe
|
|
420
|
+
});
|
|
421
|
+
span.addEvent(event, safe);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
try {
|
|
425
|
+
return await fn(facade);
|
|
426
|
+
} catch (error) {
|
|
427
|
+
spanError = sanitizeError(error);
|
|
428
|
+
try {
|
|
429
|
+
span.recordException(sanitizeError(error));
|
|
430
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
431
|
+
} catch {
|
|
432
|
+
this.operationalState.recordFailure("tracing");
|
|
433
|
+
}
|
|
434
|
+
throw error;
|
|
435
|
+
} finally {
|
|
436
|
+
this.adapters?.captureSpan?.({
|
|
437
|
+
name,
|
|
438
|
+
durationMs: performance.now() - startedAt,
|
|
439
|
+
attributes: spanAttributes,
|
|
440
|
+
events,
|
|
441
|
+
context: adapterContext(this.getContext()),
|
|
442
|
+
...spanError ? { error: spanError } : {}
|
|
443
|
+
});
|
|
444
|
+
try {
|
|
445
|
+
span.end();
|
|
446
|
+
} catch {
|
|
447
|
+
this.operationalState.recordFailure("tracing");
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
} catch {
|
|
452
|
+
this.operationalState.recordFailure("tracing");
|
|
453
|
+
return fn({
|
|
454
|
+
setAttributes() {},
|
|
455
|
+
addEvent() {}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
increment(name, value = 1, attributes) {
|
|
460
|
+
if (process.env.TELEMETRY_METRICS_ENABLED === "false") return;
|
|
461
|
+
const sanitized = sanitizeMetricAttributes(attributes);
|
|
462
|
+
this.warnRejectedMetricAttributes(name, sanitized.rejectedKeys);
|
|
463
|
+
try {
|
|
464
|
+
const instrument = counters.get(name) ?? meter.createCounter(name);
|
|
465
|
+
counters.set(name, instrument);
|
|
466
|
+
instrument.add(value, sanitized.attributes);
|
|
467
|
+
this.adapters?.captureMetric?.({
|
|
468
|
+
type: "counter",
|
|
469
|
+
name,
|
|
470
|
+
value,
|
|
471
|
+
attributes: sanitized.attributes ?? {},
|
|
472
|
+
context: adapterContext(this.getContext())
|
|
473
|
+
});
|
|
474
|
+
} catch {
|
|
475
|
+
this.operationalState.recordFailure("metrics");
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
histogram(name, value, 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 = histograms.get(name) ?? meter.createHistogram(name, { unit: "ms" });
|
|
484
|
+
histograms.set(name, instrument);
|
|
485
|
+
instrument.record(value, sanitized.attributes);
|
|
486
|
+
this.adapters?.captureMetric?.({
|
|
487
|
+
type: "histogram",
|
|
488
|
+
name,
|
|
489
|
+
value,
|
|
490
|
+
attributes: sanitized.attributes ?? {},
|
|
491
|
+
context: adapterContext(this.getContext())
|
|
492
|
+
});
|
|
493
|
+
} catch {
|
|
494
|
+
this.operationalState.recordFailure("metrics");
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
async trackDependency(options, fn) {
|
|
498
|
+
const startedAt = performance.now();
|
|
499
|
+
try {
|
|
500
|
+
const result = await this.span(`dependency.${options.name}.${options.operation}`, async () => fn(), {
|
|
501
|
+
"dependency.name": options.name,
|
|
502
|
+
"operation.name": options.operation
|
|
503
|
+
});
|
|
504
|
+
this.increment("rock_dependency_requests_total", 1, {
|
|
505
|
+
dependency: options.name,
|
|
506
|
+
operation: options.operation,
|
|
507
|
+
result: "success"
|
|
508
|
+
});
|
|
509
|
+
return result;
|
|
510
|
+
} catch (error) {
|
|
511
|
+
this.increment("rock_dependency_requests_total", 1, {
|
|
512
|
+
dependency: options.name,
|
|
513
|
+
operation: options.operation,
|
|
514
|
+
result: "error"
|
|
515
|
+
});
|
|
516
|
+
this.captureException(error, {
|
|
517
|
+
dependency: options.name,
|
|
518
|
+
operation: options.operation
|
|
519
|
+
});
|
|
520
|
+
throw error;
|
|
521
|
+
} finally {
|
|
522
|
+
this.histogram("rock_dependency_request_duration_ms", performance.now() - startedAt, {
|
|
523
|
+
dependency: options.name,
|
|
524
|
+
operation: options.operation
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
runJob(options, fn) {
|
|
529
|
+
const run = () => this.context.run({
|
|
530
|
+
...options.jobId ? { jobId: options.jobId } : {},
|
|
531
|
+
...options.correlationId ? { correlationId: options.correlationId } : {}
|
|
532
|
+
}, () => this.trackDependency({
|
|
533
|
+
name: `queue.${options.queue}`,
|
|
534
|
+
operation: options.job
|
|
535
|
+
}, fn));
|
|
536
|
+
if (!options.traceparent) return run();
|
|
537
|
+
const parent = propagation.extract(context.active(), { traceparent: options.traceparent });
|
|
538
|
+
return context.with(parent, run);
|
|
539
|
+
}
|
|
540
|
+
async fetch(input, init, options) {
|
|
541
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
542
|
+
let host = "unknown";
|
|
543
|
+
let path = url;
|
|
544
|
+
try {
|
|
545
|
+
const parsed = new URL(url);
|
|
546
|
+
host = parsed.host;
|
|
547
|
+
path = parsed.pathname;
|
|
548
|
+
} catch {}
|
|
549
|
+
const method = (init?.method ?? (input instanceof Request ? input.method : void 0) ?? "GET").toUpperCase();
|
|
550
|
+
const dependency = options?.dependency ?? host;
|
|
551
|
+
const operation = options?.operation ?? `${method} ${path}`;
|
|
552
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : {}));
|
|
553
|
+
propagation.inject(context.active(), headers, { set: (carrier, key, value) => carrier.set(key, String(value)) });
|
|
554
|
+
return this.trackDependency({
|
|
555
|
+
name: dependency,
|
|
556
|
+
operation
|
|
557
|
+
}, () => this.span(`http.client.${dependency}`, async (span) => {
|
|
558
|
+
const response = await fetch(input, {
|
|
559
|
+
...init,
|
|
560
|
+
headers
|
|
561
|
+
});
|
|
562
|
+
span.setAttributes({ "http.response.status_code": response.status });
|
|
563
|
+
return response;
|
|
564
|
+
}, {
|
|
565
|
+
"http.request.method": method,
|
|
566
|
+
"server.address": host,
|
|
567
|
+
"url.path": path,
|
|
568
|
+
...options?.attributes
|
|
569
|
+
}));
|
|
570
|
+
}
|
|
571
|
+
traceparent() {
|
|
572
|
+
const carrier = {};
|
|
573
|
+
try {
|
|
574
|
+
propagation.inject(context.active(), carrier);
|
|
575
|
+
} catch {
|
|
576
|
+
this.operationalState.recordFailure("tracing");
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
return carrier.traceparent;
|
|
580
|
+
}
|
|
581
|
+
getContext() {
|
|
582
|
+
return this.context.get();
|
|
583
|
+
}
|
|
584
|
+
async shutdown() {
|
|
585
|
+
await Promise.allSettled([this.adapters?.shutdown() ?? Promise.resolve(), shutdownTelemetry().catch(() => {
|
|
586
|
+
this.operationalState.recordFailure("logs");
|
|
587
|
+
this.operationalState.recordFailure("metrics");
|
|
588
|
+
this.operationalState.recordFailure("tracing");
|
|
589
|
+
})]);
|
|
590
|
+
}
|
|
591
|
+
write(level, event, attributes) {
|
|
592
|
+
try {
|
|
593
|
+
const context = this.getContext();
|
|
594
|
+
const safeAttributes = logAttributes(context, attributes);
|
|
595
|
+
this.logger[level]({
|
|
596
|
+
event,
|
|
597
|
+
...safeAttributes
|
|
598
|
+
});
|
|
599
|
+
this.adapters?.captureLog?.({
|
|
600
|
+
level,
|
|
601
|
+
event,
|
|
602
|
+
attributes: safeAttributes,
|
|
603
|
+
context: adapterContext(context)
|
|
604
|
+
});
|
|
605
|
+
} catch {
|
|
606
|
+
this.operationalState.recordFailure("logs");
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
writeError(event, error, attributes) {
|
|
610
|
+
try {
|
|
611
|
+
const context = this.getContext();
|
|
612
|
+
const safeAttributes = logAttributes(context, attributes);
|
|
613
|
+
this.logger.error({
|
|
614
|
+
event,
|
|
615
|
+
err: sanitizeError(error),
|
|
616
|
+
...safeAttributes
|
|
617
|
+
});
|
|
618
|
+
this.adapters?.captureLog?.({
|
|
619
|
+
level: "error",
|
|
620
|
+
event,
|
|
621
|
+
attributes: safeAttributes,
|
|
622
|
+
context: adapterContext(context)
|
|
623
|
+
});
|
|
624
|
+
} catch {
|
|
625
|
+
this.operationalState.recordFailure("logs");
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
warnRejectedMetricAttributes(metricName, rejectedKeys) {
|
|
629
|
+
if (rejectedKeys.length === 0) return;
|
|
630
|
+
this.write("warn", "telemetry.metric_attributes_rejected", {
|
|
631
|
+
metricName,
|
|
632
|
+
rejectedAttributeKeys: rejectedKeys
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
// src/modules/telemetry/core/create-telemetry.ts
|
|
638
|
+
function createTelemetry(options = {}) {
|
|
639
|
+
const context = new TelemetryContext();
|
|
640
|
+
const operationalState = new TelemetryOperationalState();
|
|
641
|
+
const logger = pino({
|
|
642
|
+
hooks: createTelemetryPinoHooks(),
|
|
643
|
+
level: process.env.LOG_LEVEL ?? "info",
|
|
644
|
+
redact: {
|
|
645
|
+
paths: DEFAULT_REDACT_PATHS,
|
|
646
|
+
censor: "[REDACTED]"
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
const adapters = new TelemetryAdapterDispatcher(options.adapters ?? [], operationalState);
|
|
650
|
+
return new CheckedTelemetry(new TelemetryService(logger, context, operationalState, options, adapters), adapters, options);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// src/modules/telemetry/shared/tracing/span.ts
|
|
654
|
+
function recordError(span, error) {
|
|
655
|
+
try {
|
|
656
|
+
span.recordException(sanitizeError(error));
|
|
657
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
658
|
+
} catch {}
|
|
659
|
+
}
|
|
660
|
+
function endSpan(span) {
|
|
661
|
+
try {
|
|
662
|
+
span.end();
|
|
663
|
+
} catch {}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// src/modules/telemetry/shared/utils/promise.ts
|
|
667
|
+
function isPromiseLike(value) {
|
|
668
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// src/modules/telemetry/core/trace.decorator.ts
|
|
672
|
+
function Trace(name, attributes = {}) {
|
|
673
|
+
return (_target, _propertyKey, descriptor) => {
|
|
674
|
+
const typedDescriptor = descriptor;
|
|
675
|
+
const original = typedDescriptor.value;
|
|
676
|
+
if (!original) return;
|
|
677
|
+
typedDescriptor.value = function traced(...args) {
|
|
678
|
+
const invoke = () => original.apply(this, args);
|
|
679
|
+
if (process.env.TELEMETRY_TRACING_ENABLED === "false") return invoke();
|
|
680
|
+
try {
|
|
681
|
+
return trace.getTracer(readEnv("OTEL_SERVICE_NAME") ?? "unknown-service").startActiveSpan(name, { attributes: sanitizeAttributes(attributes) }, (span) => {
|
|
682
|
+
try {
|
|
683
|
+
const result = invoke();
|
|
684
|
+
if (!isPromiseLike(result)) {
|
|
685
|
+
endSpan(span);
|
|
686
|
+
return result;
|
|
687
|
+
}
|
|
688
|
+
return result.then((value) => {
|
|
689
|
+
endSpan(span);
|
|
690
|
+
return value;
|
|
691
|
+
}, (error) => {
|
|
692
|
+
recordError(span, error);
|
|
693
|
+
endSpan(span);
|
|
694
|
+
throw error;
|
|
695
|
+
});
|
|
696
|
+
} catch (error) {
|
|
697
|
+
recordError(span, error);
|
|
698
|
+
endSpan(span);
|
|
699
|
+
throw error;
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
} catch {
|
|
703
|
+
return invoke();
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
export { Telemetry as a, TelemetryContext as c, CheckedTelemetry as i, createTelemetry as n, TelemetryOperationalState as o, TelemetryService as r, createTelemetryPinoHooks as s, Trace as t };
|
|
710
|
+
//# sourceMappingURL=core-C-EFrEd6.js.map
|