moduforge_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
43pub fn register_metrics() {
44    //
45}
46
47pub fn task_submitted() {
48    counter!(TASKS_SUBMITTED_TOTAL).increment(1);
49}
50
51pub fn task_processed(status: &str) {
52    counter!(TASKS_PROCESSED_TOTAL, "status" => status.to_string())
53        .increment(1);
54}
55
56pub fn task_processing_duration(duration: std::time::Duration) {
57    histogram!(TASK_PROCESSING_DURATION_SECONDS).record(duration.as_secs_f64());
58}
59
60pub fn set_queue_size(size: usize) {
61    gauge!(QUEUE_SIZE).set(size as f64);
62}
63
64pub fn increment_processing_tasks() {
65    gauge!(PROCESSING_TASKS).increment(1.0);
66}
67
68pub fn decrement_processing_tasks() {
69    gauge!(PROCESSING_TASKS).decrement(1.0);
70}
71
72pub fn task_retried() {
73    counter!(TASKS_RETRIED_TOTAL).increment(1);
74}
75
76pub fn editor_creation_duration(duration: std::time::Duration) {
77    histogram!(EDITOR_CREATION_DURATION_SECONDS).record(duration.as_secs_f64());
78}
79
80pub fn command_executed(name: &str) {
81    counter!(COMMANDS_EXECUTED_TOTAL, "command_name" => name.to_string())
82        .increment(1);
83}
84
85pub fn transaction_dispatched() {
86    counter!(TRANSACTIONS_DISPATCHED_TOTAL).increment(1);
87}
88
89pub fn middleware_execution_duration(
90    duration: std::time::Duration,
91    mw_type: &str,
92    mw_name: &str,
93) {
94    histogram!(
95        MIDDLEWARE_EXECUTION_DURATION_SECONDS,
96        "type" => mw_type.to_string(),
97        "middleware_name" => mw_name.to_string()
98    )
99    .record(duration.as_secs_f64());
100}
101
102pub fn history_operation(op: &str) {
103    counter!(HISTORY_OPERATIONS_TOTAL, "operation" => op.to_string())
104        .increment(1);
105}
106
107pub fn event_emitted(event_type: &str) {
108    counter!(EVENTS_EMITTED_TOTAL, "event_type" => event_type.to_string())
109        .increment(1);
110}
111
112pub fn extension_manager_creation_duration(duration: std::time::Duration) {
113    histogram!(EXTENSION_MANAGER_CREATION_DURATION_SECONDS)
114        .record(duration.as_secs_f64());
115}
116
117pub fn extensions_loaded(count: u64) {
118    counter!(EXTENSIONS_LOADED_TOTAL).increment(count);
119}
120
121pub fn plugins_loaded(count: u64) {
122    counter!(PLUGINS_LOADED_TOTAL).increment(count);
123}