date
related_level
slug
type
relate_date
summary
status
tags
category
last_updated
Jan 18, 2026 11:29 PM
是否已更新
orginal_page
是否推荐
client_golang 库 examples 样例和 whatsup 教程简介
参考资料
examples/simple 注册一个非全局的新注册表并手动创建 GoCollector 和 NewProcessCollector
- MustRegister 方法失败时会 panic
- 最终在
/metrics暴露指标
examples/gocollector 注册一个非全局的新注册表并添加自定义 GoCollector
- 注册 Go Runtime 的指标,并额外启用了 goroutine 调度延迟(latency)直方图
- 最终在
/metrics暴露指标 - 启用
EnableOpenMetrics以 OpenMetrics 文本格式输出,以支持 exemplars
examples/versioncollector 注册 version collector 暴露 build 信息
- version collector 需要编译时注入实际值
最终在 /metrics 暴露指标
- 暴露 Gauge 类型
<program>_build_info指标,其值恒为1,但携带丰富的构建信息作为标签
examples/customlabels 在 simple 样例的基础上添加指标并附带自定义源标签
函数添加自定义 Counter 类型指标 fires_maintained_total, sparks_distributed_total, items_forged_total
- 调用函数时通过
prometheus.WrapRegistererWith(prometheus.Labels{}, reg)向 registry 添加标签
- 最终在
/metrics暴露指标
examples/middleware 在 simple 样例的基础上创建一个 http 中间件收集 HTTP 请求的数据
中间件 httpmiddleware.go 透明地收集每个 HTTP 请求的监控数据
New 函数自定义注册表和默认桶
- 为当前 handler 创建带标签的注册器
reg := prometheus.WrapRegistererWith(prometheus.Labels{"handler": handlerName}, m.registry)
定义四个指标:http_requests_total, http_request_duration_seconds, http_request_size_bytes, http_response_size_bytes
包装当前 handler
- 最终在
/metrics暴露指标 - 调用
httpmiddleware.New()创建一个自定义中间件并包装默认的promhttp.HandlerFor()
examples/createdtimestamps 注册一个非全局的新注册表并创建 Histogram 类型指标 requestDurations 记录 HTTP 请求耗时,附带 _created 时间戳
requestDurations 记录耗时
goroutine 产生模拟样本记录
- 最终在
/metrics暴露指标 - 启用
EnableOpenMetrics以 OpenMetrics 文本格式输出,以支持 exemplars - 启用
EnableOpenMetricsTextCreatedSamples在 OpenMetrics 模式下,为每个指标额外输出_created时间戳
examples/exemplars 在 createdtimestamps 样例的基础上添加 Exemplar
- 核心区别
requestDurations.(prometheus.ExemplarObserver)类型断言确保该指标支持 Exemplar 功能- Exemplar 附加
dummyID模拟 trace_id
goroutine 产生模拟样本记录
goroutine 产生附带 Exemplar 的模拟样本记录
examples/random 注册一个非全局的新注册表并模拟收集 RPC 延迟指标
- main 主程序允许动态调整模拟参数
uniformDomain,normDomain,normMean调整分布范围oscillationPeriod调整采样频率
NewMetrics(reg, *normMean, *normDomain) 函数向 registry 注册新指标 rpcDurations, rpcDurationsHistogram
- 使用 Summary 和 Histogram 两种方式记录 RPC 延迟
- 添加 go 构筑信息 collector
reg.MustRegister(collectors.NewBuildInfoCollector())
oscillationFactor 定义动态采样频率
模拟生成三种分布的 RPC 延迟指标样本
- 指标 rpcDurations 记录均匀分布
- label 值为 uniform
- 指标 rpcDurations 和 rpcDurationsHistogram 记录正态分布
- rpcDurationsHistogram 记录附带 Exemplar
- Exemplar 附加
dummyID模拟 trace_id
- 指标 rpcDurations 记录指数分布
- label 值为 exponential
- 最终在
/metrics暴露指标 - 启用
EnableOpenMetrics以 OpenMetrics 文本格式输出,以支持 Exemplar
tutorials/whatsup 启动 HTTP 服务提供一个 /whatsup 路径用于查询 Prometheus 中 up 指标并返回正在运行的实例列表
m.HandleFunc(instrumentHandlerFunc(tracer, nil, "/whatsup", whatsUpHandler(apiClient)))
whatsUpHandler 返回 http.HandlerFunc
apiClient.Query(ctx, "up", time.Now())执行 PromQL 查询
- 将
main.go修改为包含以下内容 - 指标抓取端点(Scrape Endpoint)
- 计数器(Counter):
whatsup_queries_handled_total - 仪表盘(Gauge):
whatsup_last_response_elements - 信息型仪表盘(Info Gauge):
build_info - 直方图(Histogram):
whatsup_queries_duration_seconds - 仪表盘(Gauge):
go_goroutines
- 在
tutorials/whatsup/reference里有参考答案
- Author:白鸟3
- URL:https://blog.kun2peng.top/develop/prometheus_client_golang_examples
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
