# Set Up Metrics | Sentry for Echo

With [Sentry Metrics](https://docs.sentry.io/product/explore/metrics.md), 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.

This feature is currently in open beta. Please reach out on [GitHub](https://github.com/getsentry/sentry/discussions/102275) if you have feedback or questions. Features in beta are still in-progress and may have bugs. We recognize the irony.

## [Prerequisites](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#prerequisites)

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

```bash
go get github.com/getsentry/sentry-go
```

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

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.

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

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

### [Counter](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#counter)

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

```go
meter.Count("button_click", 1)
```

### [Gauge](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#gauge)

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.

```go
meter.Gauge("queue_depth", 42.0)
```

### [Distribution](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#distribution)

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

```go
meter.Distribution("response_time", 187.5)
```

### [Adding Attributes](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#adding-attributes)

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

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

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

### [Specifying Units](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#specifying-units)

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.

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

### [Linking Metrics to Traces](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#linking-metrics-to-traces)

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

```go
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)
}
```

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

### [Filtering and Modifying Metrics](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#filtering-and-modifying-metrics)

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.

```go
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
  },
})
```

### [Disabling Metrics](https://docs.sentry.io/platforms/go/guides/echo/metrics.md#disabling-metrics)

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

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

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

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

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

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.
