我想检查服务的运行状况,其中包含每个端点的指标。我的服务调用一些其他服务并接收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": {}
}
}
]
}
有关更多信息和功能,请查看此链接:
https://github.com/gocraft/health如果您遇到这个问题,因为您正在寻找暴露/health
端点,有一个即将发布的健康检查RFC: https://github.com/inadarei/rfc-healthcheck
并且有一个Go库health-go
用于暴露符合RFC的健康端点:https://github.com/nelkinda/health-go
的例子:
package main
import (
"github.com/nelkinda/health-go"
"net/http"
)
func main() {
// 1. Create the health Handler.
h := health.New(health.Health{Version: "1", ReleaseID: "1.0.0-SNAPSHOT"})
// 2. Add the handler to your mux/server.
http.HandleFunc("/health", h.Handler)
// 3. Start your server.
http.ListenAndServe(":80", nil)
}
它是可扩展的,并支持许多内置检查,如正常运行时间和sysinfo。
免责声明:我是health-go
的作者