doumaogui5937 2015-06-02 11:47 采纳率: 100%
浏览 652
已采纳

Golang:实现http服务器运行状况检查。 gocraft /健康

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条回答 默认 最新

  • doujiao1949 2015-06-11 12:53
    关注

    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:

    https://github.com/gocraft/health

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法