View Source Sentry.Metrics (Sentry v14.1.0)

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, 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")

Units

The :unit option takes one of the units Sentry recognizes, so it can format and convert values:

  • Duration - "nanosecond", "microsecond", "millisecond", "second", "minute", "hour", "day", "week"
  • Information - "bit", "byte", and the decimal and binary multiples of "byte": "kilobyte", "kibibyte", "megabyte", "mebibyte", "gigabyte", "gibibyte", "terabyte", "tebibyte", "petabyte", "pebibyte", "exabyte", "exbibyte"
  • Fraction - "ratio", "percent"
  • "none" - for plain counts and other values without a unit

Leave out :unit or pass "none" when you count things such as requests or clicks. Describe what is counted in the metric name instead.

Automatically Collected Metrics

Available since 14.0.0.

The SDK can also report BEAM runtime health on its own, without any calls to the functions above. This is opt-in:

config :sentry,
  metrics: [
    runtime: [enabled: true]
  ]

Once enabled, these gauges are reported:

  • process.runtime.elixir.memory.used - memory allocated by the VM, in bytes, reported once per process.runtime.elixir.memory.type attribute value: processes, atom, binary, code, ets and other. other is everything else the VM has allocated. Summing across the types gives the VM total.
  • process.runtime.elixir.run_queue.length - how many processes are waiting to run, reported once per process.runtime.elixir.run_queue.type attribute value: cpu for the normal and dirty CPU scheduler run queues and io for the dirty IO run queue. Summing across the types gives the total run queue length.
  • process.runtime.elixir.process.*, process.runtime.elixir.atom.* and process.runtime.elixir.port.* - a count, the hard VM limit, and the utilization ratio between them. The limit and utilization gauges require telemetry_poller 1.3.0 or later; older versions report only count.
  • process.runtime.elixir.scheduler.utilization - the busy fraction of scheduler time, as a ratio between 0.0 and 1.0. Unlike the others this is a delta between two samples, so the first collection only takes a baseline and the first value arrives one collection later. The SDK polls for it itself, at the period configured for telemetry_poller.

Collection Frequency

The SDK does not collect these itself. It listens to the events that telemetry_poller already emits, so how often they are reported is how often that library polls. Its default poller runs every 5 seconds, which is more often than most applications need for runtime health and costs metric volume, so consider slowing it down:

config :telemetry_poller, default: [period: 30_000]

Add the dependency if you do not have it already. Both 0.5 and 1.x work; 1.3.0 or later is needed for the limit and utilization gauges:

{:telemetry_poller, "~> 1.3"}

Runtime Version Attributes

Setting version_attributes: true adds these attributes to every reported measurement, so metrics can be grouped by runtime version:

  • process.runtime.name - always "elixir".
  • process.runtime.version - the Elixir version, as returned by System.version/0.
  • process.runtime.engine.name - always "BEAM".
  • process.runtime.engine.version - the OTP release, such as "27".

It is off by default, because those values change only on upgrade and attaching them to every point starts a fresh series for each metric on every rolling deploy.

Configuration

Metrics can be filtered 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, one of those listed under Units. Counters usually have none. 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, attributes: %{method: "GET"})
Sentry.Metrics.count("http.response.body.size", 2048, unit: "byte")
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, one of those listed under Units, such as "millisecond" or "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, one of those listed under Units, such as "byte" or "ratio". 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"})