我正在尝试使用Prometheus Java客户端库监视Java应用程序中的多个度量,但是我一次很难监视多个度量。如果我仅注册和刮取一个度量标准,那么一切似乎都可以正常工作,并且可以使用prometheus应用程序看到该度量标准,但是如果我尝试公开和监视多个度量标准,则只有一个度量标准可见。
因此,如果我的应用程序具有如下所示的检测类
package com.telemetryserver.Instrumentation;
import io.prometheus.client.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Instrumented_Class extends HttpServlet
{
private static Instrumented_Class _instance = null;
public static final Gauge metric_1 = Gauge.build().name("metric_1").help("metric_1").register();
//public static final Gauge metric_2 = Gauge.build().name("metric_2").help("metric_2").register();
public static Instrumented_Class getInstance()
{
if (_instance == null)
_instance = new Instrumented_Class();
return _instance;
}
//Getters and Setters
public static void setMetric_1(double val) { metric_1.set(val); }
public static double getMetric_1() { return metric_1.get(); }
//public static void setMetric_2(double val) { metric_2.set(val); }
//public static double getMetric_2() { return metric_2.get(); }
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException
{
double metric_1 = Instrumented_Class.getMetric_1();
Instrumented_Class.setMetric_1(metric_1 + 1);
//double metric_2 = Instrumented_Class.getMetric_2();
//Instrumented_Class.setMetric_2(metric_2 + 2);
resp.getWriter().println("Hello from Instrumented_Class!!!," +
" metric_1 = " + metric_1
//+ " metric_2 = " + metric_2
);
}
}
,主类为
package com.telemetryserver.client;
import com.telemetryserver.Instrumentation.*;
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class TelemetryApp
{
public static void main(String[] args)
{
startTestServer(2018);
}
public static void startTestServer(int port)
{
try
{
Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
//Expose our Instrumented servlet.
context.addServlet(new ServletHolder(Instrumented_Class.getInstance()), "/");
//Prometheus Metrics Servlet
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
// Add metrics about CPU, JVM memory etc.
//DefaultExports.initialize();
// Start the webserver.
server.start();
server.join();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
并使用YAML文件
global:
scrape_interval: 2s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 2s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'telemetryserver'
metrics_path : '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:2018']
我可以通过运行prometheus然后运行查询“ metric_1”来监视指标“ metric_1”。没关系,但是如果我通过取消注释已插入的类中的行来添加另一个名为metric_2的度量,则metric_2会显示在prometheus上,但metric_1会停止显示。此外,如果我取消注释DefaultExports.initialize();
行(它添加了所有默认的Prometheus度量),那么metric_1和metric_2都将停止显示,并且仅显示一个名为“ jvm_threads_current”的默认度量。
有人可以启发我如何监控普罗米修斯的多个指标吗?任何帮助表示赞赏。
答案 0 :(得分:1)
您的申请很好。
只需确保通过点击/metrics
端点对其进行测试。这就是Prometheus从您的应用程序收集指标的方式。
当我在本地进行测试时,同时使用指标和导出JVM指标,我得到的输出与此类似:
*** lots of jvm metrics
jvm_gc_collection_seconds_count{gc="PS MarkSweep",} 0.0
jvm_gc_collection_seconds_sum{gc="PS MarkSweep",} 0.0
# HELP metric_2 metric_2
# TYPE metric_2 gauge
metric_2 12.0
# HELP metric_1 metric_1
# TYPE metric_1 gauge
metric_1 6.0
# HELP jvm_threads_current Current thread count of a JVM
# TYPE jvm_threads_current gauge
jvm_threads_current 16.0
其中包含您的自定义指标以及JVM中的指标。
您的Prometheus配置也可以。要测试集成,请转到“状态”>“目标”,然后应该在下面的图像中看到您的服务:
然后转到“图形”页面并输入表达式{job="telemetryserver"}
,以检查Prometheus实际上是否正在从您的应用程序中收集度量。您应该看到类似以下内容的内容: