Golang prometheus展示自定义指标

huangapple go评论126阅读模式
英文:

Golang prometheus show custom metrics

问题

我可以帮你翻译这段内容。以下是翻译的结果:

如何只显示我的自定义Prometheus指标而不显示其他内容

我一直在尝试使用Prometheus来监控go server,通过以下方式:

wr.router.Handle(urlMatrix, promhttp.Handler()).Methods(http.MethodGet)

我的端点是/metrics

但是,当我访问该端点时,我收到了所有这些数据:

# HELP go_gc_duration_seconds 垃圾回收周期的暂停持续时间摘要。
# TYPE go_gc_duration_seconds 摘要
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines 当前存在的goroutine数量。
# TYPE go_goroutines gauge
go_goroutines 8
# HELP go_info 关于Go环境的信息。
# TYPE go_info gauge
go_info{version="go1.18.4"} 1
# HELP go_memstats_alloc_bytes 分配并仍在使用的字节数。
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 3.427776e+06
# HELP go_memstats_alloc_bytes_total 分配的字节数,即使已释放。
# TYPE go_memstats_alloc_bytes_total 计数器
go_memstats_alloc_bytes_total 3.427776e+06
# HELP go_memstats_buck_hash_sys_bytes 分析桶哈希表使用的字节数。
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 4805
# HELP go_memstats_frees_total 释放的总次数。
# TYPE go_memstats_frees_total 计数器
go_memstats_frees_total 0
# HELP go_memstats_gc_sys_bytes 用于垃圾回收系统元数据的字节数。
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 3.616496e+06
# HELP go_memstats_heap_alloc_bytes 分配并仍在使用的堆字节数。
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 3.427776e+06
# HELP go_memstats_heap_idle_bytes 等待使用的堆字节数。
# TYPE go_memstats_heap_idle_bytes gauge
go_memstats_heap_idle_bytes 4.5056e+06
# HELP go_memstats_heap_inuse_bytes 正在使用的堆字节数。
# TYPE go_memstats_heap_inuse_bytes gauge
go_memstats_heap_inuse_bytes 3.457024e+06
# HELP go_memstats_heap_objects 分配的对象数量。
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 17219
# HELP go_memstats_heap_released_bytes 释放给操作系统的堆字节数。
# TYPE go_memstats_heap_released_bytes gauge
go_memstats_heap_released_bytes 4.5056e+06
# HELP go_memstats_heap_sys_bytes 从系统获取的堆字节数。
# TYPE go_memstats_heap_sys_bytes gauge
go_memstats_heap_sys_bytes 7.962624e+06
# HELP go_memstats_last_gc_time_seconds 自1970年以来的最后一次垃圾回收的秒数。
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 0
# HELP go_memstats_lookups_total 指针查找的总次数。
# TYPE go_memstats_lookups_total 计数器
go_memstats_lookups_total 0
# HELP go_memstats_mallocs_total 分配的总次数。
# TYPE go_memstats_mallocs_total 计数器
go_memstats_mallocs_total 17219
# HELP go_memstats_mcache_inuse_bytes mcache结构正在使用的字节数。
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 12000
# HELP go_memstats_mcache_sys_bytes 从系统获取的用于mcache结构的字节数。
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 15600
# HELP go_memstats_mspan_inuse_bytes mspan结构正在使用的字节数。
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 46240
# HELP go_memstats_mspan_sys_bytes 从系统获取的用于mspan结构的字节数。
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 48960
# HELP go_memstats_next_gc_bytes 下次垃圾回收时的堆字节数。
# TYPE go_memstats_next_gc_bytes gauge
go_memstats_next_gc_bytes 4.194304e+06
# HELP go_memstats_other_sys_bytes 用于其他系统分配的字节数。
# TYPE go_memstats_other_sys_bytes gauge
go_memstats_other_sys_bytes 904747
# HELP go_memstats_stack_inuse_bytes 栈分配器正在使用的字节数。
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 425984
# HELP go_memstats_stack_sys_bytes 从系统获取的用于栈分配器的字节数。
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 425984
# HELP go_memstats_sys_bytes 从系统获取的字节数。
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 1.2979216e+07
# HELP go_threads 创建的操作系统线程数。
# TYPE go_threads gauge
go_threads 7
# HELP promhttp_metric_handler_requests_in_flight 当前正在提供的抓取数。
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total 按HTTP状态代码计算的抓取总数。
# TYPE promhttp_metric_handler_requests_total 计数器
promhttp_metric_handler_requests_total{code="200"} 0
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

所有这些都是Prometheus中的默认指标,我只想显示我编写的自定义Prometheus指标

var Exchange = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Name: "requests",
		Help: "显示请求ID",
	}, []string{"requests"})

func init() {
	prometheus.MustRegister(Exchange)
}

我只想显示这些指标而不显示默认的指标
`requests{requests="1111"} 1`

<details>
<summary>英文:</summary>

### how I can display just my Custom Prometheus Metrics without anything else

I have been trying to use Prometheus to monitor the `go server`
by this 
```GO
wr.router.Handle(urlMatrix, promhttp.Handler()).Methods(http.MethodGet)

my endpoint is /metrics

but when I hit the endpoint I receive all this data

# 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=&quot;0&quot;} 0
go_gc_duration_seconds{quantile=&quot;0.25&quot;} 0
go_gc_duration_seconds{quantile=&quot;0.5&quot;} 0
go_gc_duration_seconds{quantile=&quot;0.75&quot;} 0
go_gc_duration_seconds{quantile=&quot;1&quot;} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 8
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version=&quot;go1.18.4&quot;} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 3.427776e+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 3.427776e+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 4805
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 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 3.616496e+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 3.427776e+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 4.5056e+06
# 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.457024e+06
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 17219
# 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 4.5056e+06
# 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 7.962624e+06
# 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
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 17219
# 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 12000
# 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 15600
# 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 46240
# 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 48960
# 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.194304e+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 904747
# 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
# 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
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 1.2979216e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 7
# 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
# 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=&quot;200&quot;} 0
promhttp_metric_handler_requests_total{code=&quot;500&quot;} 0
promhttp_metric_handler_requests_total{code=&quot;503&quot;} 0

all of these are the default Metrics in Prometheus
and I need just to display my Custom Prometheus Metrics that I have been writing,

var Exchange = prometheus.NewCounterVec(
prometheus.CounterOpts{
	Name: &quot;requests&quot;,
	Help: &quot;show the requests id&quot;,
}, []string{&quot;requests&quot;})

func init() {
   prometheus.MustRegister(Exchange)
}

I need just to display these metrics without the default one
requests{requests=&quot;1111&quot;} 1

答案1

得分: 1

你可以替换默认的注册表,该注册表默认会被基本指标进行仪表化:

defaultRegistry := prometheus.NewRegistry()
prometheus.DefaultRegisterer = defaultRegistry
prometheus.DefaultGatherer = defaultRegistry

由于promhttp.Handler()也会添加仪表化,如果你不想要额外的指标,也可以将其替换为:

promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})

在Playground上查看完整的测试代码:https://go.dev/play/p/R2hQEg4nSf9

Playground上的示例代码只添加了一个指标:

$ curl localhost:3000/metrics 
# HELP namespace_one_counter_total 计数的事物数量。
# TYPE namespace_one_counter_total 计数器 namespace_one_counter_total 7
$
英文:

You can replace the default registry that by default gets instrumented with basic metrics:

defaultRegistry := prometheus.NewRegistry()
prometheus.DefaultRegisterer = defaultRegistry
prometheus.DefaultGatherer = defaultRegistry

Since promhttp.Handler() also adds instrumentation, if you want no additional metric, also replace it with:

promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})

See full test code on Playground: https://go.dev/play/p/R2hQEg4nSf9

That sample code on playground adds only one metric:

$ curl localhost:3000/metrics 
# HELP namespace_one_counter_total Number of things counted.
# TYPE namespace_one_counter_total counter namespace_one_counter_total 7
$

huangapple
  • 本文由 发表于 2022年7月28日 21:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/73153892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定