@gobing-ai/ts-infra 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/README.md +389 -0
- package/dist/api-client.d.ts +31 -0
- package/dist/api-client.d.ts.map +1 -0
- package/dist/api-client.js +112 -0
- package/dist/event-bus/event-bus.d.ts +33 -0
- package/dist/event-bus/event-bus.d.ts.map +1 -0
- package/dist/event-bus/event-bus.js +211 -0
- package/dist/event-bus/index.d.ts +3 -0
- package/dist/event-bus/index.d.ts.map +1 -0
- package/dist/event-bus/index.js +1 -0
- package/dist/event-bus/types.d.ts +35 -0
- package/dist/event-bus/types.d.ts.map +1 -0
- package/dist/event-bus/types.js +3 -0
- package/dist/events/app-events.d.ts +7 -0
- package/dist/events/app-events.d.ts.map +1 -0
- package/dist/events/app-events.js +4 -0
- package/dist/events/create-system-bus.d.ts +6 -0
- package/dist/events/create-system-bus.d.ts.map +1 -0
- package/dist/events/create-system-bus.js +7 -0
- package/dist/events/index.d.ts +3 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +9 -0
- package/dist/job-queue/index.d.ts +2 -0
- package/dist/job-queue/index.d.ts.map +1 -0
- package/dist/job-queue/index.js +0 -0
- package/dist/job-queue/types.d.ts +57 -0
- package/dist/job-queue/types.d.ts.map +1 -0
- package/dist/job-queue/types.js +8 -0
- package/dist/logger.d.ts +28 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +100 -0
- package/dist/scheduler/action.d.ts +5 -0
- package/dist/scheduler/action.d.ts.map +1 -0
- package/dist/scheduler/action.js +0 -0
- package/dist/scheduler/cloudflare.d.ts +27 -0
- package/dist/scheduler/cloudflare.d.ts.map +1 -0
- package/dist/scheduler/cloudflare.js +24 -0
- package/dist/scheduler/factory.d.ts +19 -0
- package/dist/scheduler/factory.d.ts.map +1 -0
- package/dist/scheduler/factory.js +45 -0
- package/dist/scheduler/index.d.ts +6 -0
- package/dist/scheduler/index.d.ts.map +1 -0
- package/dist/scheduler/index.js +4 -0
- package/dist/scheduler/node.d.ts +16 -0
- package/dist/scheduler/node.d.ts.map +1 -0
- package/dist/scheduler/node.js +63 -0
- package/dist/scheduler/noop.d.ts +11 -0
- package/dist/scheduler/noop.d.ts.map +1 -0
- package/dist/scheduler/noop.js +12 -0
- package/dist/scheduler/types.d.ts +12 -0
- package/dist/scheduler/types.d.ts.map +1 -0
- package/dist/scheduler/types.js +3 -0
- package/dist/telemetry/config.d.ts +42 -0
- package/dist/telemetry/config.d.ts.map +1 -0
- package/dist/telemetry/config.js +24 -0
- package/dist/telemetry/db-sanitize.d.ts +15 -0
- package/dist/telemetry/db-sanitize.d.ts.map +1 -0
- package/dist/telemetry/db-sanitize.js +72 -0
- package/dist/telemetry/index.d.ts +7 -0
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +5 -0
- package/dist/telemetry/metrics.d.ts +32 -0
- package/dist/telemetry/metrics.d.ts.map +1 -0
- package/dist/telemetry/metrics.js +109 -0
- package/dist/telemetry/sdk.d.ts +13 -0
- package/dist/telemetry/sdk.d.ts.map +1 -0
- package/dist/telemetry/sdk.js +54 -0
- package/dist/telemetry/tracing.d.ts +13 -0
- package/dist/telemetry/tracing.d.ts.map +1 -0
- package/dist/telemetry/tracing.js +54 -0
- package/package.json +50 -0
- package/src/api-client.ts +162 -0
- package/src/event-bus/event-bus.ts +236 -0
- package/src/event-bus/index.ts +9 -0
- package/src/event-bus/types.ts +40 -0
- package/src/events/app-events.ts +8 -0
- package/src/events/create-system-bus.ts +8 -0
- package/src/events/index.ts +2 -0
- package/src/index.ts +74 -0
- package/src/job-queue/index.ts +9 -0
- package/src/job-queue/types.ts +60 -0
- package/src/logger.ts +123 -0
- package/src/scheduler/action.ts +4 -0
- package/src/scheduler/cloudflare.ts +45 -0
- package/src/scheduler/factory.ts +57 -0
- package/src/scheduler/index.ts +5 -0
- package/src/scheduler/node.ts +83 -0
- package/src/scheduler/noop.ts +20 -0
- package/src/scheduler/types.ts +13 -0
- package/src/telemetry/config.ts +63 -0
- package/src/telemetry/db-sanitize.ts +79 -0
- package/src/telemetry/index.ts +29 -0
- package/src/telemetry/metrics.ts +150 -0
- package/src/telemetry/sdk.ts +65 -0
- package/src/telemetry/tracing.ts +64 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scheduler factory — selects adapter based on runtime.
|
|
3
|
+
*/
|
|
4
|
+
import type { ScheduledAction, SchedulerAdapter } from './types';
|
|
5
|
+
export declare function setSchedulerAdapter(adapter: SchedulerAdapter): void;
|
|
6
|
+
/** Reset the scheduler adapter singleton. For testing. */
|
|
7
|
+
export declare function resetSchedulerAdapter(): void;
|
|
8
|
+
export declare function getSchedulerAdapter(): SchedulerAdapter | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Initialize the scheduler adapter and register cron entries.
|
|
11
|
+
*
|
|
12
|
+
* MUST be called before `adapter.start()`. If the adapter is already
|
|
13
|
+
* running, newly registered entries will NOT be started until the next
|
|
14
|
+
* `start()` call.
|
|
15
|
+
*
|
|
16
|
+
* Returns the configured adapter (defaults to noop if none set).
|
|
17
|
+
*/
|
|
18
|
+
export declare function initScheduler(cronEntries?: Array<[string, ScheduledAction]>): SchedulerAdapter;
|
|
19
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../src/scheduler/factory.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIjE,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAEnE;AAED,0DAA0D;AAC1D,wBAAgB,qBAAqB,IAAI,IAAI,CAE5C;AAED,wBAAgB,mBAAmB,IAAI,gBAAgB,GAAG,SAAS,CAElE;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,GAAG,gBAAgB,CAa9F"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
let runtimeAdapter;
|
|
2
|
+
export function setSchedulerAdapter(adapter) {
|
|
3
|
+
runtimeAdapter = adapter;
|
|
4
|
+
}
|
|
5
|
+
/** Reset the scheduler adapter singleton. For testing. */
|
|
6
|
+
export function resetSchedulerAdapter() {
|
|
7
|
+
runtimeAdapter = undefined;
|
|
8
|
+
}
|
|
9
|
+
export function getSchedulerAdapter() {
|
|
10
|
+
return runtimeAdapter;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Initialize the scheduler adapter and register cron entries.
|
|
14
|
+
*
|
|
15
|
+
* MUST be called before `adapter.start()`. If the adapter is already
|
|
16
|
+
* running, newly registered entries will NOT be started until the next
|
|
17
|
+
* `start()` call.
|
|
18
|
+
*
|
|
19
|
+
* Returns the configured adapter (defaults to noop if none set).
|
|
20
|
+
*/
|
|
21
|
+
export function initScheduler(cronEntries) {
|
|
22
|
+
// Default: create a noop adapter. Apps inject their own via setSchedulerAdapter.
|
|
23
|
+
if (!runtimeAdapter) {
|
|
24
|
+
runtimeAdapter = createNoopScheduler();
|
|
25
|
+
}
|
|
26
|
+
if (cronEntries) {
|
|
27
|
+
for (const [cron, action] of cronEntries) {
|
|
28
|
+
runtimeAdapter.register(cron, action);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return runtimeAdapter;
|
|
32
|
+
}
|
|
33
|
+
function createNoopScheduler() {
|
|
34
|
+
return {
|
|
35
|
+
register() {
|
|
36
|
+
/* noop */
|
|
37
|
+
},
|
|
38
|
+
start() {
|
|
39
|
+
return Promise.resolve();
|
|
40
|
+
},
|
|
41
|
+
stop() {
|
|
42
|
+
return Promise.resolve();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { CloudflareSchedulerAdapter } from './cloudflare';
|
|
2
|
+
export { getSchedulerAdapter, initScheduler, resetSchedulerAdapter, setSchedulerAdapter } from './factory';
|
|
3
|
+
export { NodeSchedulerAdapter } from './node';
|
|
4
|
+
export { NoopSchedulerAdapter } from './noop';
|
|
5
|
+
export type { ScheduledAction, SchedulerAdapter } from './types';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/scheduler/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAC3G,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAC9C,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CloudflareSchedulerAdapter } from './cloudflare.js';
|
|
2
|
+
export { getSchedulerAdapter, initScheduler, resetSchedulerAdapter, setSchedulerAdapter } from './factory.js';
|
|
3
|
+
export { NodeSchedulerAdapter } from './node.js';
|
|
4
|
+
export { NoopSchedulerAdapter } from './noop.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js scheduler adapter using a simple setInterval-based approach.
|
|
3
|
+
* No external cron library dependency — cron expressions are parsed minimally.
|
|
4
|
+
*/
|
|
5
|
+
import type { ScheduledAction, SchedulerAdapter } from './types';
|
|
6
|
+
export declare class NodeSchedulerAdapter implements SchedulerAdapter {
|
|
7
|
+
private readonly entries;
|
|
8
|
+
private running;
|
|
9
|
+
constructor();
|
|
10
|
+
register(cron: string, action: ScheduledAction): void;
|
|
11
|
+
start(): Promise<void>;
|
|
12
|
+
stop(): Promise<void>;
|
|
13
|
+
private startEntry;
|
|
14
|
+
private _onScheduledTick;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../src/scheduler/node.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AA6BjE,qBAAa,oBAAqB,YAAW,gBAAgB;IACzD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwB;IAChD,OAAO,CAAC,OAAO,CAAS;;IAIxB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI;IAU/C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAStB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAU3B,OAAO,CAAC,UAAU;YAOJ,gBAAgB;CAOjC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** Simple helper to parse cron-like interval strings into milliseconds. */
|
|
2
|
+
function parseInterval(cron) {
|
|
3
|
+
// Support simple patterns: "* * * * *" (every minute), "*/5 * * * *" (every 5 min)
|
|
4
|
+
// Also support direct ms strings like "60000"
|
|
5
|
+
const num = Number(cron);
|
|
6
|
+
if (!Number.isNaN(num))
|
|
7
|
+
return num;
|
|
8
|
+
const parts = cron.trim().split(/\s+/);
|
|
9
|
+
if (parts.length === 5 && parts[0] === '*') {
|
|
10
|
+
return 60_000; // every minute default
|
|
11
|
+
}
|
|
12
|
+
// */N pattern
|
|
13
|
+
const match = parts[0]?.match(/^\*\/(\d+)$/);
|
|
14
|
+
if (match) {
|
|
15
|
+
return Number(match[1]) * 60_000;
|
|
16
|
+
}
|
|
17
|
+
return 60_000; // fallback: every minute
|
|
18
|
+
}
|
|
19
|
+
export class NodeSchedulerAdapter {
|
|
20
|
+
entries = [];
|
|
21
|
+
running = false;
|
|
22
|
+
constructor() { }
|
|
23
|
+
register(cron, action) {
|
|
24
|
+
this.entries.push({ cron, action });
|
|
25
|
+
if (this.running) {
|
|
26
|
+
const last = this.entries[this.entries.length - 1];
|
|
27
|
+
if (last) {
|
|
28
|
+
this.startEntry(last);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async start() {
|
|
33
|
+
if (this.running)
|
|
34
|
+
return;
|
|
35
|
+
this.running = true;
|
|
36
|
+
for (const entry of this.entries) {
|
|
37
|
+
this.startEntry(entry);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async stop() {
|
|
41
|
+
this.running = false;
|
|
42
|
+
for (const entry of this.entries) {
|
|
43
|
+
if (entry.timer) {
|
|
44
|
+
clearInterval(entry.timer);
|
|
45
|
+
entry.timer = undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
startEntry(entry) {
|
|
50
|
+
if (entry.timer)
|
|
51
|
+
return;
|
|
52
|
+
const interval = parseInterval(entry.cron);
|
|
53
|
+
entry.timer = setInterval(this._onScheduledTick.bind(this, entry), interval);
|
|
54
|
+
}
|
|
55
|
+
async _onScheduledTick(entry) {
|
|
56
|
+
try {
|
|
57
|
+
await entry.action();
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
// Swallow — scheduler errors should not crash the process
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* No-op scheduler adapter for testing and environments without scheduling.
|
|
3
|
+
*/
|
|
4
|
+
import type { ScheduledAction, SchedulerAdapter } from './types';
|
|
5
|
+
export declare class NoopSchedulerAdapter implements SchedulerAdapter {
|
|
6
|
+
constructor();
|
|
7
|
+
register(_cron: string, _action: ScheduledAction): void;
|
|
8
|
+
start(): Promise<void>;
|
|
9
|
+
stop(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=noop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../src/scheduler/noop.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEjE,qBAAa,oBAAqB,YAAW,gBAAgB;;IAGzD,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,IAAI;IAIjD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAG9B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scheduler types and interface.
|
|
3
|
+
*/
|
|
4
|
+
/** Signature for scheduled action handlers. */
|
|
5
|
+
export type ScheduledAction = () => Promise<void>;
|
|
6
|
+
/** Abstract scheduler interface — implementations for Node (node-cron) and Cloudflare. */
|
|
7
|
+
export interface SchedulerAdapter {
|
|
8
|
+
register(cron: string, action: ScheduledAction): void;
|
|
9
|
+
start(): Promise<void>;
|
|
10
|
+
stop(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/scheduler/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,+CAA+C;AAC/C,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;AAElD,0FAA0F;AAC1F,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI,CAAC;IACtD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry configuration interface.
|
|
3
|
+
*/
|
|
4
|
+
export interface TelemetryConfig {
|
|
5
|
+
/** Master switch — when false, all tracing degrades to no-ops. */
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
/** Logical service name emitted on every span. */
|
|
8
|
+
serviceName: string;
|
|
9
|
+
/** Deployment environment (development, staging, production). */
|
|
10
|
+
environment: string;
|
|
11
|
+
/** OTLP exporter endpoint (e.g. `http://localhost:4318/v1/traces`). */
|
|
12
|
+
exporterEndpoint?: string | undefined;
|
|
13
|
+
/** Export protocol — only `http` is supported in v1. */
|
|
14
|
+
exporterProtocol: 'http';
|
|
15
|
+
/**
|
|
16
|
+
* Debug-only DB statement capture.
|
|
17
|
+
*
|
|
18
|
+
* When true, DB spans may include sanitized SQL text in a `db.statement`
|
|
19
|
+
* attribute. SQL text is redacted — parameter values, literals, and
|
|
20
|
+
* identifiers are stripped before capture.
|
|
21
|
+
*
|
|
22
|
+
* Default: `false`. Controlled by `OTEL_DB_STATEMENT_DEBUG` env var.
|
|
23
|
+
*/
|
|
24
|
+
dbStatementDebug: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Partial telemetry config from the centralized config system.
|
|
28
|
+
*/
|
|
29
|
+
export interface TelemetryConfigPartial {
|
|
30
|
+
enabled?: boolean | undefined;
|
|
31
|
+
serviceName?: string | undefined;
|
|
32
|
+
environment?: string | undefined;
|
|
33
|
+
exporterEndpoint?: string | undefined;
|
|
34
|
+
dbStatementDebug?: boolean | undefined;
|
|
35
|
+
/** Deployment environment fallback (from app.env). */
|
|
36
|
+
appEnv?: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolve the full telemetry config by merging a partial override with defaults.
|
|
40
|
+
*/
|
|
41
|
+
export declare function getTelemetryConfig(configPartial?: TelemetryConfigPartial): TelemetryConfig;
|
|
42
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/telemetry/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,eAAe;IAC5B,kEAAkE;IAClE,OAAO,EAAE,OAAO,CAAC;IACjB,kDAAkD;IAClD,WAAW,EAAE,MAAM,CAAC;IACpB,iEAAiE;IACjE,WAAW,EAAE,MAAM,CAAC;IACpB,uEAAuE;IACvE,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,wDAAwD;IACxD,gBAAgB,EAAE,MAAM,CAAC;IACzB;;;;;;;;OAQG;IACH,gBAAgB,EAAE,OAAO,CAAC;CAC7B;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACnC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,gBAAgB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACvC,sDAAsD;IACtD,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B;AASD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,GAAE,sBAA2B,GAAG,eAAe,CAY9F"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry configuration interface.
|
|
3
|
+
*/
|
|
4
|
+
const DEFAULTS = {
|
|
5
|
+
enabled: true,
|
|
6
|
+
serviceName: 'ts-libs',
|
|
7
|
+
environment: 'development',
|
|
8
|
+
exporterProtocol: 'http',
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Resolve the full telemetry config by merging a partial override with defaults.
|
|
12
|
+
*/
|
|
13
|
+
export function getTelemetryConfig(configPartial = {}) {
|
|
14
|
+
const enabled = configPartial.enabled ?? DEFAULTS.enabled;
|
|
15
|
+
const serviceName = configPartial.serviceName ?? DEFAULTS.serviceName;
|
|
16
|
+
return {
|
|
17
|
+
enabled,
|
|
18
|
+
serviceName,
|
|
19
|
+
environment: configPartial.environment ?? configPartial.appEnv ?? DEFAULTS.environment,
|
|
20
|
+
exporterEndpoint: configPartial.exporterEndpoint,
|
|
21
|
+
exporterProtocol: DEFAULTS.exporterProtocol,
|
|
22
|
+
dbStatementDebug: configPartial.dbStatementDebug ?? false,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL sanitization for debug-mode DB statement capture.
|
|
3
|
+
*
|
|
4
|
+
* Strips parameter values, string literals, numeric literals, and
|
|
5
|
+
* identifier-specific data from SQL text before it is attached to spans.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Redact string literals and numeric literals from SQL text.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sanitizeSql(sql: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Extract the SQL operation keyword (SELECT, INSERT, UPDATE, DELETE, etc.)
|
|
13
|
+
*/
|
|
14
|
+
export declare function extractSqlOperation(sql: string): string | undefined;
|
|
15
|
+
//# sourceMappingURL=db-sanitize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-sanitize.d.ts","sourceRoot":"","sources":["../../src/telemetry/db-sanitize.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CA4D/C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGnE"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL sanitization for debug-mode DB statement capture.
|
|
3
|
+
*
|
|
4
|
+
* Strips parameter values, string literals, numeric literals, and
|
|
5
|
+
* identifier-specific data from SQL text before it is attached to spans.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Redact string literals and numeric literals from SQL text.
|
|
9
|
+
*/
|
|
10
|
+
export function sanitizeSql(sql) {
|
|
11
|
+
let result = '';
|
|
12
|
+
let i = 0;
|
|
13
|
+
const len = sql.length;
|
|
14
|
+
while (i < len) {
|
|
15
|
+
const code = sql.charCodeAt(i);
|
|
16
|
+
// Quote characters: ' (39) or " (34)
|
|
17
|
+
if (code === 39 || code === 34) {
|
|
18
|
+
const quote = code;
|
|
19
|
+
i++;
|
|
20
|
+
while (i < len) {
|
|
21
|
+
if (sql.charCodeAt(i) === quote) {
|
|
22
|
+
if (sql.charCodeAt(i + 1) === quote) {
|
|
23
|
+
i += 2;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
i++;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
i++;
|
|
30
|
+
}
|
|
31
|
+
result += '?';
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
// Numeric literal
|
|
35
|
+
if (code >= 48 && code <= 57) {
|
|
36
|
+
const prev = result.length > 0 ? result.charCodeAt(result.length - 1) : 0;
|
|
37
|
+
const isIdentifierContinuation = (prev >= 65 && prev <= 90) || (prev >= 97 && prev <= 122) || prev === 95 || (prev >= 48 && prev <= 57);
|
|
38
|
+
if (!isIdentifierContinuation) {
|
|
39
|
+
while (i < len) {
|
|
40
|
+
const c = sql.charCodeAt(i);
|
|
41
|
+
if (c < 48 || c > 57)
|
|
42
|
+
break;
|
|
43
|
+
i++;
|
|
44
|
+
}
|
|
45
|
+
if (i < len && sql.charCodeAt(i) === 46 && i + 1 < len) {
|
|
46
|
+
const next = sql.charCodeAt(i + 1);
|
|
47
|
+
if (next >= 48 && next <= 57) {
|
|
48
|
+
i++;
|
|
49
|
+
while (i < len) {
|
|
50
|
+
const c = sql.charCodeAt(i);
|
|
51
|
+
if (c < 48 || c > 57)
|
|
52
|
+
break;
|
|
53
|
+
i++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
result += '?';
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
result += sql[i] ?? '';
|
|
62
|
+
i++;
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Extract the SQL operation keyword (SELECT, INSERT, UPDATE, DELETE, etc.)
|
|
68
|
+
*/
|
|
69
|
+
export function extractSqlOperation(sql) {
|
|
70
|
+
const match = sql.trim().match(/^\s*(SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|PRAGMA)\b/i);
|
|
71
|
+
return match?.[1]?.toUpperCase();
|
|
72
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { getTelemetryConfig, type TelemetryConfig, type TelemetryConfigPartial } from './config';
|
|
2
|
+
export { extractSqlOperation, sanitizeSql } from './db-sanitize';
|
|
3
|
+
export { type Counter, getDbOperationDuration, getDbOperationErrors, getDbOperationTotal, getEventbusEmitsTotal, getEventbusErrorsTotal, getHttpClientRequestDuration, getHttpClientRequestErrors, getHttpClientRequestTotal, getHttpServerRequestDuration, getHttpServerRequestErrors, getHttpServerRequestTotal, getQueueJobCompletedTotal, getQueueJobEnqueuedTotal, getQueueJobFailedTotal, getQueueJobProcessingDuration, getSchedulerJobDuration, getSchedulerJobExecutedTotal, getSchedulerJobFailedTotal, type Histogram, initMetrics, shutdownMetrics, } from './metrics';
|
|
4
|
+
export { getTracer, initTelemetry, isTelemetryEnabled, shutdownTelemetry } from './sdk';
|
|
5
|
+
export type { Span, SpanOptions, Tracer } from './tracing';
|
|
6
|
+
export { addSpanAttributes, addSpanEvent, getActiveSpan, traceAsync, traceSync, withSpan } from './tracing';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/telemetry/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,KAAK,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,UAAU,CAAC;AACjG,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EACH,KAAK,OAAO,EACZ,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EACtB,4BAA4B,EAC5B,0BAA0B,EAC1B,yBAAyB,EACzB,4BAA4B,EAC5B,0BAA0B,EAC1B,yBAAyB,EACzB,yBAAyB,EACzB,wBAAwB,EACxB,sBAAsB,EACtB,6BAA6B,EAC7B,uBAAuB,EACvB,4BAA4B,EAC5B,0BAA0B,EAC1B,KAAK,SAAS,EACd,WAAW,EACX,eAAe,GAClB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AACxF,YAAY,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { getTelemetryConfig } from './config.js';
|
|
2
|
+
export { extractSqlOperation, sanitizeSql } from './db-sanitize.js';
|
|
3
|
+
export { getDbOperationDuration, getDbOperationErrors, getDbOperationTotal, getEventbusEmitsTotal, getEventbusErrorsTotal, getHttpClientRequestDuration, getHttpClientRequestErrors, getHttpClientRequestTotal, getHttpServerRequestDuration, getHttpServerRequestErrors, getHttpServerRequestTotal, getQueueJobCompletedTotal, getQueueJobEnqueuedTotal, getQueueJobFailedTotal, getQueueJobProcessingDuration, getSchedulerJobDuration, getSchedulerJobExecutedTotal, getSchedulerJobFailedTotal, initMetrics, shutdownMetrics, } from './metrics.js';
|
|
4
|
+
export { getTracer, initTelemetry, isTelemetryEnabled, shutdownTelemetry } from './sdk.js';
|
|
5
|
+
export { addSpanAttributes, addSpanEvent, getActiveSpan, traceAsync, traceSync, withSpan } from './tracing.js';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry metrics — lazy-initialized instruments.
|
|
3
|
+
* All degrade to no-ops when telemetry is disabled.
|
|
4
|
+
*/
|
|
5
|
+
import { type Counter, type Histogram } from '@opentelemetry/api';
|
|
6
|
+
import type { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
7
|
+
export type { Counter, Histogram } from '@opentelemetry/api';
|
|
8
|
+
import type { TelemetryConfig } from './config';
|
|
9
|
+
export declare function isMetricsInitialized(): boolean;
|
|
10
|
+
export declare function getMeterProvider(): MeterProvider;
|
|
11
|
+
export declare function getHttpServerRequestTotal(): Counter;
|
|
12
|
+
export declare function getHttpServerRequestDuration(): Histogram;
|
|
13
|
+
export declare function getHttpServerRequestErrors(): Counter;
|
|
14
|
+
export declare function getHttpClientRequestTotal(): Counter;
|
|
15
|
+
export declare function getHttpClientRequestDuration(): Histogram;
|
|
16
|
+
export declare function getHttpClientRequestErrors(): Counter;
|
|
17
|
+
export declare function getDbOperationTotal(): Counter;
|
|
18
|
+
export declare function getDbOperationDuration(): Histogram;
|
|
19
|
+
export declare function getDbOperationErrors(): Counter;
|
|
20
|
+
export declare function getEventbusEmitsTotal(): Counter;
|
|
21
|
+
export declare function getEventbusErrorsTotal(): Counter;
|
|
22
|
+
export declare function getQueueJobEnqueuedTotal(): Counter;
|
|
23
|
+
export declare function getQueueJobCompletedTotal(): Counter;
|
|
24
|
+
export declare function getQueueJobFailedTotal(): Counter;
|
|
25
|
+
export declare function getQueueJobProcessingDuration(): Histogram;
|
|
26
|
+
export declare function getSchedulerJobExecutedTotal(): Counter;
|
|
27
|
+
export declare function getSchedulerJobDuration(): Histogram;
|
|
28
|
+
export declare function getSchedulerJobFailedTotal(): Counter;
|
|
29
|
+
export declare function initMetrics(_config?: Partial<TelemetryConfig>): void;
|
|
30
|
+
export declare function shutdownMetrics(): Promise<void>;
|
|
31
|
+
export declare function _resetMetrics(): void;
|
|
32
|
+
//# sourceMappingURL=metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.d.ts","sourceRoot":"","sources":["../../src/telemetry/metrics.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,SAAS,EAAW,MAAM,oBAAoB,CAAC;AAC3E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEhE,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAKhD,wBAAgB,oBAAoB,IAAI,OAAO,CAE9C;AAED,wBAAgB,gBAAgB,IAAI,aAAa,CAEhD;AA6BD,wBAAgB,yBAAyB,IAAI,OAAO,CAEnD;AAED,wBAAgB,4BAA4B,IAAI,SAAS,CAExD;AAED,wBAAgB,0BAA0B,IAAI,OAAO,CAEpD;AAID,wBAAgB,yBAAyB,IAAI,OAAO,CAEnD;AAED,wBAAgB,4BAA4B,IAAI,SAAS,CAExD;AAED,wBAAgB,0BAA0B,IAAI,OAAO,CAEpD;AAID,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C;AAED,wBAAgB,sBAAsB,IAAI,SAAS,CAElD;AAED,wBAAgB,oBAAoB,IAAI,OAAO,CAE9C;AAID,wBAAgB,qBAAqB,IAAI,OAAO,CAE/C;AAED,wBAAgB,sBAAsB,IAAI,OAAO,CAEhD;AAID,wBAAgB,wBAAwB,IAAI,OAAO,CAElD;AAED,wBAAgB,yBAAyB,IAAI,OAAO,CAEnD;AAED,wBAAgB,sBAAsB,IAAI,OAAO,CAEhD;AAED,wBAAgB,6BAA6B,IAAI,SAAS,CAEzD;AAID,wBAAgB,4BAA4B,IAAI,OAAO,CAEtD;AAED,wBAAgB,uBAAuB,IAAI,SAAS,CAEnD;AAED,wBAAgB,0BAA0B,IAAI,OAAO,CAEpD;AAID,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,IAAI,CAGpE;AAED,wBAAgB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAG/C;AAED,wBAAgB,aAAa,IAAI,IAAI,CAMpC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry metrics — lazy-initialized instruments.
|
|
3
|
+
* All degrade to no-ops when telemetry is disabled.
|
|
4
|
+
*/
|
|
5
|
+
import { metrics } from '@opentelemetry/api';
|
|
6
|
+
let meterProvider;
|
|
7
|
+
let metricsInitialized = false;
|
|
8
|
+
export function isMetricsInitialized() {
|
|
9
|
+
return metricsInitialized;
|
|
10
|
+
}
|
|
11
|
+
export function getMeterProvider() {
|
|
12
|
+
return meterProvider ?? metrics.getMeterProvider();
|
|
13
|
+
}
|
|
14
|
+
const METER_NAME = '@gobing-ai/ts-infra';
|
|
15
|
+
const METER_VERSION = '0.1.0';
|
|
16
|
+
function getMeter() {
|
|
17
|
+
return getMeterProvider().getMeter(METER_NAME, METER_VERSION);
|
|
18
|
+
}
|
|
19
|
+
// ── Instrument cache ────────────────────────────────────────────────
|
|
20
|
+
const instruments = {};
|
|
21
|
+
function getOrCreateCounter(key, name, description, unit = '{operation}') {
|
|
22
|
+
if (!instruments[key]) {
|
|
23
|
+
instruments[key] = getMeter().createCounter(name, { description, unit });
|
|
24
|
+
}
|
|
25
|
+
return instruments[key];
|
|
26
|
+
}
|
|
27
|
+
function getOrCreateHistogram(key, name, description, unit = 'ms') {
|
|
28
|
+
if (!instruments[key]) {
|
|
29
|
+
instruments[key] = getMeter().createHistogram(name, { description, unit });
|
|
30
|
+
}
|
|
31
|
+
return instruments[key];
|
|
32
|
+
}
|
|
33
|
+
// ── HTTP server ─────────────────────────────────────────────────────
|
|
34
|
+
export function getHttpServerRequestTotal() {
|
|
35
|
+
return getOrCreateCounter('httpSrvReq', 'http.server.request.total', 'Total inbound HTTP requests', '{request}');
|
|
36
|
+
}
|
|
37
|
+
export function getHttpServerRequestDuration() {
|
|
38
|
+
return getOrCreateHistogram('httpSrvDur', 'http.server.request.duration', 'Inbound HTTP request duration');
|
|
39
|
+
}
|
|
40
|
+
export function getHttpServerRequestErrors() {
|
|
41
|
+
return getOrCreateCounter('httpSrvErr', 'http.server.request.errors', 'Inbound HTTP 5xx errors', '{error}');
|
|
42
|
+
}
|
|
43
|
+
// ── HTTP client ─────────────────────────────────────────────────────
|
|
44
|
+
export function getHttpClientRequestTotal() {
|
|
45
|
+
return getOrCreateCounter('httpCliReq', 'http.client.request.total', 'Total outbound HTTP requests', '{request}');
|
|
46
|
+
}
|
|
47
|
+
export function getHttpClientRequestDuration() {
|
|
48
|
+
return getOrCreateHistogram('httpCliDur', 'http.client.request.duration', 'Outbound HTTP request duration');
|
|
49
|
+
}
|
|
50
|
+
export function getHttpClientRequestErrors() {
|
|
51
|
+
return getOrCreateCounter('httpCliErr', 'http.client.request.errors', 'Outbound HTTP errors', '{error}');
|
|
52
|
+
}
|
|
53
|
+
// ── DB ──────────────────────────────────────────────────────────────
|
|
54
|
+
export function getDbOperationTotal() {
|
|
55
|
+
return getOrCreateCounter('dbOpTotal', 'db.client.operation.total', 'Total DB operations', '{operation}');
|
|
56
|
+
}
|
|
57
|
+
export function getDbOperationDuration() {
|
|
58
|
+
return getOrCreateHistogram('dbOpDur', 'db.client.operation.duration', 'DB operation duration');
|
|
59
|
+
}
|
|
60
|
+
export function getDbOperationErrors() {
|
|
61
|
+
return getOrCreateCounter('dbOpErr', 'db.client.operation.errors', 'DB operation errors', '{error}');
|
|
62
|
+
}
|
|
63
|
+
// ── Event bus ───────────────────────────────────────────────────────
|
|
64
|
+
export function getEventbusEmitsTotal() {
|
|
65
|
+
return getOrCreateCounter('ebEmit', 'eventbus.emits.total', 'Total event bus emits', '{emit}');
|
|
66
|
+
}
|
|
67
|
+
export function getEventbusErrorsTotal() {
|
|
68
|
+
return getOrCreateCounter('ebErr', 'eventbus.errors.total', 'Event bus errors', '{error}');
|
|
69
|
+
}
|
|
70
|
+
// ── Queue ───────────────────────────────────────────────────────────
|
|
71
|
+
export function getQueueJobEnqueuedTotal() {
|
|
72
|
+
return getOrCreateCounter('qEnq', 'queue.jobs.enqueued', 'Total jobs enqueued', '{job}');
|
|
73
|
+
}
|
|
74
|
+
export function getQueueJobCompletedTotal() {
|
|
75
|
+
return getOrCreateCounter('qComp', 'queue.jobs.completed', 'Total jobs completed', '{job}');
|
|
76
|
+
}
|
|
77
|
+
export function getQueueJobFailedTotal() {
|
|
78
|
+
return getOrCreateCounter('qFail', 'queue.jobs.failed', 'Total jobs failed', '{job}');
|
|
79
|
+
}
|
|
80
|
+
export function getQueueJobProcessingDuration() {
|
|
81
|
+
return getOrCreateHistogram('qProcDur', 'queue.jobs.processing_duration', 'Job processing duration');
|
|
82
|
+
}
|
|
83
|
+
// ── Scheduler ───────────────────────────────────────────────────────
|
|
84
|
+
export function getSchedulerJobExecutedTotal() {
|
|
85
|
+
return getOrCreateCounter('schedExec', 'scheduler.jobs.executed', 'Total scheduled job executions', '{execution}');
|
|
86
|
+
}
|
|
87
|
+
export function getSchedulerJobDuration() {
|
|
88
|
+
return getOrCreateHistogram('schedDur', 'scheduler.jobs.duration', 'Scheduled job duration');
|
|
89
|
+
}
|
|
90
|
+
export function getSchedulerJobFailedTotal() {
|
|
91
|
+
return getOrCreateCounter('schedFail', 'scheduler.jobs.failed', 'Failed scheduled jobs', '{failure}');
|
|
92
|
+
}
|
|
93
|
+
// ── Lifecycle ───────────────────────────────────────────────────────
|
|
94
|
+
export function initMetrics(_config) {
|
|
95
|
+
if (metricsInitialized)
|
|
96
|
+
return;
|
|
97
|
+
metricsInitialized = true;
|
|
98
|
+
}
|
|
99
|
+
export function shutdownMetrics() {
|
|
100
|
+
metricsInitialized = false;
|
|
101
|
+
return Promise.resolve();
|
|
102
|
+
}
|
|
103
|
+
export function _resetMetrics() {
|
|
104
|
+
for (const key of Object.keys(instruments)) {
|
|
105
|
+
instruments[key] = undefined;
|
|
106
|
+
}
|
|
107
|
+
metricsInitialized = false;
|
|
108
|
+
metrics.disable();
|
|
109
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry SDK initialisation and tracer provider management.
|
|
3
|
+
*/
|
|
4
|
+
import { type Tracer } from '@opentelemetry/api';
|
|
5
|
+
import type { TelemetryConfig } from './config';
|
|
6
|
+
export declare function getResolvedConfig(): TelemetryConfig;
|
|
7
|
+
export declare function initTelemetry(config?: Partial<TelemetryConfig>): void;
|
|
8
|
+
export declare function shutdownTelemetry(): Promise<void>;
|
|
9
|
+
export declare function getTracer(): Tracer;
|
|
10
|
+
export declare function isTelemetryEnabled(): boolean;
|
|
11
|
+
export declare function _resetTelemetry(): void;
|
|
12
|
+
export { context, propagation, trace } from '@opentelemetry/api';
|
|
13
|
+
//# sourceMappingURL=sdk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../../src/telemetry/sdk.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,KAAK,MAAM,EAAS,MAAM,oBAAoB,CAAC;AAExD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAShD,wBAAgB,iBAAiB,IAAI,eAAe,CAEnD;AAED,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,IAAI,CAarE;AAED,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC,CAQvD;AAED,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAED,wBAAgB,kBAAkB,IAAI,OAAO,CAE5C;AAED,wBAAgB,eAAe,IAAI,IAAI,CAWtC;AAED,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry SDK initialisation and tracer provider management.
|
|
3
|
+
*/
|
|
4
|
+
import { trace } from '@opentelemetry/api';
|
|
5
|
+
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
6
|
+
import { getTelemetryConfig } from './config.js';
|
|
7
|
+
let tracerProvider;
|
|
8
|
+
let telemetryInitialized = false;
|
|
9
|
+
let resolvedConfig = getTelemetryConfig();
|
|
10
|
+
const DEFAULT_TRACER = trace.getTracer('@gobing-ai/ts-infra', '0.1.0');
|
|
11
|
+
export function getResolvedConfig() {
|
|
12
|
+
return resolvedConfig;
|
|
13
|
+
}
|
|
14
|
+
export function initTelemetry(config) {
|
|
15
|
+
if (telemetryInitialized)
|
|
16
|
+
return;
|
|
17
|
+
resolvedConfig = { ...getTelemetryConfig(), ...config };
|
|
18
|
+
if (!resolvedConfig.enabled) {
|
|
19
|
+
telemetryInitialized = true;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
tracerProvider = new NodeTracerProvider();
|
|
23
|
+
tracerProvider.register();
|
|
24
|
+
telemetryInitialized = true;
|
|
25
|
+
}
|
|
26
|
+
export async function shutdownTelemetry() {
|
|
27
|
+
if (!tracerProvider) {
|
|
28
|
+
telemetryInitialized = false;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await tracerProvider.shutdown();
|
|
32
|
+
tracerProvider = undefined;
|
|
33
|
+
telemetryInitialized = false;
|
|
34
|
+
}
|
|
35
|
+
export function getTracer() {
|
|
36
|
+
return tracerProvider?.getTracer('@gobing-ai/ts-infra', '0.1.0') ?? DEFAULT_TRACER;
|
|
37
|
+
}
|
|
38
|
+
export function isTelemetryEnabled() {
|
|
39
|
+
return telemetryInitialized && resolvedConfig.enabled;
|
|
40
|
+
}
|
|
41
|
+
export function _resetTelemetry() {
|
|
42
|
+
if (tracerProvider) {
|
|
43
|
+
try {
|
|
44
|
+
tracerProvider.shutdown();
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
/* swallow */
|
|
48
|
+
}
|
|
49
|
+
tracerProvider = undefined;
|
|
50
|
+
}
|
|
51
|
+
telemetryInitialized = false;
|
|
52
|
+
resolvedConfig = getTelemetryConfig();
|
|
53
|
+
}
|
|
54
|
+
export { context, propagation, trace } from '@opentelemetry/api';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level tracing helpers for application code.
|
|
3
|
+
*/
|
|
4
|
+
import { type Span, type SpanOptions, type Tracer } from '@opentelemetry/api';
|
|
5
|
+
export declare function traceAsync<T>(name: string, fn: (span: Span) => Promise<T>, options?: SpanOptions, tracer?: Tracer): Promise<T>;
|
|
6
|
+
export declare function traceSync<T>(name: string, fn: (span: Span) => T, options?: SpanOptions, tracer?: Tracer): T;
|
|
7
|
+
export declare function addSpanAttributes(attributes: Record<string, string | number | boolean>): void;
|
|
8
|
+
export declare function addSpanEvent(name: string, attributes?: Record<string, string | number | boolean>): void;
|
|
9
|
+
export declare function getActiveSpan(): Span | undefined;
|
|
10
|
+
export declare function withSpan<T>(span: Span, fn: () => T): T;
|
|
11
|
+
export type { Span, SpanOptions, Tracer } from '@opentelemetry/api';
|
|
12
|
+
export { context, propagation, trace } from './sdk';
|
|
13
|
+
//# sourceMappingURL=tracing.d.ts.map
|