From 1a64164cad6df068847a88487f0e040d3c8e5dd6 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Thu, 28 Dec 2023 12:50:41 +0100 Subject: [PATCH] fix prometheus metrics Signed-off-by: Marc 'risson' Schmitt --- authentik/events/monitored_tasks.py | 10 ++-------- authentik/flows/signals.py | 4 ++-- authentik/outposts/consumer.py | 8 ++++---- authentik/policies/signals.py | 4 ++-- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/authentik/events/monitored_tasks.py b/authentik/events/monitored_tasks.py index 4dbd0678c..669defa28 100644 --- a/authentik/events/monitored_tasks.py +++ b/authentik/events/monitored_tasks.py @@ -6,7 +6,7 @@ from timeit import default_timer from typing import Any, Optional from django.core.cache import cache -from django.db.utils import ProgrammingError +from django.db import connection from django.utils.translation import gettext_lazy as _ from structlog.stdlib import get_logger from tenant_schemas_celery.task import TenantTask @@ -14,7 +14,6 @@ from tenant_schemas_celery.task import TenantTask from authentik.events.apps import GAUGE_TASKS from authentik.events.models import Event, EventAction from authentik.lib.utils.errors import exception_to_string -from authentik.tenants.utils import get_current_tenant LOGGER = get_logger() CACHE_KEY_PREFIX = "goauthentik.io/events/tasks/" @@ -102,13 +101,8 @@ class TaskInfo: duration = max(self.finish_timestamp - start, 0) except TypeError: duration = 0 - try: - tenant = get_current_tenant().tenant_uuid - # DB not yet initialized, let's not fail - except ProgrammingError: - tenant = "" GAUGE_TASKS.labels( - tenant=tenant, + tenant=connection.schema_name, task_name=self.task_name.split(":")[0], task_uid=self.result.uid or "", status=self.result.status.name.lower(), diff --git a/authentik/flows/signals.py b/authentik/flows/signals.py index fa07a7c35..af645fa7e 100644 --- a/authentik/flows/signals.py +++ b/authentik/flows/signals.py @@ -1,5 +1,6 @@ """authentik flow signals""" from django.core.cache import cache +from django.db import connection from django.db.models.signals import post_save, pre_delete from django.dispatch import receiver from structlog.stdlib import get_logger @@ -7,7 +8,6 @@ from structlog.stdlib import get_logger from authentik.flows.apps import GAUGE_FLOWS_CACHED from authentik.flows.planner import CACHE_PREFIX from authentik.root.monitoring import monitoring_set -from authentik.tenants.utils import get_current_tenant LOGGER = get_logger() @@ -22,7 +22,7 @@ def delete_cache_prefix(prefix: str) -> int: @receiver(monitoring_set) def monitoring_set_flows(sender, **kwargs): """set flow gauges""" - GAUGE_FLOWS_CACHED.labels(tenant=get_current_tenant().tenant_uuid).set( + GAUGE_FLOWS_CACHED.labels(tenant=connection.schema_name).set( len(cache.keys(f"{CACHE_PREFIX}*") or []) ) diff --git a/authentik/outposts/consumer.py b/authentik/outposts/consumer.py index 95a822be2..bd210f1db 100644 --- a/authentik/outposts/consumer.py +++ b/authentik/outposts/consumer.py @@ -8,13 +8,13 @@ from asgiref.sync import async_to_sync from channels.exceptions import DenyConnection from dacite.core import from_dict from dacite.data import Data +from django.db import connection from guardian.shortcuts import get_objects_for_user from structlog.stdlib import BoundLogger, get_logger from authentik.core.channels import AuthJsonConsumer from authentik.outposts.apps import GAUGE_OUTPOSTS_CONNECTED, GAUGE_OUTPOSTS_LAST_UPDATE from authentik.outposts.models import OUTPOST_HELLO_INTERVAL, Outpost, OutpostState -from authentik.tenants.utils import get_current_tenant OUTPOST_GROUP = "group_outpost_%(outpost_pk)s" @@ -77,7 +77,7 @@ class OutpostConsumer(AuthJsonConsumer): OUTPOST_GROUP % {"outpost_pk": str(self.outpost.pk)}, self.channel_name ) GAUGE_OUTPOSTS_CONNECTED.labels( - tenant=get_current_tenant().tenant_uuid, + tenant=connection.schema_name, outpost=self.outpost.name, uid=self.last_uid, expected=self.outpost.config.kubernetes_replicas, @@ -90,7 +90,7 @@ class OutpostConsumer(AuthJsonConsumer): ) if self.outpost and self.last_uid: GAUGE_OUTPOSTS_CONNECTED.labels( - tenant=get_current_tenant().tenant_uuid, + tenant=connection.schema_name, outpost=self.outpost.name, uid=self.last_uid, expected=self.outpost.config.kubernetes_replicas, @@ -115,7 +115,7 @@ class OutpostConsumer(AuthJsonConsumer): elif msg.instruction == WebsocketMessageInstruction.ACK: return GAUGE_OUTPOSTS_LAST_UPDATE.labels( - tenant=get_current_tenant().tenant_uuid, + tenant=connection.schema_name, outpost=self.outpost.name, uid=self.last_uid or "", version=state.version or "", diff --git a/authentik/policies/signals.py b/authentik/policies/signals.py index 69468cb84..767d733cb 100644 --- a/authentik/policies/signals.py +++ b/authentik/policies/signals.py @@ -1,5 +1,6 @@ """authentik policy signals""" from django.core.cache import cache +from django.db import connection from django.db.models.signals import post_save from django.dispatch import receiver from structlog.stdlib import get_logger @@ -10,7 +11,6 @@ from authentik.policies.apps import GAUGE_POLICIES_CACHED from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel from authentik.policies.types import CACHE_PREFIX from authentik.root.monitoring import monitoring_set -from authentik.tenants.utils import get_current_tenant LOGGER = get_logger() @@ -18,7 +18,7 @@ LOGGER = get_logger() @receiver(monitoring_set) def monitoring_set_policies(sender, **kwargs): """set policy gauges""" - GAUGE_POLICIES_CACHED.labels(tenant=get_current_tenant().tenant_uuid).set( + GAUGE_POLICIES_CACHED.labels(tenant=connection.schema_name).set( len(cache.keys(f"{CACHE_PREFIX}*") or []) )