$curlhttp://localhost:8080/metrics# HELP requests_total Total requests.# TYPE requests_total counterrequests_total{label="trace",}0.0requests_total{label="error",}1.0
importjavax.annotation.PostConstruct;importorg.springframework.stereotype.Component;importio.prometheus.client.hotspot.DefaultExports;importlombok.extern.slf4j.Slf4j;@Component@Slf4jpublicclassPrometheusConfig { @PostConstructpublicvoidinitialize() {log.info("prometheus init...");DefaultExports.initialize();log.info("prometheus has been initialized..."); }}
# HELP logback_appender_total Logback log statements at various log levels
# TYPE logback_appender_total counter
logback_appender_total{level="debug",} 0.0
logback_appender_total{level="warn",} 0.0
logback_appender_total{level="trace",} 0.0
logback_appender_total{level="error",} 1.0
logback_appender_total{level="info",} 15.0
3. 其他
Guava cache
Hibernate SessionFactory
Jetty server metrics
自定义Collectors
有时无法直接检测代码,因为它不在您的控制范围内。这要求您从其他系统代理指标
继承Collector实现collect方法
publicclassYourCustomCollectorextendsCollector{//每次都会执行这个方法 @OverridepublicList<MetricFamilySamples> collect() {List<MetricFamilySamples> mfs =newArrayList<MetricFamilySamples>();// With no labels.mfs.add(newGaugeMetricFamily("my_gauge","help",42));// With labelsGaugeMetricFamily labeledGauge =newGaugeMetricFamily("my_other_gauge","help",Arrays.asList("labelname"));labeledGauge.addMetric(Arrays.asList("foo"),4);labeledGauge.addMetric(Arrays.asList("bar"),5);mfs.add(labeledGauge);return mfs; }}