doumigu9594 2018-09-06 11:34
浏览 64
已采纳

普罗米修斯的理解

I would like to try simple example of using Prometheus.

  1. I have downloaded server binaries
  2. I have started simple code, but with few modifications

    var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
    func main() {
        flag.Parse()
        http.Handle("/metrics", promhttp.Handler())
        http.Handle("/test/{id}", myHandler(promhttp.Handler()))
    
        log.Fatal(http.ListenAndServe(*addr, nil))
    }
    
    func myHandler(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "hello, you've hit %s
    ", r.URL.Path)
            next.ServeHTTP(w, r)
        })
    }
    

    Questions:

    1. I assume Prometheus is monitoring tool and I would like to monitor endpoints metrics and /test/{id} separately. Did I get the idea correctly by creating several handlers and using promhttp.Handler() as middleware?
    2. What else apart of quantity and latency of requests can be monitored in e.g. simple web app with database?
  • 写回答

1条回答 默认 最新

  • dpsr1670 2018-09-06 12:04
    关注

    To follow up on @David Maze's answer, the default handler promhttp.Handler is for reporting metrics. (gathers from all registered handlers and reports them on request).

    Unfortunately, it is not a generic http middleware that gives you any metrics out of the box.

    I have seen many of go's web frameworks have some sort of community prometheus middleware (gin's) that give metrics out of the box (latency, response code, request counts, etc).

    The go prometheus client library has examples of how to add metrics to your application.

    var (
    
        httpRequests = prometheus.NewCounter(
            prometheus.CounterOpts{
                Name: "http_requests_total",
                Help: "Number of http requests.",
            },
        )
    )
    
    func init() {
        // Metrics have to be registered to be exposed:
        prometheus.MustRegister(httpRequests)
    }
    
    func myHandler(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            httpRequests.Inc()
            fmt.Fprintf(w, "hello, you've hit %s
    ", r.URL.Path)
            next.ServeHTTP(w, r)
        })
    }
    

    As for your second question lots :) Google advocates for monitoring 4 golden signals:

    https://landing.google.com/sre/book/chapters/monitoring-distributed-systems.html#xref_monitoring_golden-signals

    These are

    • Traffic - Throughput - Counts/Time
    • Latency - distribution / histogram
    • Errors - HTTP Response codes/explicit error counts
    • Saturation - resource queues ie if there is a goroutine pool how many goroutines are active at a given time

    In my experie8inces it has also been helpful to have visibility of all the interactions between your appl8ication and your database (ie the 4 golden signals applied to your database):

    • number of calls made to db from app
    • latencies of calls made
    • results (err/success) of your calls made to determine availability (success / total)
    • saturation available from your db driver (https://golang.org/pkg/database/sql/#DBStats)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站