Skip to content

Commit f5e1b97

Browse files
authored
Add metrics API example to spring boot demo app (#3253)
* Add metrics timing example to spring boot demo app * Extend metrics sample code to other backend projects
1 parent 8fba9ca commit f5e1b97

File tree

9 files changed

+54
-14
lines changed

9 files changed

+54
-14
lines changed

sentry-samples/sentry-samples-spring-boot-jakarta/src/main/java/io/sentry/samples/spring/boot/jakarta/PersonService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ Person create(Person person) {
3131
span.setMeasurement("create_count", createCount);
3232
}
3333

34-
jdbcTemplate.update(
35-
"insert into person (firstName, lastName) values (?, ?)",
36-
person.getFirstName(),
37-
person.getLastName());
34+
Sentry.metrics()
35+
.timing(
36+
"person.insert",
37+
() -> {
38+
jdbcTemplate.update(
39+
"insert into person (firstName, lastName) values (?, ?)",
40+
person.getFirstName(),
41+
person.getLastName());
42+
});
43+
3844
return person;
3945
}
4046
}

sentry-samples/sentry-samples-spring-boot-jakarta/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ sentry.debug=true
1515
sentry.graphql.ignored-error-types=SOME_ERROR,ANOTHER_ERROR
1616
sentry.enable-backpressure-handling=true
1717
sentry.enable-spotlight=true
18+
sentry.enable-metrics=true
1819
sentry.enablePrettySerializationOutput=false
1920
in-app-includes="io.sentry.samples"
2021

sentry-samples/sentry-samples-spring-boot-webflux-jakarta/src/main/java/io/sentry/samples/spring/boot/jakarta/PersonService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ public class PersonService {
1212
Mono<Person> create(Person person) {
1313
return Mono.delay(Duration.ofMillis(100))
1414
.publishOn(Schedulers.boundedElastic())
15-
.doOnNext(__ -> Sentry.captureMessage("Creating person"))
15+
.doOnNext(
16+
__ -> {
17+
Sentry.metrics()
18+
.timing(
19+
"person.insert",
20+
() -> {
21+
Sentry.captureMessage("Creating person");
22+
});
23+
})
1624
.map(__ -> person);
1725
}
1826
}

sentry-samples/sentry-samples-spring-boot-webflux-jakarta/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ sentry.logging.minimum-breadcrumb-level=debug
1010
sentry.reactive.thread-local-accessor-enabled=true
1111
sentry.enable-tracing=true
1212
sentry.enable-backpressure-handling=true
13+
sentry.enable-spotlight=true
14+
sentry.enable-metrics=true

sentry-samples/sentry-samples-spring-boot-webflux/src/main/java/io/sentry/samples/spring/boot/PersonService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ public class PersonService {
1212
Mono<Person> create(Person person) {
1313
return Mono.delay(Duration.ofMillis(100))
1414
.publishOn(Schedulers.boundedElastic())
15-
.doOnNext(__ -> Sentry.captureMessage("Creating person"))
15+
.doOnNext(
16+
__ ->
17+
Sentry.metrics()
18+
.timing(
19+
"person.insert",
20+
() -> {
21+
Sentry.captureMessage("Creating person");
22+
}))
1623
.map(__ -> person);
1724
}
1825
}

sentry-samples/sentry-samples-spring-boot-webflux/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ spring.graphql.graphiql.enabled=true
1212
spring.graphql.websocket.path=/graphql
1313
spring.graphql.schema.printer.enabled=true
1414
sentry.enable-backpressure-handling=true
15+
sentry.enable-spotlight=true
16+
sentry.enable-metrics=true

sentry-samples/sentry-samples-spring-boot/src/main/java/io/sentry/samples/spring/boot/PersonService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ Person create(Person person) {
3131
span.setMeasurement("create_count", createCount);
3232
}
3333

34-
jdbcTemplate.update(
35-
"insert into person (firstName, lastName) values (?, ?)",
36-
person.getFirstName(),
37-
person.getLastName());
34+
Sentry.metrics()
35+
.timing(
36+
"person.insert",
37+
() -> {
38+
jdbcTemplate.update(
39+
"insert into person (firstName, lastName) values (?, ?)",
40+
person.getFirstName(),
41+
person.getLastName());
42+
});
3843
return person;
3944
}
4045
}

sentry-samples/sentry-samples-spring-boot/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ sentry.ignored-checkins=ignored_monitor_slug_1,ignored_monitor_slug_2
1414
sentry.debug=true
1515
sentry.graphql.ignored-error-types=SOME_ERROR,ANOTHER_ERROR
1616
sentry.enable-backpressure-handling=true
17+
sentry.enable-spotlight=true
18+
sentry.enable-metrics=true
1719
in-app-includes="io.sentry.samples"
1820

1921
# Database configuration

sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/web/PersonService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.samples.spring.jakarta.web;
22

3+
import io.sentry.Sentry;
34
import io.sentry.spring.jakarta.tracing.SentrySpan;
45
import java.util.Map;
56
import org.springframework.stereotype.Service;
@@ -29,10 +30,16 @@ Person find(Long id) {
2930

3031
@SentrySpan
3132
Person create(Person person) {
32-
try {
33-
Thread.sleep(100);
34-
} catch (InterruptedException e) {
35-
}
33+
Sentry.metrics()
34+
.timing(
35+
"person.insert",
36+
() -> {
37+
try {
38+
Thread.sleep(100);
39+
} catch (InterruptedException e) {
40+
// ignored
41+
}
42+
});
3643
return person;
3744
}
3845
}

0 commit comments

Comments
 (0)