Skip to content

setMetric

reference
1 min readUpdated

Kind: Function

Source: src/middleware/timing/timing.ts

Part of: Middleware

Set a metric for the timing middleware.

setMetric adds a named metric to the timing data associated with the current request context. The timing middleware reads these metrics when producing timing information for the response.

Signature

ts
function setMetric(c: Context, name: string, valueDescription: number | string | undefined, description: string, precision: number)

Parameters

NameType
cContext
namestring
valueDescription`number
descriptionstring
precisionnumber

Diagram

mermaid
graph LR
  Request --> TimingMiddleware
  TimingMiddleware --> Handler
  Handler --> setMetric
  setMetric --> RequestMetrics
  RequestMetrics --> TimingMiddleware
  TimingMiddleware --> ResponseTiming

Usage

ts
import { timing, setMetric } from './middleware/timing/timing'

app.use('*', timing())

app.get('/reports', async (c) => {
  const startedAt = performance.now()

  const report = await loadReport()

  setMetric(c, 'report_load', performance.now() - startedAt)

  return c.json(report)
})

AI Coding Instructions

  • Register the timing middleware before handlers that call setMetric.
  • Pass the current request context to setMetric so the metric is attached to the correct response.
  • Use stable, descriptive metric names to keep response timing output consistent.
  • Record metric values in the unit expected by the timing middleware.

Was this page helpful?

Download as PDF
setMetric — Hono (narrator proof)