Skip to content

Commit

Permalink
Fix some remaining problems with disabling metrics, mostly deferring …
Browse files Browse the repository at this point in the history
…access to RegistryFactory (helidon-io#3663)

* Set initial MetricsSettings for creating RegistryFactory from config; still can be changed by later calls to getInstance(MetricsSettings)

* Need to support no-op Gauge after all

* Defer use of RegistryFactory as long as possible for executor metrics

* Reorder some steps in MP metrics service initialization

* In MP fault tolerance, defer access to RegistryFactory

* In Jaeger metrics defer access to RegistryFactory

* In DB client defer access to RegistryFactory

* In grpc defer access to RegistryFactory

* Add MP/metrics test for disabled metrics
  • Loading branch information
tjquinno authored and arjav-desai committed Nov 27, 2021
1 parent 1fcd836 commit 1f10f53
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 84 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 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 @@ -17,8 +17,9 @@

import java.util.logging.Logger;

import io.helidon.common.LazyValue;
import io.helidon.config.Config;
import io.helidon.metrics.RegistryFactory;
import io.helidon.metrics.api.RegistryFactory;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
Expand All @@ -40,11 +41,11 @@ public class DropwizardMetricsListener implements MetricRegistryListener {

private final String prefix;
// Helidon metrics registry
private final MetricRegistry registry;
private final LazyValue<MetricRegistry> registry = LazyValue.create(
() -> RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.VENDOR));

private DropwizardMetricsListener(String prefix) {
this.prefix = prefix;
this.registry = RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.VENDOR);
}

static MetricRegistryListener create(Config config) {
Expand All @@ -54,61 +55,61 @@ static MetricRegistryListener create(Config config) {
@Override
public void onGaugeAdded(String name, Gauge<?> gauge) {
LOGGER.finest(() -> String.format("Gauge added: %s", name));
registry.register(prefix + name, new JdbcMetricsGauge<>(gauge));
registry.get().register(prefix + name, new JdbcMetricsGauge<>(gauge));
}

@Override
public void onGaugeRemoved(String name) {
LOGGER.finest(() -> String.format("Gauge removed: %s", name));
registry.remove(prefix + name);
registry.get().remove(prefix + name);
}

@Override
public void onCounterAdded(String name, Counter counter) {
LOGGER.finest(() -> String.format("Counter added: %s", name));
registry.register(prefix + name, new JdbcMetricsCounter(counter));
registry.get().register(prefix + name, new JdbcMetricsCounter(counter));
}

@Override
public void onCounterRemoved(String name) {
LOGGER.finest(() -> String.format("Counter removed: %s", name));
registry.remove(prefix + name);
registry.get().remove(prefix + name);
}

@Override
public void onHistogramAdded(String name, Histogram histogram) {
LOGGER.finest(() -> String.format("Histogram added: %s", name));
registry.register(prefix + name, new JdbcMetricsHistogram(histogram));
registry.get().register(prefix + name, new JdbcMetricsHistogram(histogram));
}

@Override
public void onHistogramRemoved(String name) {
LOGGER.finest(() -> String.format("Histogram removed: %s", name));
registry.remove(prefix + name);
registry.get().remove(prefix + name);
}

@Override
public void onMeterAdded(String name, Meter meter) {
LOGGER.finest(() -> String.format("Meter added: %s", name));
registry.register(prefix + name, new JdbcMetricsMeter(meter));
registry.get().register(prefix + name, new JdbcMetricsMeter(meter));
}

@Override
public void onMeterRemoved(String name) {
LOGGER.finest(() -> String.format("Meter removed: %s", name));
registry.remove(prefix + name);
registry.get().remove(prefix + name);
}

@Override
public void onTimerAdded(String name, Timer timer) {
LOGGER.finest(() -> String.format("Timer added: %s", name));
registry.register(prefix + name, new JdbcMetricsTimer(timer));
registry.get().register(prefix + name, new JdbcMetricsTimer(timer));
}

@Override
public void onTimerRemoved(String name) {
LOGGER.finest(() -> String.format("Timer removed: %s", name));
registry.remove(prefix + name);
registry.get().remove(prefix + name);
}

}
7 changes: 6 additions & 1 deletion dbclient/metrics/pom.xml
Expand Up @@ -37,11 +37,16 @@
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
<artifactId>helidon-metrics-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -20,11 +20,12 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;

import io.helidon.common.LazyValue;
import io.helidon.common.reactive.Single;
import io.helidon.dbclient.DbClientServiceContext;
import io.helidon.dbclient.DbStatementType;
import io.helidon.dbclient.common.DbClientServiceBase;
import io.helidon.metrics.RegistryFactory;
import io.helidon.metrics.api.RegistryFactory;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetadataBuilder;
Expand All @@ -39,7 +40,8 @@ abstract class DbClientMetric<T extends Metric> extends DbClientServiceBase {
private final Metadata meta;
private final String description;
private final BiFunction<String, DbStatementType, String> nameFunction;
private final MetricRegistry registry;
private final LazyValue<MetricRegistry> registry = LazyValue.create(() ->
RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.APPLICATION));
private final ConcurrentHashMap<String, T> cache = new ConcurrentHashMap<>();
private final boolean measureErrors;
private final boolean measureSuccess;
Expand All @@ -54,7 +56,6 @@ protected DbClientMetric(DbClientMetricBuilderBase<?> builder) {
nameFunction = (name, statement) -> defaultNamePrefix() + name;
}
this.nameFunction = nameFunction;
this.registry = RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.APPLICATION);
this.measureErrors = builder.errors();
this.measureSuccess = builder.success();
String tmpDescription;
Expand All @@ -81,7 +82,7 @@ protected Single<DbClientServiceContext> apply(DbClientServiceContext context) {
if (description != null) {
builder = builder.withDescription(description);
}
return metric(registry, builder.build());
return metric(registry.get(), builder.build());
});

executeMetric(metric, context.statementFuture());
Expand Down
4 changes: 2 additions & 2 deletions dbclient/metrics/src/main/java/module-info.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021 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 @@ -23,7 +23,7 @@
module io.helidon.dbclient.metrics {
requires java.logging;
requires io.helidon.dbclient;
requires io.helidon.metrics;
requires io.helidon.metrics.api;
requires io.helidon.dbclient.common;

exports io.helidon.dbclient.metrics;
Expand Down
7 changes: 6 additions & 1 deletion grpc/metrics/pom.xml
Expand Up @@ -42,9 +42,14 @@
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
<artifactId>helidon-metrics-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
25 changes: 13 additions & 12 deletions grpc/metrics/src/main/java/io/helidon/grpc/metrics/GrpcMetrics.java
Expand Up @@ -24,11 +24,12 @@

import javax.annotation.Priority;

import io.helidon.common.LazyValue;
import io.helidon.grpc.core.GrpcHelper;
import io.helidon.grpc.core.InterceptorPriorities;
import io.helidon.grpc.server.MethodDescriptor;
import io.helidon.grpc.server.ServiceDescriptor;
import io.helidon.metrics.RegistryFactory;
import io.helidon.metrics.api.RegistryFactory;

import io.grpc.Context;
import io.grpc.ForwardingServerCall;
Expand Down Expand Up @@ -59,14 +60,14 @@ public class GrpcMetrics
/**
* The registry of vendor metrics.
*/
static final MetricRegistry VENDOR_REGISTRY =
RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.VENDOR);
static final LazyValue<MetricRegistry> VENDOR_REGISTRY = LazyValue.create(() ->
RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.VENDOR));

/**
* The registry of application metrics.
*/
static final MetricRegistry APP_REGISTRY =
RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.APPLICATION);
static final LazyValue<MetricRegistry> APP_REGISTRY = LazyValue.create(() ->
RegistryFactory.getInstance().getRegistry(MetricRegistry.Type.APPLICATION));

static final org.eclipse.microprofile.metrics.Metadata GRPC_METER = org.eclipse.microprofile.metrics.Metadata
.builder()
Expand Down Expand Up @@ -261,27 +262,27 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re

switch (type) {
case COUNTER:
serverCall = new CountedServerCall<>(APP_REGISTRY.counter(
serverCall = new CountedServerCall<>(APP_REGISTRY.get().counter(
rules.metadata(service, methodName), rules.toTags()), call);
break;
case METERED:
serverCall = new MeteredServerCall<>(APP_REGISTRY.meter(
serverCall = new MeteredServerCall<>(APP_REGISTRY.get().meter(
rules.metadata(service, methodName), rules.toTags()), call);
break;
case HISTOGRAM:
serverCall = new HistogramServerCall<>(APP_REGISTRY.histogram(
serverCall = new HistogramServerCall<>(APP_REGISTRY.get().histogram(
rules.metadata(service, methodName), rules.toTags()), call);
break;
case TIMER:
serverCall = new TimedServerCall<>(APP_REGISTRY.timer(
serverCall = new TimedServerCall<>(APP_REGISTRY.get().timer(
rules.metadata(service, methodName), rules.toTags()), call);
break;
case SIMPLE_TIMER:
serverCall = new SimplyTimedServerCall<>(APP_REGISTRY.simpleTimer(
serverCall = new SimplyTimedServerCall<>(APP_REGISTRY.get().simpleTimer(
rules.metadata(service, methodName), rules.toTags()), call);
break;
case CONCURRENT_GAUGE:
serverCall = new ConcurrentGaugeServerCall<>(APP_REGISTRY.concurrentGauge(
serverCall = new ConcurrentGaugeServerCall<>(APP_REGISTRY.get().concurrentGauge(
rules.metadata(service, methodName), rules.toTags()), call);
break;
case GAUGE:
Expand All @@ -290,7 +291,7 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re
serverCall = call;
}

serverCall = new MeteredServerCall<>(VENDOR_REGISTRY.meter(GRPC_METER), serverCall);
serverCall = new MeteredServerCall<>(VENDOR_REGISTRY.get().meter(GRPC_METER), serverCall);

return next.startCall(serverCall, headers);
}
Expand Down
4 changes: 2 additions & 2 deletions grpc/metrics/src/main/java/module-info.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 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 @@ -23,7 +23,7 @@
requires transitive io.helidon.grpc.core;
requires static io.helidon.grpc.client;
requires static io.helidon.grpc.server;
requires transitive io.helidon.metrics;
requires transitive io.helidon.metrics.api;

requires microprofile.metrics.api;
}

0 comments on commit 1f10f53

Please sign in to comment.