Skip to content

Commit

Permalink
instrumentation-dw5: Fix metric name issue with CustomLabelMapper (#949)
Browse files Browse the repository at this point in the history
* instrumentation-dw5: Fix issue with CustomLabelMapper

Signed-off-by: Kinshuk Bairagi <[email protected]>

* Update CustomLabelMapperTest

Signed-off-by: Kinshuk Bairagi <[email protected]>

* Refactor & Fix tests

Signed-off-by: Kinshuk Bairagi <[email protected]>

---------

Signed-off-by: Kinshuk Bairagi <[email protected]>
  • Loading branch information
kingster authored Apr 10, 2024
1 parent fca49ff commit fe02245
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DropwizardExports implements MultiCollector {
*
* @param registry a metric registry to export in prometheus.
*/
DropwizardExports(MetricRegistry registry) {
public DropwizardExports(MetricRegistry registry) {
super();
this.registry = registry;
this.metricFilter = MetricFilter.ALL;
Expand Down Expand Up @@ -61,9 +61,9 @@ private static String getHelpMessage(String metricName, Metric metric) {
metricName, metric.getClass().getName());
}


private static MetricMetadata getMetricMetaData(String metricName, Metric metric) {
return new MetricMetadata(PrometheusNaming.sanitizeMetricName(metricName), getHelpMessage(metricName, metric));
private MetricMetadata getMetricMetaData(String metricName, Metric metric) {
String name = labelMapper.isPresent() ? labelMapper.get().getName(metricName) : metricName;
return new MetricMetadata(PrometheusNaming.sanitizeMetricName(name), getHelpMessage(metricName, metric));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ public CustomLabelMapper(final List<MapperConfig> mapperConfigs) {
}
}

public String getName(final String dropwizardName){
if (dropwizardName == null) {
throw new IllegalArgumentException("Dropwizard metric name cannot be null");
}

CompiledMapperConfig matchingConfig = null;
for (CompiledMapperConfig config : this.compiledMapperConfigs) {
if (config.pattern.matches(dropwizardName)) {
matchingConfig = config;
break;
}
}

if (matchingConfig != null) {
final Map<String, String> params = matchingConfig.pattern.extractParameters(dropwizardName);
final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params);
return nameAndLabels.name;
}

return dropwizardName;
}


public Labels getLabels(final String dropwizardName, final List<String> additionalLabelNames, final List<String> additionalLabelValues){
if (dropwizardName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import io.dropwizard.metrics5.MetricFilter;
import io.dropwizard.metrics5.MetricRegistry;
import io.prometheus.metrics.core.metrics.Counter;
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
import io.prometheus.metrics.instrumentation.dropwizard5.DropwizardExports;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -69,9 +71,9 @@ public void test_WHEN_OneMatch_THEN_ShouldReturnConverted() {

metricRegistry.counter("app.okhttpclient.client.HttpClient.greatService.total").inc(1);

String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService counter\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.total, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_greatService_total{service=\"greatService\"} 1.0\n" +
String expected = "# TYPE app_okhttpclient_client_HttpClient counter\n" +
"# HELP app_okhttpclient_client_HttpClient Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.total, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_total{service=\"greatService\"} 1.0\n" +
"# EOF\n";
assertEquals(expected, convertToOpenMetricsFormat(dropwizardExports.collect()));
}
Expand All @@ -96,9 +98,9 @@ public void test_WHEN_MoreMatches_THEN_ShouldReturnFirstOne() {
metricRegistry.counter("app.okhttpclient.client.HttpClient.greatService.total").inc(1);


String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService counter\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.total, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_greatService_total{service=\"greatService\"} 1.0\n" +
String expected = "# TYPE app_okhttpclient_client_HttpClient counter\n" +
"# HELP app_okhttpclient_client_HttpClient Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.total, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_total{service=\"greatService\"} 1.0\n" +
"# EOF\n";
assertEquals(expected, convertToOpenMetricsFormat(dropwizardExports.collect()));
}
Expand All @@ -113,19 +115,26 @@ public void test_WHEN_MoreMatchesReverseOrder_THEN_ShouldReturnFirstOne() {
"app.okhttpclient.client.HttpClient",
labels
);

final MapperConfig mapperConfig2 = new MapperConfig(
"app.okhttpclient.client.HttpClient.*.*",
"app.okhttpclient.client.HttpClient2",
labels
);

final List<MapperConfig> mapperConfigs = Arrays.asList(
new MapperConfig("client-nope.*.*.*"),
mapperConfig,
new MapperConfig("app.okhttpclient.client.HttpClient.*.total") // this matches as well
mapperConfig2 // this matches as well
);

final CustomLabelMapper labelMapper = new CustomLabelMapper(mapperConfigs);
DropwizardExports dropwizardExports = new DropwizardExports(metricRegistry, MetricFilter.ALL, labelMapper);
metricRegistry.counter("app.okhttpclient.client.HttpClient.greatService.400").inc(1);

String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService_400 counter\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService_400 Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.400, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_greatService_400_total{service=\"greatService\",status=\"400\"} 1.0\n" +
String expected = "# TYPE app_okhttpclient_client_HttpClient counter\n" +
"# HELP app_okhttpclient_client_HttpClient Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.400, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_total{service=\"greatService\",status=\"400\"} 1.0\n" +
"# EOF\n";
assertEquals(expected, convertToOpenMetricsFormat(dropwizardExports.collect()));

Expand All @@ -144,7 +153,7 @@ public void test_WHEN_MoreToFormatInLabelsAndName_THEN_ShouldReturnCorrectSample
final List<MapperConfig> mapperConfigs = Arrays.asList(
new MapperConfig("client-nope.*.*.*"),
mapperConfig,
new MapperConfig("app.okhttpclient.client.HttpClient.*.total") // this matches as well
new MapperConfig("app.okhttpclient.client.HttpClient.*.*") // this matches as well
);


Expand All @@ -154,9 +163,9 @@ public void test_WHEN_MoreToFormatInLabelsAndName_THEN_ShouldReturnCorrectSample
System.out.println(convertToOpenMetricsFormat(dropwizardExports.collect()));


String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService_400 counter\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService_400 Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.400, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_greatService_400_total{service=\"greatService_400\",status=\"s_400\"} 1.0\n" +
String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService counter\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.400, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_greatService_total{service=\"greatService_400\",status=\"s_400\"} 1.0\n" +
"# EOF\n";
assertEquals(expected, convertToOpenMetricsFormat(dropwizardExports.collect()));
}
Expand All @@ -167,30 +176,24 @@ public void test_WHEN_AdditionalLabels_THEN_ShouldReturnCorrectSample() {
final Map<String, String> labels = new LinkedHashMap<String, String>();
labels.put("service", "${0}");
labels.put("status", "s_${1}");
labels.put("client", "sampleClient");
final MapperConfig mapperConfig = new MapperConfig(
"app.okhttpclient.client.HttpClient.*.*",
"app.okhttpclient.client.HttpClient.${0}",
labels
);
final List<MapperConfig> mapperConfigs = Arrays.asList(
new MapperConfig("client-nope.*.*.*"),
mapperConfig,
new MapperConfig("app.okhttpclient.client.HttpClient.*.total") // this matches as well
mapperConfig
);

final CustomLabelMapper labelMapper = new CustomLabelMapper(mapperConfigs);
DropwizardExports dropwizardExports = new DropwizardExports(metricRegistry,MetricFilter.ALL, labelMapper);
metricRegistry.histogram("app.okhttpclient.client.HttpClient.greatService.400");

String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService_400 summary\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService_400 Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.400, type=io.dropwizard.metrics5.Histogram)\n" +
"app_okhttpclient_client_HttpClient_greatService_400{service=\"greatService\",status=\"s_400\",quantile=\"0.5\"} 0.0\n" +
"app_okhttpclient_client_HttpClient_greatService_400{service=\"greatService\",status=\"s_400\",quantile=\"0.75\"} 0.0\n" +
"app_okhttpclient_client_HttpClient_greatService_400{service=\"greatService\",status=\"s_400\",quantile=\"0.95\"} 0.0\n" +
"app_okhttpclient_client_HttpClient_greatService_400{service=\"greatService\",status=\"s_400\",quantile=\"0.98\"} 0.0\n" +
"app_okhttpclient_client_HttpClient_greatService_400{service=\"greatService\",status=\"s_400\",quantile=\"0.99\"} 0.0\n" +
"app_okhttpclient_client_HttpClient_greatService_400{service=\"greatService\",status=\"s_400\",quantile=\"0.999\"} 0.0\n" +
"app_okhttpclient_client_HttpClient_greatService_400_count{service=\"greatService\",status=\"s_400\"} 0\n" +
metricRegistry.counter("app.okhttpclient.client.HttpClient.greatService.400").inc(1);

String expected = "# TYPE app_okhttpclient_client_HttpClient_greatService counter\n" +
"# HELP app_okhttpclient_client_HttpClient_greatService Generated from Dropwizard metric import (metric=app.okhttpclient.client.HttpClient.greatService.400, type=io.dropwizard.metrics5.Counter)\n" +
"app_okhttpclient_client_HttpClient_greatService_total{client=\"sampleClient\",service=\"greatService\",status=\"s_400\"} 1.0\n" +
"# EOF\n";
assertEquals(expected, convertToOpenMetricsFormat(dropwizardExports.collect()));
}
Expand Down

0 comments on commit fe02245

Please sign in to comment.