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