@softistx/telemetry 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +977 -2
- package/dist/attributes/attributes.d.ts +43 -0
- package/dist/attributes/attributes.d.ts.map +1 -0
- package/dist/context/current.d.ts +43 -0
- package/dist/context/current.d.ts.map +1 -0
- package/dist/export/console.d.ts +20 -0
- package/dist/export/console.d.ts.map +1 -0
- package/dist/export/exporter.d.ts +39 -0
- package/dist/export/exporter.d.ts.map +1 -0
- package/dist/export/file.d.ts +43 -0
- package/dist/export/file.d.ts.map +1 -0
- package/dist/export/json-lines.d.ts +17 -0
- package/dist/export/json-lines.d.ts.map +1 -0
- package/dist/export/memory.d.ts +30 -0
- package/dist/export/memory.d.ts.map +1 -0
- package/dist/export/pipeline.d.ts +71 -0
- package/dist/export/pipeline.d.ts.map +1 -0
- package/dist/export/rotation.d.ts +46 -0
- package/dist/export/rotation.d.ts.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1180 -0
- package/dist/index.js.map +29 -0
- package/dist/logger/event.d.ts +15 -0
- package/dist/logger/event.d.ts.map +1 -0
- package/dist/logger/logger.d.ts +46 -0
- package/dist/logger/logger.d.ts.map +1 -0
- package/dist/logger/standard-schema.d.ts +32 -0
- package/dist/logger/standard-schema.d.ts.map +1 -0
- package/dist/model/error.d.ts +21 -0
- package/dist/model/error.d.ts.map +1 -0
- package/dist/model/signal.d.ts +90 -0
- package/dist/model/signal.d.ts.map +1 -0
- package/dist/span/begin.d.ts +17 -0
- package/dist/span/begin.d.ts.map +1 -0
- package/dist/span/handle.d.ts +40 -0
- package/dist/span/handle.d.ts.map +1 -0
- package/dist/span/scope.d.ts +71 -0
- package/dist/span/scope.d.ts.map +1 -0
- package/dist/span/span.d.ts +15 -0
- package/dist/span/span.d.ts.map +1 -0
- package/dist/span/sync.d.ts +5 -0
- package/dist/span/sync.d.ts.map +1 -0
- package/dist/telemetry/telemetry.d.ts +89 -0
- package/dist/telemetry/telemetry.d.ts.map +1 -0
- package/dist/trace/ids.d.ts +60 -0
- package/dist/trace/ids.d.ts.map +1 -0
- package/dist/trace/sampler.d.ts +31 -0
- package/dist/trace/sampler.d.ts.map +1 -0
- package/package.json +57 -5
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Attributes } from '../attributes/attributes';
|
|
2
|
+
import { type TelemetryEvent } from '../logger/event';
|
|
3
|
+
import type { SpanKind, SpanRecord, SpanStatus } from '../model/signal';
|
|
4
|
+
import { type SpanContext, type SpanId, type TraceId } from '../trace/ids';
|
|
5
|
+
/** The open span, from inside it. Everything here is synchronous. */
|
|
6
|
+
export interface SpanScope {
|
|
7
|
+
readonly context: SpanContext;
|
|
8
|
+
readonly traceId: TraceId;
|
|
9
|
+
readonly spanId: SpanId;
|
|
10
|
+
/** Writable: a server span is named after its route, which routing knows last. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** Writable, for work that failed without throwing. An exception overrides it. */
|
|
13
|
+
status: SpanStatus;
|
|
14
|
+
/**
|
|
15
|
+
* Record a failure the span will not see thrown.
|
|
16
|
+
*
|
|
17
|
+
* A block that throws is recorded without this. It exists for the frameworks
|
|
18
|
+
* that **catch** — Hono turns a handler's exception into a 500 and hands the
|
|
19
|
+
* middleware a normal return with the failure on the side — where the only
|
|
20
|
+
* thing that knows a request failed is the framework's own field. Without
|
|
21
|
+
* it, that span carries a status and no `exception.type`, which is the one
|
|
22
|
+
* thing a trace is read for.
|
|
23
|
+
*
|
|
24
|
+
* **The first failure wins**, and stays won: a framework that catches and
|
|
25
|
+
* then rethrows its own wrapper cannot replace the one nearest the cause.
|
|
26
|
+
* The cost is that an early `fail()` for something the block went on to
|
|
27
|
+
* recover from hides a later, unrelated exception — so call it for the
|
|
28
|
+
* failure the span is about, not for every one it sees.
|
|
29
|
+
*/
|
|
30
|
+
fail(failure: unknown): void;
|
|
31
|
+
/** The header an outgoing call should carry to continue this trace. */
|
|
32
|
+
traceparent(): string;
|
|
33
|
+
attribute(name: string, value: unknown): void;
|
|
34
|
+
attributes(record: Readonly<Record<string, unknown>>): void;
|
|
35
|
+
event(name: string, attributes?: Readonly<Record<string, unknown>>): void;
|
|
36
|
+
event(event: TelemetryEvent): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The open span.
|
|
40
|
+
*
|
|
41
|
+
* Every method is synchronous and none of them can throw: they are called from
|
|
42
|
+
* application code that is already in trouble, and a span that raised would
|
|
43
|
+
* replace the failure it was opened to record.
|
|
44
|
+
*/
|
|
45
|
+
export declare class Span implements SpanScope {
|
|
46
|
+
name: string;
|
|
47
|
+
status: SpanStatus;
|
|
48
|
+
readonly context: SpanContext;
|
|
49
|
+
private readonly kind;
|
|
50
|
+
private readonly stackTraces;
|
|
51
|
+
private readonly own;
|
|
52
|
+
private readonly happened;
|
|
53
|
+
private error;
|
|
54
|
+
constructor(context: SpanContext, name: string, kind: SpanKind, stackTraces?: boolean);
|
|
55
|
+
get traceId(): TraceId;
|
|
56
|
+
get spanId(): SpanId;
|
|
57
|
+
traceparent(): string;
|
|
58
|
+
attribute(name: string, value: unknown): void;
|
|
59
|
+
attributes(record: Readonly<Record<string, unknown>>): void;
|
|
60
|
+
event(input: string | TelemetryEvent, attributes?: Readonly<Record<string, unknown>>): void;
|
|
61
|
+
fail(failure: unknown): void;
|
|
62
|
+
/**
|
|
63
|
+
* The record that is emitted. Both collections are **copied**: a scope that
|
|
64
|
+
* outlives its block — held by a callback, or by a caller that kept it —
|
|
65
|
+
* would otherwise mutate a record already sitting in the pipeline buffer.
|
|
66
|
+
* The attributes case is the easy one to miss, because `mergeAttributes`
|
|
67
|
+
* returns one side unchanged when the other is empty.
|
|
68
|
+
*/
|
|
69
|
+
record(startedAt: number, endedAt: number, parent: SpanId | undefined, inherited: Attributes): SpanRecord;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=scope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../../src/span/scope.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,UAAU,EAKf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAoB,KAAK,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAExE,OAAO,KAAK,EAGX,QAAQ,EACR,UAAU,EACV,UAAU,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAEN,KAAK,WAAW,EAChB,KAAK,MAAM,EACX,KAAK,OAAO,EACZ,MAAM,cAAc,CAAC;AAEtB,qEAAqE;AACrE,MAAM,WAAW,SAAS;IACzB,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,kFAAkF;IAClF,IAAI,EAAE,MAAM,CAAC;IACb,kFAAkF;IAClF,MAAM,EAAE,UAAU,CAAC;IACnB;;;;;;;;;;;;;;;OAeG;IACH,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7B,uEAAuE;IACvE,WAAW,IAAI,MAAM,CAAC;IACtB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IAC9C,UAAU,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAC5D,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAC1E,KAAK,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAAC;CACnC;AAED;;;;;;GAMG;AACH,qBAAa,IAAK,YAAW,SAAS;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,UAAU,CAAQ;IAC1B,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAW;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAU;IACtC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAsC;IAC1D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;IAC5C,OAAO,CAAC,KAAK,CAAwB;gBAGpC,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,QAAQ,EACd,WAAW,UAAO;IAQnB,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,WAAW,IAAI,MAAM;IAIrB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;IAI7C,UAAU,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,IAAI;IAI3D,KAAK,CACJ,KAAK,EAAE,MAAM,GAAG,cAAc,EAC9B,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC5C,IAAI;IASP,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAU5B;;;;;;OAMG;IACH,MAAM,CACL,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,SAAS,EAAE,UAAU,GACnB,UAAU;CAeb"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SpanKind } from '../model/signal';
|
|
2
|
+
import { type SpanScope } from './scope';
|
|
3
|
+
export * from './handle';
|
|
4
|
+
export * from './sync';
|
|
5
|
+
export interface SpanOptions {
|
|
6
|
+
/** Inherited by every log and span inside this one. */
|
|
7
|
+
readonly attributes?: Readonly<Record<string, unknown>>;
|
|
8
|
+
readonly kind?: SpanKind;
|
|
9
|
+
}
|
|
10
|
+
export type SpanBlock<T> = (scope: SpanScope) => T | Promise<T>;
|
|
11
|
+
export declare function span<T>(name: string, block: SpanBlock<T>): Promise<T>;
|
|
12
|
+
export declare function span<T>(name: string, options: SpanOptions, block: SpanBlock<T>): Promise<T>;
|
|
13
|
+
export declare function continuing<T>(traceparent: string | null | undefined, name: string, block: SpanBlock<T>): Promise<T>;
|
|
14
|
+
export declare function continuing<T>(traceparent: string | null | undefined, name: string, options: SpanOptions, block: SpanBlock<T>): Promise<T>;
|
|
15
|
+
//# sourceMappingURL=span.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"span.d.ts","sourceRoot":"","sources":["../../src/span/span.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAGhD,OAAO,EAAQ,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAE/C,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AAEvB,MAAM,WAAW,WAAW;IAC3B,uDAAuD;IACvD,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACxD,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC;CACzB;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEhE,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACvE,wBAAgB,IAAI,CAAC,CAAC,EACrB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GACjB,OAAO,CAAC,CAAC,CAAC,CAAC;AA0Bd,wBAAgB,UAAU,CAAC,CAAC,EAC3B,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACtC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GACjB,OAAO,CAAC,CAAC,CAAC,CAAC;AACd,wBAAgB,UAAU,CAAC,CAAC,EAC3B,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACtC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GACjB,OAAO,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type SpanScope } from './scope';
|
|
2
|
+
import type { SpanOptions } from './span';
|
|
3
|
+
export declare function spanSync<T>(name: string, block: (scope: SpanScope) => T): T;
|
|
4
|
+
export declare function spanSync<T>(name: string, options: SpanOptions, block: (scope: SpanScope) => T): T;
|
|
5
|
+
//# sourceMappingURL=sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../src/span/sync.ts"],"names":[],"mappings":"AAEA,OAAO,EAAQ,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,CAAC,GAAG,CAAC,CAAC;AAC7E,wBAAgB,QAAQ,CAAC,CAAC,EACzB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,CAAC,GAC5B,CAAC,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Exporter } from '../export/exporter';
|
|
2
|
+
import type { Resource, Severity, Signal } from '../model/signal';
|
|
3
|
+
import { type Sampler } from '../trace/sampler';
|
|
4
|
+
export interface TelemetryOptions {
|
|
5
|
+
readonly version?: string;
|
|
6
|
+
readonly environment?: string;
|
|
7
|
+
/** Stamped on the resource, so every signal carries them. */
|
|
8
|
+
readonly attributes?: Readonly<Record<string, unknown>>;
|
|
9
|
+
/** Asked once, for a root span. Default: `alwaysSample`. */
|
|
10
|
+
readonly sampler?: Sampler;
|
|
11
|
+
/** Logs below this are never built. Spans are unaffected. Default `info`. */
|
|
12
|
+
readonly minimum?: Severity;
|
|
13
|
+
/** Whether a recorded failure carries its stack. Default true. */
|
|
14
|
+
readonly stackTraces?: boolean;
|
|
15
|
+
/** Flush once this many signals are waiting. Default 512. */
|
|
16
|
+
readonly batch?: number;
|
|
17
|
+
/** Flush this long after the first signal of a batch, in ms. Default 1000. */
|
|
18
|
+
readonly linger?: number;
|
|
19
|
+
/** How long `close` waits for the backlog, in ms. Default 10000. */
|
|
20
|
+
readonly drainTimeout?: number;
|
|
21
|
+
/** Default: `console.error`. It must not throw; if it does, it is ignored. */
|
|
22
|
+
readonly onExportError?: (failure: unknown) => void;
|
|
23
|
+
/** In order. A batch reaches them one after the other. */
|
|
24
|
+
readonly exporters?: readonly Exporter[];
|
|
25
|
+
}
|
|
26
|
+
export declare const TELEMETRY_DEFAULTS: Readonly<{
|
|
27
|
+
minimum: Severity;
|
|
28
|
+
stackTraces: true;
|
|
29
|
+
batch: 512;
|
|
30
|
+
linger: 1000;
|
|
31
|
+
drainTimeout: 10000;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* A service's telemetry: its resource, its sampler, and the pipeline its
|
|
35
|
+
* signals go through.
|
|
36
|
+
*
|
|
37
|
+
* Build it with {@link createTelemetry}. `install()` makes it the one a logger
|
|
38
|
+
* or a span finds when there is none in scope.
|
|
39
|
+
*/
|
|
40
|
+
export declare class Telemetry {
|
|
41
|
+
readonly resource: Resource;
|
|
42
|
+
readonly sampler: Sampler;
|
|
43
|
+
readonly minimum: Severity;
|
|
44
|
+
readonly stackTraces: boolean;
|
|
45
|
+
private readonly pipeline;
|
|
46
|
+
constructor(options: TelemetryOptions & {
|
|
47
|
+
readonly service: string;
|
|
48
|
+
});
|
|
49
|
+
/** Never blocks, never throws. False once closed. */
|
|
50
|
+
emit(signal: Signal): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Ships the backlog now, without closing, and resolves once the exporters
|
|
53
|
+
* have it. For tests and for a checkpoint before a risky step; a shutdown
|
|
54
|
+
* awaits `close()` instead.
|
|
55
|
+
*/
|
|
56
|
+
flush(): Promise<void>;
|
|
57
|
+
/** The one a logger or a span finds with nothing in scope. Returns itself. */
|
|
58
|
+
install(): this;
|
|
59
|
+
/**
|
|
60
|
+
* Ships the backlog and closes the exporters. **It has to be awaited**:
|
|
61
|
+
* JavaScript cannot block, so a process that exits without waiting loses
|
|
62
|
+
* its last batch — which is the batch that explains the shutdown.
|
|
63
|
+
*
|
|
64
|
+
* Idempotent, and it stands down as the installed default.
|
|
65
|
+
*/
|
|
66
|
+
close(): Promise<void>;
|
|
67
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* ```ts
|
|
71
|
+
* const telemetry = createTelemetry('checkout', {
|
|
72
|
+
* environment: 'production',
|
|
73
|
+
* sampler: ratioSampler(0.1),
|
|
74
|
+
* exporters: [consoleExporter()],
|
|
75
|
+
* }).install();
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* `service` has no default on purpose: it is the key everything groups by, and
|
|
79
|
+
* a service called `unknown` is a dashboard nobody can read.
|
|
80
|
+
*/
|
|
81
|
+
export declare function createTelemetry(service: string, options?: TelemetryOptions): Telemetry;
|
|
82
|
+
/** The installed default, if `install()` has been called. */
|
|
83
|
+
export declare function installedTelemetry(): Telemetry | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Only for a test that needs the process back the way it found it. Production
|
|
86
|
+
* code stands an instance down by closing it.
|
|
87
|
+
*/
|
|
88
|
+
export declare function uninstallTelemetry(telemetry?: Telemetry): void;
|
|
89
|
+
//# sourceMappingURL=telemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/telemetry/telemetry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAgB,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE9D,MAAM,WAAW,gBAAgB;IAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,6DAA6D;IAC7D,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACxD,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,6EAA6E;IAC7E,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC;IAC5B,kEAAkE;IAClE,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAC/B,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,oEAAoE;IACpE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,8EAA8E;IAC9E,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACpD,0DAA0D;IAC1D,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;CACzC;AAED,eAAO,MAAM,kBAAkB;aACX,QAAQ;;;;;EAK1B,CAAC;AAIH;;;;;;GAMG;AACH,qBAAa,SAAS;IACrB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;gBAExB,OAAO,EAAE,gBAAgB,GAAG;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE;IAuBpE,qDAAqD;IACrD,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAI7B;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,8EAA8E;IAC9E,OAAO,IAAI,IAAI;IAKf;;;;;;OAMG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAKtB,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAG5C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,gBAAqB,GAC5B,SAAS,CAEX;AAED,6DAA6D;AAC7D,wBAAgB,kBAAkB,IAAI,SAAS,GAAG,SAAS,CAE1D;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,CAAC,EAAE,SAAS,GAAG,IAAI,CAE9D"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trace identity, and the W3C `traceparent` header.
|
|
3
|
+
*
|
|
4
|
+
* The ids are branded strings: the hex representation is the representation, so
|
|
5
|
+
* there is nothing to allocate and nothing to convert before writing a header,
|
|
6
|
+
* and the brand still stops a span id being passed where a trace id belongs.
|
|
7
|
+
*/
|
|
8
|
+
declare const traceIdBrand: unique symbol;
|
|
9
|
+
declare const spanIdBrand: unique symbol;
|
|
10
|
+
/** 32 lowercase hex characters. */
|
|
11
|
+
export type TraceId = string & {
|
|
12
|
+
readonly [traceIdBrand]: true;
|
|
13
|
+
};
|
|
14
|
+
/** 16 lowercase hex characters. */
|
|
15
|
+
export type SpanId = string & {
|
|
16
|
+
readonly [spanIdBrand]: true;
|
|
17
|
+
};
|
|
18
|
+
export declare const INVALID_TRACE_ID: TraceId;
|
|
19
|
+
export declare const INVALID_SPAN_ID: SpanId;
|
|
20
|
+
export declare function isValidTraceId(hex: string): hex is TraceId;
|
|
21
|
+
export declare function isValidSpanId(hex: string): hex is SpanId;
|
|
22
|
+
/**
|
|
23
|
+
* From `crypto.getRandomValues`, never `Math.random()`: ids that collide join
|
|
24
|
+
* two unrelated traces into one, which reads as a request that did work it
|
|
25
|
+
* never did.
|
|
26
|
+
*/
|
|
27
|
+
export declare function randomTraceId(): TraceId;
|
|
28
|
+
export declare function randomSpanId(): SpanId;
|
|
29
|
+
/** A span's place in a trace, as it travels. */
|
|
30
|
+
export interface SpanContext {
|
|
31
|
+
readonly traceId: TraceId;
|
|
32
|
+
readonly spanId: SpanId;
|
|
33
|
+
/** Decided once, by the root, and inherited by everything below it. */
|
|
34
|
+
readonly sampled: boolean;
|
|
35
|
+
/** True when this context was parsed from an inbound header. */
|
|
36
|
+
readonly remote: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* What a span carries when there is no telemetry installed. The block still
|
|
40
|
+
* runs — a library that traces must work inside an application that has never
|
|
41
|
+
* heard of this one — and nothing is emitted.
|
|
42
|
+
*/
|
|
43
|
+
export declare const DETACHED_SPAN_CONTEXT: SpanContext;
|
|
44
|
+
export declare function isDetached(context: SpanContext): boolean;
|
|
45
|
+
/** `00-<trace id>-<span id>-<flags>`, version 00, the only one we write. */
|
|
46
|
+
export declare function renderTraceparent(context: SpanContext): string;
|
|
47
|
+
/**
|
|
48
|
+
* The inbound half. It **returns null, and never throws**: an unusable header
|
|
49
|
+
* is a request that starts a fresh trace, not a request that fails.
|
|
50
|
+
*
|
|
51
|
+
* Rejected: fewer than four fields, a version that is not two characters, the
|
|
52
|
+
* forbidden version `ff`, an id of the wrong length or all zeros or not hex,
|
|
53
|
+
* and flags that are not hex. A later version is accepted and its extra fields
|
|
54
|
+
* ignored, which is what the specification asks of a version-00 reader.
|
|
55
|
+
*
|
|
56
|
+
* `tracestate` is not read. Nothing here writes one either.
|
|
57
|
+
*/
|
|
58
|
+
export declare function parseTraceparent(header: string | null | undefined): SpanContext | null;
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=ids.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../src/trace/ids.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,CAAC,MAAM,YAAY,EAAE,OAAO,MAAM,CAAC;AAC1C,OAAO,CAAC,MAAM,WAAW,EAAE,OAAO,MAAM,CAAC;AAEzC,mCAAmC;AACnC,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,CAAC,YAAY,CAAC,EAAE,IAAI,CAAA;CAAE,CAAC;AAEjE,mCAAmC;AACnC,MAAM,MAAM,MAAM,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,CAAC,WAAW,CAAC,EAAE,IAAI,CAAA;CAAE,CAAC;AAE/D,eAAO,MAAM,gBAAgB,EAAqB,OAAO,CAAC;AAC1D,eAAO,MAAM,eAAe,EAAqB,MAAM,CAAC;AAMxD,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,OAAO,CAE1D;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,MAAM,CAExD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAEvC;AAED,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAED,gDAAgD;AAChD,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,gEAAgE;IAChE,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CACzB;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,EAAE,WAKlC,CAAC;AAEH,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAExD;AAED,4EAA4E;AAC5E,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAE9D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAC/B,WAAW,GAAG,IAAI,CA4BpB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type TraceId } from './ids';
|
|
2
|
+
/**
|
|
3
|
+
* Sampling is decided once, by the root span, and the answer travels with the
|
|
4
|
+
* trace — down through every child and out over the wire in the `traceparent`.
|
|
5
|
+
* A sampler consulted per span produces traces missing their middles, and a gap
|
|
6
|
+
* in a trace looks like work that never happened.
|
|
7
|
+
*
|
|
8
|
+
* Logs are not sampled. That decision is about the volume of traces, and a log
|
|
9
|
+
* dropped because its trace was not kept is a log missing at precisely the
|
|
10
|
+
* moment somebody is reading logs to find out what happened.
|
|
11
|
+
*/
|
|
12
|
+
export interface Sampler {
|
|
13
|
+
sample(traceId: TraceId): boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const alwaysSample: Sampler;
|
|
16
|
+
export declare const neverSample: Sampler;
|
|
17
|
+
/**
|
|
18
|
+
* Keep this fraction of traces, decided **from the trace id** rather than by a
|
|
19
|
+
* coin toss: two services at the same ratio then make the same decision about
|
|
20
|
+
* the same trace. Two services tossing independently at 10% keep a whole trace
|
|
21
|
+
* 1% of the time.
|
|
22
|
+
*
|
|
23
|
+
* The rule is OpenTelemetry's — the low 64 bits of the trace id against
|
|
24
|
+
* `ratio × 2⁶⁴` — so anything else implementing it agrees with us.
|
|
25
|
+
*
|
|
26
|
+
* @throws RangeError if `ratio` is not between 0 and 1. This is a construction
|
|
27
|
+
* error, raised where the mistake is, and never on the path that writes a
|
|
28
|
+
* signal.
|
|
29
|
+
*/
|
|
30
|
+
export declare function ratioSampler(ratio: number): Sampler;
|
|
31
|
+
//# sourceMappingURL=sampler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sampler.d.ts","sourceRoot":"","sources":["../../src/trace/sampler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,OAAO,EAAE,MAAM,OAAO,CAAC;AAErD;;;;;;;;;GASG;AACH,MAAM,WAAW,OAAO;IACvB,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC;CAClC;AAED,eAAO,MAAM,YAAY,EAAE,OAAgC,CAAC;AAE5D,eAAO,MAAM,WAAW,EAAE,OAAiC,CAAC;AAK5D;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAgBnD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@softistx/telemetry",
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Logs and traces for a TypeScript service, with no OpenTelemetry SDK: spans over AsyncLocalStorage, W3C traceparent propagation, declared events, and a pipeline that never blocks the caller",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"package.json",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"telemetry",
|
|
25
|
+
"observability",
|
|
26
|
+
"tracing",
|
|
27
|
+
"logging",
|
|
28
|
+
"opentelemetry",
|
|
29
|
+
"otlp",
|
|
30
|
+
"traceparent",
|
|
31
|
+
"typescript"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/softistx/nxgt-telemetry.git",
|
|
36
|
+
"directory": "packages/telemetry"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"registry": "https://registry.npmjs.org",
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "bun run ../../build.ts",
|
|
44
|
+
"test": "bun test src",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
},
|
|
47
|
+
"nxgt": {
|
|
48
|
+
"entrypoints": [
|
|
49
|
+
"src/index.ts"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/bun": "^1.4.2"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"typescript": "^6.0.3"
|
|
57
|
+
}
|
|
58
|
+
}
|