英文:
Golang: implements http server health checking. gocraft/health
问题
我想要检查我的服务的健康状况,并获取每个端点的指标。
我的服务调用其他一些服务,并接收一个 JSON 代码,我使用它创建模板,然后将其发送到 http.ResponseWriter。
我搜索到了这个包 "gocraft/health",但我并不真正理解它是如何工作的。
是否有其他方法或包可以生成指标,或者我应该只使用 "gocraft/health"?
提前感谢你。
英文:
I want to check the health of my service,having the metrics of each endPoint.
My service calls some other services and recieves a Json code, I make templates with it, and then I send it to a http.ResponseWriter.
I searched and I found this package "gocraft/health" but I didn't really understand how it works.
Is there any other way or package to generate metrics or should I just use "gocraft/health.
Thank you in advance
答案1
得分: 2
最后,我选择了**"gocraft/health"**,这是一个很棒的库。
使用示例:
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/gocraft/health"
)
// 应该是全局变量
var stream = health.NewStream()
func main() {
// 输出到标准输出!
stream.AddSink(&health.WriterSink{os.Stdout})
// 创建 sink 并将其添加到 stream
sink := health.NewJsonPollingSink(time.Minute*5, time.Minute*20)
stream.AddSink(sink)
// 启动 HTTP 服务器!这将通过 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 分钟的数据。数据不会被持久化到磁盘。
你可以创建任意数量的作业。
```go
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
英文:
Finally, I choose "gocraft/health", a great library.
Example of usage:
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))
}
Per the initialization options above, your metrics are aggregated in 5-minute chunks. We'll keep 20 minutes worth of data in memory. Nothing is ever persisted to disk.
You can create as many jobs as you want
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)
}
}
Once you start your app, this will expose metrics via a JSON API. You can browse the /health
endpoint (eg, 127.0.0.1:5001/health
) to see the metrics. You will get something like that:
{
"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": {}
}
}
]
}
For more information and functionality check this link:
答案2
得分: 1
如果你遇到这个问题是因为你想要暴露一个/health
端点,那么有一个即将发布的健康检查的RFC:https://github.com/inadarei/rfc-healthcheck
还有一个名为health-go
的Go库,用于按照该RFC暴露健康端点:https://github.com/nelkinda/health-go
示例:
package main
import (
"github.com/nelkinda/health-go"
"net/http"
)
func main() {
// 1. 创建健康处理程序。
h := health.New(health.Health{Version: "1", ReleaseID: "1.0.0-SNAPSHOT"})
// 2. 将处理程序添加到你的mux/server。
http.HandleFunc("/health", h.Handler)
// 3. 启动你的服务器。
http.ListenAndServe(":80", nil)
}
它是可扩展的,并支持一些内置的检查,如运行时间和系统信息。
免责声明:我是health-go
的作者。
英文:
In case you came across this question because you were looking for exposing a /health
endpoint, there is an upcoming RFC for health checks: https://github.com/inadarei/rfc-healthcheck
And there's a Go library health-go
for exposing health endpoints compliant with that RFC: https://github.com/nelkinda/health-go
Example:
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)
}
It is extensible and supports a number of built-in checks such as uptime and sysinfo.
Disclaimer: I'm the author of health-go
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论