Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Make MetricCategories more flexible #1550

Merged
merged 15 commits into from
Jun 19, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private P2PNetwork createP2pNetwork() {
}

private MetricsConfiguration createMetricsConfiguration() {
return MetricsConfiguration.builder().enabled(true).build();
return MetricsConfiguration.builder().enabled(true).port(0).build();
}

private JsonRpcHttpService createJsonRpcHttpService(
Expand Down
3 changes: 1 addition & 2 deletions metrics/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ jar {
}

dependencies {
implementation project(':util')

implementation 'com.google.guava:guava'
implementation 'io.prometheus:simpleclient'
implementation 'io.prometheus:simpleclient_common'
Expand All @@ -40,6 +38,7 @@ dependencies {
runtime 'org.apache.logging.log4j:log4j-core'

// test dependencies.
testImplementation project(':util')
testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.mockito:mockito-core'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.metrics;

import java.util.Optional;

public interface MetricCategory {

String getName();

Optional<String> getAppliationPrefix();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,40 @@
public interface MetricsSystem {

default Counter createCounter(
final PantheonMetricCategory category, final String name, final String help) {
final MetricCategory category, final String name, final String help) {
return createLabelledCounter(category, name, help, new String[0]).labels();
}

LabelledMetric<Counter> createLabelledCounter(
PantheonMetricCategory category, String name, String help, String... labelNames);
MetricCategory category, String name, String help, String... labelNames);

default OperationTimer createTimer(
final PantheonMetricCategory category, final String name, final String help) {
final MetricCategory category, final String name, final String help) {
return createLabelledTimer(category, name, help, new String[0]).labels();
}

LabelledMetric<OperationTimer> createLabelledTimer(
PantheonMetricCategory category, String name, String help, String... labelNames);
MetricCategory category, String name, String help, String... labelNames);

void createGauge(
PantheonMetricCategory category, String name, String help, DoubleSupplier valueSupplier);
void createGauge(MetricCategory category, String name, String help, DoubleSupplier valueSupplier);

default void createIntegerGauge(
final PantheonMetricCategory category,
final MetricCategory category,
final String name,
final String help,
final IntSupplier valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.getAsInt());
}

default void createLongGauge(
final PantheonMetricCategory category,
final MetricCategory category,
final String name,
final String help,
final LongSupplier valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.getAsLong());
}

Stream<Observation> streamObservations(PantheonMetricCategory category);
Stream<Observation> streamObservations(MetricCategory category);

default Stream<Observation> streamObservations() {
return Stream.of(PantheonMetricCategory.values()).flatMap(this::streamObservations);
}
Stream<Observation> streamObservations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import com.google.common.base.MoreObjects;

public class Observation {
private final PantheonMetricCategory category;
private final MetricCategory category;
private final String metricName;
private final List<String> labels;
private final Object value;

public Observation(
final PantheonMetricCategory category,
final MetricCategory category,
final String metricName,
final Object value,
final List<String> labels) {
Expand All @@ -34,7 +34,7 @@ public Observation(
this.labels = labels;
}

public PantheonMetricCategory getCategory() {
public MetricCategory getCategory() {
return category;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 ConsenSys AG.
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -13,26 +13,37 @@
package tech.pegasys.pantheon.metrics;

import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

public enum PantheonMetricCategory {
BIG_QUEUE("big_queue"),
import com.google.common.collect.ImmutableSet;

public enum PantheonMetricCategory implements MetricCategory {
BLOCKCHAIN("blockchain"),
EXECUTORS("executors"),
JVM("jvm", false),
NETWORK("network"),
PEERS("peers"),
PROCESS("process", false),
PERMISSIONING("permissioning"),
KVSTORE_ROCKSDB("rocksdb"),
KVSTORE_ROCKSDB_STATS("rocksdb", false),
RPC("rpc"),
SYNCHRONIZER("synchronizer"),
TRANSACTION_POOL("transaction_pool");

// Why not BIG_QUEUE and ROCKSDB? They hurt performance under load.
public static final Set<PantheonMetricCategory> DEFAULT_METRIC_CATEGORIES =
EnumSet.complementOf(EnumSet.of(BIG_QUEUE, KVSTORE_ROCKSDB, KVSTORE_ROCKSDB_STATS));
private static final Optional<String> PANTHEON_PREFIX = Optional.of("pantheon_");
public static final Set<MetricCategory> DEFAULT_METRIC_CATEGORIES;

static {
// Why not ROCKSDB and KVSTORE_ROCKSDB_STATS? They hurt performance under load.
final EnumSet<PantheonMetricCategory> pantheonCategories =
EnumSet.complementOf(EnumSet.of(KVSTORE_ROCKSDB, KVSTORE_ROCKSDB_STATS));

DEFAULT_METRIC_CATEGORIES =
ImmutableSet.<MetricCategory>builder()
.addAll(pantheonCategories)
.addAll(EnumSet.allOf(StandardMetricCategory.class))
.build();
}

private final String name;
private final boolean pantheonSpecific;
Expand All @@ -46,11 +57,13 @@ public enum PantheonMetricCategory {
this.pantheonSpecific = pantheonSpecific;
}

@Override
public String getName() {
return name;
}

public boolean isPantheonSpecific() {
return pantheonSpecific;
@Override
public Optional<String> getAppliationPrefix() {
return pantheonSpecific ? PANTHEON_PREFIX : Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.metrics;

import java.util.Optional;

public enum StandardMetricCategory implements MetricCategory {
JVM("jvm"),
PROCESS("process");

private final String name;

StandardMetricCategory(final String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

@Override
public Optional<String> getAppliationPrefix() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

import tech.pegasys.pantheon.metrics.Counter;
import tech.pegasys.pantheon.metrics.LabelledMetric;
import tech.pegasys.pantheon.metrics.MetricCategory;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.metrics.Observation;
import tech.pegasys.pantheon.metrics.OperationTimer;
import tech.pegasys.pantheon.metrics.OperationTimer.TimingContext;
import tech.pegasys.pantheon.metrics.PantheonMetricCategory;

import java.util.function.DoubleSupplier;
import java.util.stream.Stream;
Expand All @@ -42,7 +42,7 @@ public class NoOpMetricsSystem implements MetricsSystem {

@Override
public LabelledMetric<Counter> createLabelledCounter(
final PantheonMetricCategory category,
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
Expand All @@ -64,7 +64,7 @@ public static LabelledMetric<Counter> getCounterLabelledMetric(final int labelCo

@Override
public LabelledMetric<OperationTimer> createLabelledTimer(
final PantheonMetricCategory category,
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
Expand All @@ -82,13 +82,13 @@ public static LabelledMetric<OperationTimer> getOperationTimerLabelledMetric(

@Override
public void createGauge(
final PantheonMetricCategory category,
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier valueSupplier) {}

@Override
public Stream<Observation> streamObservations(final PantheonMetricCategory category) {
public Stream<Observation> streamObservations(final MetricCategory category) {
return Stream.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static tech.pegasys.pantheon.metrics.PantheonMetricCategory.DEFAULT_METRIC_CATEGORIES;

import tech.pegasys.pantheon.metrics.PantheonMetricCategory;
import tech.pegasys.pantheon.metrics.MetricCategory;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -35,7 +35,7 @@ public class MetricsConfiguration {
private final boolean enabled;
private final int port;
private final String host;
private final Set<PantheonMetricCategory> metricCategories;
private final Set<MetricCategory> metricCategories;
private final boolean pushEnabled;
private final int pushPort;
private final String pushHost;
Expand All @@ -51,7 +51,7 @@ private MetricsConfiguration(
final boolean enabled,
final int port,
final String host,
final Set<PantheonMetricCategory> metricCategories,
final Set<MetricCategory> metricCategories,
final boolean pushEnabled,
final int pushPort,
final String pushHost,
Expand Down Expand Up @@ -82,7 +82,7 @@ public String getHost() {
return host;
}

public Set<PantheonMetricCategory> getMetricCategories() {
public Set<MetricCategory> getMetricCategories() {
return metricCategories;
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public static class Builder {
private boolean enabled = false;
private int port = DEFAULT_METRICS_PORT;
private String host = DEFAULT_METRICS_HOST;
private Set<PantheonMetricCategory> metricCategories = DEFAULT_METRIC_CATEGORIES;
private Set<MetricCategory> metricCategories = DEFAULT_METRIC_CATEGORIES;
private boolean pushEnabled = false;
private int pushPort = DEFAULT_METRICS_PUSH_PORT;
private String pushHost = DEFAULT_METRICS_PUSH_HOST;
Expand All @@ -191,7 +191,7 @@ public Builder host(final String host) {
return this;
}

public Builder metricCategories(final Set<PantheonMetricCategory> metricCategories) {
public Builder metricCategories(final Set<MetricCategory> metricCategories) {
this.metricCategories = metricCategories;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Streams.stream;
import static tech.pegasys.pantheon.util.NetworkUtility.urlForSocketAddress;

import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.util.NetworkUtility;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -30,7 +28,6 @@
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import io.netty.handler.codec.http.HttpResponseStatus;
Expand Down Expand Up @@ -68,9 +65,7 @@ class MetricsHttpService implements MetricsService {
}

private void validateConfig(final MetricsConfiguration config) {
checkArgument(
config.getPort() == 0 || NetworkUtility.isValidPort(config.getPort()),
"Invalid port configuration.");
checkArgument(config.getPort() >= 0 && config.getPort() < 65535, "Invalid port configuration.");
checkArgument(config.getHost() != null, "Required host is not configured.");
checkArgument(
!(config.isEnabled() && config.isPushEnabled()),
Expand Down Expand Up @@ -232,14 +227,6 @@ public Optional<Integer> getPort() {
return Optional.of(httpServer.actualPort());
}

@VisibleForTesting
public String url() {
if (httpServer == null) {
return "";
}
return urlForSocketAddress("http", socketAddress());
}

// Facilitate remote health-checks in AWS, inter alia.
private void handleEmptyRequest(final RoutingContext routingContext) {
routingContext.response().setStatusCode(201).end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static com.google.common.base.Preconditions.checkArgument;

import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.util.NetworkUtility;

import java.io.IOException;
import java.util.Optional;
Expand Down Expand Up @@ -45,8 +44,7 @@ class MetricsPushGatewayService implements MetricsService {

private void validateConfig(final MetricsConfiguration config) {
checkArgument(
config.getPushPort() == 0 || NetworkUtility.isValidPort(config.getPushPort()),
"Invalid port configuration.");
config.getPushPort() >= 0 && config.getPushPort() < 65536, "Invalid port configuration.");
checkArgument(config.getPushHost() != null, "Required host is not configured.");
checkArgument(
!(config.isEnabled() && config.isPushEnabled()),
Expand Down
Loading