Skip to content

Commit ffae2d8

Browse files
committed
Saving relevant files
1 parent b3594d1 commit ffae2d8

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

nodestream/cli/operations/initialize_logger.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from pythonjsonlogger.jsonlogger import JsonFormatter
66

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
910
from .operation import Operation
1011

1112

@@ -32,9 +33,13 @@ def record_factory(*args, **kwargs):
3233
)
3334
logger = logging.getLogger() # Configure the root logger.
3435
logger.handlers[0].setFormatter(formatter)
36+
37+
# Add metrics handler to track log level counts
38+
metrics_handler = MetricsLoggingHandler()
39+
logger.addHandler(metrics_handler)
3540

3641

3742
class InitializeLogger(Operation):
38-
async def perform(self, command: NodestreamCommand) -> Any:
43+
async def perform(self, command: BaseCommand) -> Any:
3944
if command.has_json_logging_set:
4045
configure_logging_with_json_defaults()

nodestream/logging_metrics.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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

nodestream/metrics.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ def tick(self):
9292
"non_fatal_errors", "Number of non-fatal errors", accumulate=True
9393
)
9494
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+
95102
NODES_UPSERTED = Metric(
96103
"nodes_upserted", "Number of nodes upserted to the graph", accumulate=True
97104
)

0 commit comments

Comments
 (0)