From 90e3ae9457de0bde66cd90050fdc3fba4329b639 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 1 Jul 2022 16:49:24 +0200 Subject: [PATCH] *: define prometheus metrics in apps to prevent re-import Signed-off-by: Jens Langhammer --- Makefile | 2 +- authentik/admin/api/workers.py | 3 --- authentik/admin/apps.py | 4 ++++ authentik/admin/signals.py | 2 +- authentik/admin/tasks.py | 3 +-- authentik/events/apps.py | 7 +++++++ authentik/events/monitored_tasks.py | 8 +------- authentik/flows/apps.py | 11 +++++++++++ authentik/flows/planner.py | 11 +---------- authentik/flows/signals.py | 2 +- authentik/outposts/apps.py | 10 ++++++++++ authentik/outposts/channels.py | 11 +---------- authentik/policies/apps.py | 23 +++++++++++++++++++++++ authentik/policies/engine.py | 11 +---------- authentik/policies/process.py | 13 +------------ authentik/policies/signals.py | 2 +- 16 files changed, 65 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index 9dd9a461d..820bb0339 100644 --- a/Makefile +++ b/Makefile @@ -45,8 +45,8 @@ lint-fix: website/developer-docs lint: - bandit -r authentik tests lifecycle -x node_modules pylint authentik tests lifecycle + bandit -r authentik tests lifecycle -x node_modules golangci-lint run -v i18n-extract: i18n-extract-core web-extract diff --git a/authentik/admin/api/workers.py b/authentik/admin/api/workers.py index eb91862e9..cfb23ea31 100644 --- a/authentik/admin/api/workers.py +++ b/authentik/admin/api/workers.py @@ -1,7 +1,6 @@ """authentik administration overview""" from django.conf import settings from drf_spectacular.utils import extend_schema, inline_serializer -from prometheus_client import Gauge from rest_framework.fields import IntegerField from rest_framework.permissions import IsAdminUser from rest_framework.request import Request @@ -10,8 +9,6 @@ from rest_framework.views import APIView from authentik.root.celery import CELERY_APP -GAUGE_WORKERS = Gauge("authentik_admin_workers", "Currently connected workers") - class WorkerView(APIView): """Get currently connected worker count.""" diff --git a/authentik/admin/apps.py b/authentik/admin/apps.py index 38da0f321..fc3d74fee 100644 --- a/authentik/admin/apps.py +++ b/authentik/admin/apps.py @@ -2,6 +2,10 @@ from importlib import import_module from django.apps import AppConfig +from prometheus_client import Gauge, Info + +PROM_INFO = Info("authentik_version", "Currently running authentik version") +GAUGE_WORKERS = Gauge("authentik_admin_workers", "Currently connected workers") class AuthentikAdminConfig(AppConfig): diff --git a/authentik/admin/signals.py b/authentik/admin/signals.py index 33d153775..6bf52ed91 100644 --- a/authentik/admin/signals.py +++ b/authentik/admin/signals.py @@ -2,7 +2,7 @@ from django.dispatch import receiver from authentik.admin.api.tasks import TaskInfo -from authentik.admin.api.workers import GAUGE_WORKERS +from authentik.admin.apps import GAUGE_WORKERS from authentik.root.celery import CELERY_APP from authentik.root.monitoring import monitoring_set diff --git a/authentik/admin/tasks.py b/authentik/admin/tasks.py index ce3d5aaaf..f571d68b5 100644 --- a/authentik/admin/tasks.py +++ b/authentik/admin/tasks.py @@ -4,11 +4,11 @@ import re from django.core.cache import cache from django.core.validators import URLValidator from packaging.version import parse -from prometheus_client import Info from requests import RequestException from structlog.stdlib import get_logger from authentik import __version__, get_build_hash +from authentik.admin.apps import PROM_INFO from authentik.events.models import Event, EventAction, Notification from authentik.events.monitored_tasks import ( MonitoredTask, @@ -25,7 +25,6 @@ VERSION_CACHE_KEY = "authentik_latest_version" VERSION_CACHE_TIMEOUT = 8 * 60 * 60 # 8 hours # Chop of the first ^ because we want to search the entire string URL_FINDER = URLValidator.regex.pattern[1:] -PROM_INFO = Info("authentik_version", "Currently running authentik version") LOCAL_VERSION = parse(__version__) diff --git a/authentik/events/apps.py b/authentik/events/apps.py index ad9e7d205..814b581d7 100644 --- a/authentik/events/apps.py +++ b/authentik/events/apps.py @@ -2,6 +2,13 @@ from importlib import import_module from django.apps import AppConfig +from prometheus_client import Gauge + +GAUGE_TASKS = Gauge( + "authentik_system_tasks", + "System tasks and their status", + ["task_name", "task_uid", "status"], +) class AuthentikEventsConfig(AppConfig): diff --git a/authentik/events/monitored_tasks.py b/authentik/events/monitored_tasks.py index 1213d85b7..d6bfa50a9 100644 --- a/authentik/events/monitored_tasks.py +++ b/authentik/events/monitored_tasks.py @@ -8,18 +8,12 @@ from typing import Any, Optional from celery import Task from django.core.cache import cache from django.utils.translation import gettext_lazy as _ -from prometheus_client import Gauge from structlog.stdlib import get_logger +from authentik.events.apps import GAUGE_TASKS from authentik.events.models import Event, EventAction from authentik.lib.utils.errors import exception_to_string -GAUGE_TASKS = Gauge( - "authentik_system_tasks", - "System tasks and their status", - ["task_name", "task_uid", "status"], -) - LOGGER = get_logger() diff --git a/authentik/flows/apps.py b/authentik/flows/apps.py index 9fcc8c52a..3940ec739 100644 --- a/authentik/flows/apps.py +++ b/authentik/flows/apps.py @@ -3,9 +3,20 @@ from importlib import import_module from django.apps import AppConfig from django.db.utils import ProgrammingError +from prometheus_client import Gauge, Histogram from authentik.lib.utils.reflection import all_subclasses +GAUGE_FLOWS_CACHED = Gauge( + "authentik_flows_cached", + "Cached flows", +) +HIST_FLOWS_PLAN_TIME = Histogram( + "authentik_flows_plan_time", + "Duration to build a plan for a flow", + ["flow_slug"], +) + class AuthentikFlowsConfig(AppConfig): """authentik flows app config""" diff --git a/authentik/flows/planner.py b/authentik/flows/planner.py index 7fb9e6165..1c3528f81 100644 --- a/authentik/flows/planner.py +++ b/authentik/flows/planner.py @@ -4,13 +4,13 @@ from typing import Any, Optional from django.core.cache import cache from django.http import HttpRequest -from prometheus_client import Gauge, Histogram from sentry_sdk.hub import Hub from sentry_sdk.tracing import Span from structlog.stdlib import BoundLogger, get_logger from authentik.core.models import User from authentik.events.models import cleanse_dict +from authentik.flows.apps import HIST_FLOWS_PLAN_TIME from authentik.flows.exceptions import EmptyFlowException, FlowNonApplicableException from authentik.flows.markers import ReevaluateMarker, StageMarker from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding, Stage @@ -26,15 +26,6 @@ PLAN_CONTEXT_SOURCE = "source" # Is set by the Flow Planner when a FlowToken was used, and the currently active flow plan # was restored. PLAN_CONTEXT_IS_RESTORED = "is_restored" -GAUGE_FLOWS_CACHED = Gauge( - "authentik_flows_cached", - "Cached flows", -) -HIST_FLOWS_PLAN_TIME = Histogram( - "authentik_flows_plan_time", - "Duration to build a plan for a flow", - ["flow_slug"], -) CACHE_TIMEOUT = int(CONFIG.y("redis.cache_timeout_flows")) diff --git a/authentik/flows/signals.py b/authentik/flows/signals.py index 70a38f763..8338aca6b 100644 --- a/authentik/flows/signals.py +++ b/authentik/flows/signals.py @@ -4,7 +4,7 @@ from django.db.models.signals import post_save, pre_delete from django.dispatch import receiver from structlog.stdlib import get_logger -from authentik.flows.planner import GAUGE_FLOWS_CACHED +from authentik.flows.apps import GAUGE_FLOWS_CACHED from authentik.root.monitoring import monitoring_set LOGGER = get_logger() diff --git a/authentik/outposts/apps.py b/authentik/outposts/apps.py index e24c4b660..4b29ed3fe 100644 --- a/authentik/outposts/apps.py +++ b/authentik/outposts/apps.py @@ -2,10 +2,20 @@ from importlib import import_module from django.apps import AppConfig +from prometheus_client import Gauge from structlog.stdlib import get_logger LOGGER = get_logger() +GAUGE_OUTPOSTS_CONNECTED = Gauge( + "authentik_outposts_connected", "Currently connected outposts", ["outpost", "uid", "expected"] +) +GAUGE_OUTPOSTS_LAST_UPDATE = Gauge( + "authentik_outposts_last_update", + "Last update from any outpost", + ["outpost", "uid", "version"], +) + class AuthentikOutpostConfig(AppConfig): """authentik outposts app config""" diff --git a/authentik/outposts/channels.py b/authentik/outposts/channels.py index 194cd6806..14217f79c 100644 --- a/authentik/outposts/channels.py +++ b/authentik/outposts/channels.py @@ -8,21 +8,12 @@ from channels.exceptions import DenyConnection from dacite import from_dict from dacite.data import Data from guardian.shortcuts import get_objects_for_user -from prometheus_client import Gauge 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 -GAUGE_OUTPOSTS_CONNECTED = Gauge( - "authentik_outposts_connected", "Currently connected outposts", ["outpost", "uid", "expected"] -) -GAUGE_OUTPOSTS_LAST_UPDATE = Gauge( - "authentik_outposts_last_update", - "Last update from any outpost", - ["outpost", "uid", "version"], -) - class WebsocketMessageInstruction(IntEnum): """Commands which can be triggered over Websocket""" diff --git a/authentik/policies/apps.py b/authentik/policies/apps.py index d36284fa2..fdb2f5311 100644 --- a/authentik/policies/apps.py +++ b/authentik/policies/apps.py @@ -2,6 +2,29 @@ from importlib import import_module from django.apps import AppConfig +from prometheus_client import Gauge, Histogram + +GAUGE_POLICIES_CACHED = Gauge( + "authentik_policies_cached", + "Cached Policies", +) +HIST_POLICIES_BUILD_TIME = Histogram( + "authentik_policies_build_time", + "Execution times complete policy result to an object", + ["object_pk", "object_type"], +) + +HIST_POLICIES_EXECUTION_TIME = Histogram( + "authentik_policies_execution_time", + "Execution times for single policies", + [ + "binding_order", + "binding_target_type", + "binding_target_name", + "object_pk", + "object_type", + ], +) class AuthentikPoliciesConfig(AppConfig): diff --git a/authentik/policies/engine.py b/authentik/policies/engine.py index 120b4e7ed..e9f2af3ce 100644 --- a/authentik/policies/engine.py +++ b/authentik/policies/engine.py @@ -5,26 +5,17 @@ from typing import Iterator, Optional from django.core.cache import cache from django.http import HttpRequest -from prometheus_client import Gauge, Histogram from sentry_sdk.hub import Hub from sentry_sdk.tracing import Span from structlog.stdlib import BoundLogger, get_logger from authentik.core.models import User +from authentik.policies.apps import HIST_POLICIES_BUILD_TIME from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel, PolicyEngineMode from authentik.policies.process import PolicyProcess, cache_key from authentik.policies.types import PolicyRequest, PolicyResult CURRENT_PROCESS = current_process() -GAUGE_POLICIES_CACHED = Gauge( - "authentik_policies_cached", - "Cached Policies", -) -HIST_POLICIES_BUILD_TIME = Histogram( - "authentik_policies_build_time", - "Execution times complete policy result to an object", - ["object_pk", "object_type"], -) class PolicyProcessInfo: diff --git a/authentik/policies/process.py b/authentik/policies/process.py index 6a8bb0c04..1c777a559 100644 --- a/authentik/policies/process.py +++ b/authentik/policies/process.py @@ -4,7 +4,6 @@ from multiprocessing.connection import Connection from typing import Optional from django.core.cache import cache -from prometheus_client import Histogram from sentry_sdk.hub import Hub from sentry_sdk.tracing import Span from structlog.stdlib import get_logger @@ -12,6 +11,7 @@ from structlog.stdlib import get_logger from authentik.events.models import Event, EventAction from authentik.lib.config import CONFIG from authentik.lib.utils.errors import exception_to_string +from authentik.policies.apps import HIST_POLICIES_EXECUTION_TIME from authentik.policies.exceptions import PolicyException from authentik.policies.models import PolicyBinding from authentik.policies.types import PolicyRequest, PolicyResult @@ -21,17 +21,6 @@ LOGGER = get_logger() FORK_CTX = get_context("fork") CACHE_TIMEOUT = int(CONFIG.y("redis.cache_timeout_policies")) PROCESS_CLASS = FORK_CTX.Process -HIST_POLICIES_EXECUTION_TIME = Histogram( - "authentik_policies_execution_time", - "Execution times for single policies", - [ - "binding_order", - "binding_target_type", - "binding_target_name", - "object_pk", - "object_type", - ], -) def cache_key(binding: PolicyBinding, request: PolicyRequest) -> str: diff --git a/authentik/policies/signals.py b/authentik/policies/signals.py index 386297d8a..94f8a82e7 100644 --- a/authentik/policies/signals.py +++ b/authentik/policies/signals.py @@ -5,7 +5,7 @@ from django.dispatch import receiver from structlog.stdlib import get_logger from authentik.core.api.applications import user_app_cache_key -from authentik.policies.engine import GAUGE_POLICIES_CACHED +from authentik.policies.apps import GAUGE_POLICIES_CACHED from authentik.root.monitoring import monitoring_set LOGGER = get_logger()