prometheus-cpp

news/2024/5/18 22:43:53 标签: prometheus

欢迎访问我的博客首页。


prometheus-cpp

  • 1. 编译安装
  • 2. 使用方法
  • 3. 参考

1. 编译安装


  根据 prometheus-cpp 的 readme 文件,需要先安装 zlib 和 libcurl。然后按照 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。

http://www.niftyadmin.cn/n/892960.html

相关文章

uboot操作环境变量的方法

新建环境变量和更改环境变量 set name value删除环境变量 set name

shell的基础编程和用法

&#xff08;1&#xff09;变量的定义和引用 :用来解析命令$:解析变量#!/bin/bash PWD"pwd/include" echo "PWD $PWD"PWD1pwd/include echo "PWD1 $PWD1"单引号和双引号的区别 (1)shell中使用字符串可以不加双引号&#xff0c;直接使用。而…

makefile目标,依赖和命令

&#xff08;1&#xff09;目标 定义&#xff1a;makefile 要生成的文件 例如:led.bin 就是目标 &#xff08;2&#xff09;依赖 定义&#xff1a;需要生成目标文件所需要的条件 例如&#xff1a;生成led.bin需要的文件有start.o led.o clock.o&#xff0c; 所用start.o led…

使用 app_process 运行 jar 包

欢迎访问我的博客首页。 使用 app_process 运行 jar 包1. 生成 jar 包3. 参考1. 生成 jar 包 app_process 可以运行一般的 jar 文件和安卓的 dex 文件。为了不依赖安卓&#xff0c;这一步我们以 jar 文件为例&#xff0c;需要 jdk 环境。假设主类名为 example&#xff0c;使用命…

makefile变量的定义和使用以及伪目标

&#xff08;1&#xff09;定义变量 Aab&#xff08;2&#xff09;使用变量 all: echo $(A) &#xff08;3&#xff09;伪目标 定义&#xff1a;伪目标的目标本身不是一个文件&#xff0c;它只是一个命令&#xff0c;一般没有依赖。例如&#xff1a;clean就是一个伪目标

makefile变量的赋值运算和环境变量

&#xff08;1&#xff09; 含义&#xff1a;对变量赋值&#xff0c;当对变量解析时&#xff0c;解析的是最后一次赋值的变量 Aab B$(A) cd Acd all: echo $(B) &#xff08;2&#xff09;: 含义&#xff1a;对变量赋值&#xff0c;当对变量解析时&#xff0c;解析的是当前赋…

特征匹配与目标检测

欢迎访问我的博客首页。 特征匹配与目标检测1. 透视变换与目标检测1.1 透视变换1.2 目标检测2. 参考目标检测应用广泛&#xff0c;但至今尚无通用的目标检测算法。因此我们可以针对特定场景设计满足需求的目标检测算法。 1. 透视变换与目标检测 1.1 透视变换 从透视变换的名字…

makefile通配符与自动变量

&#xff08;1&#xff09;自动变量 $ 规则的目标文件名 $< 规则的第一个依赖文件名 $^ 依赖的文件集合 &#xff08;2&#xff09;% 含义&#xff1a;若干个任意字符 all: 1.c 2.c 12.c text.c 1.txt echo *.c &#xff08;3&#xff09;? 含义&#xff1a;一个…