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

# How to instrument

> Expose custom metrics from your services to the inbuilt Prometheus + Grafana stack so they appear automatically in your environment's dashboards.

This is a guide to learn more about how you can instrument your services by exposing custom metrics that are crucial for
your app/use case.

For example, you can record `emails_received`, `emails_sent`, `emails_bounced` if you're building a transaction email
service. Or you can record `tcp_connections` if you're building realtime services.

## How to instrument your services

LocalOps by default embeds Prometheus in every environment. To make your service's metrics available to Prometheus,
follow these steps to expose an endpoint that responds with metrics in the format Prometheus expects.

### 1. Use a Prometheus client library in code

You can do this without a Prometheus client library too, but it is much easier if you use the library. Prometheus
officially supports client libraries for these languages:

1. Go
2. Java
3. Python
4. Rust
5. Erlang
6. NodeJS

There are unofficial community-maintained libraries for other languages too.

Check [https://prometheus.io/docs/instrumenting/clientlibs/](https://prometheus.io/docs/instrumenting/clientlibs/) for
the full list. Please follow the corresponding language guide on how to make custom metrics available.

### 2. Expose an endpoint in your service like `/metrics`

Define a specific endpoint like `/metrics` in your service and connect the Prometheus client library's URL handler to
it. This endpoint will be called by the inbuilt monitoring stack (Prometheus) periodically to scrape the metrics
exposed in step 1.

### 3. Declare the metrics endpoint in `ops.json`

`ops.json` lets you define configuration specific to your service. Just declare a `metrics` key like below in
`ops.json`:

```json theme={null}
{
    "metrics": {
        "endpoint": "/metrics",
        "interval": 15,
        "port": 9090
    }
}
```

`interval` is how often Prometheus will hit this endpoint, in seconds. Please don't keep it too low (like 1 second).
Your service might get too busy handling these requests instead of handling real user requests.

LocalOps will then make these metrics available automatically in the Grafana dashboard via Prometheus, just like any
other metric.

## Language-specific guides

End-to-end examples (dependency install, sample code, `ops.json`, and recommended community Grafana dashboard IDs) are
available for each runtime:

* [Java](/environment/services/instrument/java)
* [Python](/environment/services/instrument/python)
* [Node.js](/environment/services/instrument/nodejs)
* [Go](/environment/services/instrument/golang)
* [.NET](/environment/services/instrument/dotnet)
* [Erlang](/environment/services/instrument/erlang)

<Note>
  For Helm charts, the same instrumentation can be included in the charts you generate and publish. If a Prometheus + Grafana stack is installed and configured in any target Kubernetes cluster where your Helm chart is deployed, it will automatically collect and visualize the same metrics in Grafana.
</Note>
