prometheus-cpp

欢迎访问我的博客首页


1. 编译安装


  根据 prometheus-cpp 的 readme 文件,需要先安装 zliblibcurl。然后按照 cartographer 提供的脚本下载 prometheus-cpp。

COMMIT="4e0814ee3f93b796356a51a4795a332568940a72"

git clone https://github.com/jupp0r/prometheus-cpp.git
cd prometheus-cpp
git checkout ${COMMIT}
git submodule update --init

2. 使用方法


  prometheus 定义了 4 种指标类型(metric type):计数器(counter)、仪表盘(gauge)、直方图(histogram)、摘要(summary)。它会以这 4 种形式统计程序信息,然后在浏览器中呈现。下面是计数器的使用示例。

#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>

#include <array>
#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>

int main() {
	using namespace prometheus;

	// create an http server running on port 8080
	Exposer exposer{ "127.0.0.1:8080" };

	// create a metrics registry
	// @note it's the users responsibility to keep the object alive
	auto registry = std::make_shared<Registry>();

	// add a new counter family to the registry (families combine values with the
	// same name, but distinct label dimensions)
	//
	// @note please follow the metric-naming best-practices:
	// https://prometheus.io/docs/practices/naming/
	auto& packet_counter = BuildCounter()
		.Name("observed_packets_total")
		.Help("Number of observed packets")
		.Register(*registry);

	// add and remember dimensional data, incrementing those is very cheap
	auto& tcp_rx_counter =
		packet_counter.Add({ {"protocol", "tcp"}, {"direction", "rx"} });
	auto& tcp_tx_counter =
		packet_counter.Add({ {"protocol", "tcp"}, {"direction", "tx"} });
	auto& udp_rx_counter =
		packet_counter.Add({ {"protocol", "udp"}, {"direction", "rx"} });
	auto& udp_tx_counter =
		packet_counter.Add({ {"protocol", "udp"}, {"direction", "tx"} });

	// add a counter whose dimensional data is not known at compile time
	// nevertheless dimensional values should only occur in low cardinality:
	// https://prometheus.io/docs/practices/naming/#labels
	auto& http_requests_counter = BuildCounter()
		.Name("http_requests_total")
		.Help("Number of HTTP requests")
		.Register(*registry);

	// ask the exposer to scrape the registry on incoming HTTP requests
	exposer.RegisterCollectable(registry);

	for (;;) {
		std::this_thread::sleep_for(std::chrono::seconds(1));
		const auto random_value = std::rand();

		if (random_value & 1) tcp_rx_counter.Increment();
		if (random_value & 2) tcp_tx_counter.Increment();
		if (random_value & 4) udp_rx_counter.Increment();
		if (random_value & 8) udp_tx_counter.Increment();

		const std::array<std::string, 4> methods = { "GET", "PUT", "POST", "HEAD" };
		auto method = methods.at(random_value % methods.size());
		// dynamically calling Family<T>.Add() works but is slow and should be
		// avoided
		http_requests_counter.Add({ {"method", method} }).Increment();
	}
	return 0;
}

  上面的程序指定了端口号为 127.0.0.1:8080,在浏览器地址栏输入 http://127.0.0.1:8080/metrics 就可以看到 prometheus 的监控信息。

  下面是配置文件:

cmake_minimum_required(VERSION 3.5.1)
project(demo)

# 1. 查找依赖。
find_package(prometheus-cpp)

# 2. 设置包含目录和库目录。
include_directories(
    ${prometheus-cpp_INCLUDE_DIR}
)
link_directories(
    D:/MinGW/libraries/prometheus/lib
)

# 3. 生成可执行程序。
add_executable(main
    main.cc
)
target_link_libraries(main
    prometheus-cpp-core
    prometheus-cpp-pull
    prometheus-cpp-push
)

3. 参考


  1. 官方文档
  2. prometheus 架构和四种指标类型,CSDN,2021。
  3. prometheus 四种指标类型,CSDN,2021。
  4. prometheus 入门终极指南,知乎专栏,2021。
  5. 例子,github。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
prometheus-webhook-dingtalk 是一个用于将 Prometheus 监控告警消息发送到钉钉的 Webhook 工具。它可以帮助你将 Prometheus 监控告警通过钉钉机器人发送到指定的群组或用户。 你可以通过以下步骤来配置和使用 prometheus-webhook-dingtalk: 1. 安装 prometheus-webhook-dingtalk:你可以使用 Go 工具链来安装 prometheus-webhook-dingtalk,运行以下命令: ``` go get github.com/timonwong/prometheus-webhook-dingtalk/cmd/dingtalk ``` 2. 创建钉钉机器人:在钉钉中创建一个自定义机器人,并获取到它的 Webhook 地址,用于将告警消息发送到指定的群组或用户。 3. 创建配置文件:在 prometheus-webhook-dingtalk 的配置文件中,你需要指定钉钉机器人的 Webhook 地址以及其他相关参数。你可以创建一个名为 config.yml 的配置文件,并将以下示例内容填入: ```yaml listen: 0.0.0.0:8060 dingtalk: webhook: https://oapi.dingtalk.com/robot/send?access_token=your_webhook_token ``` 4. 启动 prometheus-webhook-dingtalk:运行以下命令来启动 prometheus-webhook-dingtalk: ``` dingtalk -config.file=config.yml ``` 5. 配置 Prometheus:在 Prometheus 的配置文件中,添加以下内容来指定告警消息的接收端: ```yaml receivers: - name: 'dingtalk' webhook_configs: - url: 'http://prometheus-webhook-dingtalk:8060/dingtalk/webhook' ``` 6. 重新启动 Prometheus:确保 Prometheus 已经重新加载了配置文件,并重启 Prometheus 服务。 现在,当 Prometheus 监控触发告警时,prometheus-webhook-dingtalk 将会将告警消息发送到钉钉机器人的 Webhook 地址,从而通知到指定的群组或用户。 请注意,以上步骤仅为一般示例,实际操作可能会因环境和需求而有所不
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值