fix prometheus metrics

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt 2023-12-28 12:50:41 +01:00
parent a6a1c96d44
commit 1a64164cad
No known key found for this signature in database
GPG Key ID: 9C3FA22FABF1AA8D
4 changed files with 10 additions and 16 deletions

View File

@ -6,7 +6,7 @@ from timeit import default_timer
from typing import Any, Optional from typing import Any, Optional
from django.core.cache import cache 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 django.utils.translation import gettext_lazy as _
from structlog.stdlib import get_logger from structlog.stdlib import get_logger
from tenant_schemas_celery.task import TenantTask 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.apps import GAUGE_TASKS
from authentik.events.models import Event, EventAction from authentik.events.models import Event, EventAction
from authentik.lib.utils.errors import exception_to_string from authentik.lib.utils.errors import exception_to_string
from authentik.tenants.utils import get_current_tenant
LOGGER = get_logger() LOGGER = get_logger()
CACHE_KEY_PREFIX = "goauthentik.io/events/tasks/" CACHE_KEY_PREFIX = "goauthentik.io/events/tasks/"
@ -102,13 +101,8 @@ class TaskInfo:
duration = max(self.finish_timestamp - start, 0) duration = max(self.finish_timestamp - start, 0)
except TypeError: except TypeError:
duration = 0 duration = 0
try:
tenant = get_current_tenant().tenant_uuid
# DB not yet initialized, let's not fail
except ProgrammingError:
tenant = ""
GAUGE_TASKS.labels( GAUGE_TASKS.labels(
tenant=tenant, tenant=connection.schema_name,
task_name=self.task_name.split(":")[0], task_name=self.task_name.split(":")[0],
task_uid=self.result.uid or "", task_uid=self.result.uid or "",
status=self.result.status.name.lower(), status=self.result.status.name.lower(),

View File

@ -1,5 +1,6 @@
"""authentik flow signals""" """authentik flow signals"""
from django.core.cache import cache from django.core.cache import cache
from django.db import connection
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
from structlog.stdlib import get_logger 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.apps import GAUGE_FLOWS_CACHED
from authentik.flows.planner import CACHE_PREFIX from authentik.flows.planner import CACHE_PREFIX
from authentik.root.monitoring import monitoring_set from authentik.root.monitoring import monitoring_set
from authentik.tenants.utils import get_current_tenant
LOGGER = get_logger() LOGGER = get_logger()
@ -22,7 +22,7 @@ def delete_cache_prefix(prefix: str) -> int:
@receiver(monitoring_set) @receiver(monitoring_set)
def monitoring_set_flows(sender, **kwargs): def monitoring_set_flows(sender, **kwargs):
"""set flow gauges""" """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 []) len(cache.keys(f"{CACHE_PREFIX}*") or [])
) )

View File

@ -8,13 +8,13 @@ from asgiref.sync import async_to_sync
from channels.exceptions import DenyConnection from channels.exceptions import DenyConnection
from dacite.core import from_dict from dacite.core import from_dict
from dacite.data import Data from dacite.data import Data
from django.db import connection
from guardian.shortcuts import get_objects_for_user from guardian.shortcuts import get_objects_for_user
from structlog.stdlib import BoundLogger, get_logger from structlog.stdlib import BoundLogger, get_logger
from authentik.core.channels import AuthJsonConsumer from authentik.core.channels import AuthJsonConsumer
from authentik.outposts.apps import GAUGE_OUTPOSTS_CONNECTED, GAUGE_OUTPOSTS_LAST_UPDATE from authentik.outposts.apps import GAUGE_OUTPOSTS_CONNECTED, GAUGE_OUTPOSTS_LAST_UPDATE
from authentik.outposts.models import OUTPOST_HELLO_INTERVAL, Outpost, OutpostState 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" 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 OUTPOST_GROUP % {"outpost_pk": str(self.outpost.pk)}, self.channel_name
) )
GAUGE_OUTPOSTS_CONNECTED.labels( GAUGE_OUTPOSTS_CONNECTED.labels(
tenant=get_current_tenant().tenant_uuid, tenant=connection.schema_name,
outpost=self.outpost.name, outpost=self.outpost.name,
uid=self.last_uid, uid=self.last_uid,
expected=self.outpost.config.kubernetes_replicas, expected=self.outpost.config.kubernetes_replicas,
@ -90,7 +90,7 @@ class OutpostConsumer(AuthJsonConsumer):
) )
if self.outpost and self.last_uid: if self.outpost and self.last_uid:
GAUGE_OUTPOSTS_CONNECTED.labels( GAUGE_OUTPOSTS_CONNECTED.labels(
tenant=get_current_tenant().tenant_uuid, tenant=connection.schema_name,
outpost=self.outpost.name, outpost=self.outpost.name,
uid=self.last_uid, uid=self.last_uid,
expected=self.outpost.config.kubernetes_replicas, expected=self.outpost.config.kubernetes_replicas,
@ -115,7 +115,7 @@ class OutpostConsumer(AuthJsonConsumer):
elif msg.instruction == WebsocketMessageInstruction.ACK: elif msg.instruction == WebsocketMessageInstruction.ACK:
return return
GAUGE_OUTPOSTS_LAST_UPDATE.labels( GAUGE_OUTPOSTS_LAST_UPDATE.labels(
tenant=get_current_tenant().tenant_uuid, tenant=connection.schema_name,
outpost=self.outpost.name, outpost=self.outpost.name,
uid=self.last_uid or "", uid=self.last_uid or "",
version=state.version or "", version=state.version or "",

View File

@ -1,5 +1,6 @@
"""authentik policy signals""" """authentik policy signals"""
from django.core.cache import cache from django.core.cache import cache
from django.db import connection
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from structlog.stdlib import get_logger 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.models import Policy, PolicyBinding, PolicyBindingModel
from authentik.policies.types import CACHE_PREFIX from authentik.policies.types import CACHE_PREFIX
from authentik.root.monitoring import monitoring_set from authentik.root.monitoring import monitoring_set
from authentik.tenants.utils import get_current_tenant
LOGGER = get_logger() LOGGER = get_logger()
@ -18,7 +18,7 @@ LOGGER = get_logger()
@receiver(monitoring_set) @receiver(monitoring_set)
def monitoring_set_policies(sender, **kwargs): def monitoring_set_policies(sender, **kwargs):
"""set policy gauges""" """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 []) len(cache.keys(f"{CACHE_PREFIX}*") or [])
) )