我想在我的应用程序中启用自定义指标。为此,我使用Prometheus客户端库在我的应用程序中收集指标。
为了使这些度量标准可用于Prometheus,我在以下位置使用Prometheus JMX Exporter: https://github.com/prometheus/jmx_exporter
以下是我的MBean:
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
public interface EmployeekpiMBean {
public Counter getTotalRequest();
public Gauge getInprogressRequests();
public Histogram getRequestLatency();
public double getCustomTotalRequestValue();
}
我注册我的MBean如下:
try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName mxbeanName = new ObjectName("com.sterlite.employee:type=Employeekpi");
Employeekpi mxbean = new Employeekpi();
mbs.registerMBean(mxbean, mxbeanName);
} catch (Exception e) {
System.out.println("Exception While Initializing MBean: " + e);
}
启动服务器后,我通过调用以下API来验证指标: http://localhost:8080/metrics
我只能看到1个指标,也就是具有“ double”类型的指标。
# HELP com_sterlite_employee_Employeekpi_CustomTotalRequestValue Attribute exposed for management (com.sterlite.employee<type=Employeekpi><>CustomTotalRequestValue)
# TYPE com_sterlite_employee_Employeekpi_CustomTotalRequestValue untyped
com_sterlite_employee_Employeekpi_CustomTotalRequestValue 0.0
Counter,Gauge或Histogram之类的其他实际Prometheus数据类型不可用。
后来,我尝试将MBean转换为MXBean,但启动时会出现以下错误:
Caused by: javax.management.openmbean.OpenDataException: Can't map io.prometheus.client.Counter to an open data type
at com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory.makeCompositeMapping(DefaultMXBeanMappingFactory.java:459)
at com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory.makeMapping(DefaultMXBeanMappingFactory.java:293)
at com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory.mappingForType(DefaultMXBeanMappingFactory.java:258)
请帮助我解决此问题。
谢谢。