feat: implemented PrometheusMetricsModule

This commit is contained in:
Andrey Terentev 2024-11-30 00:13:56 +07:00
parent 285e050168
commit c4c6210929
4 changed files with 76 additions and 0 deletions

View File

@ -15,4 +15,8 @@ dependencies {
api 'org.sql2o:sql2o:1.8.0' api 'org.sql2o:sql2o:1.8.0'
api 'com.clickhouse:clickhouse-jdbc:0.7.1' api 'com.clickhouse:clickhouse-jdbc:0.7.1'
api 'org.lz4:lz4-java:1.8.0' api 'org.lz4:lz4-java:1.8.0'
api 'io.prometheus:prometheus-metrics-core:1.3.1'
api 'io.prometheus:prometheus-metrics-instrumentation-jvm:1.3.1'
api 'io.prometheus:prometheus-metrics-exporter-httpserver:1.3.1'
} }

View File

@ -0,0 +1,43 @@
package ru.dragonestia.msb3.api.module;
import io.prometheus.metrics.exporter.httpserver.HTTPServer;
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import java.net.InetSocketAddress;
@Log4j2
public class PrometheusMetricsModule {
private static boolean used = false;
@Getter private static HTTPServer server;
public static synchronized void init(InetSocketAddress bindingAddress) {
if (used) return;
used = true;
try {
run(bindingAddress);
log.info("Listening web-server on http://{}:{}/metrics for Prometheus metrics", bindingAddress.getHostName(), bindingAddress.getPort());
} catch (Exception ex) {
log.error(ex, ex);
}
}
private static void run(InetSocketAddress address) throws Exception {
JvmMetrics.builder().register();
server = HTTPServer.builder()
.inetAddress(address.getAddress())
.port(address.getPort())
.buildAndStart();
registerDefaultMetrics();
}
private static void registerDefaultMetrics() {
}
}

View File

@ -0,0 +1,18 @@
version: '3'
services:
prometheus:
image: prom/prometheus
ports:
- '9090:9090'
volumes:
- './prometheus.yml:/etc/prometheus/prometheus.yml'
grafana:
image: grafana/grafana
ports:
- '9091:80'
- '9092:3000'
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin

11
prometheus/prometheus.yml Normal file
View File

@ -0,0 +1,11 @@
global:
scrape_interval: 5s
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'msb3'
scrape_interval: 5s
metrics_path: '/metrics'
static_configs:
- targets: [ 'host.docker.internal:7500' ]