0x01 Counter
计数器是一种累积指标,表示单个单调递增计数器,其值只能在重新启动时增加或重置为零。例如,您可以使用计数器来表示所服务的请求数、已完成的任务数或错误数。
import io.prometheus.client.Counter;
class YourClass {
static final Counter requests = Counter.build()
.name("requests_total").help("Total requests.").register();
void processRequest() {
requests.inc();
// Your code here.
}
}
0x02 Gauge
Gauge是一种度量,表示可以任意上升和下降的单个数值。通常用于当前内存使用情况、cpu使用情况等测量值。
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.").register();
void processRequest() {
inprogressRequests.inc();
// Your code here.
inprogressRequests.dec();
}
}
0x03 Histogram
直方图对观察结果(通常是请求持续时间或响应大小)进行采样,并将它们计数到可配置的存储桶中。它还提供所有观察值的总和。
class YourClass {
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
Histogram.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
requestTimer.observeDuration();
}
}
}
class YourClass {
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
requestLatency.time(new Runnable() {
public abstract void run() {
// Your code here.
}
});
// Or the Java 8 lambda equivalent
requestLatency.time(() -> {
// Your code here.
});
}
}
0x04 Summary
与直方图类似,摘要对观察结果进行采样(通常是请求持续时间和响应大小等)。虽然它还提供观察总数和所有观察值的总和,但它计算滑动时间窗口上的可配置分位数。
class YourClass {
private static final Summary requestLatency = Summary.build()
.name("requests_latency_seconds")
.help("request latency in seconds")
.register();
private static final Summary receivedBytes = Summary.build()
.name("requests_size_bytes")
.help("request size in bytes")
.register();
public void processRequest(Request req) {
Summary.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
requestTimer.observeDuration();
receivedBytes.observe(req.size());
}
}
}
0x05 Label
所有metrics都可以有label,用来对相关的time series进行分组。
class YourClass {
static final Counter requests = Counter.build()
.name("my_library_requests_total").help("Total requests.")
.labelNames("method").register();
void processGetRequest() {
requests.labels("get").inc();
// Your code here.
}
}
0x06 实战
@RestController
public class OriginalWebController {
private final Counter counter;
private final Histogram histogram;
@Autowired
private WebService service;
public OriginalWebController(CollectorRegistry collectorRegistry) {
counter = Counter.build()
.name("test_prometheus_sdk_metrics1")
.help("prometheus sdk counter...")
.labelNames("language")
.register(collectorRegistry);
histogram = Histogram.build()
.name("test_prometheus_sdk_metrics2")
.help("prometheus sdk histogram...")
.labelNames("language")
.register(collectorRegistry);
}
@GetMapping("/monitor")
public String monitor(@RequestParam String language) {
// 统计次数
counter.labels(language).inc();
// 统计耗时
Histogram.Timer timer = histogram.labels(language).startTimer();
// do something...
String result = service.doMonitor(language);
timer.observeDuration();
return result;
}
}
0x07 具体的监控数据
这里以counter和histogram为例看一下具体的指标数据: