@gobing-ai/ts-infra 0.2.7 → 0.2.9

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 (67) hide show
  1. package/README.md +111 -20
  2. package/dist/event-bus/event-bus.d.ts.map +1 -1
  3. package/dist/event-bus/event-bus.js +4 -0
  4. package/dist/index.d.ts +2 -3
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -2
  7. package/dist/job-queue/db-job-queue.d.ts +40 -0
  8. package/dist/job-queue/db-job-queue.d.ts.map +1 -0
  9. package/dist/job-queue/db-job-queue.js +154 -0
  10. package/dist/job-queue/index.d.ts +1 -0
  11. package/dist/job-queue/index.d.ts.map +1 -1
  12. package/dist/job-queue/index.js +1 -0
  13. package/dist/job-queue/types.d.ts +3 -5
  14. package/dist/job-queue/types.d.ts.map +1 -1
  15. package/dist/job-queue/types.js +2 -4
  16. package/dist/logger.js +5 -6
  17. package/dist/scheduler/cloudflare.d.ts +0 -4
  18. package/dist/scheduler/cloudflare.d.ts.map +1 -1
  19. package/dist/scheduler/cloudflare.js +10 -1
  20. package/dist/scheduler/factory.d.ts +0 -3
  21. package/dist/scheduler/factory.d.ts.map +1 -1
  22. package/dist/scheduler/factory.js +5 -14
  23. package/dist/scheduler/node.d.ts +0 -4
  24. package/dist/scheduler/node.d.ts.map +1 -1
  25. package/dist/scheduler/node.js +11 -0
  26. package/dist/telemetry/config.d.ts +0 -5
  27. package/dist/telemetry/config.d.ts.map +1 -1
  28. package/dist/telemetry/config.js +0 -3
  29. package/dist/telemetry/index.d.ts +1 -1
  30. package/dist/telemetry/index.d.ts.map +1 -1
  31. package/dist/telemetry/index.js +1 -1
  32. package/dist/telemetry/metrics.d.ts +1 -10
  33. package/dist/telemetry/metrics.d.ts.map +1 -1
  34. package/dist/telemetry/metrics.js +9 -26
  35. package/dist/telemetry/otel-node.d.ts +37 -0
  36. package/dist/telemetry/otel-node.d.ts.map +1 -0
  37. package/dist/telemetry/otel-node.js +85 -0
  38. package/dist/telemetry/sdk.d.ts +12 -1
  39. package/dist/telemetry/sdk.d.ts.map +1 -1
  40. package/dist/telemetry/sdk.js +17 -27
  41. package/package.json +36 -7
  42. package/src/event-bus/event-bus.ts +4 -0
  43. package/src/index.ts +2 -11
  44. package/src/job-queue/db-job-queue.ts +178 -0
  45. package/src/job-queue/index.ts +1 -0
  46. package/src/job-queue/types.ts +3 -5
  47. package/src/logger.ts +4 -4
  48. package/src/scheduler/cloudflare.ts +8 -1
  49. package/src/scheduler/factory.ts +2 -15
  50. package/src/scheduler/node.ts +10 -0
  51. package/src/telemetry/config.ts +0 -8
  52. package/src/telemetry/index.ts +0 -6
  53. package/src/telemetry/metrics.ts +9 -38
  54. package/src/telemetry/otel-node.ts +116 -0
  55. package/src/telemetry/sdk.ts +18 -30
  56. package/dist/events/app-events.d.ts +0 -7
  57. package/dist/events/app-events.d.ts.map +0 -1
  58. package/dist/events/app-events.js +0 -4
  59. package/dist/events/create-system-bus.d.ts +0 -6
  60. package/dist/events/create-system-bus.d.ts.map +0 -1
  61. package/dist/events/create-system-bus.js +0 -7
  62. package/dist/events/index.d.ts +0 -3
  63. package/dist/events/index.d.ts.map +0 -1
  64. package/dist/events/index.js +0 -1
  65. package/src/events/app-events.ts +0 -8
  66. package/src/events/create-system-bus.ts +0 -8
  67. package/src/events/index.ts +0 -2
@@ -0,0 +1,178 @@
1
+ import type { QueueJobDao, QueueJobRecord, QueueStats } from '@gobing-ai/ts-db';
2
+ import {
3
+ getQueueJobCompletedTotal,
4
+ getQueueJobEnqueuedTotal,
5
+ getQueueJobFailedTotal,
6
+ getQueueJobProcessingDuration,
7
+ } from '../telemetry/metrics';
8
+ import type { EnqueueOptions, Job, JobHandler, JobQueue, QueueConsumer, QueueConsumerConfig } from './types';
9
+
10
+ /** DB-backed job queue implementation over `@gobing-ai/ts-db`'s `QueueJobDao`. */
11
+ export class DBJobQueue<T = unknown> implements JobQueue<T> {
12
+ constructor(readonly dao: QueueJobDao) {}
13
+
14
+ async enqueue(type: string, payload: T, options?: EnqueueOptions): Promise<string> {
15
+ const id = await this.dao.enqueue(type, payload, options);
16
+ getQueueJobEnqueuedTotal().add(1, { type });
17
+ return id;
18
+ }
19
+
20
+ async enqueueBatch(jobs: Array<{ type: string; payload: T } & EnqueueOptions>): Promise<string[]> {
21
+ const ids = await this.dao.enqueueBatch(jobs);
22
+ getQueueJobEnqueuedTotal().add(jobs.length);
23
+ return ids;
24
+ }
25
+
26
+ async stats(): Promise<QueueStats> {
27
+ return this.dao.getStats();
28
+ }
29
+ }
30
+
31
+ /** DB-backed queue consumer with polling, retry, and visibility-timeout handling. */
32
+ export class DBQueueConsumer<T = unknown> implements QueueConsumer<T> {
33
+ private readonly handlers = new Map<string, JobHandler<T>>();
34
+ private readonly pollInterval: number;
35
+ private readonly batchSize: number;
36
+ private readonly maxConcurrency: number;
37
+ private readonly visibilityTimeout: number;
38
+ private readonly baseDelay: number;
39
+ private readonly maxDelay: number;
40
+ private readonly drainTimeoutMs: number;
41
+ private timer: ReturnType<typeof setTimeout> | null = null;
42
+ private running = false;
43
+ private inFlight = 0;
44
+
45
+ constructor(
46
+ private readonly dao: QueueJobDao,
47
+ config: QueueConsumerConfig = {},
48
+ ) {
49
+ this.pollInterval = config.pollInterval ?? 1_000;
50
+ this.batchSize = config.batchSize ?? 10;
51
+ this.maxConcurrency = config.maxConcurrency ?? this.batchSize;
52
+ this.visibilityTimeout = config.visibilityTimeout ?? 30_000;
53
+ this.baseDelay = config.baseDelay ?? 1_000;
54
+ this.maxDelay = config.maxDelay ?? 60_000;
55
+ this.drainTimeoutMs = config.drainTimeoutMs ?? 30_000;
56
+ }
57
+
58
+ register(type: string, handler: JobHandler<T>): void {
59
+ this.handlers.set(type, handler);
60
+ }
61
+
62
+ async start(): Promise<void> {
63
+ if (this.running) return;
64
+ this.running = true;
65
+ this.schedule(0);
66
+ }
67
+
68
+ async stop(): Promise<void> {
69
+ this.running = false;
70
+ if (this.timer !== null) {
71
+ clearTimeout(this.timer);
72
+ this.timer = null;
73
+ }
74
+
75
+ const deadline = Date.now() + this.drainTimeoutMs;
76
+ while (this.inFlight > 0 && Date.now() < deadline) {
77
+ await sleep(10);
78
+ }
79
+ }
80
+
81
+ async stats(): Promise<QueueStats> {
82
+ return this.dao.getStats();
83
+ }
84
+
85
+ /** Process one batch immediately. Useful for tests, schedulers, and manual drains. */
86
+ async processOnce(): Promise<number> {
87
+ await this.dao.resetStuckJobs(this.visibilityTimeout);
88
+ await this.dao.failExpiredJobs();
89
+
90
+ const jobs = await this.dao.claimReady(this.batchSize);
91
+ let processed = 0;
92
+
93
+ for (let index = 0; index < jobs.length; index += this.maxConcurrency) {
94
+ const batch = jobs.slice(index, index + this.maxConcurrency);
95
+ await Promise.all(
96
+ batch.map(async (job) => {
97
+ this.inFlight += 1;
98
+ try {
99
+ await this.processJob(job);
100
+ processed += 1;
101
+ } finally {
102
+ this.inFlight -= 1;
103
+ }
104
+ }),
105
+ );
106
+ }
107
+
108
+ return processed;
109
+ }
110
+
111
+ private schedule(delay: number): void {
112
+ this.timer = setTimeout(() => {
113
+ void this.poll();
114
+ }, delay);
115
+ }
116
+
117
+ private async poll(): Promise<void> {
118
+ if (!this.running) return;
119
+ try {
120
+ await this.processOnce();
121
+ } finally {
122
+ if (this.running) this.schedule(this.pollInterval);
123
+ }
124
+ }
125
+
126
+ private async processJob(record: QueueJobRecord): Promise<void> {
127
+ const job = toJob<T>(record);
128
+ const handler = this.handlers.get(job.type);
129
+ if (handler === undefined) {
130
+ await this.failOrRetry(job, new Error(`No handler registered for job type "${job.type}"`));
131
+ return;
132
+ }
133
+
134
+ const startMs = performance.now();
135
+ try {
136
+ await handler(job);
137
+ await this.dao.markCompleted(job.id);
138
+ getQueueJobCompletedTotal().add(1, { type: job.type });
139
+ getQueueJobProcessingDuration().record(performance.now() - startMs, { type: job.type });
140
+ } catch (error) {
141
+ getQueueJobProcessingDuration().record(performance.now() - startMs, { type: job.type });
142
+ await this.failOrRetry(job, error);
143
+ }
144
+ }
145
+
146
+ private async failOrRetry(job: Job<T>, error: unknown): Promise<void> {
147
+ const attempts = job.attempts + 1;
148
+ const message = error instanceof Error ? error.message : String(error);
149
+ if (attempts >= job.maxRetries) {
150
+ await this.dao.markFailed(job.id, attempts, message);
151
+ getQueueJobFailedTotal().add(1, { type: job.type });
152
+ return;
153
+ }
154
+
155
+ const delay = Math.min(this.maxDelay, this.baseDelay * 2 ** Math.max(0, attempts - 1));
156
+ await this.dao.markForRetry(job.id, attempts, message, Date.now() + delay);
157
+ }
158
+ }
159
+
160
+ function toJob<T>(record: QueueJobRecord): Job<T> {
161
+ return {
162
+ id: record.id,
163
+ type: record.type,
164
+ payload: JSON.parse(record.payload) as T,
165
+ status: record.status as Job<T>['status'],
166
+ attempts: record.attempts,
167
+ maxRetries: record.maxRetries,
168
+ createdAt: record.createdAt,
169
+ updatedAt: record.updatedAt,
170
+ nextRetryAt: record.nextRetryAt,
171
+ lastError: record.lastError,
172
+ processingAt: record.processingAt,
173
+ };
174
+ }
175
+
176
+ function sleep(ms: number): Promise<void> {
177
+ return new Promise((resolve) => setTimeout(resolve, ms));
178
+ }
@@ -1,3 +1,4 @@
1
+ export { DBJobQueue, DBQueueConsumer } from './db-job-queue';
1
2
  export type {
2
3
  EnqueueOptions,
3
4
  Job,
@@ -1,10 +1,8 @@
1
1
  /**
2
2
  * Job queue types for async work processing with retry.
3
3
  *
4
- * **Types only** — the DB-backed `DbQueue` and `DbConsumer` implementations
5
- * that drive the job-queue subsystem live in `@spur/core`. These interfaces
6
- * are provided here so `EventBus` and other infra components can reference
7
- * the queue contract without a circular dependency on the DB layer.
4
+ * The concrete DB-backed implementations live beside these interfaces in
5
+ * `DBJobQueue` and `DBQueueConsumer`.
8
6
  */
9
7
 
10
8
  export interface Job<T = unknown> {
@@ -30,6 +28,7 @@ export interface EnqueueOptions {
30
28
  export interface JobQueue<T = unknown> {
31
29
  enqueue(type: string, payload: T, options?: EnqueueOptions): Promise<string>;
32
30
  enqueueBatch(jobs: Array<{ type: string; payload: T } & EnqueueOptions>): Promise<string[]>;
31
+ stats(): Promise<QueueStats>;
33
32
  }
34
33
 
35
34
  export type JobHandler<T = unknown> = (job: Job<T>) => Promise<void>;
@@ -49,7 +48,6 @@ export interface QueueConsumerConfig {
49
48
  baseDelay?: number;
50
49
  maxDelay?: number;
51
50
  drainTimeoutMs?: number;
52
- maxPollBackoff?: number;
53
51
  }
54
52
 
55
53
  export interface QueueConsumer<T = unknown> {
package/src/logger.ts CHANGED
@@ -30,14 +30,14 @@ class ConsoleLogger implements Logger {
30
30
 
31
31
  constructor(
32
32
  private readonly category: string,
33
- private readonly minLevel: LogLevel = 'info',
34
33
  context: Record<string, unknown> = {},
35
34
  ) {
36
35
  this.context = { category, ...context };
37
36
  }
38
37
 
39
38
  private log(level: LogLevel, msg: string, data?: Record<string, unknown>): void {
40
- if (LEVEL_ORDER[level] < LEVEL_ORDER[this.minLevel]) return;
39
+ // Read the level dynamically so re-initialization affects cached loggers too.
40
+ if (LEVEL_ORDER[level] < LEVEL_ORDER[globalLevel]) return;
41
41
  if (globalMuted) return;
42
42
 
43
43
  const entry = {
@@ -86,7 +86,7 @@ class ConsoleLogger implements Logger {
86
86
  }
87
87
 
88
88
  child(context: Record<string, unknown>): Logger {
89
- return new ConsoleLogger(this.category, this.minLevel, { ...this.context, ...context });
89
+ return new ConsoleLogger(this.category, { ...this.context, ...context });
90
90
  }
91
91
  }
92
92
 
@@ -109,7 +109,7 @@ export function getLogger(category: string): Logger {
109
109
  const existing = loggers.get(category);
110
110
  if (existing) return existing;
111
111
 
112
- const logger = new ConsoleLogger(category, globalLevel);
112
+ const logger = new ConsoleLogger(category);
113
113
  loggers.set(category, logger);
114
114
  return logger;
115
115
  }
@@ -2,6 +2,7 @@
2
2
  * Cloudflare Workers scheduler adapter using Cron Triggers.
3
3
  * Uses minimal local type declarations — no @cloudflare/workers-types dependency.
4
4
  */
5
+ import { getSchedulerJobExecutedTotal, getSchedulerJobFailedTotal } from '../telemetry/metrics';
5
6
  import type { ScheduledAction, SchedulerAdapter } from './types';
6
7
 
7
8
  interface CfScheduledEvent {
@@ -39,7 +40,13 @@ export class CloudflareSchedulerAdapter implements SchedulerAdapter {
39
40
  handleScheduledEvent(event: CfScheduledEvent, ctx: CfEventContext): void {
40
41
  const action = this.entries.get(event.cron);
41
42
  if (action) {
42
- ctx.waitUntil(action());
43
+ getSchedulerJobExecutedTotal().add(1, { cron: event.cron });
44
+ ctx.waitUntil(
45
+ action().catch((error: unknown) => {
46
+ getSchedulerJobFailedTotal().add(1, { cron: event.cron });
47
+ throw error;
48
+ }),
49
+ );
43
50
  }
44
51
  }
45
52
  }
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Scheduler factory — selects adapter based on runtime.
3
3
  */
4
+ import { NoopSchedulerAdapter } from './noop';
4
5
  import type { ScheduledAction, SchedulerAdapter } from './types';
5
6
 
6
7
  let runtimeAdapter: SchedulerAdapter | undefined;
@@ -30,7 +31,7 @@ export function getSchedulerAdapter(): SchedulerAdapter | undefined {
30
31
  export function initScheduler(cronEntries?: Array<[string, ScheduledAction]>): SchedulerAdapter {
31
32
  // Default: create a noop adapter. Apps inject their own via setSchedulerAdapter.
32
33
  if (!runtimeAdapter) {
33
- runtimeAdapter = createNoopScheduler();
34
+ runtimeAdapter = new NoopSchedulerAdapter();
34
35
  }
35
36
 
36
37
  if (cronEntries) {
@@ -41,17 +42,3 @@ export function initScheduler(cronEntries?: Array<[string, ScheduledAction]>): S
41
42
 
42
43
  return runtimeAdapter;
43
44
  }
44
-
45
- function createNoopScheduler(): SchedulerAdapter {
46
- return {
47
- register(): void {
48
- /* noop */
49
- },
50
- start(): Promise<void> {
51
- return Promise.resolve();
52
- },
53
- stop(): Promise<void> {
54
- return Promise.resolve();
55
- },
56
- };
57
- }
@@ -2,6 +2,11 @@
2
2
  * Node.js scheduler adapter using a simple setInterval-based approach.
3
3
  * No external cron library dependency — cron expressions are parsed minimally.
4
4
  */
5
+ import {
6
+ getSchedulerJobDuration,
7
+ getSchedulerJobExecutedTotal,
8
+ getSchedulerJobFailedTotal,
9
+ } from '../telemetry/metrics';
5
10
  import type { ScheduledAction, SchedulerAdapter } from './types';
6
11
 
7
12
  /** Simple helper to parse cron-like interval strings into milliseconds. */
@@ -74,10 +79,15 @@ export class NodeSchedulerAdapter implements SchedulerAdapter {
74
79
  }
75
80
 
76
81
  private async _onScheduledTick(entry: ScheduledEntry): Promise<void> {
82
+ const startMs = performance.now();
83
+ getSchedulerJobExecutedTotal().add(1, { cron: entry.cron });
77
84
  try {
78
85
  await entry.action();
79
86
  } catch {
80
87
  // Swallow — scheduler errors should not crash the process
88
+ getSchedulerJobFailedTotal().add(1, { cron: entry.cron });
89
+ } finally {
90
+ getSchedulerJobDuration().record(performance.now() - startMs, { cron: entry.cron });
81
91
  }
82
92
  }
83
93
  }
@@ -9,10 +9,6 @@ export interface TelemetryConfig {
9
9
  serviceName: string;
10
10
  /** Deployment environment (development, staging, production). */
11
11
  environment: string;
12
- /** OTLP exporter endpoint (e.g. `http://localhost:4318/v1/traces`). */
13
- exporterEndpoint?: string | undefined;
14
- /** Export protocol — only `http` is supported in v1. */
15
- exporterProtocol: 'http';
16
12
  /**
17
13
  * Debug-only DB statement capture.
18
14
  *
@@ -32,7 +28,6 @@ export interface TelemetryConfigPartial {
32
28
  enabled?: boolean | undefined;
33
29
  serviceName?: string | undefined;
34
30
  environment?: string | undefined;
35
- exporterEndpoint?: string | undefined;
36
31
  dbStatementDebug?: boolean | undefined;
37
32
  /** Deployment environment fallback (from app.env). */
38
33
  appEnv?: string | undefined;
@@ -42,7 +37,6 @@ const DEFAULTS = {
42
37
  enabled: true as const,
43
38
  serviceName: 'ts-libs' as const,
44
39
  environment: 'development' as const,
45
- exporterProtocol: 'http' as const,
46
40
  };
47
41
 
48
42
  /**
@@ -56,8 +50,6 @@ export function getTelemetryConfig(configPartial: TelemetryConfigPartial = {}):
56
50
  enabled,
57
51
  serviceName,
58
52
  environment: configPartial.environment ?? configPartial.appEnv ?? DEFAULTS.environment,
59
- exporterEndpoint: configPartial.exporterEndpoint,
60
- exporterProtocol: DEFAULTS.exporterProtocol,
61
53
  dbStatementDebug: configPartial.dbStatementDebug ?? false,
62
54
  };
63
55
  }
@@ -2,17 +2,11 @@ export { getTelemetryConfig, type TelemetryConfig, type TelemetryConfigPartial }
2
2
  export { extractSqlOperation, sanitizeSql } from './db-sanitize';
3
3
  export {
4
4
  type Counter,
5
- getDbOperationDuration,
6
- getDbOperationErrors,
7
- getDbOperationTotal,
8
5
  getEventbusEmitsTotal,
9
6
  getEventbusErrorsTotal,
10
7
  getHttpClientRequestDuration,
11
8
  getHttpClientRequestErrors,
12
9
  getHttpClientRequestTotal,
13
- getHttpServerRequestDuration,
14
- getHttpServerRequestErrors,
15
- getHttpServerRequestTotal,
16
10
  getQueueJobCompletedTotal,
17
11
  getQueueJobEnqueuedTotal,
18
12
  getQueueJobFailedTotal,
@@ -3,28 +3,21 @@
3
3
  * All degrade to no-ops when telemetry is disabled.
4
4
  */
5
5
  import { type Counter, type Histogram, metrics } from '@opentelemetry/api';
6
- import type { MeterProvider } from '@opentelemetry/sdk-metrics';
7
6
 
8
7
  export type { Counter, Histogram } from '@opentelemetry/api';
9
8
 
10
- import type { TelemetryConfig } from './config';
11
-
12
- let meterProvider: MeterProvider | undefined;
13
9
  let metricsInitialized = false;
14
10
 
15
11
  export function isMetricsInitialized(): boolean {
16
12
  return metricsInitialized;
17
13
  }
18
14
 
19
- export function getMeterProvider(): MeterProvider {
20
- return meterProvider ?? (metrics.getMeterProvider() as MeterProvider);
21
- }
22
-
23
15
  const METER_NAME = '@gobing-ai/ts-infra';
24
16
  const METER_VERSION = '0.1.0';
25
17
 
18
+ /** Meter from the globally-registered provider (no-op when none registered). */
26
19
  function getMeter() {
27
- return getMeterProvider().getMeter(METER_NAME, METER_VERSION);
20
+ return metrics.getMeter(METER_NAME, METER_VERSION);
28
21
  }
29
22
 
30
23
  // ── Instrument cache ────────────────────────────────────────────────
@@ -45,20 +38,6 @@ function getOrCreateHistogram(key: string, name: string, description: string, un
45
38
  return instruments[key] as Histogram;
46
39
  }
47
40
 
48
- // ── HTTP server ─────────────────────────────────────────────────────
49
-
50
- export function getHttpServerRequestTotal(): Counter {
51
- return getOrCreateCounter('httpSrvReq', 'http.server.request.total', 'Total inbound HTTP requests', '{request}');
52
- }
53
-
54
- export function getHttpServerRequestDuration(): Histogram {
55
- return getOrCreateHistogram('httpSrvDur', 'http.server.request.duration', 'Inbound HTTP request duration');
56
- }
57
-
58
- export function getHttpServerRequestErrors(): Counter {
59
- return getOrCreateCounter('httpSrvErr', 'http.server.request.errors', 'Inbound HTTP 5xx errors', '{error}');
60
- }
61
-
62
41
  // ── HTTP client ─────────────────────────────────────────────────────
63
42
 
64
43
  export function getHttpClientRequestTotal(): Counter {
@@ -73,20 +52,6 @@ export function getHttpClientRequestErrors(): Counter {
73
52
  return getOrCreateCounter('httpCliErr', 'http.client.request.errors', 'Outbound HTTP errors', '{error}');
74
53
  }
75
54
 
76
- // ── DB ──────────────────────────────────────────────────────────────
77
-
78
- export function getDbOperationTotal(): Counter {
79
- return getOrCreateCounter('dbOpTotal', 'db.client.operation.total', 'Total DB operations', '{operation}');
80
- }
81
-
82
- export function getDbOperationDuration(): Histogram {
83
- return getOrCreateHistogram('dbOpDur', 'db.client.operation.duration', 'DB operation duration');
84
- }
85
-
86
- export function getDbOperationErrors(): Counter {
87
- return getOrCreateCounter('dbOpErr', 'db.client.operation.errors', 'DB operation errors', '{error}');
88
- }
89
-
90
55
  // ── Event bus ───────────────────────────────────────────────────────
91
56
 
92
57
  export function getEventbusEmitsTotal(): Counter {
@@ -131,12 +96,18 @@ export function getSchedulerJobFailedTotal(): Counter {
131
96
 
132
97
  // ── Lifecycle ───────────────────────────────────────────────────────
133
98
 
134
- export function initMetrics(_config?: Partial<TelemetryConfig>): void {
99
+ export function initMetrics(): void {
135
100
  if (metricsInitialized) return;
136
101
  metricsInitialized = true;
137
102
  }
138
103
 
139
104
  export function shutdownMetrics(): Promise<void> {
105
+ // The meter provider is owned by whoever registered it globally (the host
106
+ // app or `@gobing-ai/ts-infra/otel-node`). Here we only drop the instrument
107
+ // cache so post-shutdown getters rebuild against a fresh meter.
108
+ for (const key of Object.keys(instruments)) {
109
+ instruments[key] = undefined;
110
+ }
140
111
  metricsInitialized = false;
141
112
  return Promise.resolve();
142
113
  }
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Opt-in Node OTLP wiring for `@gobing-ai/ts-infra`.
3
+ *
4
+ * The core telemetry surface (`initTelemetry`, `getTracer`, the metric getters)
5
+ * only *instruments* — it records spans and metrics against whatever OTel
6
+ * provider is registered globally, and degrades to no-ops when none is. This
7
+ * subpath is the convenience layer that actually *exports*: it builds Node
8
+ * tracer + meter providers wired to OTLP/HTTP and registers them globally, so
9
+ * the already-instrumented call sites start flowing without any core change.
10
+ *
11
+ * Import this only when you want turnkey export. It pulls the OTLP exporter
12
+ * packages (declared as optional peers); the main barrel never imports them, so
13
+ * BYO-collector consumers stay lean and avoid global-provider conflicts.
14
+ */
15
+ import { metrics } from '@opentelemetry/api';
16
+ import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
17
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
18
+ import { resourceFromAttributes } from '@opentelemetry/resources';
19
+ import { MeterProvider, type MetricReader, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
20
+ import { BatchSpanProcessor, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
21
+ import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
22
+
23
+ /** Options for {@link initNodeTelemetry}. */
24
+ export interface NodeTelemetryOptions {
25
+ /** Logical service name emitted on every span/metric (resource attribute). */
26
+ serviceName: string;
27
+ /** Optional service version (resource attribute). */
28
+ serviceVersion?: string;
29
+ /**
30
+ * OTLP/HTTP collector endpoint base, e.g. `http://localhost:4318`.
31
+ * Signal-specific paths (`/v1/traces`, `/v1/metrics`) are appended by the
32
+ * exporters. When omitted, the OTel SDK default (`http://localhost:4318`)
33
+ * applies, honouring the standard `OTEL_EXPORTER_OTLP_*` env vars.
34
+ */
35
+ endpoint?: string;
36
+ /** Extra headers sent on every OTLP request (e.g. an auth token). */
37
+ headers?: Record<string, string>;
38
+ /** Metric export interval in ms. Default: 60_000. */
39
+ metricExportIntervalMs?: number;
40
+ /** When false, skip metric export and wire traces only. Default: true. */
41
+ metrics?: boolean;
42
+ }
43
+
44
+ let traceProvider: NodeTracerProvider | undefined;
45
+ let meterProvider: MeterProvider | undefined;
46
+
47
+ /**
48
+ * Wire Node OTLP/HTTP export for traces and (by default) metrics, registering
49
+ * both providers globally. Idempotent: a second call is a no-op until
50
+ * {@link shutdownNodeTelemetry} runs.
51
+ *
52
+ * Call once at process startup, before the first span/metric is recorded.
53
+ */
54
+ export function initNodeTelemetry(options: NodeTelemetryOptions): void {
55
+ if (traceProvider) return;
56
+
57
+ const { serviceName, serviceVersion, endpoint, headers, metricExportIntervalMs = 60_000 } = options;
58
+ const wireMetrics = options.metrics ?? true;
59
+
60
+ const resource = resourceFromAttributes({
61
+ [ATTR_SERVICE_NAME]: serviceName,
62
+ ...(serviceVersion ? { [ATTR_SERVICE_VERSION]: serviceVersion } : {}),
63
+ });
64
+
65
+ const traceExporter = new OTLPTraceExporter({
66
+ ...(endpoint ? { url: `${endpoint.replace(/\/$/, '')}/v1/traces` } : {}),
67
+ ...(headers ? { headers } : {}),
68
+ });
69
+
70
+ traceProvider = new NodeTracerProvider({
71
+ resource,
72
+ spanProcessors: [new BatchSpanProcessor(traceExporter)],
73
+ });
74
+ traceProvider.register();
75
+
76
+ if (wireMetrics) {
77
+ const metricExporter = new OTLPMetricExporter({
78
+ ...(endpoint ? { url: `${endpoint.replace(/\/$/, '')}/v1/metrics` } : {}),
79
+ ...(headers ? { headers } : {}),
80
+ });
81
+
82
+ const reader: MetricReader = new PeriodicExportingMetricReader({
83
+ exporter: metricExporter,
84
+ exportIntervalMillis: metricExportIntervalMs,
85
+ });
86
+
87
+ meterProvider = new MeterProvider({ resource, readers: [reader] });
88
+ metrics.setGlobalMeterProvider(meterProvider);
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Flush and shut down the providers installed by {@link initNodeTelemetry}.
94
+ * Safe to call when nothing was initialised. Wire this to SIGTERM/SIGINT for a
95
+ * clean drain of buffered spans and metrics on shutdown.
96
+ */
97
+ export async function shutdownNodeTelemetry(): Promise<void> {
98
+ const pending: Array<Promise<void>> = [];
99
+ if (meterProvider) {
100
+ pending.push(meterProvider.shutdown());
101
+ meterProvider = undefined;
102
+ }
103
+ if (traceProvider) {
104
+ pending.push(traceProvider.shutdown());
105
+ traceProvider = undefined;
106
+ }
107
+ metrics.disable();
108
+ await Promise.all(pending);
109
+ }
110
+
111
+ /** Test-only: reset module state without requiring exporter teardown to resolve. */
112
+ export function _resetNodeTelemetry(): void {
113
+ meterProvider = undefined;
114
+ traceProvider = undefined;
115
+ metrics.disable();
116
+ }
@@ -1,48 +1,44 @@
1
1
  /**
2
- * OpenTelemetry SDK initialisation and tracer provider management.
2
+ * Telemetry enablement + tracer access.
3
+ *
4
+ * The core only *instruments* against whatever tracer provider is registered
5
+ * globally — it never constructs or owns a provider. Provider + exporter wiring
6
+ * is the consumer's concern: either the host app registers its own OTel SDK, or
7
+ * it opts into `@gobing-ai/ts-infra/otel-node` for turnkey OTLP export. This
8
+ * keeps the main barrel free of any SDK runtime dependency.
3
9
  */
4
10
  import { type Tracer, trace } from '@opentelemetry/api';
5
- import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
6
11
  import type { TelemetryConfig } from './config';
7
12
  import { getTelemetryConfig } from './config';
8
13
 
9
- let tracerProvider: NodeTracerProvider | undefined;
14
+ const TRACER_NAME = '@gobing-ai/ts-infra';
15
+ const TRACER_VERSION = '0.1.0';
16
+
10
17
  let telemetryInitialized = false;
11
18
  let resolvedConfig: TelemetryConfig = getTelemetryConfig();
12
19
 
13
- const DEFAULT_TRACER = trace.getTracer('@gobing-ai/ts-infra', '0.1.0');
14
-
15
20
  export function getResolvedConfig(): TelemetryConfig {
16
21
  return resolvedConfig;
17
22
  }
18
23
 
24
+ /**
25
+ * Resolve telemetry config and mark telemetry enabled. Does not register any
26
+ * provider — spans flow to the globally-registered provider (none ⇒ no-op).
27
+ */
19
28
  export function initTelemetry(config?: Partial<TelemetryConfig>): void {
20
29
  if (telemetryInitialized) return;
21
-
22
30
  resolvedConfig = { ...getTelemetryConfig(), ...config };
23
-
24
- if (!resolvedConfig.enabled) {
25
- telemetryInitialized = true;
26
- return;
27
- }
28
-
29
- tracerProvider = new NodeTracerProvider();
30
- tracerProvider.register();
31
31
  telemetryInitialized = true;
32
32
  }
33
33
 
34
- export async function shutdownTelemetry(): Promise<void> {
35
- if (!tracerProvider) {
36
- telemetryInitialized = false;
37
- return;
38
- }
39
- await tracerProvider.shutdown();
40
- tracerProvider = undefined;
34
+ export function shutdownTelemetry(): Promise<void> {
41
35
  telemetryInitialized = false;
36
+ return Promise.resolve();
42
37
  }
43
38
 
39
+ /** The infra tracer from the globally-registered provider (no-op when none). */
44
40
  export function getTracer(): Tracer {
45
- return tracerProvider?.getTracer('@gobing-ai/ts-infra', '0.1.0') ?? DEFAULT_TRACER;
41
+ return trace.getTracer(TRACER_NAME, TRACER_VERSION);
46
42
  }
47
43
 
48
44
  export function isTelemetryEnabled(): boolean {
@@ -50,14 +46,6 @@ export function isTelemetryEnabled(): boolean {
50
46
  }
51
47
 
52
48
  export function _resetTelemetry(): void {
53
- if (tracerProvider) {
54
- try {
55
- tracerProvider.shutdown();
56
- } catch {
57
- /* swallow */
58
- }
59
- tracerProvider = undefined;
60
- }
61
49
  telemetryInitialized = false;
62
50
  resolvedConfig = getTelemetryConfig();
63
51
  }