我正在嘗試向我的 Java 應用程式添加 Prometheus 指標匯出器。該應用程式當前javax.ws.rs用于定義 REST 端點。
例如:
Import javax.ws.rs.*;
Import javax.ws.rs.core.MediaType;
Import javax.ws.rs.core.Response;
@GET
@Path(“/example”)
@Timed
Public Response example(@QueryParam(“id”) Integer id) {
return Response.ok(“testing”)
}
我找到的在 Java 中設定 Prometheus 的所有示例都使用 Spring。他們建議如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import io.prometheus.client.exporter.HTTPServer;
import java.io.IOException;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
try {
HTTPServer server = new HTTPServer(8081);
} catch (IOException e) { e.printStackTrace(); }
}
}
有沒有一種方法可以在我當前的設定中簡單地定義一個新端點,例如:
@GET
@Path(“/metrics”)
@Timed
Public Response example {
return Response.ok(“return prom metrics here”)
}
無需引入Spring堆疊?
uj5u.com熱心網友回復:
這可以按如下方式完成:
import io.prometheus.client.Counter;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.TextFormat;
CollectorRegistry registry = new CollectorRegistry();
Counter exCounter = Counter.build().name(“example”).register(registry);
@GET
@Path(“/metrics”)
Public String getMetrics() {
Writer writer = new StringWriter();
try {
TextFormat.write004(writer, registry.metricFamilySamples());
return writer.toString();
} catch (IOException e) {
return “error”;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/352368.html
下一篇:使用聚合函式時獲取NULL值
