Skip to content

Observability

Seeing what the client is doing: lifecycle events, metrics, and OpenTelemetry. Back to the README.

The client emits typed events across all requests. Use them for logging, alerting or your own metrics.

Exactly one of success, failure or cancelled fires per ticket.

client.on("request", ({ ticketId, url, method, partition }) => {});
client.on("retry", ({ ticketId, url, partition, attempt, delayMs, error }) => {});
client.on("success", ({ ticketId, url, partition, attempts, durationMs, queuedMs, statusCode }) => {});
client.on("failure", ({ ticketId, url, partition, attempts, durationMs, queuedMs, error }) => {});
client.on("cancelled", ({ ticketId, url, partition, attempts, durationMs, queuedMs }) => {});

In retry, attempt is a zero-based retry index: 0 is the first retry, after the initial attempt.

Example (removing a listener)

const onFailure = ({ url, error }: { url: string; error: { kind: string } }) =>
console.error("request failed", url, error.kind);
client.on("failure", onFailure);
client.off("failure", onFailure); // same signature as on()

A listener, or a metrics sink, that throws never affects the request. Every other listener still runs.

The error is rethrown on a microtask, so it surfaces as an uncaught error:

  • On Node: process.on("uncaughtException").
  • On runtimes that have one: the global error event.

queuedMs is the total time a ticket waited, summed across every attempt. It counts waits for a bulkhead permit, a global-semaphore permit or a rate-limit token.

It’s 0 when a request never had to wait. The default global cap is 50 concurrent requests, so most single-service consumers never hit it.

Example (spotting throttling)

client.on("success", ({ partition, durationMs, queuedMs }) => {
if (queuedMs > durationMs / 2) console.warn("mostly queued", partition, queuedMs);
});

A queuedMs that stays high relative to durationMs means a concurrency limit is throttling you, not downstream latency. For the matching gauges, vereda.queue_depth and vereda.global_queue_depth, see Wiring a metrics sink.

With a retry budget configured, retryBudgetExhausted fires when the budget refuses a retry. It fires right before that ticket’s failure event.

client.on("retryBudgetExhausted", ({ ticketId, url, partition }) => {});

With the circuit breaker enabled, a partition also fires circuitOpen and circuitClose. These don’t belong to any single ticket.

client.on("circuitOpen", ({ partition }) => {});
client.on("circuitClose", ({ partition }) => {});

Pass a metrics sink, and the client reports counters, histograms and gauges as requests run.

A sink is three methods. That makes it a thin adapter over OpenTelemetry, StatsD, Prometheus or whatever your service already uses.

Example (a sink that logs every data point)

import { HttpClient, type MetricsSink } from "@vereda/http";
const metrics: MetricsSink = {
counter: (name, value, tags) => console.log("counter", name, value, tags),
histogram: (name, value, tags) => console.log("histogram", name, value, tags),
gauge: (name, value, tags) => console.log("gauge", name, value, tags),
};
const client = HttpClient.create({ timeout: { attemptMs: 5_000 }, metrics });
MetricTypeTags
vereda.requestscounterpartition, method
vereda.retriescounterpartition, kind (what triggered the retry)
vereda.duration_mshistogrampartition, kind (success, cancelled, or the error kind)
vereda.circuit_opencounterpartition
vereda.retry_budget_exhaustedcounterpartition
vereda.queue_depthgaugepartition
vereda.in_flight, vereda.global_queue_depthgaugenone

Everything that concerns a single dependency is tagged with partition: its host, unless you set one. So one struggling upstream gets its own line on a graph instead of being averaged into all the others.

For OpenTelemetry, use the ready-made sink. The operations guide covers each metric in detail, including how to read the queue-depth gauges.

@vereda/http/otel connects a client to OpenTelemetry.

It needs @opentelemetry/api, an optional peer dependency that only this entry point imports. Set up the SDK as usual.

Example (metrics and traces)

import { metrics, trace } from "@opentelemetry/api";
import { HttpClient } from "@vereda/http";
import { instrumentTracing, otelMetricsSink } from "@vereda/http/otel";
const client = HttpClient.create({
timeout: { attemptMs: 5_000 },
metrics: otelMetricsSink(metrics.getMeter("checkout")),
});
// After your own client.use() calls, so the attempt span sits closest to fetch.
const stop = instrumentTracing(client, { tracer: trace.getTracer("checkout") });
What you get
TracesOne span per ticket, covering queueing, every attempt and backoff, with a child CLIENT span per attempt
PropagationEach attempt’s traceparent is injected into the request headers through your registered propagator, so downstream services join the trace
Redactionurl.full never contains credentials, and query values become [redacted]
MetricsotelMetricsSink records the metrics above as counters, a histogram (unit ms) and observable gauges
  • Attributes follow the HTTP semantic conventions: http.request.method, url.full, http.response.status_code, http.request.resend_count on retries, and error.type.
  • Each retry adds a vereda.retry event to the ticket span.
  • A cancelled ticket keeps an unset status.
  • Attempt errors are recorded by type only, since an error message can contain a URL.
  • propagate: false turns off header injection.
  • redactQuery: false keeps query values in url.full. Credentials are still removed.

stop() ends any open ticket spans and stops recording.

The attempt middleware stays registered, since middleware can’t be removed, but it no longer records anything.