1111TASKS_ENQUEUED_TOTAL = Counter (
1212 "memos_scheduler_tasks_enqueued_total" ,
1313 "Total number of tasks enqueued" ,
14- ["user_id" , "task_type" ]
14+ ["user_id" , "task_type" ],
1515)
1616
1717TASKS_DEQUEUED_TOTAL = Counter (
1818 "memos_scheduler_tasks_dequeued_total" ,
1919 "Total number of tasks dequeued" ,
20- ["user_id" , "task_type" ]
20+ ["user_id" , "task_type" ],
2121)
2222
2323TASK_DURATION_SECONDS = Summary (
2424 "memos_scheduler_task_duration_seconds" ,
2525 "Task processing duration in seconds" ,
26- ["user_id" , "task_type" ]
26+ ["user_id" , "task_type" ],
2727)
2828
2929TASK_WAIT_DURATION_SECONDS = Summary (
3030 "memos_scheduler_task_wait_duration_seconds" ,
3131 "Task waiting duration in seconds" ,
32- ["user_id" , "task_type" ]
32+ ["user_id" , "task_type" ],
3333)
3434
3535TASKS_FAILED_TOTAL = Counter (
3636 "memos_scheduler_tasks_failed_total" ,
3737 "Total number of failed tasks" ,
38- ["user_id" , "task_type" , "error_type" ]
38+ ["user_id" , "task_type" , "error_type" ],
3939)
4040
4141TASKS_COMPLETED_TOTAL = Counter (
4545)
4646
4747QUEUE_LENGTH = Gauge (
48- "memos_scheduler_queue_length" ,
49- "Current length of the task queue" ,
50- ["user_id" ]
48+ "memos_scheduler_queue_length" , "Current length of the task queue" , ["user_id" ]
5149)
5250
5351INTERNAL_SPAN_DURATION = Histogram (
54- ' memos_scheduler_internal_span_duration_seconds' ,
55- ' Duration of internal operations' ,
56- [' span_name' , ' user_id' , ' task_id' ]
52+ " memos_scheduler_internal_span_duration_seconds" ,
53+ " Duration of internal operations" ,
54+ [" span_name" , " user_id" , " task_id" ],
5755)
5856
5957
6058# --- Instrumentation Functions ---
6159
60+
6261def task_enqueued (user_id : str , task_type : str , count : int = 1 ):
6362 TASKS_ENQUEUED_TOTAL .labels (user_id = user_id , task_type = task_type ).inc (count )
6463
64+
6565def task_dequeued (user_id : str , task_type : str , count : int = 1 ):
6666 TASKS_DEQUEUED_TOTAL .labels (user_id = user_id , task_type = task_type ).inc (count )
6767
68+
6869def observe_task_duration (duration : float , user_id : str , task_type : str ):
6970 TASK_DURATION_SECONDS .labels (user_id = user_id , task_type = task_type ).observe (duration )
7071
72+
7173def observe_task_wait_duration (duration : float , user_id : str , task_type : str ):
7274 TASK_WAIT_DURATION_SECONDS .labels (user_id = user_id , task_type = task_type ).observe (duration )
7375
76+
7477def task_failed (user_id : str , task_type : str , error_type : str ):
7578 TASKS_FAILED_TOTAL .labels (user_id = user_id , task_type = task_type , error_type = error_type ).inc ()
7679
80+
7781def task_completed (user_id : str , task_type : str , count : int = 1 ):
7882 TASKS_COMPLETED_TOTAL .labels (user_id = user_id , task_type = task_type ).inc (count )
7983
84+
8085def update_queue_length (length : int , user_id : str ):
8186 QUEUE_LENGTH .labels (user_id = user_id ).set (length )
8287
88+
8389def observe_internal_span (duration : float , span_name : str , user_id : str , task_id : str ):
84- INTERNAL_SPAN_DURATION .labels (span_name = span_name , user_id = user_id , task_id = task_id ).observe (duration )
90+ INTERNAL_SPAN_DURATION .labels (span_name = span_name , user_id = user_id , task_id = task_id ).observe (
91+ duration
92+ )
8593
8694
8795# --- TimingSpan Context Manager ---
8896
97+
8998class TimingSpan (ContextDecorator ):
9099 """
91100 A context manager/decorator to measure the duration of a code block and record it
@@ -100,6 +109,7 @@ def my_function():
100109 with TimingSpan("another_op", user_id="user456", task_id="t1"):
101110 ...
102111 """
112+
103113 def __init__ (self , span_name : str , user_id : str = "unknown" , task_id : str = "unknown" ):
104114 self .span_name = span_name
105115 self .user_id = user_id
@@ -112,4 +122,4 @@ def __enter__(self):
112122
113123 def __exit__ (self , exc_type , exc_val , exc_tb ):
114124 duration = time .perf_counter () - self .start_time
115- observe_internal_span (duration , self .span_name , self .user_id , self .task_id )
125+ observe_internal_span (duration , self .span_name , self .user_id , self .task_id )
0 commit comments