13.9. 使用 Micrometer 和 Prometheus 监控您的科学时间 OptaPlanner Java 应用程序
OptaPlanner 通过 Micrometer (一个 Java 应用程序的指标检测库)公开指标。您可以将 Micrometer 与 Prometheus 搭配使用,来监控 IANA 时间应用程序中的 OptaPlanner solver。
先决条件
- 您已使用 Java 创建了 OptaPlanner Demo timetable 应用程序。
- 已安装 Prometheus。有关安装 Prometheus 的详情,请查看 Prometheus 网站。
流程
将 Micrometer Prometheus 依赖项添加到 IANA timetable
pom.xml
文件中,其中 <MICROMETER_VERSION
> 是您安装的 Micrometer 版本:<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version><MICROMETER_VERSION></version> </dependency>
注意还需要
micrometer-core
依赖项。但是,这个依赖项包含在optaplanner-core
依赖项中,因此您不需要将其添加到pom.xml
文件中。将以下导入语句添加到
TimeTableApp.java
类。import io.micrometer.core.instrument.Metrics; import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry;
在
TimeTableApp.java
类的主方法的顶部添加以下行,以便 Prometheus 可以在解决方案启动前从com.sun.net.httpserver.HttpServer
中的数据:PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); try { HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); server.createContext("/prometheus", httpExchange -> { String response = prometheusRegistry.scrape(); httpExchange.sendResponseHeaders(200, response.getBytes().length); try (OutputStream os = httpExchange.getResponseBody()) { os.write(response.getBytes()); } }); new Thread(server::start).start(); } catch (IOException e) { throw new RuntimeException(e); } Metrics.addRegistry(prometheusRegistry); solve(); }
添加以下行来控制计时。通过调整计时,您可以了解指标如何根据时间变化。
withTerminationSpentLimit(Duration.ofMinutes(5)));
- 启动 7000 timetable 应用程序。
-
在 Web 浏览器中打开
http://localhost:8080/prometheus
,以在 Prometheus 中查看时间化应用程序。 打开您的监控系统,以查看您的 OptaPlanner 项目的指标。
公开以下指标:
-
optaplanner_solver_errors_total
:自测量开始以来发生的错误总数。 -
optaplanner_solver_solve_duration_seconds_active_count
: 当前地址器的数量。 -
optaplanner_solver_solve_duration_seconds_max
: 运行 longest-running 当前活跃解析程序的时间。 -
optaplanner_solver_solve_duration_seconds_duration_sum
: 每个活跃解析器解析持续时间的总和。例如,如果有两个活跃的解析器,一个会运行一个三分钟,另一个一分钟,总地址时间为 4 分钟。
-