Skip to content

Commit

Permalink
add support for immediate metrics collection
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrez committed Aug 6, 2022
1 parent 5576520 commit ee62135
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.ibm.jesseg</groupId>
<artifactId>ibmi-prom-client</artifactId>
<packaging>jar</packaging>
<version>0.7.2</version>
<version>0.8.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -25,6 +25,16 @@
<artifactId>simpleclient_httpserver</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.0.21</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.7.v20120910</version>
</dependency>
<dependency>
<groupId>net.sf.jt400</groupId>
<artifactId>jt400</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ibm/jesseg/prometheus/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ public String getHostNameForDisplay() {
}
return m_displayHostname = localhost;
}
m_displayHostname = getHostNameForConnection();
return m_displayHostname = getHostNameForConnection();
}
return m_hostname = val.toString();
return m_displayHostname = val.toString();
}

public String getPassword() throws IOException {
Expand Down
69 changes: 49 additions & 20 deletions src/main/java/com/ibm/jesseg/prometheus/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.github.theprez.jcmdutils.AppLogger;
import com.github.theprez.jcmdutils.ConsoleQuestionAsker;
import com.ibm.jesseg.prometheus.Config.SQLQuery;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.exporter.MetricsServlet;

public class MainApp {

public static void main(String[] _args) {
AppLogger logger = AppLogger.getSingleton(Boolean.getBoolean("promclient.verbose"));
AppLogger logger = AppLogger.getSingleton(!Boolean.getBoolean("promclient.verbose"));

List<String> args = Arrays.asList(_args);
if (args.contains("sc")) {
Expand All @@ -43,18 +47,19 @@ public static void main(String[] _args) {
return;
}

com.sun.net.httpserver.HttpServer rootServer = null;

Server server = null;
int port = -1;
try {

CollectorRegistry registry = CollectorRegistry.defaultRegistry;

File jsonConfig = getConfigFile(logger);
Config config = new Config(logger, jsonConfig);
int port = config.getPort(logger);
port = config.getPort(logger);

List<SQLMetricPopulator> populators = new LinkedList<SQLMetricPopulator>();
for (SQLQuery query : config.getSQLQueries()) {
final List<SQLQuery> queries = config.getSQLQueries();
for (SQLQuery query : queries) {
populators.add(new SQLMetricPopulator(logger, registry, config,
new ConnectionManager(config),
query.getInterval(),
Expand All @@ -63,34 +68,58 @@ public static void main(String[] _args) {
query.getShowHostname(),
query.getGaugePrefix()));
}
rootServer = com.sun.net.httpserver.HttpServer.create();

try {
rootServer.bind(new InetSocketAddress(port), 100);
} catch (BindException e) {
throw new IOException("Port " + port + " is already in use", e);
}
logger.println("Verifying metrics collection....");
for (SQLMetricPopulator populator : populators) {
populator.gatherNow(12);
}
logger.println_success("Metrics collection verified.");

HTTPServer server = new HTTPServer.Builder()
.withHttpServer(rootServer)
.build();
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server = new Server(port);
server.setHandler(context);
MetricsServlet metricsServlet = new MetricsServlet(registry);

ServletHolder nowGatherer = new ServletHolder(metricsServlet) {
public void handle(org.eclipse.jetty.server.Request baseRequest, javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
throws javax.servlet.ServletException, javax.servlet.UnavailableException, IOException {
for (SQLMetricPopulator queryPopulator : populators) {
try {
queryPopulator.gatherNow(4);
} catch (SQLException e) {
throw new IOException(e);
}
}
super.handle(baseRequest, request, response);
};
};

context.addServlet(new ServletHolder(metricsServlet), "/metrics");
context.addServlet(nowGatherer, "/metrics_now");
context.addServlet(new ServletHolder(metricsServlet), "/");
server.start();

String successMessage = "\n\n\n";
successMessage += "==============================================================\n";
successMessage += "Successfully started Prometheus client on port " + port + "\n";
successMessage += "==============================================================\n";
logger.println_success(successMessage);
} catch (Exception e) {

server.join();
} catch (Exception e) {
if (e instanceof BindException) {
logger.println_err("Port " + port + " is already in use");
}
logger.printfln_err("ERROR: %s", e.getLocalizedMessage());
logger.printExceptionStack_verbose(e);
if (null != rootServer) {
rootServer.stop(0);
if (null != server) {
try {
server.stop();
} catch (Exception e1) {
logger.printfln_err("ERROR: %s", e1.getLocalizedMessage());
logger.printExceptionStack_verbose(e);
}
}
}
}
Expand Down

0 comments on commit ee62135

Please sign in to comment.