Skip to content

Commit

Permalink
Refactor Prometheus exporter (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
brfrn169 committed Aug 25, 2021
1 parent 94de3c7 commit c365f41
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/scalardb-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ It contains two sections, Server configurations and Underlining storage/database
# Port number of Scalar DB server. 60051 by deafult
#scalar.db.server.port=60051
# Port number of Prometheus HTTP endpoint. 8080 by default
#scalar.db.server.prometheus_http_endpoint_port=8080
# Prometheus exporter port. Use 8080 if this is not given. Prometheus exporter will not be started if a negative number is given.
#scalar.db.server.prometheus_exporter_port=8080
#
# Underlining storage/database configurations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void setUpBeforeClass() throws Exception {
testEnv.insertMetadata();

Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void setUpBeforeClass() throws Exception {
testEnv.insertMetadata();

Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void setUpBeforeClass() throws Exception {
testEnv.insertMetadata();

Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ public static void setUpBeforeClass() throws Exception {
testEnv.insertMetadata();

Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public static void setUpBeforeClass() throws Exception {
Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(DatabaseConfig.ISOLATION_LEVEL, "SERIALIZABLE");
serverProperties.setProperty(DatabaseConfig.SERIALIZABLE_STRATEGY, "EXTRA_READ");
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public static void setUpBeforeClass() throws Exception {
Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(DatabaseConfig.ISOLATION_LEVEL, "SERIALIZABLE");
serverProperties.setProperty(DatabaseConfig.SERIALIZABLE_STRATEGY, "EXTRA_WRITE");
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public static void setUpBeforeClass() throws Exception {

Properties serverProperties = new Properties(testEnv.getJdbcConfig().getProperties());
serverProperties.setProperty(DatabaseConfig.TRANSACTION_MANAGER, "jdbc");
serverProperties.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "0");
serverProperties.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "-1");
server = new ScalarDbServer(serverProperties);
server.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ public class ServerConfig {

public static final String PREFIX = "scalar.db.server.";
public static final String PORT = PREFIX + "port";
public static final String PROMETHEUS_HTTP_ENDPOINT_PORT =
PREFIX + "prometheus_http_endpoint_port";
public static final String PROMETHEUS_EXPORTER_PORT = PREFIX + "prometheus_exporter_port";

public static final int DEFAULT_PORT = 60051;
public static final int DEFAULT_PROMETHEUS_HTTP_ENDPOINT_PORT = 8080;
public static final int DEFAULT_PROMETHEUS_EXPORTER_PORT = 8080;

private final Properties props;
private int port;
private int prometheusHttpEndpointPort;
private int prometheusExporterPort;

public ServerConfig(File propertiesFile) throws IOException {
this(new FileInputStream(propertiesFile));
Expand All @@ -49,8 +48,7 @@ public Properties getProperties() {

private void load() {
port = getInt(PORT, DEFAULT_PORT);
prometheusHttpEndpointPort =
getInt(PROMETHEUS_HTTP_ENDPOINT_PORT, DEFAULT_PROMETHEUS_HTTP_ENDPOINT_PORT);
prometheusExporterPort = getInt(PROMETHEUS_EXPORTER_PORT, DEFAULT_PROMETHEUS_EXPORTER_PORT);
}

private int getInt(String name, int defaultValue) {
Expand All @@ -73,7 +71,7 @@ public int getPort() {
return port;
}

public int getPrometheusHttpEndpointPort() {
return prometheusHttpEndpointPort;
public int getPrometheusExporterPort() {
return prometheusExporterPort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Pauser providePauser() {
Metrics provideMetrics() {
MetricRegistry metricRegistry = new MetricRegistry();
startJmxReporter(metricRegistry);
startPrometheusHttpEndpoint(metricRegistry);
startPrometheusExporter(metricRegistry);
return new Metrics(metricRegistry);
}

Expand All @@ -77,23 +77,23 @@ private void startJmxReporter(MetricRegistry metricRegistry) {
Runtime.getRuntime().addShutdownHook(new Thread(reporter::stop));
}

private void startPrometheusHttpEndpoint(MetricRegistry metricRegistry) {
int prometheusHttpEndpointPort = config.getPrometheusHttpEndpointPort();
if (prometheusHttpEndpointPort == 0) {
private void startPrometheusExporter(MetricRegistry metricRegistry) {
int prometheusExporterPort = config.getPrometheusExporterPort();
if (prometheusExporterPort < 0) {
return;
}

CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry));

Server server = new Server(prometheusHttpEndpointPort);
Server server = new Server(prometheusExporterPort);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
context.addServlet(new ServletHolder(new MetricsServlet()), "/stats/prometheus");
server.setStopAtShutdown(true);
try {
server.start();
LOGGER.info("Prometheus HTTP endpoint started, listening on {}", prometheusHttpEndpointPort);
LOGGER.info("Prometheus exporter started, listening on {}", prometheusExporterPort);
} catch (Exception e) {
LOGGER.error("failed to start Jetty server", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public void constructor_NothingGiven_ShouldUseDefault() {

// Assert
assertThat(config.getPort()).isEqualTo(ServerConfig.DEFAULT_PORT);
assertThat(config.getPrometheusHttpEndpointPort())
.isEqualTo(ServerConfig.DEFAULT_PROMETHEUS_HTTP_ENDPOINT_PORT);
assertThat(config.getPrometheusExporterPort())
.isEqualTo(ServerConfig.DEFAULT_PROMETHEUS_EXPORTER_PORT);
}

@Test
Expand Down Expand Up @@ -50,29 +50,29 @@ public void constructor_InvalidPortGiven_ShouldUseDefault() {
}

@Test
public void constructor_ValidPrometheusHttpEndpointPortGiven_ShouldLoadProperly() {
public void constructor_ValidPrometheusExporterPortGiven_ShouldLoadProperly() {
// Arrange
Properties props = new Properties();
props.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, Integer.toString(ANY_PORT));
props.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, Integer.toString(ANY_PORT));

// Act
ServerConfig config = new ServerConfig(props);

// Assert
assertThat(config.getPrometheusHttpEndpointPort()).isEqualTo(ANY_PORT);
assertThat(config.getPrometheusExporterPort()).isEqualTo(ANY_PORT);
}

@Test
public void constructor_InvalidPrometheusHttpEndpointPortGiven_ShouldUseDefault() {
public void constructor_InvalidPrometheusExporterPortGiven_ShouldUseDefault() {
// Arrange
Properties props = new Properties();
props.setProperty(ServerConfig.PROMETHEUS_HTTP_ENDPOINT_PORT, "abc");
props.setProperty(ServerConfig.PROMETHEUS_EXPORTER_PORT, "abc");

// Act
ServerConfig config = new ServerConfig(props);

// Assert
assertThat(config.getPrometheusHttpEndpointPort())
.isEqualTo(ServerConfig.DEFAULT_PROMETHEUS_HTTP_ENDPOINT_PORT);
assertThat(config.getPrometheusExporterPort())
.isEqualTo(ServerConfig.DEFAULT_PROMETHEUS_EXPORTER_PORT);
}
}

0 comments on commit c365f41

Please sign in to comment.