Skip to content

Commit

Permalink
feat(metric): throw an exception when failed to start prometheus.
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed May 22, 2024
1 parent 12b4f16 commit 6a2f020
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
25 changes: 12 additions & 13 deletions common/src/main/java/org/tron/common/prometheus/Metrics.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
package org.tron.common.prometheus;

import com.google.common.annotations.VisibleForTesting;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Histogram;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
import java.io.IOException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.parameter.CommonParameter;

@Slf4j(topic = "metrics")
public class Metrics {

public static final double MILLISECONDS_PER_SECOND = Collector.MILLISECONDS_PER_SECOND;
@Setter
@VisibleForTesting
private static volatile boolean initialized = false;

private Metrics() {
throw new IllegalStateException("Metrics");
}

public static synchronized void init() {
public static synchronized void init() throws IOException {
if(initialized) {
return;
}
if (CommonParameter.getInstance().isMetricsPrometheusEnable()) {
try {
DefaultExports.initialize();
new OperatingSystemExports().register(CollectorRegistry.defaultRegistry);
new GuavaCacheExports().register(CollectorRegistry.defaultRegistry);
int port = CommonParameter.getInstance().getMetricsPrometheusPort();
new HTTPServer.Builder().withPort(port).build();
logger.info("prometheus exposed on port : {}", port);
initialized = true;
} catch (IOException e) {
CommonParameter.getInstance().setMetricsPrometheusEnable(false);
logger.error("{}", e.getMessage());
}
DefaultExports.initialize();
new OperatingSystemExports().register(CollectorRegistry.defaultRegistry);
new GuavaCacheExports().register(CollectorRegistry.defaultRegistry);
int port = CommonParameter.getInstance().getMetricsPrometheusPort();
new HTTPServer.Builder().withPort(port).build();
logger.info("prometheus exposed on port : {}", port);
initialized = true;
}
}

Expand Down
14 changes: 14 additions & 0 deletions framework/src/main/java/org/tron/common/metrics/MetricService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.tron.common.metrics;

import org.tron.common.prometheus.Metrics;

public class MetricService {

public static void startPrometheus() {
try {
Metrics.init();
} catch (Exception e) {
throw new IllegalStateException("start Prometheus service failed.", e);
}
}
}
4 changes: 2 additions & 2 deletions framework/src/main/java/org/tron/program/FullNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.log.LogResetter;
import org.tron.common.metrics.MetricService;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.Metrics;
import org.tron.core.Constant;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static void main(String[] args) {
}

// init metrics first
Metrics.init();
MetricService.startPrometheus();

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.setAllowCircularReferences(false);
Expand Down
4 changes: 2 additions & 2 deletions framework/src/main/java/org/tron/program/SolidityNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.client.DatabaseGrpcClient;
import org.tron.common.metrics.MetricService;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.Metrics;
import org.tron.core.ChainBaseManager;
import org.tron.core.Constant;
import org.tron.core.capsule.BlockCapsule;
Expand Down Expand Up @@ -75,7 +75,7 @@ public static void main(String[] args) {
return;
}
// init metrics first
Metrics.init();
MetricService.startPrometheus();

Application appT = ApplicationFactory.create(context);
SolidityNode node = new SolidityNode(appT.getDbManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.junit.Before;
import org.junit.Test;
import org.tron.common.BaseTest;
import org.tron.common.metrics.MetricService;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.Metrics;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Constant;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class JsonrpcServiceTest extends BaseTest {
CommonParameter.getInstance().setJsonRpcHttpPBFTNodeEnable(true);
CommonParameter.getInstance().setJsonRpcHttpSolidityNodeEnable(true);
CommonParameter.getInstance().setMetricsPrometheusEnable(true);
Metrics.init();
MetricService.startPrometheus();

OWNER_ADDRESS =
Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.tron.common.BaseTest;
import org.tron.common.crypto.ECKey;
import org.tron.common.metrics.MetricService;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.MetricLabels;
import org.tron.common.prometheus.Metrics;
Expand Down Expand Up @@ -54,11 +57,14 @@ public class PrometheusApiServiceTest extends BaseTest {
@Resource
private ChainBaseManager chainManager;

@Rule
public ExpectedException thrown = ExpectedException.none();

static {
Args.setParam(new String[] {"-d", dbPath(), "-w"}, Constant.TEST_CONF);
Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet());
initParameter(Args.getInstance());
Metrics.init();
MetricService.startPrometheus();
}

protected static void initParameter(CommonParameter parameter) {
Expand Down Expand Up @@ -134,6 +140,12 @@ public void testMetric() throws Exception {
generateBlock(witnessAndAccount);
}
check();
// try to start prometheus again
MetricService.startPrometheus();
Metrics.setInitialized(false);
thrown.expect(IllegalStateException.class);
// try to start prometheus again
MetricService.startPrometheus();
}

private Map<ByteString, String> addTestWitnessAndAccount() {
Expand Down

0 comments on commit 6a2f020

Please sign in to comment.