Skip to content

Commit

Permalink
Merge pull request #383 from jmartisk/master-issue-361
Browse files Browse the repository at this point in the history
Convert metrics to dimensional
  • Loading branch information
phillip-kruger authored Aug 31, 2020
2 parents 272eb3c + 430c3e5 commit 475845b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.annotation.RegistryType;

import io.smallrye.graphql.api.Context;
Expand All @@ -27,24 +28,24 @@
public class MetricsService implements EventingService {

private final MetricRegistry metricRegistry;

private final Map<Context, Long> startTimes = Collections.synchronizedMap(new IdentityHashMap<>());
private static final String METRIC_NAME = "mp_graphql";
private final String DESCRIPTION = "Call statistics for the operation denoted by the 'name' tag";

public MetricsService() {
this.metricRegistry = CDI.current().select(MetricRegistry.class, new VendorType()).get();
}

@Override
public Operation createOperation(Operation operation) {
final String name = getName(operation);
final String description = getDescription(operation);
final Tag[] tags = getTags(operation);

Metadata metadata = Metadata.builder()
.withName(name)
.withName(METRIC_NAME)
.withType(MetricType.SIMPLE_TIMER)
.withDescription(description)
.withDescription(DESCRIPTION)
.build();
metricRegistry.simpleTimer(metadata);
metricRegistry.simpleTimer(metadata, tags);
return operation;
}

Expand All @@ -58,7 +59,7 @@ public void afterDataFetch(Context context) {
Long startTime = startTimes.remove(context);
if (startTime != null) {
long duration = System.nanoTime() - startTime;
metricRegistry.simpleTimer(getName(context))
metricRegistry.simpleTimer(METRIC_NAME, getTags(context))
.update(Duration.ofNanos(duration));
}
}
Expand All @@ -68,25 +69,20 @@ public String getConfigKey() {
return ConfigKey.ENABLE_METRICS;
}

private String getName(Context context) {
return PRE + context.getOperationType().toString() + UNDERSCORE + context.getFieldName();
}

private String getName(Operation operation) {
return PRE + operation.getOperationType().toString() + UNDERSCORE + operation.getName();
private Tag[] getTags(Context context) {
return new Tag[] {
new Tag("name", context.getFieldName()),
new Tag("type", context.getOperationType().toString())
};
}

private String getDescription(Operation operation) {
return "Call statistics for the "
+ operation.getOperationType().toString().toLowerCase()
+ " '"
+ operation.getName()
+ "'";
private Tag[] getTags(Operation operation) {
return new Tag[] {
new Tag("name", operation.getName()),
new Tag("type", operation.getOperationType().toString())
};
}

private static final String PRE = "mp_graphql_";
private static final String UNDERSCORE = "_";

class VendorType extends AnnotationLiteral<RegistryType> implements RegistryType {
@Override
public MetricRegistry.Type type() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.SimpleTimer;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.annotation.RegistryType;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
Expand Down Expand Up @@ -45,13 +46,16 @@ public static WebArchive deployment() {
@Test
@InSequence(99)
public void verifyMetricsAreRegisteredEagerly() {
SimpleTimer metricForHelloQuery = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql_Query_get"));
SimpleTimer metricForHelloQuery = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql",
new Tag("type", "Query"), new Tag("name", "get")));
Assert.assertNotNull("Metric should be registered eagerly (Query)", metricForHelloQuery);

SimpleTimer metricForMutation = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql_Mutation_mutate"));
SimpleTimer metricForMutation = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql",
new Tag("type", "Mutation"), new Tag("name", "mutate")));
Assert.assertNotNull("Metric should be registered eagerly (Mutation)", metricForMutation);

SimpleTimer metricForSource = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql_Source_description"));
SimpleTimer metricForSource = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql",
new Tag("type", "Source"), new Tag("name", "description")));
Assert.assertNotNull("Metric should be registered eagerly (Source)", metricForSource);
}

Expand All @@ -70,19 +74,22 @@ public void invokeApi() throws Exception {
@Test
@InSequence(101)
public void verifyMetricsAreUpdated() {
SimpleTimer metricForGetQuery = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql_Query_get"));
SimpleTimer metricForGetQuery = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql",
new Tag("type", "Query"), new Tag("name", "get")));
Assert.assertEquals("The 'get' query was called three times, this should be reflected in metric value",
3, metricForGetQuery.getCount());
Assert.assertTrue("Total elapsed time for query should be greater than zero",
metricForGetQuery.getElapsedTime().toNanos() > 0);

SimpleTimer metricForMutation = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql_Mutation_mutate"));
SimpleTimer metricForMutation = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql",
new Tag("type", "Mutation"), new Tag("name", "mutate")));
Assert.assertEquals("The query was called twice, this should be reflected in metric value",
2, metricForMutation.getCount());
Assert.assertTrue("Total elapsed time for query should be greater than zero",
metricForMutation.getElapsedTime().toNanos() > 0);

SimpleTimer metricForSource = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql_Source_description"));
SimpleTimer metricForSource = metricRegistry.getSimpleTimers().get(new MetricID("mp_graphql",
new Tag("type", "Source"), new Tag("name", "description")));
Assert.assertEquals("The get{description} query was called once, this should be reflected in metric value",
1, metricForSource.getCount());
Assert.assertTrue("Total elapsed time for query should be greater than zero",
Expand Down

0 comments on commit 475845b

Please sign in to comment.