Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add a Micrometer metrics integration with opt-in Spring Boot support ([#6116](https://github.com/getsentry/sentry-java/pull/6116))

## 8.56.0

### Fixes
Expand Down
28 changes: 28 additions & 0 deletions sentry-micrometer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ SentryMeterRegistry sentryRegistry = new SentryMeterRegistry();
Metrics.addRegistry(sentryRegistry);
```

### Spring Boot

Spring Boot 2, 3, and 4 applications can use auto-configuration by adding `sentry-micrometer`
alongside the matching Sentry Spring Boot starter. The starter does not install this module
transitively.

```kotlin
dependencies {
implementation("io.sentry:sentry-spring-boot-starter:<version>")
implementation("io.sentry:sentry-micrometer:<version>")
}
```

Enable the integration explicitly and optionally configure passive polling:

```properties
sentry.micrometer.enabled=true
sentry.micrometer.poll-interval-millis=60000
```

Auto-configuration requires Sentry to be initialized and backs off when the application provides
its own `SentryMeterRegistry` bean. Spring Boot adds the registry to its primary composite registry,
applies compatible `MeterRegistryCustomizer` beans, and closes it with the application context.
Supported metrics registered automatically by Spring Boot Actuator鈥攊ncluding HTTP server, JVM,
process, and logging metrics鈥攁re forwarded through the same registry. Set the polling interval to
zero to keep immediate forwarding enabled without a polling worker; passive auto-generated meters
then remain registered but are not sent.

## Metric mappings

Active meters are forwarded when they are recorded:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
implementation(Config.Libs.kotlinReflect)
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
implementation(projects.sentrySpringBoot4Starter)
implementation(projects.sentryMicrometer)
implementation(projects.sentryLogback)
implementation(projects.sentryGraphql22)
implementation(projects.sentryQuartz)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ sentry.enable-spotlight=true
sentry.enablePrettySerializationOutput=false
sentry.in-app-includes="io.sentry.samples"
sentry.logs.enabled=true
sentry.micrometer.enabled=true
sentry.micrometer.poll-interval-millis=1000
sentry.profile-session-sample-rate=1.0
sentry.profiling-traces-dir-path=tmp/sentry/profiling-traces
sentry.profile-lifecycle=TRACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.sentry.systemtest
import io.sentry.systemtest.util.TestHelper
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import org.junit.Before

class MetricsSystemTest {
Expand All @@ -27,6 +28,43 @@ class MetricsSystemTest {
}
}

@Test
fun `Spring Boot generated HTTP metric is forwarded through Micrometer`() {
val restClient = testHelper.restClient
assertTrue(restClient.getActuatorHealth()?.contains("\"status\":\"UP\"") == true)
assertEquals(200, restClient.lastKnownStatusCode)

testHelper.ensureMetricsReceived { event, header ->
event.items.any { metric ->
metric.name == "http_server_requests" &&
metric.type == "distribution" &&
metric.unit == "millisecond" &&
metric.attributes?.get("sentry.origin")?.value == "auto.metrics.micrometer"
} &&
header.sdkVersion?.integrationSet?.contains("Micrometer") == true &&
header.sdkVersion?.packageSet?.any {
it.name == "maven:io.sentry:sentry-micrometer"
} == true
}
}

@Test
fun `Spring Boot generated process metric is forwarded through Micrometer polling`() {
testHelper.ensureMetricsReceived { event, header ->
event.items.any { metric ->
metric.name == "process_uptime" &&
metric.type == "gauge" &&
metric.unit == "millisecond" &&
metric.value > 0.0 &&
metric.attributes?.get("sentry.origin")?.value == "auto.metrics.micrometer"
} &&
header.sdkVersion?.integrationSet?.contains("Micrometer") == true &&
header.sdkVersion?.packageSet?.any {
it.name == "maven:io.sentry:sentry-micrometer"
} == true
}
}

@Test
fun `gauge metric`() {
val restClient = testHelper.restClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
implementation(Config.Libs.kotlinReflect)
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
implementation(projects.sentrySpringBootStarterJakarta)
implementation(projects.sentryMicrometer)
implementation(projects.sentryLogback)
implementation(projects.sentryGraphql22)
implementation(projects.sentryQuartz)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ sentry.enable-spotlight=false
sentry.enablePrettySerializationOutput=false
sentry.in-app-includes="io.sentry.samples"
sentry.logs.enabled=true
sentry.micrometer.enabled=true
sentry.micrometer.poll-interval-millis=1000
sentry.profile-session-sample-rate=1.0
sentry.profiling-traces-dir-path=tmp/sentry/profiling-traces
sentry.profile-lifecycle=TRACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.sentry.systemtest
import io.sentry.systemtest.util.TestHelper
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import org.junit.Before

class MetricsSystemTest {
Expand All @@ -27,6 +28,43 @@ class MetricsSystemTest {
}
}

@Test
fun `Spring Boot generated HTTP metric is forwarded through Micrometer`() {
val restClient = testHelper.restClient
assertTrue(restClient.getActuatorHealth()?.contains("\"status\":\"UP\"") == true)
assertEquals(200, restClient.lastKnownStatusCode)

testHelper.ensureMetricsReceived { event, header ->
event.items.any { metric ->
metric.name == "http_server_requests" &&
metric.type == "distribution" &&
metric.unit == "millisecond" &&
metric.attributes?.get("sentry.origin")?.value == "auto.metrics.micrometer"
} &&
header.sdkVersion?.integrationSet?.contains("Micrometer") == true &&
header.sdkVersion?.packageSet?.any {
it.name == "maven:io.sentry:sentry-micrometer"
} == true
}
}

@Test
fun `Spring Boot generated process metric is forwarded through Micrometer polling`() {
testHelper.ensureMetricsReceived { event, header ->
event.items.any { metric ->
metric.name == "process_uptime" &&
metric.type == "gauge" &&
metric.unit == "millisecond" &&
metric.value > 0.0 &&
metric.attributes?.get("sentry.origin")?.value == "auto.metrics.micrometer"
} &&
header.sdkVersion?.integrationSet?.contains("Micrometer") == true &&
header.sdkVersion?.packageSet?.any {
it.name == "maven:io.sentry:sentry-micrometer"
} == true
}
}

@Test
fun `gauge metric`() {
val restClient = testHelper.restClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
implementation(Config.Libs.kotlinReflect)
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
implementation(projects.sentrySpringBootStarter)
implementation(projects.sentryMicrometer)
implementation(projects.sentryLogback)
if (includeGraphql) {
implementation(projects.sentryGraphql)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sentry.samples.spring.boot;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.sentry.Sentry;
import io.sentry.metrics.MetricsUnit;
import org.slf4j.Logger;
Expand All @@ -14,6 +16,14 @@
public class MetricController {
private static final Logger LOGGER = LoggerFactory.getLogger(MetricController.class);

private final MeterRegistry meterRegistry;
private final SimpleMeterRegistry simpleMeterRegistry;

public MetricController(MeterRegistry meterRegistry, SimpleMeterRegistry simpleMeterRegistry) {
this.meterRegistry = meterRegistry;
this.simpleMeterRegistry = simpleMeterRegistry;
}

@GetMapping("count")
String count() {
Sentry.setAttribute("user.type", "admin");
Expand All @@ -22,6 +32,14 @@ String count() {
return "count metric increased";
}

@GetMapping("micrometer")
String micrometer() {
meterRegistry.counter("micrometer.counter", "source", "spring").increment();
double simpleRegistryCount =
simpleMeterRegistry.get("micrometer.counter").tag("source", "spring").counter().count();
return "micrometer metric increased: " + simpleRegistryCount;
}

@GetMapping("gauge/{value}")
String gauge(@PathVariable("value") Long value) {
Sentry.metrics().gauge("memory.free", value.doubleValue(), MetricsUnit.Information.BYTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.sentry.quartz.SentryJobListener.SENTRY_SLUG_KEY;

import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.sentry.samples.spring.boot.quartz.SampleJob;
import java.util.Collections;
import org.quartz.JobDetail;
Expand Down Expand Up @@ -36,6 +37,11 @@ WebClient webClient(WebClient.Builder builder) {
return builder.build();
}

@Bean
SimpleMeterRegistry simpleMeterRegistry() {
return new SimpleMeterRegistry();
}

@Bean
public JobDetailFactoryBean jobDetail() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ sentry.graphql.ignored-error-types=SOME_ERROR,ANOTHER_ERROR
sentry.enable-backpressure-handling=true
sentry.enable-spotlight=true
sentry.logs.enabled=true
sentry.micrometer.enabled=true
sentry.micrometer.poll-interval-millis=1000
sentry.in-app-includes="io.sentry.samples"
sentry.profile-session-sample-rate=1.0
sentry.profiling-traces-dir-path=tmp/sentry/profiling-traces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.sentry.systemtest
import io.sentry.systemtest.util.TestHelper
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.junit.Before

class MetricsSystemTest {
Expand All @@ -27,6 +29,66 @@ class MetricsSystemTest {
}
}

@Test
fun `Micrometer metric is forwarded to Sentry and another registry`() {
val restClient = testHelper.restClient
val response = assertNotNull(restClient.getMicrometerMetric())
val responsePrefix = "micrometer metric increased: "
assertTrue(response.startsWith(responsePrefix))
assertTrue(response.removePrefix(responsePrefix).toDouble() > 0.0)
assertEquals(200, restClient.lastKnownStatusCode)

testHelper.ensureMetricsReceived { event, header ->
testHelper.doesContainMetric(event, "micrometer_counter", "counter", 1.0) &&
testHelper.doesMetricHaveAttribute(
event,
"micrometer_counter",
"source",
"spring",
) &&
header.sdkVersion?.integrationSet?.contains("Micrometer") == true &&
header.sdkVersion?.packageSet?.any {
it.name == "maven:io.sentry:sentry-micrometer"
} == true
}
}

@Test
fun `Spring Boot generated HTTP metric is forwarded through Micrometer`() {
val restClient = testHelper.restClient
assertTrue(restClient.getActuatorHealth()?.contains("\"status\":\"UP\"") == true)
assertEquals(200, restClient.lastKnownStatusCode)

testHelper.ensureMetricsReceived { event, _ ->
event.items.any { metric ->
metric.name == "http_server_requests" &&
metric.type == "distribution" &&
metric.unit == "millisecond" &&
metric.attributes?.get("method")?.value == "GET" &&
metric.attributes?.get("status")?.value == "200" &&
metric.attributes?.get("uri")?.value == "/actuator/health" &&
metric.attributes?.get("sentry.origin")?.value == "auto.metrics.micrometer"
}
}
}

@Test
fun `Spring Boot generated process metric is forwarded through Micrometer polling`() {
testHelper.ensureMetricsReceived { event, header ->
event.items.any { metric ->
metric.name == "process_uptime" &&
metric.type == "gauge" &&
metric.unit == "millisecond" &&
metric.value > 0.0 &&
metric.attributes?.get("sentry.origin")?.value == "auto.metrics.micrometer"
} &&
header.sdkVersion?.integrationSet?.contains("Micrometer") == true &&
header.sdkVersion?.packageSet?.any {
it.name == "maven:io.sentry:sentry-micrometer"
} == true
}
}

@Test
fun `gauge metric`() {
val restClient = testHelper.restClient
Expand Down
10 changes: 10 additions & 0 deletions sentry-spring-boot-4/api/sentry-spring-boot-4.api
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class io/sentry/spring/boot4/SentryProperties : io/sentry/SentryOptions {
public fun getExceptionResolverOrder ()I
public fun getGraphql ()Lio/sentry/spring/boot4/SentryProperties$Graphql;
public fun getLogging ()Lio/sentry/spring/boot4/SentryProperties$Logging;
public fun getMicrometer ()Lio/sentry/spring/boot4/SentryProperties$Micrometer;
public fun getReactive ()Lio/sentry/spring/boot4/SentryProperties$Reactive;
public fun getUserFilterOrder ()Ljava/lang/Integer;
public fun isEnableAotCompatibility ()Z
Expand All @@ -43,6 +44,7 @@ public class io/sentry/spring/boot4/SentryProperties : io/sentry/SentryOptions {
public fun setGraphql (Lio/sentry/spring/boot4/SentryProperties$Graphql;)V
public fun setKeepTransactionsOpenForAsyncResponses (Z)V
public fun setLogging (Lio/sentry/spring/boot4/SentryProperties$Logging;)V
public fun setMicrometer (Lio/sentry/spring/boot4/SentryProperties$Micrometer;)V
public fun setReactive (Lio/sentry/spring/boot4/SentryProperties$Reactive;)V
public fun setUseGitCommitIdAsRelease (Z)V
public fun setUserFilterOrder (Ljava/lang/Integer;)V
Expand All @@ -68,6 +70,14 @@ public class io/sentry/spring/boot4/SentryProperties$Logging {
public fun setMinimumLevel (Lorg/slf4j/event/Level;)V
}

public class io/sentry/spring/boot4/SentryProperties$Micrometer {
public fun <init> ()V
public fun getPollIntervalMillis ()J
public fun isEnabled ()Z
public fun setEnabled (Z)V
public fun setPollIntervalMillis (J)V
}

public class io/sentry/spring/boot4/SentryProperties$Reactive {
public fun <init> ()V
public fun isThreadLocalAccessorEnabled ()Z
Expand Down
4 changes: 4 additions & 0 deletions sentry-spring-boot-4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ tasks.withType<KotlinCompile>().configureEach {
dependencies {
api(projects.sentry)
api(projects.sentrySpring7)
compileOnly(projects.sentryMicrometer)
compileOnly("io.micrometer:micrometer-core")
compileOnly(projects.sentryLogback)
compileOnly(projects.sentryApacheHttpClient5)
compileOnly(platform(SpringBootPlugin.BOM_COORDINATES))
Expand Down Expand Up @@ -64,6 +66,7 @@ dependencies {
errorprone(libs.nullaway)

// tests
testImplementation(projects.sentryMicrometer)
testImplementation(projects.sentryLogback)
testImplementation(projects.sentryApacheHttpClient5)
testImplementation(projects.sentryGraphql)
Expand Down Expand Up @@ -95,6 +98,7 @@ dependencies {
*/
// testImplementation(libs.springboot4.otel)
testImplementation(libs.springboot4.starter)
testImplementation(libs.springboot4.starter.actuator)
testImplementation(libs.springboot4.starter.aspectj)
testImplementation(libs.springboot4.starter.graphql)
testImplementation(libs.spring.kafka4)
Expand Down
Loading
Loading