@adhd/apigen-plugin-tracing 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/README.md +115 -2
- package/index.d.ts +3 -0
- package/index.js +1 -0
- package/index.mjs +151 -0
- package/lib/plugin.d.ts +109 -0
- package/package.json +31 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @adhd/apigen-plugin-tracing
|
|
2
|
+
|
|
3
|
+
## 0.2.0 (2026-10-01)
|
|
4
|
+
|
|
5
|
+
### ⚠️ Breaking Changes
|
|
6
|
+
|
|
7
|
+
- **apigen-plugin-tracing:** `makeTracingPlugin(opts)` / `makeTraceLayer(opts)` now REQUIRE
|
|
8
|
+
`opts.serviceName` (previously the options object defaulted to `{}` with an implicit `'apigen'`
|
|
9
|
+
namespace). Reserved span attribute and event keys changed from `apigen.op` / `apigen.transport` /
|
|
10
|
+
`apigen.op.error` to `<serviceName>.op` / `<serviceName>.transport` / `<serviceName>.op.error`.
|
|
11
|
+
Every consumer must now pass an explicit `serviceName` (adhd products pass `'adhd'`) and read the
|
|
12
|
+
namespaced keys. 0.x breaking ⇒ minor.
|
|
13
|
+
|
|
14
|
+
### ❤️ Thank You
|
|
15
|
+
|
|
16
|
+
- pseudosky
|
|
17
|
+
|
|
18
|
+
## 0.1.0
|
|
19
|
+
|
|
20
|
+
### Minor Changes
|
|
21
|
+
|
|
22
|
+
- Initial release. A `Layer` capability that emits one OTel span per dispatched operation
|
|
23
|
+
(operation id, transport, outcome, duration) to the `@adhd/sox-telemetry` JSONL sink, correlated
|
|
24
|
+
by `trace_id`. Declares an empty `target` capability for interface parity (`--type tracing` is a
|
|
25
|
+
valid no-op).
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- The span's `apigen.transport` is now supplied by the engine from the operation plan's own
|
|
30
|
+
transport rather than inferred, so every transport — `http`, `grpc`, `mcp`, `cli` — reports
|
|
31
|
+
its own transport, never `undefined`. Reserved span keys (`apigen.op`, `apigen.transport`,
|
|
32
|
+
`trace_id`) can no longer be shadowed by an `envelopeAttrs` key of the same name; the reserved keys
|
|
33
|
+
always win. Streaming operations are quarantined to a unary span (see README): per-chunk records
|
|
34
|
+
with a chunk count are no longer emitted under the current `Next` contract.
|
package/README.md
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @adhd/apigen-plugin-tracing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Layer plugin: one OTel span per dispatched operation, written as durable JSONL trace records.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
`@adhd/apigen-plugin-tracing` instruments **every operation dispatched through an apigen transport**
|
|
8
|
+
— `http`, `grpc`, `mcp`, and `cli` — because all transports converge on a single composed invoker.
|
|
9
|
+
For each dispatched operation it emits a span carrying the operation id, the transport, the outcome,
|
|
10
|
+
and the duration, correlated by a `trace_id` that ties every span of one logical request together.
|
|
11
|
+
|
|
12
|
+
Records are written through [`@adhd/sox-telemetry`](https://www.npmjs.com/package/@adhd/sox-telemetry)
|
|
13
|
+
to the process's durable JSONL sink (`<dir>/<service>.<role>-<date>.jsonl`), so a second process — or
|
|
14
|
+
the backlog graph — can reconstruct one request across transports.
|
|
15
|
+
|
|
16
|
+
It is implemented as a **`Layer` capability** (one layer instruments all three transports). A
|
|
17
|
+
`target` capability is declared for interface parity but emits nothing: `--type tracing` resolves to a
|
|
18
|
+
valid no-op rather than an error. This mirrors `apigen-plugin-logger`.
|
|
19
|
+
|
|
20
|
+
Tracing composes cleanly alongside the logger plugin — same seam, no coupling:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
usePlugins: [tracingPlugin, loggerPlugin, batchPlugin];
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm add @adhd/apigen-plugin-tracing
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### CLI — bare slug
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
apigen run --source ./api.ts --type mcp --use tracing
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Also usable through the v2 multi-use option:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
apigen run --source ./api.ts --type mcp --use @adhd/apigen-plugin-tracing
|
|
44
|
+
# `--type tracing` is a valid no-op target (emits no files, throws no error)
|
|
45
|
+
apigen run --source ./api.ts --type tracing
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Programmatic
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { tracingPlugin, makeTracingPlugin, TraceHandle } from '@adhd/apigen-plugin-tracing';
|
|
52
|
+
|
|
53
|
+
// apigen's own singleton — span prefix `apigen` (for apigen's CLI and self-test only).
|
|
54
|
+
// adhd products must NOT use this: they pass their own namespace explicitly (see below).
|
|
55
|
+
run({ usePlugins: [tracingPlugin] });
|
|
56
|
+
|
|
57
|
+
// `makeTracingPlugin(...)` is the configured entry point — `serviceName` is REQUIRED (no
|
|
58
|
+
// implicit default) and is the namespace every emitted span/record/attribute is prefixed
|
|
59
|
+
// with. Pass your product's namespace (e.g. `adhd`), plus any envelope headers to copy.
|
|
60
|
+
|
|
61
|
+
// configured
|
|
62
|
+
run({
|
|
63
|
+
usePlugins: [
|
|
64
|
+
makeTracingPlugin({
|
|
65
|
+
serviceName: 'checkout',
|
|
66
|
+
envelopeAttrs: ['x-request-id'],
|
|
67
|
+
}),
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Annotating the live span from domain code
|
|
73
|
+
|
|
74
|
+
The layer seeds a `TraceHandle` into `call.ctx`; read it back and add attributes to the live span:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { TraceHandle } from '@adhd/apigen-plugin-tracing';
|
|
78
|
+
|
|
79
|
+
export async function placeOrder(call, ...args) {
|
|
80
|
+
const trace = call.ctx.get(TraceHandle);
|
|
81
|
+
trace?.annotate({ 'order.region': region });
|
|
82
|
+
// …
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`annotate` is a no-op when the operation is not currently inside a unary span (e.g. before the
|
|
87
|
+
downstream resolved, or after the span has closed). A streaming call is still spanned — as a
|
|
88
|
+
unary span covering only the point the stream is obtained (see [Records](#records)).
|
|
89
|
+
|
|
90
|
+
## Records
|
|
91
|
+
|
|
92
|
+
| Event | Level | Carries |
|
|
93
|
+
|---|---|---|
|
|
94
|
+
| `<serviceName>.<op>.start` | info | `<serviceName>.op`, `<serviceName>.transport`, `trace_id` |
|
|
95
|
+
| `<serviceName>.<op>.finish` | info | the above + `duration_ms` |
|
|
96
|
+
| `<serviceName>.<op>.error` | error | the above + `error` (the thrown message) |
|
|
97
|
+
| `<serviceName>.op.error` | error | `<serviceName>.op`, `<serviceName>.transport`, `trace_id`, `span`, `duration_ms`, `err` |
|
|
98
|
+
|
|
99
|
+
Streaming operations are **quarantined**: under the current `Next` contract
|
|
100
|
+
(`() => Promise<LayerResult>`), an `AsyncIterable` can only be a *resolved* value, never an
|
|
101
|
+
unresolved one, so the layer spans a streaming op only up to the point the stream is obtained — as a
|
|
102
|
+
unary span. Per-chunk `.{start,finish,error}` records with a chunk count are **not** emitted. The
|
|
103
|
+
`traceStream` helper remains in the package (directly unit-tested) behind a quarantine banner for a
|
|
104
|
+
future reopen-span follow-up; `traceUnary` is the only layer path.
|
|
105
|
+
|
|
106
|
+
## Options
|
|
107
|
+
|
|
108
|
+
| Option | Type | Default | Description |
|
|
109
|
+
|---|---|---|---|
|
|
110
|
+
| `serviceName` | `string` | _(required)_ | Span / record / attribute name prefix — the emitting product's namespace (e.g. `adhd`). |
|
|
111
|
+
| `envelopeAttrs` | `string[]` | `[]` | Extra `call.envelope` keys copied verbatim onto each span. |
|
|
112
|
+
|
|
113
|
+
## Part of the apigen toolchain
|
|
114
|
+
|
|
115
|
+
See [`@adhd/apigen-cli`](https://www.npmjs.com/package/@adhd/apigen-cli) for the full
|
|
116
|
+
TypeScript-to-API system and the list of available plugins.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});let e=require("node:crypto"),t=require("node:fs"),n=require("node:child_process"),r=require("node:path"),i=require("@adhd/sox-telemetry");function a(e){try{let t=(0,n.spawnSync)(`git`,[`rev-parse`,...e],{cwd:process.cwd(),encoding:`utf8`,timeout:2e3});if(t.status===0&&t.stdout){let e=t.stdout.trim();return e.length>0?e:null}}catch{}return null}function o(){let n=process.argv[1];if(n===void 0)return null;try{return`sha256:${(0,e.createHash)(`sha256`).update((0,t.readFileSync)(n)).digest(`hex`)}`}catch{return null}}function s(){let e=a([`--show-toplevel`]),t=a([`--path-format=absolute`,`--git-common-dir`]);return e===null||t===null?`none`:/[\\/]\.git$/.test(t)&&(0,r.resolve)((0,r.dirname)(t))!==(0,r.resolve)(e)?`linked`:`main`}var c=null;function l(){if(c===null){let t=process.env.ADHD_SESSION_ID,n=process.env.npm_package_version;c={sessionId:t!==void 0&&t.length>0?t:(0,e.randomUUID)(),worktree:s(),release:{version:n!==void 0&&n.length>0?n:null,gitSha:a([`HEAD`]),artifactSha256:o()}}}return c}var u=class{constructor(e,t,n){this.traceId=e,this.spanName=t,this.startedAt=n}annotate(e){let t=d.get(this);t!==void 0&&t.setAttributes(f(e))}},d=new WeakMap;function f(e){let t={};for(let[n,r]of Object.entries(e))(typeof r==`string`||typeof r==`number`||typeof r==`boolean`)&&(t[n]=r);return t}function p(e,t){let n={};for(let r of t){let t=e[r];t!==void 0&&(n[r]=t)}return n}function m(e){return e instanceof Error?e.message:typeof e==`string`?e:typeof e==`object`&&e&&`message`in e&&typeof e.message==`string`?e.message:String(e)}function h(e,t,n,r,a){return(0,i.withSpan)(t,f(n),async o=>{d.set(r,o);try{return await e}catch(e){throw i.log.error(`${a}.op.error`,{...n,span:t,duration_ms:Date.now()-r.startedAt,err:m(e)}),e}finally{d.delete(r)}})}function g(e){let t=e.serviceName;if(typeof t!=`string`||t.trim()===``)throw Error('@adhd/apigen-plugin-tracing: `serviceName` is required and must be a non-empty string — it namespaces every span name, attribute key, and error record. Construct the plugin explicitly, e.g. `makeTracingPlugin({ serviceName: "adhd" })`.');let n=e.envelopeAttrs??[];return(e,r)=>{let a=`${t}.${e.operation.id}`,o=(0,i.currentTraceId)()??(0,i.newTraceId)(),s=new u(o,a,Date.now());e.ctx.set(u,s);let c=l(),d={...p(e.envelope,n),[`${t}.op`]:e.operation.id,[`${t}.transport`]:e.transport,trace_id:o,session_id:c.sessionId,worktree:c.worktree,role:(0,i.currentRuntimeState)().role};return c.release.version!==null&&(d[`release.version`]=c.release.version),c.release.gitSha!==null&&(d[`release.git_sha`]=c.release.gitSha),c.release.artifactSha256!==null&&(d[`release.artifact_sha256`]=c.release.artifactSha256),(0,i.withTrace)(o,()=>h(Promise.resolve(r()),a,d,s,t))}}var _={id:`tracing`,description:`Layer plugin: emits one OTel span per dispatched operation (op id, transport, outcome, duration) to the sox-telemetry JSONL sink, correlated by trace_id.`,language:`ts`,optionsSchema:{type:`object`,properties:{serviceName:{type:`string`,description:`Span / record / attribute name prefix. REQUIRED — the emitting product namespace (e.g. adhd).`},envelopeAttrs:{type:`array`,items:{type:`string`},description:`Extra envelope keys copied verbatim onto each span. Default: [].`}},required:[`serviceName`],additionalProperties:!1},capabilities:{target:{name:`tracing`,generate(){return[]}},layer:{layer:g({serviceName:`apigen`})}}};function v(e){return{..._,capabilities:{..._.capabilities,layer:{layer:g(e)}}}}exports.TraceHandle=u,exports.default=_,exports.tracingPlugin=_,exports.makeTracingPlugin=v;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { createHash as e, randomUUID as t } from "node:crypto";
|
|
2
|
+
import { readFileSync as n } from "node:fs";
|
|
3
|
+
import { spawnSync as r } from "node:child_process";
|
|
4
|
+
import { dirname as i, resolve as a } from "node:path";
|
|
5
|
+
import { currentRuntimeState as o, currentTraceId as s, log as c, newTraceId as l, withSpan as u, withTrace as d } from "@adhd/sox-telemetry";
|
|
6
|
+
//#region src/lib/plugin.ts
|
|
7
|
+
function f(e) {
|
|
8
|
+
try {
|
|
9
|
+
let t = r("git", ["rev-parse", ...e], {
|
|
10
|
+
cwd: process.cwd(),
|
|
11
|
+
encoding: "utf8",
|
|
12
|
+
timeout: 2e3
|
|
13
|
+
});
|
|
14
|
+
if (t.status === 0 && t.stdout) {
|
|
15
|
+
let e = t.stdout.trim();
|
|
16
|
+
return e.length > 0 ? e : null;
|
|
17
|
+
}
|
|
18
|
+
} catch {}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
function p() {
|
|
22
|
+
let t = process.argv[1];
|
|
23
|
+
if (t === void 0) return null;
|
|
24
|
+
try {
|
|
25
|
+
return `sha256:${e("sha256").update(n(t)).digest("hex")}`;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function m() {
|
|
31
|
+
let e = f(["--show-toplevel"]), t = f(["--path-format=absolute", "--git-common-dir"]);
|
|
32
|
+
return e === null || t === null ? "none" : /[\\/]\.git$/.test(t) && a(i(t)) !== a(e) ? "linked" : "main";
|
|
33
|
+
}
|
|
34
|
+
var h = null;
|
|
35
|
+
function g() {
|
|
36
|
+
if (h === null) {
|
|
37
|
+
let e = process.env.ADHD_SESSION_ID, n = process.env.npm_package_version;
|
|
38
|
+
h = {
|
|
39
|
+
sessionId: e !== void 0 && e.length > 0 ? e : t(),
|
|
40
|
+
worktree: m(),
|
|
41
|
+
release: {
|
|
42
|
+
version: n !== void 0 && n.length > 0 ? n : null,
|
|
43
|
+
gitSha: f(["HEAD"]),
|
|
44
|
+
artifactSha256: p()
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return h;
|
|
49
|
+
}
|
|
50
|
+
var _ = class {
|
|
51
|
+
constructor(e, t, n) {
|
|
52
|
+
this.traceId = e, this.spanName = t, this.startedAt = n;
|
|
53
|
+
}
|
|
54
|
+
annotate(e) {
|
|
55
|
+
let t = v.get(this);
|
|
56
|
+
t !== void 0 && t.setAttributes(y(e));
|
|
57
|
+
}
|
|
58
|
+
}, v = /* @__PURE__ */ new WeakMap();
|
|
59
|
+
function y(e) {
|
|
60
|
+
let t = {};
|
|
61
|
+
for (let [n, r] of Object.entries(e)) (typeof r == "string" || typeof r == "number" || typeof r == "boolean") && (t[n] = r);
|
|
62
|
+
return t;
|
|
63
|
+
}
|
|
64
|
+
function b(e, t) {
|
|
65
|
+
let n = {};
|
|
66
|
+
for (let r of t) {
|
|
67
|
+
let t = e[r];
|
|
68
|
+
t !== void 0 && (n[r] = t);
|
|
69
|
+
}
|
|
70
|
+
return n;
|
|
71
|
+
}
|
|
72
|
+
function x(e) {
|
|
73
|
+
return e instanceof Error ? e.message : typeof e == "string" ? e : typeof e == "object" && e && "message" in e && typeof e.message == "string" ? e.message : String(e);
|
|
74
|
+
}
|
|
75
|
+
function S(e, t, n, r, i) {
|
|
76
|
+
return u(t, y(n), async (a) => {
|
|
77
|
+
v.set(r, a);
|
|
78
|
+
try {
|
|
79
|
+
return await e;
|
|
80
|
+
} catch (e) {
|
|
81
|
+
throw c.error(`${i}.op.error`, {
|
|
82
|
+
...n,
|
|
83
|
+
span: t,
|
|
84
|
+
duration_ms: Date.now() - r.startedAt,
|
|
85
|
+
err: x(e)
|
|
86
|
+
}), e;
|
|
87
|
+
} finally {
|
|
88
|
+
v.delete(r);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function C(e) {
|
|
93
|
+
let t = e.serviceName;
|
|
94
|
+
if (typeof t != "string" || t.trim() === "") throw Error("@adhd/apigen-plugin-tracing: `serviceName` is required and must be a non-empty string — it namespaces every span name, attribute key, and error record. Construct the plugin explicitly, e.g. `makeTracingPlugin({ serviceName: \"adhd\" })`.");
|
|
95
|
+
let n = e.envelopeAttrs ?? [];
|
|
96
|
+
return (e, r) => {
|
|
97
|
+
let i = `${t}.${e.operation.id}`, a = s() ?? l(), c = new _(a, i, Date.now());
|
|
98
|
+
e.ctx.set(_, c);
|
|
99
|
+
let u = g(), f = {
|
|
100
|
+
...b(e.envelope, n),
|
|
101
|
+
[`${t}.op`]: e.operation.id,
|
|
102
|
+
[`${t}.transport`]: e.transport,
|
|
103
|
+
trace_id: a,
|
|
104
|
+
session_id: u.sessionId,
|
|
105
|
+
worktree: u.worktree,
|
|
106
|
+
role: o().role
|
|
107
|
+
};
|
|
108
|
+
return u.release.version !== null && (f["release.version"] = u.release.version), u.release.gitSha !== null && (f["release.git_sha"] = u.release.gitSha), u.release.artifactSha256 !== null && (f["release.artifact_sha256"] = u.release.artifactSha256), d(a, () => S(Promise.resolve(r()), i, f, c, t));
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
var w = {
|
|
112
|
+
id: "tracing",
|
|
113
|
+
description: "Layer plugin: emits one OTel span per dispatched operation (op id, transport, outcome, duration) to the sox-telemetry JSONL sink, correlated by trace_id.",
|
|
114
|
+
language: "ts",
|
|
115
|
+
optionsSchema: {
|
|
116
|
+
type: "object",
|
|
117
|
+
properties: {
|
|
118
|
+
serviceName: {
|
|
119
|
+
type: "string",
|
|
120
|
+
description: "Span / record / attribute name prefix. REQUIRED — the emitting product namespace (e.g. adhd)."
|
|
121
|
+
},
|
|
122
|
+
envelopeAttrs: {
|
|
123
|
+
type: "array",
|
|
124
|
+
items: { type: "string" },
|
|
125
|
+
description: "Extra envelope keys copied verbatim onto each span. Default: []."
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
required: ["serviceName"],
|
|
129
|
+
additionalProperties: !1
|
|
130
|
+
},
|
|
131
|
+
capabilities: {
|
|
132
|
+
target: {
|
|
133
|
+
name: "tracing",
|
|
134
|
+
generate() {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
layer: { layer: C({ serviceName: "apigen" }) }
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
function T(e) {
|
|
142
|
+
return {
|
|
143
|
+
...w,
|
|
144
|
+
capabilities: {
|
|
145
|
+
...w.capabilities,
|
|
146
|
+
layer: { layer: C(e) }
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
export { _ as TraceHandle, w as default, w as tracingPlugin, T as makeTracingPlugin };
|
package/lib/plugin.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Plugin, Call, Next, Result, Chunk } from '@adhd/apigen-core-client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where `process.cwd()` sits relative to git — three-valued so the main checkout and a
|
|
5
|
+
* cwd outside any repository (a boolean reported both as the same value) stay
|
|
6
|
+
* distinguishable when records are aggregated.
|
|
7
|
+
*/
|
|
8
|
+
export type WorktreeKind = 'main' | 'linked' | 'none';
|
|
9
|
+
/**
|
|
10
|
+
* Classify `process.cwd()` as a LINKED git worktree, a repository's main checkout, or
|
|
11
|
+
* nothing at all:
|
|
12
|
+
*
|
|
13
|
+
* - `'linked'` — inside a LINKED worktree: `--git-common-dir` names the main
|
|
14
|
+
* checkout's `.git` directory (a path ending in `.git`), and its
|
|
15
|
+
* parent is not this worktree's own `--show-toplevel`.
|
|
16
|
+
* - `'main'` — inside a repository but not a linked worktree (the main checkout).
|
|
17
|
+
* - `'none'` — not inside any repository (either `git rev-parse` probe fails).
|
|
18
|
+
*
|
|
19
|
+
* Exported only so all three values can be unit-tested directly — the same
|
|
20
|
+
* test-only, not-in-the-public-API precedent as `traceStream`; NOT re-exported
|
|
21
|
+
* from `src/index.ts`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function classifyWorktree(): WorktreeKind;
|
|
24
|
+
/**
|
|
25
|
+
* Options for {@link makeTracingPlugin}.
|
|
26
|
+
*
|
|
27
|
+
* Layer plugins receive no opts at call time — `LayerCapability.layer` is invoked with only
|
|
28
|
+
* `(call, next)` — so configuration is a factory, exactly as `makeLoggerPlugin` does it.
|
|
29
|
+
*/
|
|
30
|
+
export type TracingOptions = {
|
|
31
|
+
/**
|
|
32
|
+
* Span / record / attribute name prefix — the emitting product's namespace. REQUIRED: there
|
|
33
|
+
* is no implicit default, so a product cannot silently emit another product's telemetry.
|
|
34
|
+
* adhd products pass `'adhd'`; apigen's own CLI and self-test pass `'apigen'` explicitly.
|
|
35
|
+
*/
|
|
36
|
+
serviceName: string;
|
|
37
|
+
/**
|
|
38
|
+
* Extra attribute keys copied verbatim from `call.envelope` onto each span. Default: `[]`
|
|
39
|
+
* (only the built-in attrs `${serviceName}.op`, `${serviceName}.transport`, `trace_id` are
|
|
40
|
+
* emitted).
|
|
41
|
+
*/
|
|
42
|
+
envelopeAttrs?: readonly string[];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Per-call trace handle, seeded into `call.ctx`. The class is the ctx token: domain code reads
|
|
46
|
+
* it back with `call.ctx.get(TraceHandle)`.
|
|
47
|
+
*/
|
|
48
|
+
export declare class TraceHandle {
|
|
49
|
+
/** Correlation id shared by every span of one logical request. */
|
|
50
|
+
readonly traceId: string;
|
|
51
|
+
/** Span name — `${serviceName}.${operation.id}`. */
|
|
52
|
+
readonly spanName: string;
|
|
53
|
+
/** Epoch millis at which the span opened. */
|
|
54
|
+
readonly startedAt: number;
|
|
55
|
+
constructor(
|
|
56
|
+
/** Correlation id shared by every span of one logical request. */
|
|
57
|
+
traceId: string,
|
|
58
|
+
/** Span name — `${serviceName}.${operation.id}`. */
|
|
59
|
+
spanName: string,
|
|
60
|
+
/** Epoch millis at which the span opened. */
|
|
61
|
+
startedAt: number);
|
|
62
|
+
/**
|
|
63
|
+
* Add attributes to the live span from domain code — opt-in. A no-op when the operation is
|
|
64
|
+
* not currently inside a unary span (e.g. before the downstream resolved, or after the span
|
|
65
|
+
* has closed). Streaming is not special-cased: the layer spans a streaming op as a unary
|
|
66
|
+
* span covering only the point the stream is obtained.
|
|
67
|
+
*/
|
|
68
|
+
annotate(fields: Record<string, unknown>): void;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* QUARANTINED — unreachable under the current `Next` contract; see CONTRACT-FIX §4.
|
|
72
|
+
* Exported only so the streaming branch can be exercised by a direct unit test; no layer
|
|
73
|
+
* path reaches it (a `Next` resolves to a `LayerResult`, so this detector can never be
|
|
74
|
+
* true for the unresolved `next()` promise). Not re-exported from the package entry
|
|
75
|
+
* (`src/index.ts`), so it is not part of the public API either. Retained — never
|
|
76
|
+
* deleted — pending a `Next` that can yield an iterable before resolution.
|
|
77
|
+
*/
|
|
78
|
+
export declare function isAsyncIterable(value: unknown): value is AsyncIterable<Chunk>;
|
|
79
|
+
/**
|
|
80
|
+
* QUARANTINED — unreachable under the current `Next` contract; see CONTRACT-FIX §4.
|
|
81
|
+
* The layer no longer branches on `next()` (an `AsyncIterable` can only be a *resolved*
|
|
82
|
+
* value, never the unresolved promise), so this is retained with its body intact and
|
|
83
|
+
* exported solely for a direct unit test (and not re-exported from the package entry
|
|
84
|
+
* `src/index.ts`, so not part of the public API). Do NOT re-wire it into the layer by
|
|
85
|
+
* awaiting `next()` first — that would destroy the span-before-body invariant guarded by
|
|
86
|
+
* `plugin.spec.ts` (the hang-visibility test).
|
|
87
|
+
*
|
|
88
|
+
* Streaming branch — `withSpan` cannot await an iterable, so the `.start` record is emitted
|
|
89
|
+
* synchronously on entry and an async-generator wrapper yields each chunk unchanged, then
|
|
90
|
+
* emits `.finish` with the chunk count (or `.error` and re-throws).
|
|
91
|
+
*/
|
|
92
|
+
export declare function traceStream(downstream: AsyncIterable<Chunk>, spanName: string, attrs: Record<string, unknown>): AsyncIterable<Chunk>;
|
|
93
|
+
/**
|
|
94
|
+
* Build the tracing layer for a given configuration. The returned layer calls `next()` exactly
|
|
95
|
+
* once and returns its resolved value (or iterable) unchanged, wrapping it in a span.
|
|
96
|
+
*/
|
|
97
|
+
export declare function makeTraceLayer(opts: TracingOptions): (call: Call, next: Next) => Promise<Result> | AsyncIterable<Chunk>;
|
|
98
|
+
/**
|
|
99
|
+
* Tracing plugin.
|
|
100
|
+
*
|
|
101
|
+
* Implemented as a **Layer** capability — a single layer instruments every transport (http, grpc,
|
|
102
|
+
* mcp, cli) because they all dispatch through one composed invoker. The `target` capability is
|
|
103
|
+
* declared but emits nothing (`generate()` returns `[]`), so `--type tracing` resolves to a valid
|
|
104
|
+
* no-op rather than an error — the same precedent as `apigen-plugin-logger`.
|
|
105
|
+
*/
|
|
106
|
+
export declare const tracingPlugin: Plugin<TracingOptions>;
|
|
107
|
+
/** Configured factory — rebuilds the layer with `opts` (logger precedent). */
|
|
108
|
+
export declare function makeTracingPlugin(opts: TracingOptions): Plugin<TracingOptions>;
|
|
109
|
+
export default tracingPlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adhd/apigen-plugin-tracing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@adhd/apigen-core-client": "^0.3.3",
|
|
6
|
+
"@adhd/sox-telemetry": "^0.4.1"
|
|
7
|
+
},
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"module": "./index.mjs",
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"description": "Tracing plugin for apigen servers",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"tracing",
|
|
17
|
+
"telemetry",
|
|
18
|
+
"otel",
|
|
19
|
+
"apigen",
|
|
20
|
+
"plugin",
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/PseudoSky/adhd.git",
|
|
27
|
+
"directory": "packages/apigen/apigen-plugin-tracing"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/PseudoSky/adhd/tree/main/packages/apigen/apigen-plugin-tracing#readme",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/PseudoSky/adhd/issues"
|
|
32
|
+
}
|
|
33
|
+
}
|