gpt4 book ai didi

java - 使用 spring java 进行微米度量(无 spring boot)

转载 作者:行者123 更新时间:2023-12-02 10:04:45 24 4
gpt4 key购买 nike

我是 Micrometer/Prometheus 世界的新手。

我需要使用它来获取现有 Spring 应用程序的一些指标。没有 Spring Boot,只有普通的 Spring Java。

我在注册时遇到了很多麻烦,例如MeterRegistry,以便在我想要获取指标的某些端点上实现计数器或计时器,并将其暴露给普罗米修斯进行抓取。

您能给我一些关于这个主题的建议吗?

谢谢。

我尝试注册 MeterRegistry 的 bean,但似乎无法为 PrometheusMeterRegestry 或 PrometheuConfig 执行此操作。

此外,仅从 spring 公开 localhost:8080/metrics 端点似乎没有向 Prometheus 服务器提供任何内容。

我希望能够从我的应用程序中获取一些简单的指标,例如某些端点被调用了多少次以及某些端点完成作业或某些数据处理需要多长时间。

最佳答案

在 pom.xml 中添加以下依赖项:

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.2.0</version>
</dependency>

这是一个简单的测试类:

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Tags;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.junit.jupiter.api.Test;

public class MicrometerTest {

private PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);

@Test
public void test() {}

// @Test
public void test_metrics() {
Counter.builder("http_requests_total").description("Http Request Total").tags("method", "GET", "handler",
"/employee", "status", "200").register(registry).increment();
Counter.builder("http_requests_total").description("Http Request Total").tags("method", "GET", "handler",
"/employee", "status", "200").register(registry).increment();

DistributionSummary.builder("http_response_time_milliseconds").description("Request completed time in milliseconds")
.tags("method", "GET", "handler", "/employee", "status", "200")
.publishPercentiles(.5,.95,.99)
.register(registry).record(40d);
DistributionSummary.builder("http_response_time_milliseconds").description("Request completed time in milliseconds")
.tags("method", "GET", "handler", "/employee", "status", "200")
.publishPercentiles(.5,.95,.99)
.register(registry).record(50d);

registry.counter("http_requests_total2", "method", "GET", "status", "200").increment();
registry.counter("http_requests_total2", "method", "Post", "status", "200").increment();
registry.counter("http_requests_total2", "method", "GET", "status", "200").increment();

registry.newCounter(new Meter.Id("query time", Tags.of("select query", "country"), null, "query desc", Meter.Type.COUNTER));
System.out.println(registry.scrape());
}
}

如果您想通过 servlet 公开它,请添加另一个依赖项:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>

用于公开指标的示例 servlet:

import com.aeris.amp.metrics.util.MeterRegistry;
import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;

@WebServlet("/metrics")
public class MetricsServlet extends HttpServlet {

public static PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);

public void init() {
new JvmThreadMetrics().bindTo(registry);
new JvmGcMetrics().bindTo(registry);
new JvmMemoryMetrics().bindTo(registry);
new DiskSpaceMetrics(new File("/")).bindTo(registry);
new ProcessorMetrics().bindTo(registry); // metrics related to the CPU stats
new UptimeMetrics().bindTo(registry);
}

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {

resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(TextFormat.CONTENT_TYPE_004);

Writer writer = resp.getWriter();
try {
registry.scrape(writer);
writer.flush();
} finally {
writer.close();
}

}

public static String getMetricsString() {
if (registry == null)
registry = MeterRegistry.registry;
return registry.scrape();
}
}

关于java - 使用 spring java 进行微米度量(无 spring boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55395769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com