Skip to content

Commit

Permalink
Support for Dropwizard Instrumentation with 1.0.0 (#912)
Browse files Browse the repository at this point in the history
* Rename project simpleclient-archive/simpleclient_dropwizard to prometheus-metrics-instrumentation-dropwizard

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Implement DropwizardExports

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Label Changes Attempt 1

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Label Changes Attempt2

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Support for Custom Labels

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Minor Refactor

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Add builder for dwexports and change package name to dw5

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Rename package to dw5

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

* Rename to dropwizard5

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>

---------

Signed-off-by: Kinshuk Bairagi <hi@kinsh.uk>
  • Loading branch information
kingster authored Feb 9, 2024
1 parent bf6bc2c commit e6edcab
Show file tree
Hide file tree
Showing 17 changed files with 735 additions and 855 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<module>prometheus-metrics-exporter-httpserver</module>
<module>prometheus-metrics-exporter-opentelemetry</module>
<module>prometheus-metrics-instrumentation-jvm</module>
<module>prometheus-metrics-instrumentation-dropwizard5</module>
<module>prometheus-metrics-simpleclient-bridge</module>
<!--<module>prometheus-metrics-shaded-dependencies</module>-->
<module>examples</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
<parent>
<groupId>io.prometheus</groupId>
<artifactId>client_java</artifactId>
<version>1.0.0-beta-2-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
</parent>

<artifactId>simpleclient_dropwizard</artifactId>
<artifactId>prometheus-metrics-instrumentation-dropwizard5</artifactId>
<packaging>bundle</packaging>

<name>Prometheus Java Simpleclient Dropwizard</name>
<name>Prometheus Metrics Instrumentation - Dropwizard 5.x</name>
<description>
Collector of data from Dropwizard metrics library.
Instrumentation library for Dropwizard metrics 5.x
</description>

<properties>
<automatic.module.name>io.prometheus.metrics.instrumentation.dropwizard5</automatic.module.name>
</properties>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
Expand All @@ -29,17 +33,25 @@
<name>Francois Visconte</name>
<email>f.visconte@gmail.com</email>
</developer>

<developer>
<id>kingster</id>
<name>Kinshuk Bairagi</name>
<email>hi@kinsh.uk</email>
</developer>

</developers>

<dependencies>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<artifactId>prometheus-metrics-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<groupId>io.dropwizard.metrics5</groupId>
<artifactId>metrics-core</artifactId>
<version>4.2.9</version>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Test Dependencies Follow -->
Expand All @@ -61,6 +73,20 @@
<version>4.6.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.prometheus</groupId>
<artifactId>prometheus-metrics-exporter-httpserver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>prometheus-metrics-exposition-formats</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package io.prometheus.metrics.instrumentation.dropwizard5;

import io.dropwizard.metrics5.Timer;
import io.dropwizard.metrics5.*;
import io.prometheus.metrics.instrumentation.dropwizard5.labels.CustomLabelMapper;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.*;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Collect Dropwizard metrics from a MetricRegistry.
*/
public class DropwizardExports implements MultiCollector {
private static final Logger LOGGER = Logger.getLogger(DropwizardExports.class.getName());
private final MetricRegistry registry;
private final MetricFilter metricFilter;
private final Optional<CustomLabelMapper> labelMapper;

/**
* Creates a new DropwizardExports and {@link MetricFilter#ALL}.
*
* @param registry a metric registry to export in prometheus.
*/
DropwizardExports(MetricRegistry registry) {
super();
this.registry = registry;
this.metricFilter = MetricFilter.ALL;
this.labelMapper = Optional.empty();
}

/**
* Creates a new DropwizardExports with a custom {@link MetricFilter}.
*
* @param registry a metric registry to export in prometheus.
* @param metricFilter a custom metric filter.
*/
public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter) {
this.registry = registry;
this.metricFilter = metricFilter;
this.labelMapper = Optional.empty();
}

/**
* @param registry a metric registry to export in prometheus.
* @param metricFilter a custom metric filter.
* @param labelMapper a labelMapper to use to map labels.
*/
public DropwizardExports(MetricRegistry registry, MetricFilter metricFilter, CustomLabelMapper labelMapper) {
this.registry = registry;
this.metricFilter = metricFilter;
this.labelMapper = Optional.ofNullable(labelMapper);
}

private static String getHelpMessage(String metricName, Metric metric) {
return String.format("Generated from Dropwizard metric import (metric=%s, type=%s)",
metricName, metric.getClass().getName());
}


private static MetricMetadata getMetricMetaData(String metricName, Metric metric) {
return new MetricMetadata(PrometheusNaming.sanitizeMetricName(metricName), getHelpMessage(metricName, metric));
}

/**
* Export counter as Prometheus <a href="https://prometheus.io/docs/concepts/metric_types/#gauge">Gauge</a>.
*/
MetricSnapshot fromCounter(String dropwizardName, Counter counter) {
MetricMetadata metadata = getMetricMetaData(dropwizardName, counter);
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder = CounterSnapshot.CounterDataPointSnapshot.builder().value(Long.valueOf(counter.getCount()).doubleValue());
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
}

/**
* Export gauge as a prometheus gauge.
*/
MetricSnapshot fromGauge(String dropwizardName, Gauge gauge) {
Object obj = gauge.getValue();
double value;
if (obj instanceof Number) {
value = ((Number) obj).doubleValue();
} else if (obj instanceof Boolean) {
value = ((Boolean) obj) ? 1 : 0;
} else {
LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", PrometheusNaming.sanitizeMetricName(dropwizardName),
obj == null ? "null" : obj.getClass().getName()));
return null;
}
MetricMetadata metadata = getMetricMetaData(dropwizardName, gauge);
GaugeSnapshot.GaugeDataPointSnapshot.Builder dataPointBuilder = GaugeSnapshot.GaugeDataPointSnapshot.builder().value(value);
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
return new GaugeSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
}

/**
* Export a histogram snapshot as a prometheus SUMMARY.
*
* @param dropwizardName metric name.
* @param snapshot the histogram snapshot.
* @param count the total sample count for this snapshot.
* @param factor a factor to apply to histogram values.
*/
MetricSnapshot fromSnapshotAndCount(String dropwizardName, Snapshot snapshot, long count, double factor, String helpMessage) {
Quantiles quantiles = Quantiles.builder()
.quantile(0.5, snapshot.getMedian() * factor)
.quantile(0.75, snapshot.get75thPercentile() * factor)
.quantile(0.95, snapshot.get95thPercentile() * factor)
.quantile(0.98, snapshot.get98thPercentile() * factor)
.quantile(0.99, snapshot.get99thPercentile() * factor)
.quantile(0.999, snapshot.get999thPercentile() * factor)
.build();

MetricMetadata metadata = new MetricMetadata(PrometheusNaming.sanitizeMetricName(dropwizardName), helpMessage);
SummarySnapshot.SummaryDataPointSnapshot.Builder dataPointBuilder = SummarySnapshot.SummaryDataPointSnapshot.builder().quantiles(quantiles).count(count);
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
return new SummarySnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
}

/**
* Convert histogram snapshot.
*/
MetricSnapshot fromHistogram(String dropwizardName, Histogram histogram) {
return fromSnapshotAndCount(dropwizardName, histogram.getSnapshot(), histogram.getCount(), 1.0,
getHelpMessage(dropwizardName, histogram));
}

/**
* Export Dropwizard Timer as a histogram. Use TIME_UNIT as time unit.
*/
MetricSnapshot fromTimer(String dropwizardName, Timer timer) {
return fromSnapshotAndCount(dropwizardName, timer.getSnapshot(), timer.getCount(),
1.0D / TimeUnit.SECONDS.toNanos(1L), getHelpMessage(dropwizardName, timer));
}

/**
* Export a Meter as a prometheus COUNTER.
*/
MetricSnapshot fromMeter(String dropwizardName, Meter meter) {
MetricMetadata metadata = getMetricMetaData(dropwizardName + "_total", meter);
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder = CounterSnapshot.CounterDataPointSnapshot.builder().value(meter.getCount());
labelMapper.map(mapper -> dataPointBuilder.labels(mapper.getLabels(dropwizardName, Collections.emptyList(), Collections.emptyList())));
return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
}

@Override
public MetricSnapshots collect() {
MetricSnapshots.Builder metricSnapshots = MetricSnapshots.builder();
for (SortedMap.Entry<MetricName, Gauge> entry : registry.getGauges(metricFilter).entrySet()) {
Optional.ofNullable(fromGauge(entry.getKey().getKey(), entry.getValue())).map(metricSnapshots::metricSnapshot);
}
for (SortedMap.Entry<MetricName, Counter> entry : registry.getCounters(metricFilter).entrySet()) {
metricSnapshots.metricSnapshot(fromCounter(entry.getKey().getKey(), entry.getValue()));
}
for (SortedMap.Entry<MetricName, Histogram> entry : registry.getHistograms(metricFilter).entrySet()) {
metricSnapshots.metricSnapshot(fromHistogram(entry.getKey().getKey(), entry.getValue()));
}
for (SortedMap.Entry<MetricName, Timer> entry : registry.getTimers(metricFilter).entrySet()) {
metricSnapshots.metricSnapshot(fromTimer(entry.getKey().getKey(), entry.getValue()));
}
for (SortedMap.Entry<MetricName, Meter> entry : registry.getMeters(metricFilter).entrySet()) {
metricSnapshots.metricSnapshot(fromMeter(entry.getKey().getKey(), entry.getValue()));
}
return metricSnapshots.build();
}

public static Builder builder() {
return new Builder();
}

//Builder class for DropwizardExports
public static class Builder {
private MetricRegistry registry;
private MetricFilter metricFilter;
private CustomLabelMapper labelMapper;

private Builder() {
this.metricFilter = MetricFilter.ALL;
}

public Builder dropwizardRegistry(MetricRegistry registry) {
this.registry = registry;
return this;
}

public Builder metricFilter(MetricFilter metricFilter) {
this.metricFilter = metricFilter;
return this;
}

public Builder customLabelMapper(CustomLabelMapper labelMapper) {
this.labelMapper = labelMapper;
return this;
}

DropwizardExports build() {
if (registry == null) {
throw new IllegalArgumentException("MetricRegistry must be set");
}
if (labelMapper == null) {
return new DropwizardExports(registry, metricFilter);
} else {
return new DropwizardExports(registry, metricFilter, labelMapper);
}
}

public void register() {
register(PrometheusRegistry.defaultRegistry);
}

public void register(PrometheusRegistry registry) {
DropwizardExports dropwizardExports = build();
registry.register(dropwizardExports);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package io.prometheus.client.dropwizard.samplebuilder;
package io.prometheus.metrics.instrumentation.dropwizard5.labels;

import io.prometheus.client.Collector;
import io.prometheus.metrics.model.snapshots.Labels;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Custom {@link SampleBuilder} implementation to allow Dropwizard metrics to be translated to Prometheus metrics including custom labels and names.
* A LabelMapper to allow Dropwizard metrics to be translated to Prometheus metrics including custom labels and names.
* Prometheus metric name and labels are extracted from the Dropwizard name based on the provided list of {@link MapperConfig}s.
* The FIRST matching config will be used.
* If no config is matched, the {@link DefaultSampleBuilder} is used.
*/
public class CustomMappingSampleBuilder implements SampleBuilder {
public class CustomLabelMapper {
private final List<CompiledMapperConfig> compiledMapperConfigs;
private final DefaultSampleBuilder defaultMetricSampleBuilder = new DefaultSampleBuilder();

public CustomMappingSampleBuilder(final List<MapperConfig> mapperConfigs) {
public CustomLabelMapper(final List<MapperConfig> mapperConfigs) {
if (mapperConfigs == null || mapperConfigs.isEmpty()) {
throw new IllegalArgumentException("CustomMappingSampleBuilder needs some mapper configs!");
throw new IllegalArgumentException("CustomLabelMapper needs some mapper configs!");
}

this.compiledMapperConfigs = new ArrayList<CompiledMapperConfig>(mapperConfigs.size());
Expand All @@ -28,8 +25,8 @@ public CustomMappingSampleBuilder(final List<MapperConfig> mapperConfigs) {
}
}

@Override
public Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value) {

public Labels getLabels(final String dropwizardName, final List<String> additionalLabelNames, final List<String> additionalLabelValues){
if (dropwizardName == null) {
throw new IllegalArgumentException("Dropwizard metric name cannot be null");
}
Expand All @@ -47,21 +44,10 @@ public Collector.MetricFamilySamples.Sample createSample(final String dropwizard
final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params);
nameAndLabels.labelNames.addAll(additionalLabelNames);
nameAndLabels.labelValues.addAll(additionalLabelValues);
return defaultMetricSampleBuilder.createSample(
nameAndLabels.name, nameSuffix,
nameAndLabels.labelNames,
nameAndLabels.labelValues,
value
);
return Labels.of(nameAndLabels.labelNames, nameAndLabels.labelValues);
}


return defaultMetricSampleBuilder.createSample(
dropwizardName, nameSuffix,
additionalLabelNames,
additionalLabelValues,
value
);
return Labels.of(additionalLabelNames, additionalLabelValues);
}

protected NameAndLabels getNameAndLabels(final MapperConfig config, final Map<String, String> parameters) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.prometheus.client.dropwizard.samplebuilder;
package io.prometheus.metrics.instrumentation.dropwizard5.labels;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.prometheus.client.dropwizard.samplebuilder.MapperConfig.METRIC_GLOB_REGEX;
import static io.prometheus.metrics.instrumentation.dropwizard5.labels.MapperConfig.METRIC_GLOB_REGEX;

/**
* GraphiteNamePattern is initialised with a simplified glob pattern that only allows '*' as special character.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.prometheus.client.dropwizard.samplebuilder;
package io.prometheus.metrics.instrumentation.dropwizard5.labels;

import java.util.HashMap;
import java.util.Map;
Expand Down
Loading

0 comments on commit e6edcab

Please sign in to comment.