# setMetric

**Kind:** Function

**Source:** [`src/middleware/timing/timing.ts`](https://github.com/honojs/hono/blob/main/src/middleware/timing/timing.ts#L149)

**Part of:** [Middleware](subsystem-src-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

| Name | Type |
|---|---|
| `c` | `Context` |
| `name` | `string` |
| `valueDescription` | `number | string | undefined` |
| `description` | `string` |
| `precision` | `number` |

## 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.
