---
title: "Metrics"
description: "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."
url: https://docs.sentry.io/platforms/java/guides/spring-boot/metrics/
---

# Set Up Metrics | Sentry for Spring Boot

With Sentry Metrics, you can send counters, gauges, distributions, and sets 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.

## [Requirements](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#requirements)

Metrics are supported in Sentry Java SDK version `8.30.0` and above.

## [Usage](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#usage)

Once the SDK is initialized, you can send metrics using the `Sentry.metrics()` APIs.

The `metrics` namespace exposes methods that you can use to capture different types of metric information: `count`, `gauge` and `distribution`.

### [Counter](https://docs.sentry.io/platforms/java/guides/spring-boot/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.

```java
import io.sentry.Sentry;

Sentry.metrics().count("button_click", 1.0);
```

### [Gauge](https://docs.sentry.io/platforms/java/guides/spring-boot/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.

```java
import io.sentry.Sentry;

Sentry.metrics().gauge("queue_depth", 42.0);
```

### [Distribution](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#distribution)

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

```java
import io.sentry.Sentry;

Sentry.metrics().distribution("response_time", 187.5);
```

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

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

```java
import java.util.Map;

import io.sentry.Sentry;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
import io.sentry.metrics.MetricsUnit;
import io.sentry.metrics.SentryMetricsParameters;

Sentry.metrics().distribution(
  "page_load",
  1.0,
  MetricsUnit.Duration.MILLISECOND,
  SentryMetricsParameters.create(
    Map.of("browser", "Firefox")
  )
);

Sentry.metrics().distribution(
  "response_time",
  1.0,
  MetricsUnit.Duration.MILLISECOND,
  SentryMetricsParameters.create(
    SentryAttributes.of(
      SentryAttribute.stringAttribute("browser", "Firefox")
    )
  )
);
```

### [Scope Attributes](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#scope-attributes)

You can set attributes on the scope that will be automatically included in all metrics captured within that scope. This is useful for attaching contextual information that should appear on every metric.

```java
import io.sentry.Sentry;
import io.sentry.ScopeType;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;

// Set an attribute on the global scope so it applies to all metrics
Sentry.configureScope(ScopeType.GLOBAL, scope -> {
    scope.setAttribute("region", "us-east-1");
});

// Set a single attribute with automatic type inference
Sentry.setAttribute("request.id", "abc-123");

// Or use a factory method to set the type explicitly
Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150));

// Set multiple attributes at once
Sentry.setAttributes(SentryAttributes.of(
    SentryAttribute.stringAttribute("service", "checkout"),
    SentryAttribute.booleanAttribute("canary", false)
));

// All subsequent metrics will include these attributes
Sentry.metrics().count("order_placed", 1.0);

// Remove an attribute when it's no longer relevant
Sentry.removeAttribute("canary");
```

Attributes passed directly to a metric call override scope attributes with the same key.

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

For `gauge` and `distribution` metrics, you can specify a unit using the `unit` option. This helps Sentry display the metric value in a human-readable format. `MetricsUnit` offers constants for units supported by Sentry. Sending in custom units is not supported.

```java
import io.sentry.Sentry;
import io.sentry.metrics.MetricsUnit;

Sentry.metrics().distribution(
  "response_time",
  187.5,
  MetricsUnit.Duration.MILLISECOND
);

Sentry.metrics().gauge(
  "memory_usage",
  1024.0,
  MetricsUnit.Information.BYTE
);
```

## [Options](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#options)

#### [beforeSendMetric](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#beforesendmetric)

To filter metrics, or update them before they are sent to Sentry, you can expose a bean implementing the metrics `beforeSend` callback:

```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.sentry.SentryLogEventAttributeValue;
import io.sentry.SentryOptions;
import io.sentry.SentryAttributeType;
import io.sentry.SentryOptions.Metrics.BeforeSendMetricCallback;

@Configuration
public class SentryMetricsConfiguration {
  @Bean
  public BeforeSendMetricCallback beforeSendMetric() {
    return (metric, hint) -> {
      // Drop metrics with specific attributes
      if (metric.getAttributes().containsKey("dropmetric")) {
        return null;
      }

      // Modify metric attributes
      metric.setAttribute("processed", new SentryLogEventAttributeValue(SentryAttributeType.BOOLEAN, true));
      return metric;
    };
  }
}
```

The `beforeSend` function receives a metric object, and should return the metric object if you want it to be sent to Sentry, or `null` if you want to discard it.

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

If you want to disable metrics collection entirely, you can do so by disabling the `metrics.enabled` flag:

```properties
sentry.metrics.enabled=false
```

## [Default Attributes](https://docs.sentry.io/platforms/java/guides/spring-boot/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`.

### [User Attributes](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#user-attributes)

If user information is available in the current scope, the following attributes are added to the log:

* `user.id`: The user ID.
* `user.name`: The username.
* `user.email`: The email address.

### [Server Attributes](https://docs.sentry.io/platforms/java/guides/spring-boot/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.

### [Scope Attributes](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#scope-attributes)

Any attributes set on the current scope via `Sentry.setAttribute()` or `Sentry.setAttributes()` are automatically included on all metrics. See [Usage](https://docs.sentry.io/platforms/java/guides/spring-boot/metrics.md#usage) above for details.
