Prometheus監(jiān)控業(yè)務(wù)指標


Lgging,展現(xiàn)的是應(yīng)用運行而產(chǎn)生的事件或者程序在執(zhí)行的過程中間產(chǎn)生的一些日志,可以詳細解釋系統(tǒng)的運行狀態(tài),但是存儲和查詢需要消耗大量的資源。所以往往使用過濾器減少數(shù)據(jù)量。
Metrics,是一種聚合數(shù)值,存儲空間很小,可以觀察系統(tǒng)的狀態(tài)和趨勢,但對于問題定位缺乏細節(jié)展示。這個時候使用等高線指標等多維數(shù)據(jù)結(jié)構(gòu)來增強對于細節(jié)的表現(xiàn)力。例如統(tǒng)計一個服務(wù)的 TBS 的正確率、成功率、流量等,這是常見的針對單個指標或者某一個數(shù)據(jù)庫的。
Tracing,面向的是請求,可以輕松分析出請求中異常點,但與Logging有相同的問題就是資源消耗較大。通常也需要通過采樣的方式減少數(shù)據(jù)量。比如一次請求的范圍,也就是從瀏覽器或者手機端發(fā)起的任何一次調(diào)用,一個流程化的東西,我們需要軌跡去追蹤。

{
rate(http_requests_total[5m])
node_memory_MemFree
#?HELP?go_gc_duration_seconds?A?summary?of?the?pause?duration?of?garbage?collection?cycles.
#?TYPE?go_gc_duration_seconds?summary
go_gc_duration_seconds{quantile="0"}?3.98e-05
go_gc_duration_seconds{quantile="0.25"}?5.31e-05
go_gc_duration_seconds{quantile="0.5"}?6.77e-05
go_gc_duration_seconds{quantile="0.75"}?0.0001428
go_gc_duration_seconds{quantile="1"}?0.0008099
go_gc_duration_seconds_sum?0.0114183
go_gc_duration_seconds_count?85
#?HELP?prometheus_http_response_size_bytes?Histogram?of?response?size?for?HTTP?requests.
#?TYPE?prometheus_http_response_size_bytes?histogram
prometheus_http_response_size_bytes_bucket{handler="/",le="100"}?1
prometheus_http_response_size_bytes_bucket{handler="/",le="1000"}?1
prometheus_http_response_size_bytes_bucket{handler="/",le="10000"}?1
prometheus_http_response_size_bytes_bucket{handler="/",le="100000"}?1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+06"}?1
prometheus_http_response_size_bytes_bucket{handler="/",le="+Inf"}?1
prometheus_http_response_size_bytes_sum{handler="/"}?29
prometheus_http_response_size_bytes_count{handler="/"}?1

server?:=?gin.New()
server.Use(middlewares.AccessLogger(),?middlewares.Metric(),?gin.Recovery())
server.GET("/health",?func(c?*gin.Context)?{
????c.JSON(http.StatusOK,?gin.H{
????????"message":?"ok",
????})
})
server.GET("/metrics",?Monitor)
func?Monitor(c?*gin.Context)?{
?h?:=?promhttp.Handler()
?h.ServeHTTP(c.Writer,?c.Request)
}?
var?(
?//HTTPReqDuration?metric:http_request_duration_seconds
?HTTPReqDuration?*prometheus.HistogramVec
?//HTTPReqTotal?metric:http_request_total
?HTTPReqTotal?*prometheus.CounterVec
?//?TaskRunning?metric:task_running
?TaskRunning?*prometheus.GaugeVec
)
func?init()?{
?//?監(jiān)控接口請求耗時
?//?指標類型是Histogram
?HTTPReqDuration?=?prometheus.NewHistogramVec(prometheus.HistogramOpts{
??Name:????"http_request_duration_seconds",
??Help:????"http?request?latencies?in?seconds",
??Buckets:?nil,
?},?[]string{"method",?"path"})
?//?"method"、"path"?是?label
?//?監(jiān)控接口請求次數(shù)
?//?指標類型是?Counter
?HTTPReqTotal?=?prometheus.NewCounterVec(prometheus.CounterOpts{
??Name:?"http_requests_total",
??Help:?"total?number?of?http?requests",
?},?[]string{"method",?"path",?"status"})
?//?"method"、"path"、"status"?是?label
?//?監(jiān)控當(dāng)前在執(zhí)行的task數(shù)量
?//?監(jiān)控類型是Gauge
?TaskRunning?=?prometheus.NewGaugeVec(prometheus.GaugeOpts{
??Name:?"task_running",
??Help:?"current?count??of?running?task",
?},?[]string{"type",?"state"})
?//?"type"、"state"?是?label
?prometheus.MustRegister(
??HTTPReqDuration,
??HTTPReqTotal,
??TaskRunning,
?)
}
start?:=?time.Now()
c.Next()
duration?:=?float64(time.Since(start))?/?float64(time.Second)
path?:=?c.Request.URL.Path
//?請求數(shù)加1
controllers.HTTPReqTotal.With(prometheus.Labels{
????"method":?c.Request.Method,
????"path":???path,
????"status":?strconv.Itoa(c.Writer.Status()),
}).Inc()
//??記錄本次請求處理時間
controllers.HTTPReqDuration.With(prometheus.Labels{
????"method":?c.Request.Method,
????"path":???path,
}).Observe(duration)
//?模擬新建任務(wù)
controllers.TaskRunning.With(prometheus.Labels{
????"type":??shuffle([]string{"video",?"audio"}),
????"state":?shuffle([]string{"process",?"queue"}),
}).Inc()
//?模擬任務(wù)完成
controllers.TaskRunning.With(prometheus.Labels{
????"type":??shuffle([]string{"video",?"audio"}),
????"state":?shuffle([]string{"process",?"queue"}),
}).Dec()
#?抓取間隔
scrape_interval:?5s
#?目標
scrape_configs:
??-?job_name:?'prometheus'
????static_configs:
??????-?targets:?['prometheus:9090']
??-?job_name:?'local-service'
????metrics_path:?/metrics
????static_configs:
??????-?targets:?['host.docker.internal:8000']


原文鏈接:https://www.lxkaka.wang/app-metrics/
Prometheus+Zabbix實戰(zhàn)訓(xùn)練營
馬永亮:開源技術(shù)愛好者及布道師
張士杰:十年資深架構(gòu)師

