二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • 最佳实践

  • 迎刃而解

  • Nginx

  • Php

  • Zabbix

  • AWS

  • Prometheus

    • Prometheus安装部署及简单监控
    • Prometheus邮件报警配置
    • Prometheus配置Grafana Dashboard
    • Prometheus 监控之 Redis
    • Prometheus 监控之 MySQL
    • 从CPU的获取来学习理解Prometheus查询语句
    • Prometheus监控之kafka集群
    • Prometheus监控之elasticsearch集群
    • Prometheus关于with和by的作用及用法
    • 利用promwrite对prometheus进行remote-write写入
      • 前言
      • 相关资料
      • 实践介绍
    • prometheus结合nginx-lua-prometheus监控openresty
  • Grafana

  • Loki

  • CentOS

  • Supervisord

  • Systemd

  • Docker

  • Docker-Compose

  • Rancher

  • Ansible

  • OpenLdap

  • GitLab

  • GitHub

  • Etcd

  • Consul

  • RabbitMQ

  • Kafka

  • Mysql

  • MongoDB

  • OpenVPN

  • Kvm

  • VMware

  • 配置文件详解

  • Other

  • 运维观止
  • Prometheus
二丫讲梵
2024-04-10
目录

利用promwrite对prometheus进行remote-write写入

文章发布较早,内容可能过时,阅读注意甄别。

# 前言

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,未经授权,严禁转载,侵权必究!此乃文中随机水印,敬请读者谅解。

Copyright 二丫讲梵 (opens new window) 版权所有

# 实践介绍

本文实际主要想介绍的,是基于 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 补充即可。

微信 支付宝
上次更新: 2024/09/26, 21:41:44
Prometheus关于with和by的作用及用法
prometheus结合nginx-lua-prometheus监控openresty

← Prometheus关于with和by的作用及用法 prometheus结合nginx-lua-prometheus监控openresty→

最近更新
01
理论正确,事实错误
06-21
02
学习周刊-总第216期-2025年第25周
06-20
03
睡着的人不关灯
06-12
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式