Repository: robustmq/robustmq (language: rust)

Issue:
[RBIP][Docs][Subtask] Prepare a design plan for metric integration.
### Have you checked the documentation for submitting an Issue?

- [x] Yes.

### Search before asking

- [x] I have searched in the [issues](https://github.com/robustmq/robustmq/issues) and found no similar issues.


### Describe the subtask

We need to have a comprehensive plan to complete the collection of the metric part, which will help us to implement other related metrics in the future.

### Parent issue

#775 

### Are you willing to submit PR?

- [ ] Yes. I would be willing to submit a PR with guidance from the RobustMQ community to improve.
- [x] No. I cannot submit a PR at this time.


Discussion / hints:

Fix patch:
diff --git a/docs/.design/logic/Integrate the metrics module.md b/docs/.design/logic/Integrate the metrics module.md
new file mode 100644
index 000000000..134b8c0de
--- /dev/null
+++ b/docs/.design/logic/Integrate the metrics module.md
@@ -0,0 +1,88 @@
+# 背景介绍
+对于集成指标的背景就不赘述，这里给的背景是关于otel和prometheus的，
+根据调研之后发现，可观测部分实现otel部分对于metrics部分还属于实验阶段，
+由于otel是后续可观测性的标准协议，因此对于trace部分和log部分的集成，
+依然会采用其rust的实现，但是对于metrics部分，业界通常使用的是prometheus，
+并且在之前也有过open metrics的标准，最终发展后并入了prometheus当中，
+因此在选择上，我们可以直接选择prometheus，总结一下相关选择理由：
+1. prometheus的open metrics是业界对于metrics的一个事实标准
+2. otel在rust这块儿还处于发展阶段，同时对于metrics指标最终也是集成到
+   prometheus作为后端居多，不如一步到位
+3. 在[prometheus3.0](https://github.com/prometheus/prometheus/releases/tag/v3.0.0)版本
+上也能够对otel进行支持，即使后期有统一使用otel的rust库需求，依然可以
+转换到otel库上
+
+# 集成步骤
+1. 选择prometheus库
+
+由于prometheus前期并没有官方库进行支持，所以项目内部现在使用的是另一个
+非官方库，虽然在功能上可能有些多，但是已经停止了维护[is this project still maintained](https://github.com/tikv/rust-prometheus/issues/530)
+所以在这边实现上会采用[官方库](https://github.com/prometheus/client_rust)进行实现
+
+2. 编写对应指标内容
+
+对于指标编写这块儿，databend具有相应的metrics编写实现，所以最初编写实现
+会参考databend的编写方式，集成到当前的实现当中
+
+# 具体实现
+
+1. 首先构建全局的mtrics注册对象，用于后续进行指标内容的注册
+```rust
+pub static REGISTRY: LazyLock<Mutex<Registry>> = LazyLock::new(|| Mutex::new(Registry::default()));
+
+pub fn load_global_registry() -> MutexGuard<'static, Registry> {
+    REGISTRY.lock().unwrap()
+}
+```
+2. 构建指标存放集合
+```rust
+struct ConnectionJitterLabels {
+    client_id: String,
+}
+
+struct ServerMetrics {
+   connect_jitter_counter: Family<ConnectionJitterLabels, Counter>,
+}
+
+```
+3. 设定初始化方法等
+
+```rust
+static SERVER_METRICS: LazyLock<ServerMetrics> = LazyLock::new(|| ServerMetrics::init());
+
+impl ServerMetrics {
+
+
+   fn init() -> Self {
+      let metrics = Self {
+         connect_jitter_counter: Family::default(),
+      };
+
+      let registry = load_global_registry();
+
+      registry.register(
+         "connect_jitter_counter",
+         "current connect jitter times",
+         metrics.connect_jitter_counter.clone(),
+      );
+
+      metrics
+   }
+}
+
+pub fn incr_connect_jitter_counter(client_id: &str) {
+   let labels = ConnectionJitterLabels {
+      client_id: client_id.to_string(),
+   };
+
+   SERVER_METRICS.connect_jitter_counter.get_or_create(&labels).inc();
+}
+
+pub fn get_connect_jitter_counter(client_id: &str) -> i64 {
+   let labels = ConnectionJitterLabels {
+      client_id: client_id.to_string(),
+   };
+
+   SERVER_METRICS.connect_jitter_counter.get_or_create(&labels).get()
+}
+```
diff --git a/docs/.design/observability/metrics.md b/docs/.design/observability/metrics.md
new file mode 100644
index 000000000..a012f70d7
--- /dev/null
+++ b/docs/.design/observability/metrics.md
@@ -0,0 +1,85 @@
+# 指标
+
+# 字节总数指标
+
+| Metric Name      | Description |
+|------------------|-------------|
+| `bytes_received` | 已接受字节数      |
+| `bytes_sent`     | 已发送字节数      |
+
+# 报文指标
+
+| Metric Name                    | Description                                                  |
+|--------------------------------|--------------------------------------------------------------|
+| `packets_received`             | 已接受报文数                                                       |
+| `packets_connect_received`     | 接收的CONNECT报文数量                                               |
+| `packets_publish_received`     | 接收的PUBLISH报文数量                                               |
+| `packets_puback_received`      | 接收的PUBACK报文数量                                                |
+| `packets_pubrec_received`      | 接收的PUBREC报文数量                                                |
+| `packets_pubrel_received`      | 接收的PUBREL报文数量                                                |
+| `packets_pubcomp_received`     | 接收的PUBCOMP报文数量                                               |
+| `packets_subscribe_received`   | 接收的SUBSCRIBE报文数量                                             |
+| `packets_unsubscribe_received` | 接收的UNSUBSCRIBE报文数量                                           |
+| `packets_pingreq_received`     | 接收的PINGREQ报文数量                                               |
+| `packets_disconnect_received`  | 接收的DISCONNECT报文数量                                            |
+| `packets_auth_received`        | 接收的AUTH报文数量                                                  |
+| `packets_sent`                 | 已发送报文数                                                       |
+| `packets_connack_sent`         | 发送的CONNACK报文数量                                               |
+| `packets_publish_sent`         | 发送的PUBLISH报文数量                                               |
+| `packets_puback_sent`          | 发送的PUBACK报文数量                                                |
+| `packets_pubrec_sent`          | 发送的PUBREC报文数量                                                |
+| `packets_pubrel_sent`          | 发送的PUBREL报文数量                                                |
+| `packets_pubcomp_sent`         | 发送的PUBCOMP报文数量                                               |
+| `packets_suback_sent`          | 发送的SUBACK报文数量                                                |
+| `packets_unsuback_sent`        | 发送的UNSUBACK报文数量                                              |
+| `packets_pingresp_sent`        | 发送的PINGRESP报文数量                                              |
+| `packets_disconnect_sent`      | 发送的DISCONNECT报文数量                                            |
+| `packets_auth_sent`            | 发送的AUTH报文数量                                                  |
+| `packets_connack_auth_error`   | 发送的原因码为0x86和0x87的CONNACK报文数量                                 |
+| `packets_connack_error`        | 发送的不为0x00的CONNACK报文数量，此指标的值大于等于`packects_connack_auth_error` |
+
+
+# 消息指标
+
+
+| Metric Name          | Description    |
+|----------------------|----------------|
+| `mmessages_delayed`  | 存储的延迟发布的消息数量   |
+| `messages_delivered` | 内部转发到订阅进程的消息数量 |
+
+# 事件指标
+
+| Metric Name      | Description               |
+|------------------|---------------------------|
+| `client_connack` | 客户端收到连接确认(CONNACK)消息的次数   |
+| `client_connect` | 客户端发起连接请求的次数，包括成功和失败的连接请求 |
+
+
+# 会话指标
+
+| Metric Name          | Description |
+|----------------------|-------------|
+| `sessions_created`   | 创建的会话数量     |
+| `sessions_discarded` | 已被丢弃的会话数量   |
+
+# 认证和授权指标
+
+| Metric Name           | Description                  |
+|-----------------------|------------------------------|
+| `authorization_allow` | 授权总的通过次数（包括命中缓存和规则未匹配时默认通过的  |
+| `authorization_deny`  | 授权总的拒绝次数（包括命中缓存和规则未匹配时默认拒绝的） |
+
+# 消息分发指标
+
+| ｜ Metric Name                  | Description                |
+|--------------------------------|----------------------------|
+| ｜`delivery_dropped_too_large`  | 发送时由于长度超过限制而丢弃的消息数量        |
+| ｜`delivery_dropped_queue_full` | 发送时由于消息队列满而被丢弃的QoS不为0的消息数量 |
+
+
+# 统计指标
+
+| Metric Name         | Description |
+|---------------------|-------------|
+| `connections_count` | 当前连接数量      |
+| `connections_max`   | 连接数量的历史最大值  |


Test patch: