当前位置: 首页 > ops >正文

c++ 项目使用 prometheus + grafana 进行实时监控

监控指标设置

使用 prometheus-cpp 进行监控指标设置,这里以 mqtt-server 的活跃连接数为例,统计四种协议类型的活跃连接数,在接收到连接进行连接数加一,连接断开时进行连接数减一,使用 gauge 类型

#pragma once#include <unordered_map>#include "metrics/IMetrics.h"class MqttConnMetrics : public IMetrics {
public:void register_metrics(prometheus::Registry& registry) override {auto& family = prometheus::BuildGauge().Name("mqtt_active_connections").Help("Number of active MQTT connections").Register(registry);protocol_gauges_ = {{MQTT_PROTOCOL::MQTT, &family.Add({{"protocol", "mqtt"}})},{MQTT_PROTOCOL::MQTTS, &family.Add({{"protocol", "mqtts"}})},{MQTT_PROTOCOL::WS, &family.Add({{"protocol", "ws"}})},{MQTT_PROTOCOL::WSS, &family.Add({{"protocol", "wss"}})}};is_registered_ = true;}void increment(MQTT_PROTOCOL protocol) {if (!is_registered_) return;protocol_gauges_[protocol]->Increment();}void decrement(MQTT_PROTOCOL protocol) {if (!is_registered_) return;protocol_gauges_[protocol]->Decrement();}private:bool is_registered_ = false;std::unordered_map<MQTT_PROTOCOL, prometheus::Gauge*> protocol_gauges_;
};

开发完成后,可以通过 http://<address>:<port>/metrics 路径访问到所有的监控指标

prometheus 采集监控指标

安装好 prometheus 之后修改一下 prometheus.yml 配置中 采集的目的地址

# my global config
global:scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:# - "first_rules.yml"# - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: "mqtt-server"scrape_interval: 1s# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:8085"]

启动后访问 prometheus 界面,检查是否成功采集到,默认地址 http://localhost:9090

grafana 展示监控数据

安装完 grafana 之后,进入界面添加 prometheus 数据源,然后新增仪表盘,配置监控指标等

http://www.xdnf.cn/news/14264.html

相关文章:

  • 安卓9.0系统修改定制化____默认开启 开发者选项中的OEM锁解锁选项 开搞篇 五
  • Ubuntu安装Gym及其仿真
  • 基于51单片机的污水ph值和液压监测系统
  • 关于MCU、MPU、SoC、DSP四大类型芯片
  • Python学习小结
  • 山东大学项目实训——基于DeepSeek的智能写作与训练平台(十四)
  • 智能语音交互技术深度解析:从原理到产业实践
  • 访问vLLM启动的大模型,报错The model `XXX/XXX` does not exist
  • 嵌入式开发--汇川伺服干扰造成FDCAN模块错误过多导致死机
  • 芯片测试之 trim修调测试详解
  • AI结构化数据:智能聊天与传统开发的结合
  • Java大模型开发入门 (6/15):对话的灵魂 - 深入理解LangChain4j中的模型、提示和解析器
  • 从0到1掌握数据库安全:用户认证与授权的深度实践
  • ToonMe:照片变卡通,创意无限
  • Java大模型开发入门 (8/15):连接外部世界(上) - RAG入门与文档加载
  • <10>-MySQL索引特性
  • 华为云Flexus+DeepSeek征文|基于华为云Flexus云服务的Dify一键部署
  • HTML+CSS 登陆框动态切换
  • NGINX 四层上游健康检查模块实战`ngx_stream_upstream_hc_module`
  • 会计 - 财务报告
  • 力扣 2616. 最小化数对的最大差值 题解
  • cpu微码大全 微码添加工具 八九代cpu针脚屏蔽图
  • c++ 右值引用移动构造函数
  • 功能安全实战系列10-英飞凌TC3xx_SRI总线监控开发
  • 动态代理选择:JDK vs CGLIB
  • 2.6 激光雷达消息格式
  • ESP32开发-ESP32P4环境配置
  • 【AD笔记】嘉立创元件导入到AD中(原理图-pcd-3D模型)
  • std::ifstream file(filename);详细解释
  • 十字滑台是否可以进行自动化控制?