Spring 使用 Prometheus 的执行器,永远不会调用自定义 MetricWriter
Spring actuator with Prometheus, custom MetricWriter is never called
我想为我的 Spring 启动应用程序添加公制测量。指标将通过 grafana 显示。
我发现 article 与 prometheus 的集成 spring 执行器很好。
根据示例,我应该实现我的自定义 MetricWriter
以在 Prometheus CollectorRegistry
中更新相应的 Counter
或 Gauge
它看起来像这样:
class PrometheusMetricWriter implements MetricWriter {
CollectorRegistry registry
private final ConcurrentMap<String, Gauge> counters = new ConcurrentHashMap<>()
private final ConcurrentHashMap<String, Gauge> gauges = new ConcurrentHashMap<>()
@Autowired
PrometheusMetricWriter(CollectorRegistry registry) {
this.registry = registry
}
@Override
void increment(Delta<?> delta) {
counter(delta.name).inc(delta.value.doubleValue())
}
@Override
void reset(String metricName) {
counter(metricName).clear()
}
@Override
void set(Metric<?> value) {
println "inside metric writer"
gauge(value.name).set(value.value.doubleValue())
}
private Counter counter(String name) {
def key = sanitizeName(name)
counters.computeIfAbsent key, { k ->
Counter.build().name(k).help(k).register(registry)
}
}
private Gauge gauge(String name) {
def key = sanitizeName(name)
gauges.computeIfAbsent key, { k ->
Gauge.build().name(k).help(k).register(registry)
}
}
private String sanitizeName(String name) {
return name.replaceAll("[^a-zA-Z0-9_]", "_")
}
}
其他 3 个必需元素是:PrometheusEndpoint
、PrometheusMvcEndpoint
、PrometheusEndpointContextConfiguration
。
毕竟,可以通过调用“http://localhost:8080/prometheus”url 查看收集到的指标。
结果应该是这样的:
# HELP gauge_com_egalacoral_spark_stage_vto gauge_com_egalacoral_spark_stage_vto
# TYPE gauge_com_egalacoral_spark_stage_vto gauge
gauge_com_egalacoral_spark_stage_vto 4.0
# HELP gauge_com_egalacoral_spark_test_vfl gauge_com_egalacoral_spark_test_vfl
# TYPE gauge_com_egalacoral_spark_test_vfl gauge
gauge_com_egalacoral_spark_test_vfl 16.0
但是,我收到一个空白页面。
这是用于收集一些指标的测试 class。
class Runner {
@Autowired
GaugeService gaugeService
private static final Logger logger = LoggerFactory.getLogger(Runner.class)
private ZoneId zoneId = ZoneId.of('Europe/London')
private Integer delay = 10
@Scheduled(fixedDelay = 10000L)
void runOnTest() throws Exception {
def now = ZonedDateTime.now(zoneId)
println "collect metrics"
gaugeService.submit("test", 2.0)
}
}
P.S: 我注意到 GaugeService
有几个实现,在我的应用程序中是 DropwizardMetricServices
。此实现已包含它自己的指标注册表,用于收集我的指标。
因此,当我调用“/prometheus”端点时,我得到一个空页面,因为 CollectorRegistry' in my custom
PrometheusMetricWriter` 从未填充我的指标。
请告诉我如何解决这个问题?
所以,我找到了使用 GaugeService
的 DropwizardMetricServices
实现的原因。
根据 spring 文档:
50.10 Dropwizard Metrics
A default MetricRegistry Spring bean will be created when you declare a dependency to the
io.dropwizard.metrics:metrics-core library; you can also register you
own @Bean instance if you need customizations. Users of the Dropwizard
‘Metrics’ library will find that Spring Boot metrics are automatically
published to com.codahale.metrics.MetricRegistry. Metrics from the
MetricRegistry are also automatically exposed via the /metrics
endpoint
When Dropwizard metrics are in use, the default CounterService and
GaugeService are replaced with a DropwizardMetricServices, which is a
wrapper around the MetricRegistry (so you can @Autowired one of those
services and use it as normal). You can also create “special”
Dropwizard metrics by prefixing your metric names with the appropriate
type (i.e. timer., histogram. for gauges, and meter.* for counters).
不知何故 io.dropwizard.metrics:metrics-core 库被包含到项目依赖项中。
之后,我删除它,一切都开始按预期工作。
我想为我的 Spring 启动应用程序添加公制测量。指标将通过 grafana 显示。
我发现 article 与 prometheus 的集成 spring 执行器很好。
根据示例,我应该实现我的自定义 MetricWriter
以在 Prometheus CollectorRegistry
中更新相应的 Counter
或 Gauge
它看起来像这样:
class PrometheusMetricWriter implements MetricWriter {
CollectorRegistry registry
private final ConcurrentMap<String, Gauge> counters = new ConcurrentHashMap<>()
private final ConcurrentHashMap<String, Gauge> gauges = new ConcurrentHashMap<>()
@Autowired
PrometheusMetricWriter(CollectorRegistry registry) {
this.registry = registry
}
@Override
void increment(Delta<?> delta) {
counter(delta.name).inc(delta.value.doubleValue())
}
@Override
void reset(String metricName) {
counter(metricName).clear()
}
@Override
void set(Metric<?> value) {
println "inside metric writer"
gauge(value.name).set(value.value.doubleValue())
}
private Counter counter(String name) {
def key = sanitizeName(name)
counters.computeIfAbsent key, { k ->
Counter.build().name(k).help(k).register(registry)
}
}
private Gauge gauge(String name) {
def key = sanitizeName(name)
gauges.computeIfAbsent key, { k ->
Gauge.build().name(k).help(k).register(registry)
}
}
private String sanitizeName(String name) {
return name.replaceAll("[^a-zA-Z0-9_]", "_")
}
}
其他 3 个必需元素是:PrometheusEndpoint
、PrometheusMvcEndpoint
、PrometheusEndpointContextConfiguration
。
毕竟,可以通过调用“http://localhost:8080/prometheus”url 查看收集到的指标。
结果应该是这样的:
# HELP gauge_com_egalacoral_spark_stage_vto gauge_com_egalacoral_spark_stage_vto
# TYPE gauge_com_egalacoral_spark_stage_vto gauge
gauge_com_egalacoral_spark_stage_vto 4.0
# HELP gauge_com_egalacoral_spark_test_vfl gauge_com_egalacoral_spark_test_vfl
# TYPE gauge_com_egalacoral_spark_test_vfl gauge
gauge_com_egalacoral_spark_test_vfl 16.0
但是,我收到一个空白页面。 这是用于收集一些指标的测试 class。
class Runner {
@Autowired
GaugeService gaugeService
private static final Logger logger = LoggerFactory.getLogger(Runner.class)
private ZoneId zoneId = ZoneId.of('Europe/London')
private Integer delay = 10
@Scheduled(fixedDelay = 10000L)
void runOnTest() throws Exception {
def now = ZonedDateTime.now(zoneId)
println "collect metrics"
gaugeService.submit("test", 2.0)
}
}
P.S: 我注意到 GaugeService
有几个实现,在我的应用程序中是 DropwizardMetricServices
。此实现已包含它自己的指标注册表,用于收集我的指标。
因此,当我调用“/prometheus”端点时,我得到一个空页面,因为 CollectorRegistry' in my custom
PrometheusMetricWriter` 从未填充我的指标。
请告诉我如何解决这个问题?
所以,我找到了使用 GaugeService
的 DropwizardMetricServices
实现的原因。
根据 spring 文档:
50.10 Dropwizard Metrics
A default MetricRegistry Spring bean will be created when you declare a dependency to the io.dropwizard.metrics:metrics-core library; you can also register you own @Bean instance if you need customizations. Users of the Dropwizard ‘Metrics’ library will find that Spring Boot metrics are automatically published to com.codahale.metrics.MetricRegistry. Metrics from the MetricRegistry are also automatically exposed via the /metrics endpoint
When Dropwizard metrics are in use, the default CounterService and GaugeService are replaced with a DropwizardMetricServices, which is a wrapper around the MetricRegistry (so you can @Autowired one of those services and use it as normal). You can also create “special” Dropwizard metrics by prefixing your metric names with the appropriate type (i.e. timer., histogram. for gauges, and meter.* for counters).
不知何故 io.dropwizard.metrics:metrics-core 库被包含到项目依赖项中。 之后,我删除它,一切都开始按预期工作。