Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.servlets.MetricsServlet;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.metrics.instrumentation.dropwizard.DropwizardExports;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

@WebListener
public class CdaMetricsContextListener extends MetricsServlet.ContextListener {

public static final MetricRegistry METRIC_REGISTRY = new MetricRegistry();

static {
CollectorRegistry.defaultRegistry.register(new DropwizardExports(METRIC_REGISTRY));
DropwizardExports.builder()
.dropwizardRegistry(METRIC_REGISTRY)
.register(PrometheusRegistry.defaultRegistry);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion cwms-data-api/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<servlet>
<servlet-name>metrics-prometheus</servlet-name>
<servlet-class>io.prometheus.client.exporter.MetricsServlet</servlet-class>
<servlet-class>io.prometheus.metrics.exporter.servlet.javax.PrometheusMetricsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>metrics-prometheus</servlet-name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cwms.cda;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.codahale.metrics.MetricRegistry;
import io.prometheus.metrics.instrumentation.dropwizard.DropwizardExports;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import org.junit.jupiter.api.Test;

class CdaMetricsContextListenerTest {

@Test
void webXmlServletClassIsOnClasspath() {
assertDoesNotThrow(() -> Class.forName(
"io.prometheus.metrics.exporter.servlet.javax.PrometheusMetricsServlet"));
}

@Test
void codahaleCounterIsExportedToPrometheusRegistry() {
MetricRegistry codaHale = new MetricRegistry();
PrometheusRegistry prom = new PrometheusRegistry();
DropwizardExports.builder().dropwizardRegistry(codaHale).register(prom);

codaHale.counter("cda_test_counter").inc(7);

MetricSnapshots snapshots = prom.scrape();
CounterSnapshot snapshot = (CounterSnapshot) snapshots.iterator().next();
assertEquals("cda_test_counter", snapshot.getMetadata().getPrometheusName());
assertEquals(7.0, snapshot.getDataPoints().get(0).getValue());
}
}
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ swagger-core = "2.2.23"
jackson = "2.17.1"
geojson-jackson = "1.14"
dropwizard = "4.2.12"
dropwizard_prometheus = "0.15.0"
prometheus_client = "1.6.1"
owasp = "20220608.1"
java-ee = "8.0.1"
cwms-tomcat-auth = "1.1.0"
Expand Down Expand Up @@ -73,8 +73,8 @@ javalin-openapi = { module = "io.javalin:javalin-openapi", version.ref = "javali
swagger-core = { module = "io.swagger.core.v3:swagger-core", version.ref = "swagger-core" }
metrics-core = { module = "io.dropwizard.metrics:metrics-core", version.ref = "dropwizard" }
metrics-servlets = { module = "io.dropwizard.metrics:metrics-servlets", version.ref = "dropwizard" }
metrics-prometheus-client = { module = "io.prometheus:simpleclient_dropwizard", version.ref = "dropwizard_prometheus" }
metrics-prometheus-servlets = { module = "io.prometheus:simpleclient_servlet", version.ref = "dropwizard_prometheus" }
metrics-prometheus-client = { module = "io.prometheus:prometheus-metrics-instrumentation-dropwizard", version.ref = "prometheus_client" }
metrics-prometheus-servlets = { module = "io.prometheus:prometheus-metrics-exporter-servlet-javax", version.ref = "prometheus_client" }
password4j = { module = "com.password4j:password4j", version = "1.8.4" }


Expand Down
Loading