Skip to content

Commit

Permalink
Enabling disabled_metrics config for Micrometer (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalkoren authored Sep 30, 2020
1 parent 883b656 commit c609815
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ endif::[]
metricset tag keys - {pull}1413[1413]
* Fix invalid micrometer metrics with non-numeric values {pull}1419[1419]
* Fix `NoClassDefFoundError` with JDBC instrumentation plugin {pull}1409[1409]
* Apply `disable_metrics` config to Micrometer metrics - {pull}1421[1421]
[[release-notes-1.x]]
=== Java Agent version 1.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.matcher.WildcardMatcher;
import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.report.ReporterConfiguration;
import co.elastic.apm.agent.sdk.weakmap.WeakMapSupplier;
Expand All @@ -37,6 +38,7 @@
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void run() {
return;
}
final long timestamp = System.currentTimeMillis() * 1000;
MeterMapConsumer meterConsumer = new MeterMapConsumer();
MeterMapConsumer meterConsumer = new MeterMapConsumer(tracer.getConfig(ReporterConfiguration.class).getDisableMetrics());
for (MeterRegistry registry : meterRegistries) {
registry.forEachMeter(meterConsumer);
}
Expand All @@ -97,11 +99,21 @@ public void run() {
}

private static class MeterMapConsumer implements Consumer<Meter> {

private final List<WildcardMatcher> disabledMetrics;

public MeterMapConsumer(List<WildcardMatcher> disabledMetrics) {
this.disabledMetrics = disabledMetrics;
}

final Map<Meter.Id, Meter> meters = new HashMap<>();

@Override
public void accept(Meter meter) {
meters.put(meter.getId(), meter);
Meter.Id meterId = meter.getId();
if (WildcardMatcher.isNoneMatch(disabledMetrics, meterId.getName())) {
meters.put(meterId, meter);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import co.elastic.apm.agent.MockReporter;
import co.elastic.apm.agent.MockTracer;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.matcher.WildcardMatcher;
import co.elastic.apm.agent.report.ReporterConfiguration;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -64,14 +65,15 @@ class MicrometerMetricsReporterTest {
private MicrometerMetricsReporter metricsReporter;
private MockReporter reporter;
private ObjectMapper objectMapper = new ObjectMapper();
private ElasticApmTracer tracer;

@BeforeEach
void setUp() {
SimpleMeterRegistry simpleMeterRegistry = new SimpleMeterRegistry();
CompositeMeterRegistry nestedCompositeMeterRegistry = new CompositeMeterRegistry(Clock.SYSTEM, List.of(simpleMeterRegistry));
meterRegistry = new CompositeMeterRegistry(Clock.SYSTEM, List.of(nestedCompositeMeterRegistry));
reporter = new MockReporter();
ElasticApmTracer tracer = MockTracer.createRealTracer(reporter);
tracer = MockTracer.createRealTracer(reporter);
doReturn(0L).when(tracer.getConfig(ReporterConfiguration.class)).getMetricsIntervalMs();
metricsReporter = new MicrometerMetricsReporter(tracer);
metricsReporter.registerMeterRegistry(meterRegistry);
Expand All @@ -93,6 +95,22 @@ void testSameMetricSet() {
assertThat(metricSet.get("metricset").get("samples").get("gauge").get("value").doubleValue()).isEqualTo(42);
}

@Test
void testDisabledMetrics() {
doReturn(List.of(WildcardMatcher.valueOf("root.metric"), WildcardMatcher.valueOf("root.metric.exclude.*")))
.when(tracer.getConfig(ReporterConfiguration.class)).getDisableMetrics();

List<Tag> tags = List.of(Tag.of("foo", "bar"));
meterRegistry.counter("root.metric", tags).increment(42);
meterRegistry.counter("root.metric.include", tags).increment(42);
meterRegistry.counter("root.metric.exclude.counter", tags).increment(42);

JsonNode metricSet = getSingleMetricSet();
assertThat(metricSet.get("metricset").get("tags").get("foo").textValue()).isEqualTo("bar");
assertThat(metricSet.get("metricset").get("samples")).hasSize(1);
assertThat(metricSet.get("metricset").get("samples").get("root.metric.include").get("value").doubleValue()).isEqualTo(42);
}

@Test
void testTagsSanitation() {
List<Tag> tags = List.of(
Expand Down

0 comments on commit c609815

Please sign in to comment.