Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test metrics are registered with correct units #553

Merged
merged 1 commit into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,6 +31,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import java.util.concurrent.ExecutionException;

Expand All @@ -41,8 +43,14 @@
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.RetryResult;
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.RetryRetried;
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.TimeoutTimedOut;
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition;
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricGetter;
import org.eclipse.microprofile.fault.tolerance.tck.util.Packages;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricRegistry.Type;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.annotation.RegistryType;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand Down Expand Up @@ -79,6 +87,10 @@ public static WebArchive deploy() {
@Inject
private AllMetricsBean allMetricsBean;

@Inject
@RegistryType(type = Type.BASE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me think:
Do we have test to verify the metrics is under base instead of application?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have an explicit test, but reading the Metrics API docs, I thought that the registries were separate and it wouldn't give you an aggregated view. When you inject a MetricsRegistry without specifying the type, you get the application registry.

In our tests we specify that we want the base registry, both here and in MetricGetter

Copy link
Member Author

@Azquelt Azquelt Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other test we could add would be to ensure that metrics are not registered under the application scope. I'm not sure we should add such a test since the spec doesn't explicitly forbid implementers from putting the same metrics in the application scope.

private MetricRegistry metricRegistry;

@Test
public void testAllMetrics() throws InterruptedException, ExecutionException {
MetricGetter m = new MetricGetter(AllMetricsBean.class, "doWork");
Expand Down Expand Up @@ -128,4 +140,19 @@ public void testAllMetrics() throws InterruptedException, ExecutionException {
assertThat("bulkhead queue wait time histogram present", m.getBulkheadWaitingDuration().isPresent(), is(true));
}

@Test
public void testMetricUnits() throws InterruptedException, ExecutionException {
// Call the method to ensure that all metrics get registered
allMetricsBean.doWork().get();
Ladicek marked this conversation as resolved.
Show resolved Hide resolved

// Validate that each metric has metadata which declares the correct unit
for (MetricDefinition metric : MetricDefinition.values()) {
Metadata metadata = metricRegistry.getMetadata().get(metric.getName());

assertNotNull(metadata, "Missing metadata for metric " + metric);

assertEquals(metadata.getUnit().orElse(MetricUnits.NONE), metric.getUnit(), "Incorrect unit for metric " + metric);
}
}

}
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.microprofile.metrics.Gauge;
import org.eclipse.microprofile.metrics.Histogram;
import org.eclipse.microprofile.metrics.Metric;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.Tag;

/**
Expand All @@ -48,26 +49,34 @@ public enum MetricDefinition {
RETRY_CALLS("ft.retry.calls.total", Counter.class, RetryRetried.class, RetryResult.class),
RETRY_RETRIES("ft.retry.retries.total", Counter.class),
TIMEOUT_CALLS("ft.timeout.calls.total", Counter.class, TimeoutTimedOut.class),
TIMEOUT_EXECUTION_DURATION("ft.timeout.executionDuration", Histogram.class),
TIMEOUT_EXECUTION_DURATION("ft.timeout.executionDuration", Histogram.class, MetricUnits.NANOSECONDS),
CIRCUITBREAKER_CALLS("ft.circuitbreaker.calls.total", Counter.class, CircuitBreakerResult.class),
CIRCUITBREAKER_STATE("ft.circuitbreaker.state.total", Gauge.class, CircuitBreakerState.class),
CIRCUITBREAKER_STATE("ft.circuitbreaker.state.total", Gauge.class, MetricUnits.NANOSECONDS, CircuitBreakerState.class),
CIRCUITBREAKER_OPENED("ft.circuitbreaker.opened.total", Counter.class),
BULKHEAD_CALLS("ft.bulkhead.calls.total", Counter.class, BulkheadResult.class),
BULKHEAD_EXECUTIONS_RUNNING("ft.bulkhead.executionsRunning", Gauge.class),
BULKHEAD_EXECUTIONS_WAITING("ft.bulkhead.executionsWaiting", Gauge.class),
BULKHEAD_RUNNING_DURATION("ft.bulkhead.runningDuration", Histogram.class),
BULKHEAD_WAITING_DURATION("ft.bulkhead.waitingDuration", Histogram.class);
BULKHEAD_RUNNING_DURATION("ft.bulkhead.runningDuration", Histogram.class, MetricUnits.NANOSECONDS),
BULKHEAD_WAITING_DURATION("ft.bulkhead.waitingDuration", Histogram.class, MetricUnits.NANOSECONDS);

private String name;
private String unit;
private Class<? extends Metric> metricClass;
private Class<? extends TagValue>[] tagClasses;

@SafeVarargs
private MetricDefinition(String name, Class<? extends Metric> metricClass, Class<? extends TagValue>... tagClasses) {
private MetricDefinition(String name, Class<? extends Metric> metricClass, String unit, Class<? extends TagValue>... tagClasses) {
this.name = name;
this.unit = unit;
this.metricClass = metricClass;
this.tagClasses = tagClasses;
}

@SafeVarargs
private MetricDefinition(String name, Class<? extends Metric> metricClass, Class<? extends TagValue>... tagClasses) {
this(name, metricClass, MetricUnits.NONE, tagClasses);
}


/**
* The metric name
Expand All @@ -77,6 +86,15 @@ private MetricDefinition(String name, Class<? extends Metric> metricClass, Class
public String getName() {
return name;
}

/**
* The metric unit
*
* @return the unit
*/
public String getUnit() {
return unit;
}

/**
* The subclass of {@link Metric} used by this metric
Expand Down