From 082a1684c77d2033edfe624edd0a193afe107d98 Mon Sep 17 00:00:00 2001 From: Jochen Traunecker Date: Tue, 2 Jul 2024 16:29:44 +0200 Subject: [PATCH] Datago-74735: Add micrometer / StatsD metrics support (#190) --- service/application/pom.xml | 4 ++ .../src/main/resources/application.yml | 25 ++++++++ .../MicrometerStatsDConfigTest.java | 62 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 service/application/src/test/java/com/solace/maas/ep/event/management/agent/micrometer/MicrometerStatsDConfigTest.java diff --git a/service/application/pom.xml b/service/application/pom.xml index 262cafe07..59a870d73 100644 --- a/service/application/pom.xml +++ b/service/application/pom.xml @@ -274,6 +274,10 @@ micrometer-observation-test test + + io.micrometer + micrometer-registry-statsd + org.springframework.boot spring-boot-starter-actuator diff --git a/service/application/src/main/resources/application.yml b/service/application/src/main/resources/application.yml index 31385b9af..02d6f8758 100644 --- a/service/application/src/main/resources/application.yml +++ b/service/application/src/main/resources/application.yml @@ -5,6 +5,31 @@ springdoc: swagger-ui: path: /event-management-agent/swagger-ui.html +# micrometer / statsd +management: + metrics: + tags: + # key - value pairs + maas_id: my-maas-id + enable: + # enable / disable specific metrics + all: false + # sample metrics to be exposed + application: + started: + time: true + jvm: + info: true + statsd: + metrics: + export: + # enable / disable shipping metrics to StatsD endpoint + enabled: false + flavor: datadog + host: 127.0.0.1 + port: 8125 + protocol: udp + server: port: 8180 diff --git a/service/application/src/test/java/com/solace/maas/ep/event/management/agent/micrometer/MicrometerStatsDConfigTest.java b/service/application/src/test/java/com/solace/maas/ep/event/management/agent/micrometer/MicrometerStatsDConfigTest.java new file mode 100644 index 000000000..4b70b4c2a --- /dev/null +++ b/service/application/src/test/java/com/solace/maas/ep/event/management/agent/micrometer/MicrometerStatsDConfigTest.java @@ -0,0 +1,62 @@ +package com.solace.maas.ep.event.management.agent.micrometer; + +import io.micrometer.statsd.StatsdConfig; +import io.micrometer.statsd.StatsdFlavor; +import io.micrometer.statsd.StatsdProtocol; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; + +@SuppressWarnings("PMD") +@ActiveProfiles("TEST") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { + "management.metrics.tags.maas_id=my-maas-id", + "management.metrics.enable.all=false", + "management.metrics.enable.application.started.time=true", + "management.metrics.enable.jvm.info=true", + "management.statsd.metrics.export.enabled=true", + "management.statsd.metrics.export.flavor=datadog", + "management.statsd.metrics.export.host=127.0.0.1", + "management.statsd.metrics.export.port=8128", + "management.statsd.metrics.export.protocol=udp" +}) +@DirtiesContext(classMode = AFTER_CLASS) +class MicrometerStatsDConfigTest { + + @Autowired + private StatsdConfig statsdConfig; + + @Autowired + private MetricsProperties metricsProperties; + + @Test + void testStatsdConfig(){ + assertNotNull(statsdConfig); + Assertions.assertTrue(statsdConfig.enabled()); + Assertions.assertEquals(StatsdFlavor.DATADOG,statsdConfig.flavor()); + Assertions.assertEquals("127.0.0.1", statsdConfig.host()); + Assertions.assertEquals(8128, statsdConfig.port()); + Assertions.assertEquals(StatsdProtocol.UDP, statsdConfig.protocol()); + } + + @Test + void testMicrometerRegistryConfig(){ + assertNotNull(metricsProperties); + Assertions.assertTrue(metricsProperties.getTags().containsKey("maas_id")); + Assertions.assertEquals("my-maas-id", metricsProperties.getTags().get("maas_id")); + Assertions.assertTrue(metricsProperties.getEnable().containsKey("all")); + Assertions.assertEquals(false, metricsProperties.getEnable().get("all")); + Assertions.assertTrue(metricsProperties.getEnable().containsKey("application.started.time")); + Assertions.assertEquals(true, metricsProperties.getEnable().get("application.started.time")); + Assertions.assertTrue(metricsProperties.getEnable().containsKey("jvm.info")); + Assertions.assertEquals(true, metricsProperties.getEnable().get("jvm.info")); + + } +}