File tree Expand file tree Collapse file tree 3 files changed +51
-3
lines changed Expand file tree Collapse file tree 3 files changed +51
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
from pythonjsonlogger .jsonlogger import JsonFormatter
6
6
7
- from ...metrics import Metrics
8
- from ..commands .nodestream_command import NodestreamCommand
7
+ from nodestream .cli .commands .nodestream_command import BaseCommand
8
+ from nodestream .metrics import Metrics
9
+ from nodestream .logging_metrics import MetricsLoggingHandler
9
10
from .operation import Operation
10
11
11
12
@@ -32,9 +33,13 @@ def record_factory(*args, **kwargs):
32
33
)
33
34
logger = logging .getLogger () # Configure the root logger.
34
35
logger .handlers [0 ].setFormatter (formatter )
36
+
37
+ # Add metrics handler to track log level counts
38
+ metrics_handler = MetricsLoggingHandler ()
39
+ logger .addHandler (metrics_handler )
35
40
36
41
37
42
class InitializeLogger (Operation ):
38
- async def perform (self , command : NodestreamCommand ) -> Any :
43
+ async def perform (self , command : BaseCommand ) -> Any :
39
44
if command .has_json_logging_set :
40
45
configure_logging_with_json_defaults ()
Original file line number Diff line number Diff line change
1
+ """
2
+ Logging handler that increments metrics based on log levels.
3
+ """
4
+
5
+ import logging
6
+ from typing import Optional
7
+
8
+ from .metrics import (
9
+ LOG_DEBUG_COUNT ,
10
+ LOG_ERROR_COUNT ,
11
+ LOG_INFO_COUNT ,
12
+ LOG_WARNING_COUNT ,
13
+ Metrics ,
14
+ )
15
+
16
+
17
+ class MetricsLoggingHandler (logging .Handler ):
18
+ """A logging handler that increments metrics based on log levels."""
19
+
20
+ def __init__ (self , metrics : Optional [Metrics ] = None ):
21
+ super ().__init__ ()
22
+ self .metrics = metrics or Metrics .get ()
23
+
24
+ def emit (self , record : logging .LogRecord ) -> None :
25
+ """Increment the appropriate metric based on log level."""
26
+ try :
27
+ if record .levelno >= logging .ERROR :
28
+ self .metrics .increment (LOG_ERROR_COUNT )
29
+ elif record .levelno >= logging .WARNING :
30
+ self .metrics .increment (LOG_WARNING_COUNT )
31
+ elif record .levelno >= logging .INFO :
32
+ self .metrics .increment (LOG_INFO_COUNT )
33
+ elif record .levelno >= logging .DEBUG :
34
+ self .metrics .increment (LOG_DEBUG_COUNT )
35
+ except Exception :
36
+ self .handleError (record ) # Handles any errors during metric increment
Original file line number Diff line number Diff line change @@ -92,6 +92,13 @@ def tick(self):
92
92
"non_fatal_errors" , "Number of non-fatal errors" , accumulate = True
93
93
)
94
94
FATAL_ERRORS = Metric ("fatal_errors" , "Number of fatal errors" , accumulate = True )
95
+
96
+ # Logging metrics
97
+ LOG_ERROR_COUNT = Metric ("log_error_count" , "Number of error log messages" , accumulate = True )
98
+ LOG_WARNING_COUNT = Metric ("log_warning_count" , "Number of warning log messages" , accumulate = True )
99
+ LOG_INFO_COUNT = Metric ("log_info_count" , "Number of info log messages" , accumulate = True )
100
+ LOG_DEBUG_COUNT = Metric ("log_debug_count" , "Number of debug log messages" , accumulate = True )
101
+
95
102
NODES_UPSERTED = Metric (
96
103
"nodes_upserted" , "Number of nodes upserted to the graph" , accumulate = True
97
104
)
You can’t perform that action at this time.
0 commit comments