---
title: "Application Metrics"
description: "Metrics allow you to send, view and query counters, gauges and measurements sent from your applications within Sentry using the Elixir SDK."
url: https://docs.sentry.io/platforms/elixir/metrics/
---

# Set Up Metrics for Elixir

Sentry metrics help you pinpoint and solve issues that impact user experience and app performance by measuring the data points that are important to you. You can track things like processing time, event size, user signups, and conversion rates, then correlate them back to tracing data in order to get deeper insights and solve issues faster.

Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.

## [Requirements](https://docs.sentry.io/platforms/elixir/metrics.md#requirements)

Metrics for Elixir are supported in Sentry Elixir SDK version `13.0.0` and above.

Add `sentry` to your `mix.exs` dependencies:

```elixir
defp deps do
  [
    {:sentry, "~> 14.0"}
  ]
end
```

## [Usage](https://docs.sentry.io/platforms/elixir/metrics.md#usage)

Metrics are always on. Once you initialize the SDK, you can send metrics using the `Sentry.Metrics` module.

The module exposes three functions that you can use to capture different types of metric information: `count`, `gauge`, and `distribution`.

### [Emit a Counter](https://docs.sentry.io/platforms/elixir/metrics.md#emit-a-counter)

Counters are one of the more basic types of metrics and can be used to count certain event occurrences.

To emit a counter, do the following:

```elixir
# Record five total button clicks
Sentry.Metrics.count(
  "button_click",
  5,
  attributes: %{browser: "Firefox", app_version: "1.0.0"}
)
```

### [Emit a Distribution](https://docs.sentry.io/platforms/elixir/metrics.md#emit-a-distribution)

Distributions help you get the most insights from your data by allowing you to obtain aggregations such as `p90`, `min`, `max`, and `avg`.

To emit a distribution, do the following:

```elixir
# Add '15.0' to a distribution used for tracking the loading times per page.
Sentry.Metrics.distribution(
  "page_load",
  15.0,
  unit: "millisecond",
  attributes: %{page: "/home"}
)
```

### [Emit a Gauge](https://docs.sentry.io/platforms/elixir/metrics.md#emit-a-gauge)

Gauges let you obtain aggregates like `min`, `max`, `avg`, `sum`, and `count`. They can be represented in a more space-efficient way than distributions, but they can't be used to get percentiles. If percentiles aren't important to you, we recommend using gauges.

To emit a gauge, do the following:

```elixir
# Add '15.0' to a gauge used for tracking the loading times for a page.
Sentry.Metrics.gauge(
  "page_load",
  15.0,
  unit: "millisecond",
  attributes: %{page: "/home"}
)
```

## [Options](https://docs.sentry.io/platforms/elixir/metrics.md#options)

#### [before\_send\_metric](https://docs.sentry.io/platforms/elixir/metrics.md#before_send_metric)

To filter metrics, or update them before they are sent to Sentry, you can use the `before_send_metric` option. If the callback returns `nil`, the metric is not emitted. Attributes can also be updated in the callback function.

```elixir
config :sentry,
  before_send_metric: fn metric ->
    # filter metric
    if metric.name == "removed-metric" do
      nil
    else
      # add attributes
      attributes = Map.put(metric.attributes, "extra", "foo")

      # remove attributes
      attributes = Map.delete(attributes, "browser")

      %{metric | attributes: attributes}
    end
  end
```

You can also pass a `{module, function}` tuple:

```elixir
config :sentry,
  before_send_metric: {MyApp.SentryFilters, :filter_metric}
```

The `before_send_metric` callback receives a `Sentry.Metric` struct, and should return a `Sentry.Metric` struct if you want it to be sent to Sentry, or `nil` if you want to discard it.

If the callback raises an exception, throws, or exits, the SDK logs the failure at the `:error` level and drops that metric. Other metrics in the same batch are still processed.

The `Sentry.Metric` struct has the following fields:

* `name`: (`String.t()`) The name of the metric.
* `type`: (`:counter | :gauge | :distribution`) The type of metric.
* `value`: (`number()`) The numeric value of the metric.
* `unit`: (`String.t() | nil`) The unit of measurement for the metric value.
* `attributes`: (`map()`) Additional attributes to be sent with the metric.
* `timestamp`: (`float()`) Timestamp in seconds indicating when the metric was recorded.
* `trace_id`: (`String.t() | nil`) The trace ID of the trace this metric belongs to.
* `span_id`: (`String.t() | nil`) The span ID of the span that was active when the metric was emitted.

Metrics are always on, so `before_send_metric` is also how you stop sending them. Return `nil` for every metric to drop all of them:

```elixir
config :sentry,
  before_send_metric: fn _metric -> nil end
```

## [Default Attributes](https://docs.sentry.io/platforms/elixir/metrics.md#default-attributes)

The Elixir SDK automatically sets several default attributes on all metrics to provide context and improve debugging:

### [Core Attributes](https://docs.sentry.io/platforms/elixir/metrics.md#core-attributes)

* `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
* `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
* `sdk.name`: The name of the SDK that sent the metric. This is sent from the SDK as `sentry.sdk.name`.
* `sdk.version`: The version of the SDK that sent the metric. This is sent from the SDK as `sentry.sdk.version`.

### [Server Attributes](https://docs.sentry.io/platforms/elixir/metrics.md#server-attributes)

* `server.address`: The value of the `server_name` option. Unlike errors, metrics don't fall back to the host name, so this attribute is only sent when you set `server_name`.
