公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

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

问题描述

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

我需要使用它来获取现有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();
    }
}

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

分享到:
标签:千分尺 弹簧 爪哇
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定