Go1.9接入prometheus监控小实例

package main

import (
    "bytes"
    "fmt"
    "net/http"
    "strings"
    "sync/atomic"

    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/common/expfmt"
)

type statusCollect struct {
    reqDesc       *prometheus.CounterVec
    respsizeDesc  *prometheus.Desc
    respsizevalue int64
}

func (s *statusCollect) ReqAdd(code, method string) {
    s.reqDesc.WithLabelValues(code, method).Inc()
}

func (s *statusCollect) ReqSizeAdd(size int64) {
    atomic.AddInt64(&s.respsizevalue, size)
}

func (s *statusCollect) Describe(ch chan<- *prometheus.Desc) {
    ch <- s.respsizeDesc
    s.reqDesc.Describe(ch)
}

func (s *statusCollect) Collect(ch chan<- prometheus.Metric) {
    ch <- prometheus.MustNewConstMetric(s.respsizeDesc, prometheus.CounterValue, float64(s.respsizevalue))
    s.reqDesc.Collect(ch)
}

func NewStatusCollect() *statusCollect {
    opts := prometheus.CounterOpts{Namespace: "Test", Subsystem: "http", Name: "request count", Help: "requst count"}
    return &statusCollect{
        reqDesc:      prometheus.NewCounterVec(opts, []string{"code", "method"}),
        respsizeDesc: prometheus.NewDesc("Namespace_http_respsize_count", "http respsize count", nil, nil),
    }
}

const (
    contentTypeHeader   = "Content-Type"
    contentLengthHeader = "Content-Length"
)

func main() {
    status := NewStatusCollect()
    regist := prometheus.NewRegistry()
    regist.MustRegister(status)

    http.HandleFunc("/api/metric", func(w http.ResponseWriter, r *http.Request) {
        status.ReqAdd("200", strings.ToLower(r.Method))

        entry, err := regist.Gather()
        if err != nil {
            http.Error(w, "An error has occurred during metrics collection:\n\n"+err.Error(), http.StatusInternalServerError)
            return
        }

        buf := bytes.NewBuffer(nil)
        contentType := expfmt.Negotiate(r.Header)
        enc := expfmt.NewEncoder(buf, contentType)

        for _, met := range entry {
            if err := enc.Encode(met); err != nil {
                http.Error(w, "An error has occurred during metrics encoding:\n\n"+err.Error(), http.StatusInternalServerError)
                return
            }
        }

        if buf.Len() == 0 {
            http.Error(w, "No metrics encoded, last error:\n\n"+err.Error(), http.StatusInternalServerError)
            return
        }
        status.ReqSizeAdd(int64(buf.Len()))
        header := w.Header()
        header.Set(contentTypeHeader, string(contentType))
        header.Set(contentLengthHeader, fmt.Sprint(buf.Len()))
        w.Write(buf.Bytes())
    })

    http.ListenAndServe(":1789", nil)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值