Avoid auto-boxing for gauge metrics. (#1526)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Adrian Sutton 6 years ago committed by GitHub
parent 335cae2249
commit 9ef6002a4a
  1. 4
      ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/worldstate/WorldStateDownloader.java
  2. 3
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java
  3. 4
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetwork.java
  4. 26
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java
  5. 15
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/MetricsSystem.java
  6. 4
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/noop/NoOpMetricsSystem.java
  7. 9
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/CurrentValueCollector.java
  8. 4
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystem.java

@ -24,7 +24,7 @@ import java.time.Clock;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.IntSupplier;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -78,7 +78,7 @@ public class WorldStateDownloader {
downloadStateValue(WorldDownloadState::getOutstandingTaskCount)); downloadStateValue(WorldDownloadState::getOutstandingTaskCount));
} }
private Supplier<Integer> downloadStateValue(final Function<WorldDownloadState, Integer> getter) { private IntSupplier downloadStateValue(final Function<WorldDownloadState, Integer> getter) {
return () -> { return () -> {
final WorldDownloadState state = this.downloadState.get(); final WorldDownloadState state = this.downloadState.get();
return state != null ? getter.apply(state) : 0; return state != null ? getter.apply(state) : 0;

@ -32,6 +32,7 @@ import java.net.InetSocketAddress;
import java.net.SocketException; import java.net.SocketException;
import java.util.OptionalInt; import java.util.OptionalInt;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.IntSupplier;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@ -69,7 +70,7 @@ public class VertxPeerDiscoveryAgent extends PeerDiscoveryAgent {
pendingTaskCounter(vertx.nettyEventLoopGroup())); pendingTaskCounter(vertx.nettyEventLoopGroup()));
} }
private Supplier<Integer> pendingTaskCounter(final EventLoopGroup eventLoopGroup) { private IntSupplier pendingTaskCounter(final EventLoopGroup eventLoopGroup) {
return () -> return () ->
StreamSupport.stream(eventLoopGroup.spliterator(), false) StreamSupport.stream(eventLoopGroup.spliterator(), false)
.filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor) .filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor)

@ -69,7 +69,7 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.IntSupplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@ -249,7 +249,7 @@ public class DefaultP2PNetwork implements P2PNetwork {
return new Builder(); return new Builder();
} }
private Supplier<Integer> pendingTaskCounter(final EventLoopGroup eventLoopGroup) { private IntSupplier pendingTaskCounter(final EventLoopGroup eventLoopGroup) {
return () -> return () ->
StreamSupport.stream(eventLoopGroup.spliterator(), false) StreamSupport.stream(eventLoopGroup.spliterator(), false)
.filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor) .filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor)

@ -29,7 +29,7 @@ import tech.pegasys.pantheon.util.enode.EnodeURL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.function.Supplier; import java.util.function.IntSupplier;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -44,7 +44,7 @@ public class SyncStatusNodePermissioningProviderTest {
@Mock private Counter checkCounter; @Mock private Counter checkCounter;
@Mock private Counter checkPermittedCounter; @Mock private Counter checkPermittedCounter;
@Mock private Counter checkUnpermittedCounter; @Mock private Counter checkUnpermittedCounter;
private Supplier<Integer> syncGauge; private IntSupplier syncGauge;
private static final EnodeURL bootnode = private static final EnodeURL bootnode =
EnodeURL.fromString( EnodeURL.fromString(
@ -70,8 +70,8 @@ public class SyncStatusNodePermissioningProviderTest {
bootnodes.add(bootnode); bootnodes.add(bootnode);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final ArgumentCaptor<Supplier<Integer>> syncGaugeCallbackCaptor = final ArgumentCaptor<IntSupplier> syncGaugeCallbackCaptor =
ArgumentCaptor.forClass(Supplier.class); ArgumentCaptor.forClass(IntSupplier.class);
when(metricsSystem.createCounter( when(metricsSystem.createCounter(
MetricCategory.PERMISSIONING, MetricCategory.PERMISSIONING,
@ -106,7 +106,7 @@ public class SyncStatusNodePermissioningProviderTest {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse(); assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0); assertThat(syncGauge.getAsInt()).isEqualTo(0);
} }
@Test @Test
@ -114,29 +114,29 @@ public class SyncStatusNodePermissioningProviderTest {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 1)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 1));
assertThat(provider.hasReachedSync()).isTrue(); assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1); assertThat(syncGauge.getAsInt()).isEqualTo(1);
} }
@Test @Test
public void whenInSyncChangesFromTrueToFalseHasReachedSyncShouldReturnTrue() { public void whenInSyncChangesFromTrueToFalseHasReachedSyncShouldReturnTrue() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse(); assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0); assertThat(syncGauge.getAsInt()).isEqualTo(0);
syncStatusListener.onSyncStatus(new SyncStatus(0, 2, 1)); syncStatusListener.onSyncStatus(new SyncStatus(0, 2, 1));
assertThat(provider.hasReachedSync()).isTrue(); assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1); assertThat(syncGauge.getAsInt()).isEqualTo(1);
syncStatusListener.onSyncStatus(new SyncStatus(0, 2, 3)); syncStatusListener.onSyncStatus(new SyncStatus(0, 2, 3));
assertThat(provider.hasReachedSync()).isTrue(); assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1); assertThat(syncGauge.getAsInt()).isEqualTo(1);
} }
@Test @Test
public void whenHasNotSyncedNonBootnodeShouldNotBePermitted() { public void whenHasNotSyncedNonBootnodeShouldNotBePermitted() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse(); assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0); assertThat(syncGauge.getAsInt()).isEqualTo(0);
boolean isPermitted = provider.isPermitted(enode1, enode2); boolean isPermitted = provider.isPermitted(enode1, enode2);
@ -150,7 +150,7 @@ public class SyncStatusNodePermissioningProviderTest {
public void whenHasNotSyncedBootnodeIncomingConnectionShouldNotBePermitted() { public void whenHasNotSyncedBootnodeIncomingConnectionShouldNotBePermitted() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse(); assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0); assertThat(syncGauge.getAsInt()).isEqualTo(0);
boolean isPermitted = provider.isPermitted(bootnode, enode1); boolean isPermitted = provider.isPermitted(bootnode, enode1);
@ -164,7 +164,7 @@ public class SyncStatusNodePermissioningProviderTest {
public void whenHasNotSyncedBootnodeOutgoingConnectionShouldBePermitted() { public void whenHasNotSyncedBootnodeOutgoingConnectionShouldBePermitted() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse(); assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0); assertThat(syncGauge.getAsInt()).isEqualTo(0);
boolean isPermitted = provider.isPermitted(enode1, bootnode); boolean isPermitted = provider.isPermitted(enode1, bootnode);
@ -178,7 +178,7 @@ public class SyncStatusNodePermissioningProviderTest {
public void whenHasSyncedIsPermittedShouldReturnTrue() { public void whenHasSyncedIsPermittedShouldReturnTrue() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 1)); syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 1));
assertThat(provider.hasReachedSync()).isTrue(); assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1); assertThat(syncGauge.getAsInt()).isEqualTo(1);
boolean isPermitted = provider.isPermitted(enode1, enode2); boolean isPermitted = provider.isPermitted(enode1, enode2);

@ -12,7 +12,9 @@
*/ */
package tech.pegasys.pantheon.metrics; package tech.pegasys.pantheon.metrics;
import java.util.function.Supplier; import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.stream.Stream; import java.util.stream.Stream;
public interface MetricsSystem { public interface MetricsSystem {
@ -33,23 +35,22 @@ public interface MetricsSystem {
LabelledMetric<OperationTimer> createLabelledTimer( LabelledMetric<OperationTimer> createLabelledTimer(
MetricCategory category, String name, String help, String... labelNames); MetricCategory category, String name, String help, String... labelNames);
void createGauge( void createGauge(MetricCategory category, String name, String help, DoubleSupplier valueSupplier);
MetricCategory category, String name, String help, Supplier<Double> valueSupplier);
default void createIntegerGauge( default void createIntegerGauge(
final MetricCategory category, final MetricCategory category,
final String name, final String name,
final String help, final String help,
final Supplier<Integer> valueSupplier) { final IntSupplier valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.get()); createGauge(category, name, help, () -> (double) valueSupplier.getAsInt());
} }
default void createLongGauge( default void createLongGauge(
final MetricCategory category, final MetricCategory category,
final String name, final String name,
final String help, final String help,
final Supplier<Long> valueSupplier) { final LongSupplier valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.get()); createGauge(category, name, help, () -> (double) valueSupplier.getAsLong());
} }
Stream<Observation> streamObservations(MetricCategory category); Stream<Observation> streamObservations(MetricCategory category);

@ -20,7 +20,7 @@ import tech.pegasys.pantheon.metrics.Observation;
import tech.pegasys.pantheon.metrics.OperationTimer; import tech.pegasys.pantheon.metrics.OperationTimer;
import tech.pegasys.pantheon.metrics.OperationTimer.TimingContext; import tech.pegasys.pantheon.metrics.OperationTimer.TimingContext;
import java.util.function.Supplier; import java.util.function.DoubleSupplier;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@ -85,7 +85,7 @@ public class NoOpMetricsSystem implements MetricsSystem {
final MetricCategory category, final MetricCategory category,
final String name, final String name,
final String help, final String help,
final Supplier<Double> valueSupplier) {} final DoubleSupplier valueSupplier) {}
@Override @Override
public Stream<Observation> streamObservations(final MetricCategory category) { public Stream<Observation> streamObservations(final MetricCategory category) {

@ -16,7 +16,7 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.DoubleSupplier;
import io.prometheus.client.Collector; import io.prometheus.client.Collector;
import io.prometheus.client.Collector.MetricFamilySamples.Sample; import io.prometheus.client.Collector.MetricFamilySamples.Sample;
@ -25,10 +25,10 @@ class CurrentValueCollector extends Collector {
private final String metricName; private final String metricName;
private final String help; private final String help;
private final Supplier<Double> valueSupplier; private final DoubleSupplier valueSupplier;
public CurrentValueCollector( public CurrentValueCollector(
final String metricName, final String help, final Supplier<Double> valueSupplier) { final String metricName, final String help, final DoubleSupplier valueSupplier) {
this.metricName = metricName; this.metricName = metricName;
this.help = help; this.help = help;
this.valueSupplier = valueSupplier; this.valueSupplier = valueSupplier;
@ -36,7 +36,8 @@ class CurrentValueCollector extends Collector {
@Override @Override
public List<MetricFamilySamples> collect() { public List<MetricFamilySamples> collect() {
final Sample sample = new Sample(metricName, emptyList(), emptyList(), valueSupplier.get()); final Sample sample =
new Sample(metricName, emptyList(), emptyList(), valueSupplier.getAsDouble());
return singletonList( return singletonList(
new MetricFamilySamples(metricName, Type.GAUGE, help, singletonList(sample))); new MetricFamilySamples(metricName, Type.GAUGE, help, singletonList(sample)));
} }

@ -29,7 +29,7 @@ import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier; import java.util.function.DoubleSupplier;
import java.util.stream.Stream; import java.util.stream.Stream;
import io.prometheus.client.Collector; import io.prometheus.client.Collector;
@ -138,7 +138,7 @@ public class PrometheusMetricsSystem implements MetricsSystem {
final MetricCategory category, final MetricCategory category,
final String name, final String name,
final String help, final String help,
final Supplier<Double> valueSupplier) { final DoubleSupplier valueSupplier) {
final String metricName = convertToPrometheusName(category, name); final String metricName = convertToPrometheusName(category, name);
if (enabledCategories.contains(category)) { if (enabledCategories.contains(category)) {
final Collector collector = new CurrentValueCollector(metricName, help, valueSupplier); final Collector collector = new CurrentValueCollector(metricName, help, valueSupplier);

Loading…
Cancel
Save