瀏覽代碼

feat(api): add exception metrics

Peter Thomassen 1 年之前
父節點
當前提交
24c01eec1c
共有 2 個文件被更改,包括 8 次插入2 次删除
  1. 3 2
      api/desecapi/exception_handlers.py
  2. 5 0
      api/desecapi/metrics.py

+ 3 - 2
api/desecapi/exception_handlers.py

@@ -15,11 +15,12 @@ def exception_handler(exc, context):
     we default to restframework's exception handling. See also
     https://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling
     """
+    class_path = f"{exc.__class__.__module__}.{exc.__class__.__name__}"
 
     def _log():
         logger = logging.getLogger("django.request")
         logger.error(
-            f"{exc.__class__.__module__}.{exc.__class__.__name__} Supplementary Information",
+            f"{class_path} Supplementary Information",
             exc_info=exc,
             stack_info=False,
         )
@@ -67,7 +68,7 @@ def exception_handler(exc, context):
 
     for exception_class, handler in handlers.items():
         if isinstance(exc, exception_class):
-            # TODO add metrics
+            metrics.get("desecapi_exception").labels(class_path).inc()
             return handler()
 
     return drf_exception_handler(exc, context)

+ 5 - 0
api/desecapi/metrics.py

@@ -60,6 +60,11 @@ set_counter(
 
 # exception_handlers.py metrics
 set_counter("desecapi_database_unavailable", "number of times database was unavailable")
+set_counter(
+    "desecapi_exception",
+    "number of times an exception was raised",
+    ["exception_class"],
+)
 
 # pdns.py metrics
 set_counter(