Skip to content

@ogygia/devtools · API

@ogygia/devtools/profiler

span(), instrument() and tag(), for naming your own server work inside a profiled request.

On this page

This page is generated from the package’s own .d.ts files on every build, so the signatures, descriptions and examples below always match the source.

Exports instrument · set_span_recorder · span · span_recording · tag · SpanHandle · SpanRecord · SpanRecorder · InstrumentAttrs · SpanAttrs

import { … } from '@ogygia/devtools/profiler';

instrument

function

INSTRUMENT a function once, so every call is a span — for a renderer or a client you call from many places and cannot wrap at each site: a design system's renderToString, a database client's query, an SDK method.

Two forms: • instrument(fn, name, attrs?) returns a wrapped function (an ESM import is read-only, so bind the result: const renderToString = instrument(renderToStringCore, 'ds.render')). • instrument(obj, 'method', name, attrs?) patches the method in place (a client instance, a prototype) and returns a function that restores it.

attrs gets the result AND the call's arguments, so a span can carry the tag it rendered or the bytes it produced: (html, tag) => ({ tag, bytes: html.length }). Sync in, sync out; a promise in, a promise out; this preserved. Outside a recording the wrapper is one if.

import { instrument } from '@ogygia/devtools/profiler';
import { renderToString as core } from '@acme/design-system/hydrate';
const renderToString = instrument(core, 'ds.render', (r, tag) => ({ tag, bytes: r.html.length }));
function instrument<A extends unknown[], R>(fn: (...args: A) => R, name: string, attrs?: InstrumentAttrs<A, R>): (...args: A) => R;
function instrument<T extends object, K extends keyof T>(target: T, method: K, name: string, attrs?: T[K] extends ((...args: infer A) => infer R) ? InstrumentAttrs<A, R> : never): () => void;

set_span_recorder

function

The profiler host installs a recorder for a recording; null uninstalls.

function set_span_recorder(r: SpanRecorder | null): void;

span

function

Names a stretch of a request the sampler cannot see: a database driver on its own socket pool, a queue, a cache lookup, a promise chain. While a recording runs it is recorded like an outbound call (start and end, the request it ran in, the caller's file and line, its parent span), and the report shows it as a named block on the timeline (a gap inside it reads "in db.user", not "nothing recorded"), a row in the Spans table, and a finding when it repeats or fails. With no recording (the client, a universal load in the browser, a server not recording) it runs fn and returns what it returns: one if.

Sync in, sync out; a promise in, a promise out — the wrapper never changes the shape of what it wraps. A throw (or rejection) is recorded on the span and rethrown. attrs is a plain record, or a function of the result for what is only known afterwards ((r) => ({ cache: r.fromCache ? 'hit' : 'miss', rows: r.length })). Reserved keys the report reads: cache ('hit' | 'miss'), rows, bytes, key; anything else is carried and shown.

import { span, tag } from '@ogygia/devtools/profiler';

export const load = async ({ params, locals }) => {
  tag('tenant', locals.tenant);
  const user = await span('db.user', () => db.users.find(params.id));
  const orders = await span('db.orders', () => db.orders.byUser(user.id), (r) => ({ rows: r.length }));
  return { user, orders };
};
function span<T>(name: string, fn: () => T, attrs?: SpanAttrs | ((result: Awaited<T>) => SpanAttrs | undefined)): T;

span_recording

function

Whether a recorder is installed (tests / the host).

function span_recording(): boolean;

tag

function

Stamp the current request with key = value (string, number or boolean; kept to 64 chars, at most 16 keys). Shown on the request's log row and in the report; the dashboard splits the slowest routes by any tag. A no-op outside a recording, and always on the client.

function tag(key: string, value: string | number | boolean): void;

SpanHandle

interface

A span opened by hand (span.start) for what a callback cannot wrap: a stream, an event.

SpanHandle.set

add or change an attribute while the span runs

SpanHandle.end

close it; ending twice is a no-op

SpanHandle.fail

close it as failed

interface SpanHandle {/*…*/}
set(key: string, value: string | number | boolean): void;
end(attrs?: SpanAttrs): void;
fail(error: unknown): void;

SpanRecord

interface

SpanRecord.id

SpanRecord.name

SpanRecord.start

performance.now() at start

SpanRecord.ms

duration; -1 while running (an open span at the end of a recording keeps -1)

SpanRecord.attrs

SpanRecord.error

the thrown error's message, when fn threw / the handle was failed

SpanRecord.open

never ended before the recording stopped

SpanRecord.parent

the enclosing span's id, when nested

SpanRecord.caller

resolved at report time from caller_site (same as an outbound call's)

SpanRecord.caller_site

SpanRecord.route

the page render that ran it, when known

SpanRecord.path

interface SpanRecord {/*…*/}
id: number;
name: string;
start: number;
ms: number;
attrs?: SpanAttrs;
error?: string;
open?: boolean;
parent?: number;
caller?: string;
caller_site?: CallerSite;
route: string | null;
path: string | null;

SpanRecorder

interface

What the profiler host installs while a recording runs.

SpanRecorder.begin

SpanRecorder.within

run fn with s as the current span (nesting); the host binds it to its async context

SpanRecorder.end

SpanRecorder.tag

interface SpanRecorder {/*…*/}
begin(name: string, attrs?: SpanAttrs): SpanRecord;
within<T>(s: SpanRecord, fn: () => T): T;
end(s: SpanRecord, attrs?: SpanAttrs, error?: unknown): void;
tag(key: string, value: string): void;

InstrumentAttrs

type

The attrs callback of instrument: the result, then the call's arguments. Declared method-style so a callback naming fewer arguments than the function takes ((r, tag) => … for a two-argument renderer) is accepted, and so it never narrows the wrapped signature. NoInfer wraps the whole callback, not its rest argument: on the rest tuple alone it made a shorter callback a tuple-length mismatch, so the (r, tag) => … form above did not type-check.

type InstrumentAttrs<A extends unknown[], R> = NoInfer<{
  bivariant(result: Awaited<R>, ...args: A): SpanAttrs | undefined;
}["bivariant"]>;

SpanAttrs

type

type SpanAttrs = Record<string, string | number | boolean>;