简体   繁体   English

Golang:实现http服务器健康检查。 gocraft/健康

[英]Golang: implements http server health checking. 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.我的服务调用其他一些服务并接收一个 Json 代码,我用它制作模板,然后将它发送到 http.ResponseWriter。

I searched and I found this package "gocraft/health" but I didn't really understand how it works.我搜索并找到了这个包“gocraft/health”,但我并没有真正理解它是如何工作的。

Is there any other way or package to generate metrics or should I just use "gocraft/health.有没有其他方法或包来生成指标,或者我应该只使用“gocraft/health.js”吗?

Thank you in advance先感谢您

Finally, I choose "gocraft/health" , a great library.最后,我选择了“gocraft/health” ,一个很棒的库。

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.根据上面的初始化选项,您的指标以 5 分钟为一组进行聚合。 We'll keep 20 minutes worth of data in memory.我们将在内存中保留 20 分钟的数据。 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.启动应用程序后,这将通过 JSON API 公开指标。 You can browse the /health endpoint (eg, 127.0.0.1:5001/health ) to see the metrics.您可以浏览/health端点(例如, 127.0.0.1:5001/health )以查看指标。 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:有关更多信息和功能,请查看此链接:

https://github.com/gocraft/health https://github.com/gocraft/health

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如果您因为要公开/health端点而遇到此问题,则即将推出用于健康检查的 RFC: 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还有一个 Go 库health-go用于公开符合该 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 .免责声明:我是health-go的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM