簡體   English   中英

在golang的單獨端口中公開Prometheus指標

[英]Expose Prometheus metrics in a separate port in golang

在運行gin-gonic的微服務中,我試圖在路由器中收集指標並將其暴露在第二個指標中,但這似乎是不可能的。 有沒有人有經驗?

我發現的所有文檔都將Prometheus處理程序設置在同一應用程序路由器中。

預期結果

參見promhttp_metric_handler_requests_total{code="200"} ,對於:8000/test路由的每次訪問均增加

實際結果

promhttp_metric_handler_requests_total{code="200"}僅在每次訪問:7100/metrics路由時增加

范例程式碼

(以下是可以解釋該問題的完整應用程序)

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/zsais/go-gin-prometheus"
    "net/http"
)

func main() {
    router1 := gin.New()
    router1.GET("/test", func(c *gin.Context) {
        c.String(http.StatusOK, "it works")
    })

    p := ginprometheus.NewPrometheus("test")
    router1.Use(p.HandlerFunc())

    router2 := gin.New()
    p.SetMetricsPath(router2)

    go func() { router2.Run(":7100") }()
    router1.Run(":8000")
}

結果

加載幾次后,前面的代碼的結果如下:8000/test端點:8000/test如下,您可以看到名稱空間test統計信息為“空”

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0.0"} 0.0
go_gc_duration_seconds{quantile="0.25"} 0.0
go_gc_duration_seconds{quantile="0.5"} 0.0
go_gc_duration_seconds{quantile="0.75"} 0.0
go_gc_duration_seconds{quantile="1.0"} 0.0
go_gc_duration_seconds_sum 0.0
go_gc_duration_seconds_count 0.0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 10.0
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.11.2"} 1.0
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.043376e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 2.043376e+06
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.443509e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 374.0
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 0.0
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 2.234368e+06
# HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 2.043376e+06
# HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE go_memstats_heap_idle_bytes gauge
go_memstats_heap_idle_bytes 6.3578112e+07
# HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE go_memstats_heap_inuse_bytes gauge
go_memstats_heap_inuse_bytes 3.104768e+06
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 6219.0
# HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
# TYPE go_memstats_heap_released_bytes gauge
go_memstats_heap_released_bytes 8192.0
# HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE go_memstats_heap_sys_bytes gauge
go_memstats_heap_sys_bytes 6.668288e+07
# HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 0.0
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0.0
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 6593.0
# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 6912.0
# HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 16384.0
# HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 33592.0
# HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 49152.0
# HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE go_memstats_next_gc_bytes gauge
go_memstats_next_gc_bytes 4.473924e+06
# HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE go_memstats_other_sys_bytes gauge
go_memstats_other_sys_bytes 1.038915e+06
# HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 425984.0
# HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 425984.0
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 7.1891192e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 7.0
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1.0
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 1.0
promhttp_metric_handler_requests_total{code="500"} 0.0
promhttp_metric_handler_requests_total{code="503"} 0.0
# HELP test_request_duration_seconds The HTTP request latencies in seconds.
# TYPE test_request_duration_seconds summary
test_request_duration_seconds{quantile="0.5"} NaN
test_request_duration_seconds{quantile="0.9"} NaN
test_request_duration_seconds{quantile="0.99"} NaN
test_request_duration_seconds_sum 0.0
test_request_duration_seconds_count 0.0
# HELP test_request_size_bytes The HTTP request sizes in bytes.
# TYPE test_request_size_bytes summary
test_request_size_bytes{quantile="0.5"} NaN
test_request_size_bytes{quantile="0.9"} NaN
test_request_size_bytes{quantile="0.99"} NaN
test_request_size_bytes_sum 0.0
test_request_size_bytes_count 0.0
# HELP test_response_size_bytes The HTTP response sizes in bytes.
# TYPE test_response_size_bytes summary
test_response_size_bytes{quantile="0.5"} NaN
test_response_size_bytes{quantile="0.9"} NaN
test_response_size_bytes{quantile="0.99"} NaN
test_response_size_bytes_sum 0.0
test_response_size_bytes_count 0.0

Prometheus使用promhttp_metric_XXXXX作為數據,它收集有關自身的數據, 這里

正如您可以從http:// localhost:9090 / metrics收集的那樣,Prometheus導出的關於其自身的一個度量稱為promhttp_metric_handler_requests_total(Prometheus服務器已處理的/ metrics請求總數)。

您所尋找的稱為gin_requests_total ,以10請求為例:

gin_requests_total{code="200",handler="main.main.func1",host="localhost:8000",method="GET",url="/test"} 10

檢查p.SetListenAddressWithRouter()是否使用單獨的路由器公開指標。

編輯我看到您將子系統命名為"test" 因此,您應該尋找test_XXXXX ,確切地說是test_requests_total

多虧了所有嘗試尋找解決方案的人,我才發現我在路由之后注冊了中間件:這樣,在中間件之前聲明的路由不會被同一中間件“覆蓋”。

該代碼正常工作:

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/zsais/go-gin-prometheus"
    "net/http"
)

func main() {
    router1 := gin.New()
    router2 := gin.New()

    p := ginprometheus.NewPrometheus("test")
    router1.Use(p.HandlerFunc())
    p.SetMetricsPath(router2)

    router1.GET("/test", func(c *gin.Context) {
        c.String(http.StatusOK, "it works")
    })

    go func() { router2.Run(":7100") }()
    router1.Run(":8000")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM