moduforge_core/
metrics.rs

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