Skip to content

Commit

Permalink
Add metrics tests for disabling using config (#9310)
Browse files Browse the repository at this point in the history
* Add metrics tests for disabling using config

Signed-off-by: Tim Quinn <[email protected]>

* Copyright and style fixes

---------

Signed-off-by: Tim Quinn <[email protected]>
  • Loading branch information
tjquinno authored Oct 1, 2024
1 parent 6f0e61f commit b14316b
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,16 @@
package io.helidon.metrics.provider.tests;

import java.util.List;
import java.util.Map;

import io.helidon.common.testing.junit5.OptionalMatcher;
import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.metrics.api.Counter;
import io.helidon.metrics.api.MeterRegistry;
import io.helidon.metrics.api.Metrics;
import io.helidon.metrics.api.MetricsConfig;
import io.helidon.metrics.api.MetricsFactory;
import io.helidon.metrics.api.Tag;
import io.helidon.metrics.api.Timer;

Expand All @@ -28,7 +34,9 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -78,4 +86,38 @@ void testSameNameSameTwoTags() {
.tags(tags));
assertThat("Counter with same name, same two tags", counter1, is(sameInstance(counter2)));
}

@Test
void testDisabledYieldsNoOp() {
// Disable metrics using config.
Config metricsDisabledConfig = Config.just(ConfigSources.create(Map.of("enabled", "false")));
MeterRegistry shouldBeNoOp = MetricsFactory.getInstance()
.createMeterRegistry(MetricsConfig.create(metricsDisabledConfig));

Counter shouldBeNoOpCounter = shouldBeNoOp.getOrCreate(Counter.builder("shouldBeNoOpCounter"));
assertThat("Counters after registration", shouldBeNoOp.meters(), is(emptyIterable()));

shouldBeNoOpCounter.increment();
assertThat("Counter value after increment", shouldBeNoOpCounter.count(), is(0L));
}

@Test
void testDisabledMeters() {
Config config = Config.just(ConfigSources.create(Map.of("scoping.scopes.0.name", "application",
"scoping.scopes.0.filter.exclude", ".*Ignore.*")));
MeterRegistry selectiveRegistry = MetricsFactory.getInstance().createMeterRegistry(MetricsConfig.create(config));

Counter shouldBeNoOpCounter = selectiveRegistry.getOrCreate(Counter.builder("pleaseIgnoreThis"));
Counter shouldBeLive = selectiveRegistry.getOrCreate(Counter.builder("pleaseIncludeThis"));
assertThat("Counters after ignored registration", selectiveRegistry.meters(), hasSize(1));
assertThat("Counter retrieved",
selectiveRegistry.counter("pleaseIncludeThis", List.of()),
OptionalMatcher.optionalPresent());

shouldBeNoOpCounter.increment();
shouldBeLive.increment();

assertThat("Incremented disabled counter", shouldBeNoOpCounter.count(), is(0L));
assertThat("Incremented live counter", shouldBeLive.count(), is(1L));
}
}

0 comments on commit b14316b

Please sign in to comment.