使用 Micrometer 的自定义指标在 Prometheus 端点中不可用

Custom metrics using Micrometer is not available in Prometheus endpoint

我在 Java Spring Boot 1.5 应用程序中使用 Micrometer 来抓取 Prometheus 的自定义指标。

我正在使用 "guageCollectionSize" 方法来计算 HashMap 中的条目数。当我打印 guage 值时,我得到了预期的正确输出。但问题是我无法在 /prometheus 端点中查看自定义指标。

请在下面找到代码

private Map<String, String> _strings;
private MeterRegistry registry;
_strings = new ConcurrentHashMap<String, String>();
_strings.put("Test","Test");
_strings.put("Test2","Test");
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);

registry.gaugeCollectionSize("strings_count", Tags.empty(),_strings.keySet());
System.out.println(registry.get("strings_count").gauge().value());

有人可以帮我解决这个问题吗?

正如我在 the GitHub issue 上告诉您的那样,您在项目存储库中创建了它,您正在创建自己的 PrometheusMeterRegistry 但您应该使用自动配置的。

我为它创建了 a sample that uses the auto-configured one and added a test