我想检查我的服务的运行状况,了解每个端点的指标。 我的服务调用其他一些服务并接收 Json 代码,我用它制作模板,然后将其发送到 http.ResponseWriter。
我搜索了一下,发现了这个包“gocraft/health”,但我并不太明白它是如何工作的。
是否有其他方法或包来生成指标,或者我应该只使用“gocraft/health”。
提前谢谢
最佳答案
最后,我选择“gocraft/health”,一个很棒的库。
使用示例:
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/gocraft/health"
)
//should be global Var
var stream = health.NewStream()
func main() {
// Log to stdout!
stream.AddSink(&health.WriterSink{os.Stdout})
// Make sink and add it to stream
sink := health.NewJsonPollingSink(time.Minute*5, time.Minute*20)
stream.AddSink(sink)
// Start the HTTP server! This will expose metrics via a JSON API.
adr := "127.0.0.1:5001"
sink.StartServer(adr)
http.HandleFunc("/api/getVastPlayer", vastPlayer)
log.Println("Listening...")
panic(http.ListenAndServe(":2001", nil))
}
根据上述初始化选项,您的指标将以 5 分钟为单位进行聚合。我们将在内存中保留 20 分钟的数据。没有任何内容会持久保存到磁盘上。
您可以创造任意数量的就业机会
func vastPlayer(w http.ResponseWriter, r *http.Request) {
job_1 := stream.NewJob("/api/getVastPlayer")
...
...
if bol {
job_1.Complete(health.Success)
} else {
job_1.Complete(health.Error)
}
}
启动应用程序后,这将通过 JSON API 公开指标。您可以浏览 /health
端点(例如 127.0.0.1:5001/health
)来查看指标。你会得到类似这样的东西:
{
"instance_id": "sd-69536.29342",
"interval_duration": 86400000000000,
"aggregations": [
{
"interval_start": "2015-06-11T02:00:00+02:00",
"serial_number": 1340,
"jobs": {
"/api/getVastPlayer": {
"timers": {},
"events": {},
"event_errs": {},
"count": 1328,
"nanos_sum": 140160794784,
"nanos_sum_squares": 9.033775178022173E+19,
"nanos_min": 34507863,
"nanos_max": 2736850494,
"count_success": 62,
"count_validation_error": 1266,
"count_panic": 0,
"count_error": 0,
"count_junk": 0
},
"timers": {},
"events": {},
"event_errs": {}
}
}
]
}
有关更多信息和功能,请查看此链接:
关于Golang : implements http server health checking. gocraft/health,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30595236/