> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Metrics

> Track custom Prometheus metrics in EVM pipes

Pipes SDK can expose a Prometheus-compatible metrics server. You can customize it to add counters, gauges, histograms, and summaries.

```ts theme={"system"}
import { commonAbis, evmDecoder, evmPortalStream } from "@subsquid/pipes/evm";
import { metricsServer } from "@subsquid/pipes/metrics/node";

async function main() {
  const stream = evmPortalStream({
    id: 'evm-decoder',
    portal: 'https://portal.sqd.dev/datasets/ethereum-mainnet',
    outputs: evmDecoder({
      range: {
        from: 'latest',
      },
      events: {
        transfers: commonAbis.erc20.events.Transfer,
      },
    }),
    metrics: metricsServer({
      port: 9090
    }), // equivalent to metricsServer(), as 9090 is the default port
  })

  for await (const { data, ctx } of stream) {
    // Add custom counter metric
    ctx.metrics
      .counter({
        name: "my_transfers_counter",
        help: "Number of processed transactions",
      })
      .inc(data.transfers.length);
  }
}

void main()
```

<Check>
  Access metrics at `http://localhost:9090/metrics` to verify they're being exposed correctly.

  ```
  # HELP my_transfers_counter Number of processed transactions
  # TYPE my_transfers_counter counter
  my_transfers_counter 218598
  ```
</Check>

<Tip>
  Use Grafana dashboards to visualize block processing rate, error rates, and latency trends from your Prometheus metrics.
</Tip>

## Available metric types

You can create different types of Prometheus metrics:

```ts theme={"system"}
for await (const { data, ctx } of stream) {
  // Counter - monotonically increasing value
  ctx.metrics.counter({ name: "events_total", help: "Total events" }).inc();

  // Gauge - value that can go up or down
  ctx.metrics
    .gauge({ name: "queue_size", help: "Current queue size" })
    .set(queueSize);

  // Histogram - observations with configurable buckets
  ctx.metrics
    .histogram({ name: "batch_size", help: "Batch size distribution" })
    .observe(data.transfers.length);
}
```

<Info>
  Expose metrics with `metricsServer()` on your source, then visualize them with [Pipes UI](../basic-development/pipes-ui).
</Info>

See the [Profiling](./profiling) guide for the built-in per-batch profiler exposed on the same metrics endpoint.
