惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
腾讯CDC
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
量子位
F
Fortinet All Blogs
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
D
Docker
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
M
MIT News - Artificial intelligence
博客园 - 【当耐特】

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周
利用promwrite对prometheus进行remote-write写入
二丫讲梵 · 2024-04-11 · via 二丫讲梵

# 前言

prometheus 大概在 2.30 版本左右的时候,增加了 remote write 的能力,这是一种有别于 exporter 暴漏指标由 prometheus 拉,以及 pushgateway 推的指标上报方式,你可以借助于这种方式上报你的指标,也可以基于此能力,将多个集群的指标汇聚到一个集群之中。

腾讯云的 cls 推出了指标主题的类型,其支持的,也正是这种 remote write 写入的方式。

# 相关资料

prometheus 官方相关文档: https://prometheus.io/docs/practices/remote_write/ (opens new window) 腾讯云指标主题的介绍:指标上报 (opens new window)

申明

原创文章eryajf,未经授权,严禁转载,侵权必究!此乃文中随机水印,敬请读者谅解。

# 实践介绍

本文实际主要想介绍的,是基于 go 语言的 remote write 上报实践,这里就结合我的个人实际体验,来进行记录。

我在实践的过程中,找到了一个比较好用的包: https://github.com/castai/promwrite (opens new window),但实际应用过程中发现,此包没有支持开启了认证的场景,而腾讯云 cls 默认是开启认证的,因此个人对此包做了一些功能扩展,提交了 pr,但是之后源仓库维护者没有理会,我就单独重置了 mod,因此接下来的示例就用我的包来做了。事实上直接看 readme 的介绍也可以了解用法。

我的地址为: https://github.com/eryajf/promwrite (opens new window)

安装包:

go get github.com/eryajf/promwrite

1

示例代码:

	client := promwrite.NewClient("http://prometheus:8428/api/v1/write")
	resp, err := client.Write(context.Background(), &promwrite.WriteRequest{
		TimeSeries: []promwrite.TimeSeries{
			{
				Labels: []promwrite.Label{
					{
						Name:  "__name__",
						Value: "my_metric_name",
					},
				},
				Sample: promwrite.Sample{
					Time:  time.Now(),
					Value: 123,
				},
			},
		},
	})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

如果你的集群包含认证,那么可以使用如下代码:

	client := promwrite.NewClient("http://prometheus:8428/api/v1/write",
		promwrite.HttpClientWithAuth(
			&http.Client{},
			&promwrite.BasicAuth{
				Username: "admin",
				Password: "xxxxxx",
			}))

	resp, err := client.Write(context.Background(), &promwrite.WriteRequest{
		TimeSeries: []promwrite.TimeSeries{
			{
				Labels: []promwrite.Label{
					{
						Name:  "__name__",
						Value: "my_metric_name",
					},
				},
				Sample: promwrite.Sample{
					Time:  time.Now(),
					Value: 123,
				},
			},
		},
	})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

其中的 __name__ 是保留字段,会自动作为该记录的的指标名。剩下的如果你需要更多的补充字段,则在 []promwrite.Label 补充即可。