Set Up Metrics

Metrics allow you to send, view and query counters, gauges and measurements from your Sentry-configured apps to track application health and drill down into related traces, logs, and errors.

With Sentry Metrics, you can send counters, gauges, and distributions from your applications to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.

Metrics for Go are supported in Sentry Go SDK version 0.42.0 and above.

Copied
go get github.com/getsentry/sentry-go

Once the SDK initialized, you can record metrics using a meter.

A meter supports three types of measurements: Count, Gauge, and Distribution. Each is suited to a different kind of data.

Copied
import (
	"context"
	"github.com/getsentry/sentry-go"
)

meter := sentry.NewMeter(context.Background())

Use Count to track an incrementing value, such as the number of times a button was clicked or a function was called.

Copied
meter.Count("button_click", 1)

Use Gauge to track a value that can go up and down, such as the current memory usage or the number of items in a queue.

Copied
meter.Gauge("queue_depth", 42.0)

Use Distribution to track the distribution of a value, such as the response time of a request.

Copied
meter.Distribution("response_time", 187.5)

You can pass additional attributes to any of the metric methods. Attributes allow you to filter and group metrics.

Copied
import (
	"github.com/getsentry/sentry-go/attribute"
)

meter.Distribution("page_load", 1.0,
	sentry.WithUnit(sentry.UnitMillisecond),
	sentry.WithAttributes(
		attribute.String("browser", "Firefox"),
	),
)

For Gauge and Distribution metrics, you can specify a unit using the WithUnit option. This helps Sentry display the metric value in a human-readable format.

Copied
meter.Distribution("response_time", 187.5, sentry.WithUnit(sentry.UnitMillisecond))
meter.Gauge("memory_usage", 1024.0, sentry.WithUnit(sentry.UnitByte))

Create a meter using sentry.NewMeter(ctx) or Use WithCtx to link metrics to the current trace.

Copied
func handler(w http.ResponseWriter, r *http.Request) {
	// Use r.Context() and `WithCtx` to link the metric to the current request's span.
	// The sentryhttp middleware adds the span to the request context.
	meter.WithCtx(r.Context()).Count("page_views", 1,
		sentry.WithAttributes(
			attribute.String("path", r.URL.Path),
			attribute.String("method", r.Method),
		),
	)

	w.WriteHeader(http.StatusOK)
}

Use the BeforeSendMetric callback to filter or modify metrics before they're sent to Sentry. This is useful for:

  • Removing sensitive data from metric attributes
  • Dropping metrics you don't want to send
  • Adding or modifying attributes

The callback receives a metric object and must return either a modified metric or nil to drop it.

Copied
err := sentry.Init(sentry.ClientOptions{
  Dsn: "___PUBLIC_DSN___",
  BeforeSendMetric: func(metric *sentry.Metric) *sentry.Metric {
    // Filter metrics based on metric type and value
    switch metric.Type {
    case sentry.MetricTypeCounter:
      if v, ok := metric.Value.Int64(); ok && v < 5 {
        return nil // drop low-value counters
      }
    case sentry.MetricTypeGauge:
      if v, ok := metric.Value.Float64(); ok && v < 10.0 {
        return nil // drop low gauge readings
      }
    case sentry.MetricTypeDistribution:
      // keep all distributions
    }

    // Alternative: handle value types directly
    if v, ok := metric.Value.Int64(); ok && v > 1 {
      // handle all int64 values (counters)
    }
    // Drop metrics with specific attributes
    if _, ok := metric.Attributes["dropmetric"]; ok {
      return nil
    }

    return metric
  },
})

Metrics are enabled by default. If you want to disable metrics collection entirely, you can do so by setting DisableMetrics to true:

Copied
err := sentry.Init(sentry.ClientOptions{
	Dsn:           "___PUBLIC_DSN___",
	DisableMetrics: true,
})

By default the SDK will attach the following attributes to a metric:

  • 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.

The SDK will attach the following:

  • server.address: The address of the server that sent the metric. Equivalent to server_name that gets attached to Sentry errors.
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").