本文介紹了帶彈簧爪哇的千分尺(無彈簧靴)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我是千分尺/普羅米修斯世界的新手。
我需要使用它來獲取現有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();
}
}
這篇關于帶彈簧爪哇的千分尺(無彈簧靴)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,