prometheus-cpp

欢迎访问我的博客首页。


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 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

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.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。

你可能感兴趣的:(SLAM,prometheus)