使用 Go 语言编写 HTTP 服务器并进行指标检测
在本教程中,我们将创建一个简单的 Go HTTP 服务器,并通过添加计数器 (Counter) 指标来记录服务器处理的请求总数,从而完成对其的指标检测。
这里我们有一个简单的 HTTP 服务器,其中包含一个 /ping 端点,该端点返回 pong 作为响应。
package main
import (
"fmt"
"net/http"
)
func ping(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w,"pong")
}
func main() {
http.HandleFunc("/ping", ping)
http.ListenAndServe(":8090", nil)
}
编译并运行服务器
go build server.go
./server
现在在浏览器中打开 https://:8090/ping,你应该能看到 pong。
现在让我们向服务器添加一个指标,用于统计对 ping 端点的请求数量。计数器 (Counter) 指标类型非常适合此场景,因为我们知道请求次数只会增加而不会减少。
创建 Prometheus 计数器
type metrics struct {
pingCounter prometheus.Counter
}
func newMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
pingCounter: promauto.With(reg).NewCounter(
prometheus.CounterOpts {
Name: "ping_request_count",
Help: "No of requests handled by Ping handler",
}),
}
return m
}
接下来,我们更新 ping 处理程序,使用 metrics.pingCounter.Inc() 来增加计数器的数值。
func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
m.pingCounter.Inc()
fmt.Fprintf(w, "pong")
}
}
然后,使用 Prometheus 注册表 (Registry) 注册指标(在本例中仅有一个计数器),并对外公开这些指标。
func main() {
reg := prometheus.NewRegistry()
m := newMetrics(reg)
http.HandleFunc("/ping", ping(m))
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
http.ListenAndServe(":8090", nil)
}
prometheus.MustRegister 函数会将 pingCounter 注册到默认注册表中。为了公开指标,Go Prometheus 客户端库提供了 promhttp 包。promhttp.Handler() 提供了一个 http.Handler,用于公开注册在默认注册表中的指标。
现在的示例代码如下:
package main
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type metrics struct {
pingCounter prometheus.Counter
}
func newMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
pingCounter: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "ping_request_count",
Help: "No of request handled by Ping handler",
}),
}
return m
}
func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
m.pingCounter.Inc()
fmt.Fprintf(w, "pong")
}
}
func main() {
reg := prometheus.NewRegistry()
m := newMetrics(reg)
http.HandleFunc("/ping", ping(m))
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
http.ListenAndServe(":8090", nil)
}
运行示例
go mod init prom_example
go mod tidy
go run server.go
现在多次访问 localhost:8090/ping 端点,然后向 localhost:8090/metrics 发送请求以查看指标。
在这里,ping_request_count 显示 /ping 端点被调用了 3 次。
默认注册表中自带了用于收集 Go 运行时指标的采集器,这就是为什么我们能看到诸如 go_threads、go_goroutines 等其他指标的原因。
我们已经构建了第一个指标导出器。让我们更新 Prometheus 配置以抓取服务器上的指标。
global:
scrape_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ["localhost:9090"]
- job_name: simple_server
static_configs:
- targets: ["localhost:8090"]
prometheus --config.file=prometheus.yml

