mf_core/
metrics.rs

1use metrics::{counter, gauge, histogram};
2
3/// 已提交任务总数
4pub const TASKS_SUBMITTED_TOTAL: &str = "core.tasks.submitted.total";
5/// 已处理任务总数
6pub const TASKS_PROCESSED_TOTAL: &str = "core.tasks.processed.total";
7/// 任务处理耗时(秒)
8pub const TASK_PROCESSING_DURATION_SECONDS: &str =
9    "core.task.processing.duration.seconds";
10/// 当前任务队列大小
11pub const QUEUE_SIZE: &str = "core.queue.size";
12/// 当前正在处理的任务数
13pub const PROCESSING_TASKS: &str = "core.processing.tasks";
14/// 任务重试总数
15pub const TASKS_RETRIED_TOTAL: &str = "core.tasks.retried.total";
16
17// 编辑器/运行时 指标
18/// 编辑器创建耗时(秒)
19pub const EDITOR_CREATION_DURATION_SECONDS: &str =
20    "core.editor.creation.duration.seconds";
21/// 已执行命令总数
22pub const COMMANDS_EXECUTED_TOTAL: &str = "core.commands.executed.total";
23/// 已分发事务总数
24pub const TRANSACTIONS_DISPATCHED_TOTAL: &str =
25    "core.transactions.dispatched.total";
26/// 中间件执行耗时(秒)
27pub const MIDDLEWARE_EXECUTION_DURATION_SECONDS: &str =
28    "core.middleware.execution.duration.seconds";
29/// 历史操作(撤销/重做)总数
30pub const HISTORY_OPERATIONS_TOTAL: &str = "core.history.operations.total";
31/// 已分发事件总数
32pub const EVENTS_EMITTED_TOTAL: &str = "core.events.emitted.total";
33
34// 插件管理器 指标
35/// 插件管理器创建耗时(秒)
36pub const EXTENSION_MANAGER_CREATION_DURATION_SECONDS: &str =
37    "core.extension_manager.creation.duration.seconds";
38/// 已加载扩展总数
39pub const EXTENSIONS_LOADED_TOTAL: &str = "core.extensions.loaded.total";
40/// 已加载插件总数
41pub const PLUGINS_LOADED_TOTAL: &str = "core.plugins.loaded.total";
42/// XML解析耗时(秒)
43pub const XML_PARSING_DURATION_SECONDS: &str =
44    "core.xml.parsing.duration.seconds";
45
46pub fn register_metrics() {
47    //
48}
49
50pub fn task_submitted() {
51    counter!(TASKS_SUBMITTED_TOTAL).increment(1);
52}
53
54pub fn task_processed(status: &str) {
55    counter!(TASKS_PROCESSED_TOTAL, "status" => status.to_string())
56        .increment(1);
57}
58
59pub fn task_processing_duration(duration: std::time::Duration) {
60    histogram!(TASK_PROCESSING_DURATION_SECONDS).record(duration.as_secs_f64());
61}
62
63pub fn set_queue_size(size: usize) {
64    gauge!(QUEUE_SIZE).set(size as f64);
65}
66
67pub fn increment_processing_tasks() {
68    gauge!(PROCESSING_TASKS).increment(1.0);
69}
70
71pub fn decrement_processing_tasks() {
72    gauge!(PROCESSING_TASKS).decrement(1.0);
73}
74
75pub fn task_retried() {
76    counter!(TASKS_RETRIED_TOTAL).increment(1);
77}
78
79pub fn editor_creation_duration(duration: std::time::Duration) {
80    histogram!(EDITOR_CREATION_DURATION_SECONDS).record(duration.as_secs_f64());
81}
82
83pub fn command_executed(name: &str) {
84    counter!(COMMANDS_EXECUTED_TOTAL, "command_name" => name.to_string())
85        .increment(1);
86}
87
88pub fn transaction_dispatched() {
89    counter!(TRANSACTIONS_DISPATCHED_TOTAL).increment(1);
90}
91
92pub fn middleware_execution_duration(
93    duration: std::time::Duration,
94    mw_type: &str,
95    mw_name: &str,
96) {
97    histogram!(
98        MIDDLEWARE_EXECUTION_DURATION_SECONDS,
99        "type" => mw_type.to_string(),
100        "middleware_name" => mw_name.to_string()
101    )
102    .record(duration.as_secs_f64());
103}
104
105pub fn history_operation(op: &str) {
106    counter!(HISTORY_OPERATIONS_TOTAL, "operation" => op.to_string())
107        .increment(1);
108}
109
110pub fn event_emitted(event_type: &str) {
111    counter!(EVENTS_EMITTED_TOTAL, "event_type" => event_type.to_string())
112        .increment(1);
113}
114
115pub fn extension_manager_creation_duration(duration: std::time::Duration) {
116    histogram!(EXTENSION_MANAGER_CREATION_DURATION_SECONDS)
117        .record(duration.as_secs_f64());
118}
119
120pub fn extensions_loaded(count: u64) {
121    counter!(EXTENSIONS_LOADED_TOTAL).increment(count);
122}
123
124pub fn plugins_loaded(count: u64) {
125    counter!(PLUGINS_LOADED_TOTAL).increment(count);
126}
127
128pub fn xml_parsing_duration(duration: std::time::Duration) {
129    histogram!(XML_PARSING_DURATION_SECONDS).record(duration.as_secs_f64());
130}