View Source Sentry.Metrics (Sentry v13.0.1)

Public API for recording metrics and sending them to Sentry.

Metrics follow the Sentry Metrics Protocol as defined in: https://develop.sentry.dev/sdk/telemetry/metrics/

Metric Types

The SDK supports three metric types:

  • Counter - Tracks a value that always increments
  • Gauge - Tracks a value that can go up or down
  • Distribution - Tracks a distribution of values for statistical aggregation

Usage

# Record a counter
Sentry.Metrics.count("button.clicks", 1)
Sentry.Metrics.count("button.clicks", 5, unit: "click", attributes: %{button_id: "submit"})

# Record a gauge
Sentry.Metrics.gauge("memory.usage", 1024, unit: "megabyte")

# Record a distribution
Sentry.Metrics.distribution("response.time", 42.5, unit: "millisecond")

Configuration

Metrics can be disabled globally via configuration:

config :sentry, enable_metrics: false

You can also filter metrics using the :before_send_metric callback:

config :sentry,
  before_send_metric: fn metric ->
    # Drop metrics from test environments
    if metric.attributes["sentry.environment"] == "test", do: nil, else: metric
  end

Summary

Functions

Records a counter metric.

Records a distribution metric.

Records a gauge metric.

Functions

Link to this function

count(name, value, opts \\ [])

View Source (since 13.0.0)
@spec count(String.t(), number(), keyword()) :: :ok

Records a counter metric.

Counters track values that always increment, such as the number of requests, button clicks, or error counts.

Options

  • :unit - The unit of measurement (e.g., "click", "request"). Optional.
  • :attributes - A map of key-value pairs to attach to the metric. Optional.

Examples

Sentry.Metrics.count("button.clicks", 1)
Sentry.Metrics.count("http.requests", 5, unit: "request", attributes: %{method: "GET"})
Link to this function

distribution(name, value, opts \\ [])

View Source (since 13.0.0)
@spec distribution(String.t(), number(), keyword()) :: :ok

Records a distribution metric.

Distributions track a distribution of values for statistical aggregation, such as response times, payload sizes, or query durations.

Options

  • :unit - The unit of measurement (e.g., "millisecond", "byte"). Optional.
  • :attributes - A map of key-value pairs to attach to the metric. Optional.

Examples

Sentry.Metrics.distribution("response.time", 42.5, unit: "millisecond")
Sentry.Metrics.distribution("payload.size", 2048, unit: "byte", attributes: %{endpoint: "/api"})
Link to this function

gauge(name, value, opts \\ [])

View Source (since 13.0.0)
@spec gauge(String.t(), number(), keyword()) :: :ok

Records a gauge metric.

Gauges track values that can go up or down, such as memory usage, active connections, or queue depth.

Options

  • :unit - The unit of measurement (e.g., "byte", "connection"). Optional.
  • :attributes - A map of key-value pairs to attach to the metric. Optional.

Examples

Sentry.Metrics.gauge("memory.usage", 1024, unit: "megabyte")
Sentry.Metrics.gauge("active.connections", 42, attributes: %{pool: "main"})