Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions docs/content/1.guide/24.tracing-channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export default defineDevframe({
})
```

`tracePromise` writes the resolved value to `context.result` and a rejection to `context.error`, so a record shows both. Publishing costs nothing while nobody records: `TracingChannel` skips its work when `hasSubscribers` is `false`.
For the query above, Node emits `end` when the synchronous part of the call finishes. The trace can already show `ok` while the database query is still running. For this `tracePromise` call, `asyncEnd` marks the end of its asynchronous work.

If you keep recording without clearing the buffer, the same record shows the result or changes to `error` when the promise settles. If you call `ctx.tracing.clear('my-tool:query')` while the query is pending, its next recorded event starts a record with a new ID.

`TracingChannel` skips publishing while `hasSubscribers` is `false`.

## Recording

Expand Down Expand Up @@ -89,7 +93,7 @@ The context, result and error pass through a serializer that caps depth at 4, tr

## Attaching a tool's own recorder

A tool that already traces on its own does not need to move its recording into `ctx.tracing`. It keeps its own recorder and, when a devframe mounts it, attaches the recorder so the channels and records show up there. Devframe relays them. It does not subscribe to the channels itself.
A tool can keep its own recorder and attach it to `ctx.tracing` when a devframe mounts it. Devframe then relays the tool's records.

`createTracingRecorder()` from `devframe/node` returns a recorder with the same recording methods as `ctx.tracing`, but without the wire:

Expand All @@ -111,11 +115,9 @@ export default defineDevframe({
})
```

`attach()` returns a function that detaches the recorder and removes its channels from the list. While the recorder is attached:
When several recorders declare the same channel name, the most recently attached source takes precedence. The channel's `recording` flag and count describe that source. `records(name)` reads its buffer, and `onRecord(name, fn)` receives its next updates. Calls to `record(name)`, `stop(name)` and `clear(name)`, including their RPC equivalents, go to it.

- `list()`, `records(name)` and `onRecord(name)` include its channels.
- `record(name)`, `stop(name)` and `clear(name)`, and the matching RPC actions, call the recorder's own methods. Devframe never records an attached channel itself.
- When the recorder and `ctx.tracing` both know a name, the recorder wins. Use a prefix such as `my-tool:` to keep names apart.
For example, if `ctx.tracing` was already recording `module.require` when a tool attached a recorder with that name, `stop('module.require')` stops the tool's recorder. The local recorder keeps running. After you detach the tool, `records('module.require')` includes the calls the local recorder captured during that time. Use a prefix such as `my-tool:` for channels your tool defines.

The recorder does not have to come from devframe. Any object with the `DevframeTracingSource` shape works, so the tool can keep devframe out of its runtime dependencies:

Expand Down Expand Up @@ -149,7 +151,11 @@ for await (const record of rpc.streaming.subscribe('devframe:tracing', streamId)
}
```

`record()` and `clear()` issue a fresh `streamId`, so watch the shared state and re-subscribe when it changes. The RPC actions `devframe:tracing:record`, `devframe:tracing:stop` and `devframe:tracing:clear` take the channel name.
Watch the shared state and subscribe whenever a new `streamId` appears. With the built-in recorder, calling `record()` again while recording keeps the current stream. A clear during recording gives it a new ID. Stopping removes the ID, and restarting creates a new one. An attached source can implement controls differently. Its recording state and clear notifications determine the published ID.

If two sources are recording the same name, attaching the new source before detaching the old one keeps the stream open. A new subscription can then replay updates from the previous source. If detaching the old source first leaves the channel absent or idle, attaching the new recording source starts a stream with a new ID.

The RPC actions `devframe:tracing:record`, `devframe:tracing:stop` and `devframe:tracing:clear` take the channel name.

## Runtimes

Expand Down
2 changes: 1 addition & 1 deletion docs/content/8.references/3.events.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Published by the core node side; read them with `rpc.sharedState.get(key)` and `
| Name | Kind | Carries |
|---|---|---|
| `devframe:tracing:channels` | shared state | Known [Tracing Channels](/guide/tracing-channels) keyed by base name (`DevframeTracingChannelInfo`): source, description, `recording`, `count`, and the live `streamId` while recording. |
| `devframe:tracing` | streaming channel | Trace records (`DevframeTraceRecord`) of one recording Tracing Channel per stream; the stream id is the `streamId` from `devframe:tracing:channels`. A fresh id is issued on every `record()` and `clear()`. |
| `devframe:tracing` | streaming channel | Trace records (`DevframeTraceRecord`) of one recording Tracing Channel per stream; the stream id is the `streamId` from `devframe:tracing:channels`. Use the advertised ID. Devframe creates a stream when the channel starts recording and replaces the stream when the selected source reports a clear during recording. Repeating `record()` on the built-in recorder keeps the current stream. |

### In-page channel notifications (page script → panel)

Expand Down
4 changes: 2 additions & 2 deletions docs/content/8.references/4.node-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The methods on `ctx.services`: [Cross-Devframe Services](/guide/services#the-dev

## `DevframeTracingHost`

The methods on `ctx.tracing`: [Tracing Channels](/guide/tracing-channels).
This table lists the methods on `ctx.tracing`. The recording controls and buffer limits describe the built-in recorder. An attached source supplies its own records and decides which controls it supports. See [Tracing Channels](/guide/tracing-channels).

| Method | Signature | Role |
|--------|-----------|------|
Expand All @@ -144,7 +144,7 @@ The methods on `ctx.tracing`: [Tracing Channels](/guide/tracing-channels).
| `records` | `(name) => DevframeTraceRecord[]` | Buffered records, oldest first, at most 500 per channel. |
| `clear` | `(name) => void` | Drop the buffer and, while recording, start a fresh stream. |
| `onRecord` | `(name, fn) => unsubscribe` | Run `fn` with the whole record on every lifecycle update. |
| `attach` | `(source) => detach` | Show a tool's own recorder (`createTracingRecorder()` or any `DevframeTracingSource`). Devframe relays its channels and records and routes `record` / `stop` / `clear` to it, but never records them itself. The source attached last wins a name. See [Attaching a tool's own recorder](/guide/tracing-channels#attaching-a-tools-own-recorder). |
| `attach` | `(source) => detach` | Show a tool's own recorder (`createTracingRecorder()` or any `DevframeTracingSource`). For a shared name, the source attached last handles controls and supplies the buffer read by `records(name)`. Earlier recorders keep their own recording state. See [Attaching a tool's own recorder](/guide/tracing-channels#attaching-a-tools-own-recorder). |

## Service tiers

Expand Down
14 changes: 7 additions & 7 deletions packages/devframe/src/types/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export interface DevframeTracingChannelInfo {
name: string
description?: string
source: DevframeTracingChannelSource
/** `true` while devframe is subscribed to the Node channel. */
/** Whether the selected source is recording this channel. */
recording: boolean
/**
* Id of the live stream on the `devframe:tracing` streaming channel that
* carries this channel's records. Set while recording; a fresh id is
* issued on every `record()` and `clear()`, so a client re-subscribes
* when it changes.
* carries this channel's records. Published in `devframe:tracing:channels` while recording. Devframe starts
* a new stream when recording starts or the selected source reports a
* clear during recording. Subscribe again when the published ID changes.
*/
streamId?: string
/** Records currently held in the ring buffer. */
Expand Down Expand Up @@ -91,7 +91,7 @@ export type DevframeTracingSourceEvent
* Anything that owns Tracing Channels and their records, outside of
* `ctx.tracing`. A tool that keeps its own tracing instance attaches it with
* `ctx.tracing.attach(source)`: devframe then shows the source's channels and
* relays its records, but never subscribes to them itself. The source records.
* relays its records. Other recorders keep their own recording state.
*
* `createTracingRecorder()` from `devframe/node` returns one. Any object with
* this shape works too, so a tool does not need devframe at runtime.
Expand Down Expand Up @@ -161,8 +161,8 @@ export interface DevframeTracingHost {
onRecord: (name: string, fn: (record: DevframeTraceRecord) => void) => () => void
/**
* Show the channels and records of a tool's own tracing instance. Devframe
* relays them over the wire but does not record them; the source does.
* When two sources own the same name, the one attached last wins.
* relays records from the source attached last for each name. Attaching
* or detaching a source preserves the other recorders' recording state.
* Returns a function that detaches the source.
*/
attach: (source: DevframeTracingSource) => () => void
Expand Down
Loading