@zeroxsolutions/telemetry 0.0.0-stage → 0.0.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## 0.0.2 (2026-11-23)
2
+
3
+ ### 🚀 Features
4
+
5
+ - **telemetry:** add the span port, its adapters and exception rules ([24b1700](https://github.com/zeroxsolutions/zeroxsolutions/commit/24b1700))
6
+
7
+ ### ❤️ Thank You
8
+
9
+ - Lương Văn Tú
package/README.md CHANGED
@@ -1,3 +1,87 @@
1
- # Temporary Holding Version
1
+ # @zeroxsolutions/telemetry
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The span port every trigger writes, `ISpan`, with one adapter per runtime, and what a failed unit of work
4
+ records: the type the failure is grouped by and the attributes of the exception's log record, each as the
5
+ OpenTelemetry conventions spell them.
6
+
7
+ The root imports no runtime. `@zeroxsolutions/telemetry/cloudflare` adapts a Cloudflare Workers `Span`, and
8
+ `@zeroxsolutions/telemetry/opentelemetry` an `@opentelemetry/api` span. `@zeroxsolutions/queue` and
9
+ `@zeroxsolutions/server` write their spans through `ISpan`, so either adapter serves both.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pnpm add @zeroxsolutions/telemetry
15
+ ```
16
+
17
+ Both runtimes are optional peers: `@cloudflare/workers-types` (5.20260925.1 or later, types only) for
18
+ `./cloudflare`, and `@opentelemetry/api` for `./opentelemetry`.
19
+
20
+ ## The port
21
+
22
+ | Member | Writes |
23
+ | -------------------------- | ---------------------------------------------------------- |
24
+ | `rename(name)` | the span's name, a class of spans such as a route template |
25
+ | `setAttribute(key, value)` | one attribute |
26
+ | `fail(errorType)` | `error.type` and status Error |
27
+
28
+ A span whose work succeeded is never failed. `untracedSpan` is the span of work no trace records: it
29
+ takes every call and drops it, so a caller never branches on a runtime that has no span open.
30
+
31
+ | Export | Is |
32
+ | -------------------- | ----------------------------------------------------------------------------------- |
33
+ | `errorType(error)` | an `Error`'s `name`, or the `typeof` of anything else thrown |
34
+ | `exceptionOf(error)` | `{ 'exception.type', 'exception.message'? }`, the exception log record's attributes |
35
+
36
+ `errorType` reads `name`, which every house error class sets in its constructor, so the type is the
37
+ class's own name however the bundle renames the class. `exception.type` is `errorType` too, so the log
38
+ record and the span's `error.type` name one failure alike. `exception.message` is left out where the
39
+ thrown value carries no non-empty message. The span gets no exception event: the log record is the one
40
+ place the exception is written.
41
+
42
+ ## On Cloudflare Workers
43
+
44
+ ```ts
45
+ import { getLogger } from '@logtape/logtape';
46
+ import { tracing } from 'cloudflare:workers';
47
+ import { errorType, exceptionOf } from '@zeroxsolutions/telemetry';
48
+ import { CloudflareSpan } from '@zeroxsolutions/telemetry/cloudflare';
49
+
50
+ const logger = getLogger(['billing', 'invoice-job']);
51
+
52
+ const span = CloudflareSpan.from(tracing.getActiveSpan());
53
+ span.rename('invoice-job');
54
+ try {
55
+ await issueInvoices();
56
+ } catch (error) {
57
+ span.fail(errorType(error));
58
+ logger.error('invoice.job.failed', exceptionOf(error));
59
+ throw error;
60
+ }
61
+ ```
62
+
63
+ `CloudflareSpan.from` answers `untracedSpan` where `getActiveSpan()` returns none. Around a span
64
+ `tracing.enterSpan` opens, wrap the span it hands the callback: `new CloudflareSpan(span)`.
65
+
66
+ ## On OpenTelemetry
67
+
68
+ ```ts
69
+ import { trace } from '@opentelemetry/api';
70
+ import { errorType } from '@zeroxsolutions/telemetry';
71
+ import { OpenTelemetrySpan } from '@zeroxsolutions/telemetry/opentelemetry';
72
+
73
+ trace.getTracer('billing').startActiveSpan('invoice-job', async (otel) => {
74
+ const span = new OpenTelemetrySpan(otel);
75
+ try {
76
+ await issueInvoices();
77
+ } catch (error) {
78
+ span.fail(errorType(error));
79
+ throw error;
80
+ } finally {
81
+ otel.end();
82
+ }
83
+ });
84
+ ```
85
+
86
+ `fail` sets the status as `SpanStatusCode.ERROR`. `OpenTelemetrySpan.from(trace.getActiveSpan())`
87
+ answers `untracedSpan` where there is no active span. The span stays the caller's to end.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The span port and what a failed unit of work records: the type the failure is grouped by and the exception's
3
+ * log record. A runtime's span adapter lives under its own subpath, as `@zeroxsolutions/telemetry/cloudflare`.
4
+ */
5
+ export type { ISpan } from './lib/span/port.js';
6
+ export { untracedSpan } from './lib/span/untraced-span.js';
7
+ export { errorType } from './lib/error-type.js';
8
+ export { exceptionOf } from './lib/exception-record.js';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,YAAY,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { untracedSpan } from './lib/span/untraced-span.js';
2
+ export { errorType } from './lib/error-type.js';
3
+ export { exceptionOf } from './lib/exception-record.js';
@@ -0,0 +1,22 @@
1
+ import type { ISpan } from '../span/port.js';
2
+ /**
3
+ * The part of a Workers `Span` {@link CloudflareSpan} calls, so a fake needs nothing else. Each call's return is
4
+ * dropped: `Span` returns itself, which only `Span` can.
5
+ */
6
+ export type WorkersSpan = {
7
+ [Method in 'updateName' | 'setAttribute' | 'setStatus']: (...args: Parameters<Span[Method]>) => unknown;
8
+ };
9
+ /**
10
+ * Backs the span port with a Cloudflare Workers `Span`, whose status codes are the specification's names. Needs
11
+ * `@cloudflare/workers-types` 5.20260925.1 or later, the first to declare `setStatus` and `updateName`.
12
+ */
13
+ export declare class CloudflareSpan implements ISpan {
14
+ private readonly span;
15
+ /** Wraps the span `tracing.getActiveSpan()` returned, or answers {@link untracedSpan} where it returned none. */
16
+ static from(span: WorkersSpan | undefined): ISpan;
17
+ constructor(span: WorkersSpan);
18
+ rename(name: string): void;
19
+ setAttribute(key: string, value: string | number | boolean): void;
20
+ fail(errorType: string): void;
21
+ }
22
+ //# sourceMappingURL=cloudflare-span.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare-span.d.ts","sourceRoot":"","sources":["../../../src/lib/cloudflare/cloudflare-span.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAG7C;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG;KACvB,MAAM,IAAI,YAAY,GAAG,cAAc,GAAG,WAAW,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,OAAO;CACxG,CAAC;AAEF;;;GAGG;AACH,qBAAa,cAAe,YAAW,KAAK;IAM9B,OAAO,CAAC,QAAQ,CAAC,IAAI;IALjC,iHAAiH;IACjH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,GAAG,SAAS,GAAG,KAAK;gBAIpB,IAAI,EAAE,WAAW;IAE9C,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAI1B,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI;IAIjE,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;CAI9B"}
@@ -0,0 +1,25 @@
1
+ import { untracedSpan } from '../span/untraced-span.js';
2
+ /**
3
+ * Backs the span port with a Cloudflare Workers `Span`, whose status codes are the specification's names. Needs
4
+ * `@cloudflare/workers-types` 5.20260925.1 or later, the first to declare `setStatus` and `updateName`.
5
+ */
6
+ export class CloudflareSpan {
7
+ span;
8
+ /** Wraps the span `tracing.getActiveSpan()` returned, or answers {@link untracedSpan} where it returned none. */
9
+ static from(span) {
10
+ return span === undefined ? untracedSpan : new CloudflareSpan(span);
11
+ }
12
+ constructor(span) {
13
+ this.span = span;
14
+ }
15
+ rename(name) {
16
+ this.span.updateName(name);
17
+ }
18
+ setAttribute(key, value) {
19
+ this.span.setAttribute(key, value);
20
+ }
21
+ fail(errorType) {
22
+ this.span.setAttribute('error.type', errorType);
23
+ this.span.setStatus({ code: 'error' });
24
+ }
25
+ }
@@ -0,0 +1,3 @@
1
+ /** The Cloudflare Workers runtime half: the span port backed by a Workers `Span`. */
2
+ export { CloudflareSpan, type WorkersSpan } from './cloudflare-span.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/cloudflare/index.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,2 @@
1
+ /** The Cloudflare Workers runtime half: the span port backed by a Workers `Span`. */
2
+ export { CloudflareSpan } from './cloudflare-span.js';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The type a failure is grouped by, written as a span's `error.type` and a log record's `exception.type`: an
3
+ * `Error`'s `name`, or the `typeof` of anything else thrown. Every house error class sets `this.name`, so the type
4
+ * is the class's own name however the bundle renames the class; a class that sets none reads as `'Error'`.
5
+ */
6
+ export declare function errorType(error: unknown): string;
7
+ //# sourceMappingURL=error-type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-type.d.ts","sourceRoot":"","sources":["../../src/lib/error-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEhD"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The type a failure is grouped by, written as a span's `error.type` and a log record's `exception.type`: an
3
+ * `Error`'s `name`, or the `typeof` of anything else thrown. Every house error class sets `this.name`, so the type
4
+ * is the class's own name however the bundle renames the class; a class that sets none reads as `'Error'`.
5
+ */
6
+ export function errorType(error) {
7
+ return error instanceof Error ? error.name : typeof error;
8
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The attributes of the log record an exception is written as, per OpenTelemetry's exception log record:
3
+ * `exception.type` is {@link errorType}, the same type the span's `error.type` carries, and `exception.message`
4
+ * is the thrown value's non-empty string `message`, or the thrown string itself. Where there is neither, the
5
+ * key is left out rather than written empty.
6
+ *
7
+ * The message is what the code that threw wrote, so it can carry what a log line must not; the stack is not
8
+ * included.
9
+ */
10
+ export declare function exceptionOf(error: unknown): {
11
+ 'exception.type': string;
12
+ 'exception.message'?: string;
13
+ };
14
+ //# sourceMappingURL=exception-record.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exception-record.d.ts","sourceRoot":"","sources":["../../src/lib/exception-record.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG;IAAE,gBAAgB,EAAE,MAAM,CAAC;IAAC,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAAE,CAKtG"}
@@ -0,0 +1,24 @@
1
+ import { errorType } from './error-type.js';
2
+ /**
3
+ * The attributes of the log record an exception is written as, per OpenTelemetry's exception log record:
4
+ * `exception.type` is {@link errorType}, the same type the span's `error.type` carries, and `exception.message`
5
+ * is the thrown value's non-empty string `message`, or the thrown string itself. Where there is neither, the
6
+ * key is left out rather than written empty.
7
+ *
8
+ * The message is what the code that threw wrote, so it can carry what a log line must not; the stack is not
9
+ * included.
10
+ */
11
+ export function exceptionOf(error) {
12
+ const message = messageOf(error);
13
+ return message
14
+ ? { 'exception.type': errorType(error), 'exception.message': message }
15
+ : { 'exception.type': errorType(error) };
16
+ }
17
+ function messageOf(error) {
18
+ if (typeof error === 'string')
19
+ return error;
20
+ if (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string') {
21
+ return error.message;
22
+ }
23
+ return undefined;
24
+ }
@@ -0,0 +1,3 @@
1
+ /** The OpenTelemetry API half: the span port backed by an `@opentelemetry/api` span. */
2
+ export { OpenTelemetrySpan } from './open-telemetry-span.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/opentelemetry/index.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,2 @@
1
+ /** The OpenTelemetry API half: the span port backed by an `@opentelemetry/api` span. */
2
+ export { OpenTelemetrySpan } from './open-telemetry-span.js';
@@ -0,0 +1,13 @@
1
+ import { type Span } from '@opentelemetry/api';
2
+ import type { ISpan } from '../span/port.js';
3
+ /** Backs the span port with an `@opentelemetry/api` span, whose status codes are `SpanStatusCode` numbers. */
4
+ export declare class OpenTelemetrySpan implements ISpan {
5
+ private readonly span;
6
+ /** Wraps the span `trace.getActiveSpan()` returned, or answers {@link untracedSpan} where it returned none. */
7
+ static from(span: Span | undefined): ISpan;
8
+ constructor(span: Span);
9
+ rename(name: string): void;
10
+ setAttribute(key: string, value: string | number | boolean): void;
11
+ fail(errorType: string): void;
12
+ }
13
+ //# sourceMappingURL=open-telemetry-span.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"open-telemetry-span.d.ts","sourceRoot":"","sources":["../../../src/lib/opentelemetry/open-telemetry-span.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAG7C,8GAA8G;AAC9G,qBAAa,iBAAkB,YAAW,KAAK;IAMjC,OAAO,CAAC,QAAQ,CAAC,IAAI;IALjC,+GAA+G;IAC/G,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,GAAG,KAAK;gBAIb,IAAI,EAAE,IAAI;IAEvC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAI1B,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI;IAIjE,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;CAI9B"}
@@ -0,0 +1,23 @@
1
+ import { SpanStatusCode } from '@opentelemetry/api';
2
+ import { untracedSpan } from '../span/untraced-span.js';
3
+ /** Backs the span port with an `@opentelemetry/api` span, whose status codes are `SpanStatusCode` numbers. */
4
+ export class OpenTelemetrySpan {
5
+ span;
6
+ /** Wraps the span `trace.getActiveSpan()` returned, or answers {@link untracedSpan} where it returned none. */
7
+ static from(span) {
8
+ return span === undefined ? untracedSpan : new OpenTelemetrySpan(span);
9
+ }
10
+ constructor(span) {
11
+ this.span = span;
12
+ }
13
+ rename(name) {
14
+ this.span.updateName(name);
15
+ }
16
+ setAttribute(key, value) {
17
+ this.span.setAttribute(key, value);
18
+ }
19
+ fail(errorType) {
20
+ this.span.setAttribute('error.type', errorType);
21
+ this.span.setStatus({ code: SpanStatusCode.ERROR });
22
+ }
23
+ }
@@ -0,0 +1,16 @@
1
+ /** The span over one unit of work, as every house helper writes it; a runtime supplies the implementation. */
2
+ /**
3
+ * One span, named and annotated by the OpenTelemetry semantic conventions. A span whose work succeeded is never
4
+ * failed and keeps the status its runtime gave it.
5
+ */
6
+ export interface ISpan {
7
+ /** Names the span; a name is a class of spans, such as a route template, never one instance. */
8
+ rename(name: string): void;
9
+ setAttribute(key: string, value: string | number | boolean): void;
10
+ /**
11
+ * Marks the span Error and sets `error.type`: the exception's type (see `errorType`), or the status code of an
12
+ * answered failure. The exception itself is written as a log record, not on the span.
13
+ */
14
+ fail(errorType: string): void;
15
+ }
16
+ //# sourceMappingURL=port.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"port.d.ts","sourceRoot":"","sources":["../../../src/lib/span/port.ts"],"names":[],"mappings":"AAAA,8GAA8G;AAE9G;;;GAGG;AACH,MAAM,WAAW,KAAK;IACpB,gGAAgG;IAChG,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;IAClE;;;OAGG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B"}
@@ -0,0 +1,2 @@
1
+ /** The span over one unit of work, as every house helper writes it; a runtime supplies the implementation. */
2
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { ISpan } from './port.js';
2
+ /** The span of work no trace records, for where the runtime has no span open; every call is accepted and dropped. */
3
+ export declare const untracedSpan: ISpan;
4
+ //# sourceMappingURL=untraced-span.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"untraced-span.d.ts","sourceRoot":"","sources":["../../../src/lib/span/untraced-span.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEvC,qHAAqH;AACrH,eAAO,MAAM,YAAY,EAAE,KAI1B,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** The span of work no trace records, for where the runtime has no span open; every call is accepted and dropped. */
2
+ export const untracedSpan = {
3
+ rename: () => undefined,
4
+ setAttribute: () => undefined,
5
+ fail: () => undefined,
6
+ };
package/package.json CHANGED
@@ -1,6 +1,60 @@
1
1
  {
2
2
  "name": "@zeroxsolutions/telemetry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.2",
4
+ "private": false,
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "description": "The span port every trigger writes, with one adapter per runtime, and what a failed unit of work records: the type the failure is grouped by and the attributes of the exception's log record. The root imports no runtime; ./cloudflare wraps a Workers Span and ./opentelemetry an @opentelemetry/api span.",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "files": [
12
+ "dist",
13
+ "README.md",
14
+ "CHANGELOG.md",
15
+ "!**/*.tsbuildinfo"
16
+ ],
17
+ "exports": {
18
+ "./package.json": "./package.json",
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js",
22
+ "default": "./dist/index.js"
23
+ },
24
+ "./cloudflare": {
25
+ "types": "./dist/lib/cloudflare/index.d.ts",
26
+ "import": "./dist/lib/cloudflare/index.js",
27
+ "default": "./dist/lib/cloudflare/index.js"
28
+ },
29
+ "./opentelemetry": {
30
+ "types": "./dist/lib/opentelemetry/index.d.ts",
31
+ "import": "./dist/lib/opentelemetry/index.js",
32
+ "default": "./dist/lib/opentelemetry/index.js"
33
+ }
34
+ },
35
+ "dependencies": {
36
+ "tslib": "^2.3.0"
37
+ },
38
+ "peerDependencies": {
39
+ "@cloudflare/workers-types": "^5.20260925.1",
40
+ "@opentelemetry/api": "^1.0.0"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "@cloudflare/workers-types": {
44
+ "optional": true
45
+ },
46
+ "@opentelemetry/api": {
47
+ "optional": true
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "@cloudflare/workers-types": "5.20261109.1",
52
+ "@opentelemetry/api": "1.9.1",
53
+ "@opentelemetry/sdk-trace": "2.12.0"
54
+ },
55
+ "nx": {
56
+ "targets": {
57
+ "lint-deps": {}
58
+ }
59
+ }
6
60
  }