Skip to content

Commit 9988b7e

Browse files
committed
Add E2E tests for Metrics
1 parent 8330a3e commit 9988b7e

File tree

38 files changed

+1423
-0
lines changed

38 files changed

+1423
-0
lines changed

sentry-samples/sentry-samples-console-opentelemetry-noagent/src/main/java/io/sentry/samples/console/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public static void main(String[] args) throws InterruptedException {
6363

6464
Sentry.addFeatureFlag("my-feature-flag", true);
6565

66+
captureMetrics();
67+
6668
// Sending exception:
6769
Exception exception = new RuntimeException("Some error!");
6870
Sentry.captureException(exception);
@@ -136,6 +138,12 @@ public static void main(String[] args) throws InterruptedException {
136138
// Sentry.close();
137139
}
138140

141+
private static void captureMetrics() {
142+
Sentry.metrics().count("countMetric");
143+
Sentry.metrics().gauge("gaugeMetric", 5.0);
144+
Sentry.metrics().distribution("distributionMetric", 7.0);
145+
}
146+
139147
private static class SomeEventProcessor implements EventProcessor {
140148
@Override
141149
public SentryEvent process(SentryEvent event, Hint hint) {

sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,11 @@ class ConsoleApplicationSystemTest {
9797
breadcrumb.message?.contains("Processed by") == true
9898
} == true
9999
}
100+
101+
testHelper.ensureMetricsReceived { metricsEvents, sentryEnvelopeHeader ->
102+
testHelper.doesContainMetric(metricsEvents, "countMetric", "counter", 1.0) &&
103+
testHelper.doesContainMetric(metricsEvents, "gaugeMetric", "gauge", 5.0) &&
104+
testHelper.doesContainMetric(metricsEvents, "distributionMetric", "distribution", 7.0)
105+
}
100106
}
101107
}

sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public static void main(String[] args) throws InterruptedException {
128128

129129
Sentry.addFeatureFlag("my-feature-flag", true);
130130

131+
captureMetrics();
132+
131133
// Sending exception:
132134
Exception exception = new RuntimeException("Some error!");
133135
Sentry.captureException(exception);
@@ -187,6 +189,12 @@ public static void main(String[] args) throws InterruptedException {
187189
// Sentry.close();
188190
}
189191

192+
private static void captureMetrics() {
193+
Sentry.metrics().count("countMetric");
194+
Sentry.metrics().gauge("gaugeMetric", 5.0);
195+
Sentry.metrics().distribution("distributionMetric", 7.0);
196+
}
197+
190198
private static class SomeEventProcessor implements EventProcessor {
191199
@Override
192200
public SentryEvent process(SentryEvent event, Hint hint) {

sentry-samples/sentry-samples-console/src/test/kotlin/io/sentry/systemtest/ConsoleApplicationSystemTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,11 @@ class ConsoleApplicationSystemTest {
9393
breadcrumb.message?.contains("Processed by") == true
9494
} == true
9595
}
96+
97+
testHelper.ensureMetricsReceived { metricsEvents, sentryEnvelopeHeader ->
98+
testHelper.doesContainMetric(metricsEvents, "countMetric", "counter", 1.0) &&
99+
testHelper.doesContainMetric(metricsEvents, "gaugeMetric", "gauge", 5.0) &&
100+
testHelper.doesContainMetric(metricsEvents, "distributionMetric", "distribution", 7.0)
101+
}
96102
}
97103
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.sentry.samples.spring7.web;
2+
3+
import io.sentry.Sentry;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/metric/")
13+
public class MetricController {
14+
private static final Logger LOGGER = LoggerFactory.getLogger(MetricController.class);
15+
16+
@GetMapping("count")
17+
String count() {
18+
Sentry.metrics().count("countMetric");
19+
return "count metric increased";
20+
}
21+
22+
@GetMapping("gauge/{count}")
23+
String gauge(@PathVariable Long count) {
24+
Sentry.metrics().gauge("memory.free", count.doubleValue(), "byte");
25+
return "gauge metric tracked";
26+
}
27+
28+
@GetMapping("distribution/{count}")
29+
String distribution(@PathVariable Long count) {
30+
Sentry.metrics().distribution("distributionMetric", count.doubleValue(), "child");
31+
return "distribution metric tracked";
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.sentry.systemtest
2+
3+
import io.sentry.systemtest.util.TestHelper
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import org.junit.Before
7+
8+
class MetricsSystemTest {
9+
lateinit var testHelper: TestHelper
10+
11+
@Before
12+
fun setup() {
13+
testHelper = TestHelper("http://localhost:8080")
14+
testHelper.reset()
15+
}
16+
17+
@Test
18+
fun `count metric`() {
19+
val restClient = testHelper.restClient
20+
assertEquals("count metric increased", restClient.getCountMetric())
21+
assertEquals(200, restClient.lastKnownStatusCode)
22+
23+
Thread.sleep(10000)
24+
25+
testHelper.ensureMetricsReceived { event, header ->
26+
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0)
27+
}
28+
}
29+
30+
@Test
31+
fun `gauge metric`() {
32+
val restClient = testHelper.restClient
33+
assertEquals("gauge metric tracked", restClient.getGaugeMetric(14))
34+
assertEquals(200, restClient.lastKnownStatusCode)
35+
36+
Thread.sleep(10000)
37+
38+
testHelper.ensureMetricsReceived { event, header ->
39+
testHelper.doesContainMetric(event, "memory.free", "gauge", 14.0)
40+
}
41+
}
42+
43+
@Test
44+
fun `distribution metric`() {
45+
val restClient = testHelper.restClient
46+
assertEquals("distribution metric tracked", restClient.getDistributionMetric(23))
47+
assertEquals(200, restClient.lastKnownStatusCode)
48+
49+
Thread.sleep(10000)
50+
51+
testHelper.ensureMetricsReceived { event, header ->
52+
testHelper.doesContainMetric(event, "distributionMetric", "distribution", 23.0)
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.sentry.samples.spring.boot4;
2+
3+
import io.sentry.Sentry;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/metric/")
13+
public class MetricController {
14+
private static final Logger LOGGER = LoggerFactory.getLogger(MetricController.class);
15+
16+
@GetMapping("count")
17+
String count() {
18+
Sentry.metrics().count("countMetric");
19+
return "count metric increased";
20+
}
21+
22+
@GetMapping("gauge/{count}")
23+
String gauge(@PathVariable Long count) {
24+
Sentry.metrics().gauge("memory.free", count.doubleValue(), "byte");
25+
return "gauge metric tracked";
26+
}
27+
28+
@GetMapping("distribution/{count}")
29+
String distribution(@PathVariable Long count) {
30+
Sentry.metrics().distribution("distributionMetric", count.doubleValue(), "child");
31+
return "distribution metric tracked";
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.sentry.systemtest
2+
3+
import io.sentry.systemtest.util.TestHelper
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import org.junit.Before
7+
8+
class MetricsSystemTest {
9+
lateinit var testHelper: TestHelper
10+
11+
@Before
12+
fun setup() {
13+
testHelper = TestHelper("http://localhost:8080")
14+
testHelper.reset()
15+
}
16+
17+
@Test
18+
fun `count metric`() {
19+
val restClient = testHelper.restClient
20+
assertEquals("count metric increased", restClient.getCountMetric())
21+
assertEquals(200, restClient.lastKnownStatusCode)
22+
23+
Thread.sleep(10000)
24+
25+
testHelper.ensureMetricsReceived { event, header ->
26+
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0)
27+
}
28+
}
29+
30+
@Test
31+
fun `gauge metric`() {
32+
val restClient = testHelper.restClient
33+
assertEquals("gauge metric tracked", restClient.getGaugeMetric(14))
34+
assertEquals(200, restClient.lastKnownStatusCode)
35+
36+
Thread.sleep(10000)
37+
38+
testHelper.ensureMetricsReceived { event, header ->
39+
testHelper.doesContainMetric(event, "memory.free", "gauge", 14.0)
40+
}
41+
}
42+
43+
@Test
44+
fun `distribution metric`() {
45+
val restClient = testHelper.restClient
46+
assertEquals("distribution metric tracked", restClient.getDistributionMetric(23))
47+
assertEquals(200, restClient.lastKnownStatusCode)
48+
49+
Thread.sleep(10000)
50+
51+
testHelper.ensureMetricsReceived { event, header ->
52+
testHelper.doesContainMetric(event, "distributionMetric", "distribution", 23.0)
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.sentry.samples.spring.boot4;
2+
3+
import io.sentry.Sentry;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/metric/")
13+
public class MetricController {
14+
private static final Logger LOGGER = LoggerFactory.getLogger(MetricController.class);
15+
16+
@GetMapping("count")
17+
String count() {
18+
Sentry.metrics().count("countMetric");
19+
return "count metric increased";
20+
}
21+
22+
@GetMapping("gauge/{count}")
23+
String gauge(@PathVariable Long count) {
24+
Sentry.metrics().gauge("memory.free", count.doubleValue(), "byte");
25+
return "gauge metric tracked";
26+
}
27+
28+
@GetMapping("distribution/{count}")
29+
String distribution(@PathVariable Long count) {
30+
Sentry.metrics().distribution("distributionMetric", count.doubleValue(), "child");
31+
return "distribution metric tracked";
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.sentry.systemtest
2+
3+
import io.sentry.systemtest.util.TestHelper
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import org.junit.Before
7+
8+
class MetricsSystemTest {
9+
lateinit var testHelper: TestHelper
10+
11+
@Before
12+
fun setup() {
13+
testHelper = TestHelper("http://localhost:8080")
14+
testHelper.reset()
15+
}
16+
17+
@Test
18+
fun `count metric`() {
19+
val restClient = testHelper.restClient
20+
assertEquals("count metric increased", restClient.getCountMetric())
21+
assertEquals(200, restClient.lastKnownStatusCode)
22+
23+
Thread.sleep(10000)
24+
25+
testHelper.ensureMetricsReceived { event, header ->
26+
testHelper.doesContainMetric(event, "countMetric", "counter", 1.0)
27+
}
28+
}
29+
30+
@Test
31+
fun `gauge metric`() {
32+
val restClient = testHelper.restClient
33+
assertEquals("gauge metric tracked", restClient.getGaugeMetric(14))
34+
assertEquals(200, restClient.lastKnownStatusCode)
35+
36+
Thread.sleep(10000)
37+
38+
testHelper.ensureMetricsReceived { event, header ->
39+
testHelper.doesContainMetric(event, "memory.free", "gauge", 14.0)
40+
}
41+
}
42+
43+
@Test
44+
fun `distribution metric`() {
45+
val restClient = testHelper.restClient
46+
assertEquals("distribution metric tracked", restClient.getDistributionMetric(23))
47+
assertEquals(200, restClient.lastKnownStatusCode)
48+
49+
Thread.sleep(10000)
50+
51+
testHelper.ensureMetricsReceived { event, header ->
52+
testHelper.doesContainMetric(event, "distributionMetric", "distribution", 23.0)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)