# Collect metrics

Tenzir keeps track of metrics about node resource usage, pipeline state, and runtime performance.

Metrics are stored as internal events in the node’s storage engine, allowing you to work with metrics just like regular data. Use the [`metrics`](https://preview.docs.tenzir.com/375/375/reference/operators/metrics.md) input operator to access the metrics. The operator documentation lists [all available metrics](https://preview.docs.tenzir.com/375/375/reference/operators/metrics.md#schemas) in detail.

The `metrics` operator provides a *copy* of existing metrics. You can use it multiple time to reference the same metrics feed.

## Write metrics to a file

Export metrics continuously to a file via `metrics --live`:

```tql
metrics live=true
to_file "metrics.json", append=true {
  write_ndjson
}
```

This attaches to incoming metrics feed, renders them as NDJSON, and then writes the output to a file. Without the `live` option, the `metrics` operator returns the snapshot of all historical metrics.

## Summarize metrics

You can [transform](https://preview.docs.tenzir.com/375/375/guides/transformation/filter-and-select-data.md) metrics like ordinary data, for example, write aggregations over metrics to compute runtime statistics suitable for reporting or dashboarding:

```tql
metrics "operator"
where sink == true
summarize runtime=sum(duration), pipeline_id
sort -runtime
```

The previous example computes the total runtime over all pipelines grouped by their unique ID.

## Inspect pipeline throughput

Use `tenzir.metrics.pipeline` events to compare the number of events and bytes that enter and leave each pipeline:

```tql
metrics "pipeline"
summarize ingress_events=sum(ingress.events), ingress_bytes=sum(ingress.bytes), egress_events=sum(egress.events), egress_bytes=sum(egress.bytes), pipeline_id
sort -egress_events
```

The `events` fields count records that passed through the pipeline during the metric period. The `bytes` fields approximate the amount of data transferred.

## Send metrics to Prometheus

Use `shape="prometheus"` with [`metrics`](https://preview.docs.tenzir.com/375/375/reference/operators/metrics.md) and [`to_prometheus`](https://preview.docs.tenzir.com/375/375/reference/operators/to_prometheus.md) to send live metrics to a Prometheus-compatible Remote Write receiver:

```tql
metrics live=true, shape="prometheus"
to_prometheus "https://prometheus.example/api/v1/write"
```

The [Prometheus](https://preview.docs.tenzir.com/375/375/integrations/prometheus.md) integration page covers endpoint setup, authentication headers, and additional examples.