带弹簧爪哇的千分尺(无弹簧靴)

Micrometer metrics with spring java (NO spring boot)(带弹簧爪哇的千分尺(无弹簧靴))

本文介绍了带弹簧爪哇的千分尺(无弹簧靴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是千分尺/普罗米修斯世界的新手。

我需要使用它来获取现有Spring应用程序的一些指标。没有弹簧靴,只有普通的弹簧爪哇。

我在注册时遇到了很多问题,例如,为了在一些我想要有指标的终结点上实现计数器或计时器,以及将其暴露给普罗米修斯进行抓取。

你能就这个话题给我一些建议吗?

谢谢。

我已尝试注册MeterRegistry的Bean,但似乎无法为PrometheusMeterRegestry或PrometheuConfig注册该Bean。

另外,仅从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();
    }
}

这篇关于带弹簧爪哇的千分尺(无弹簧靴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带弹簧爪哇的千分尺(无弹簧靴)

基础教程推荐