From 0232c4e162190c2111d48d3a7c7dec5d76d16c12 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 1 Feb 2022 15:01:39 +0100 Subject: [PATCH] lifecycle: send analytics in gunicorn config to decrease outgoing requests when workers get restarted Signed-off-by: Jens Langhammer --- authentik/root/settings.py | 26 +------------------------- lifecycle/gunicorn.conf.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/authentik/root/settings.py b/authentik/root/settings.py index 26d1ed876..e3c163852 100644 --- a/authentik/root/settings.py +++ b/authentik/root/settings.py @@ -19,12 +19,11 @@ from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.redis import RedisIntegration from sentry_sdk.integrations.threading import ThreadingIntegration -from authentik import ENV_GIT_HASH_KEY, __version__, get_build_hash, get_full_version +from authentik import ENV_GIT_HASH_KEY, __version__, get_build_hash from authentik.core.middleware import structlog_add_request_id from authentik.lib.config import CONFIG from authentik.lib.logging import add_process_id from authentik.lib.sentry import before_send -from authentik.lib.utils.http import get_http_session from authentik.lib.utils.reflection import get_env from authentik.stages.password import BACKEND_APP_PASSWORD, BACKEND_INBUILT, BACKEND_LDAP @@ -413,29 +412,6 @@ if _ERROR_REPORTING: "Error reporting is enabled", env=CONFIG.y("error_reporting.environment", "customer"), ) -if not CONFIG.y_bool("disable_startup_analytics", False): - should_send = env not in ["dev", "ci"] - if should_send: - try: - get_http_session().post( - "https://goauthentik.io/api/event", - json={ - "domain": "authentik", - "name": "pageview", - "referrer": get_full_version(), - "url": ( - f"http://localhost/{env}?utm_source={get_full_version()}&utm_medium={env}" - ), - }, - headers={ - "User-Agent": sha512(str(SECRET_KEY).encode("ascii")).hexdigest()[:16], - "Content-Type": "application/json", - }, - timeout=5, - ) - # pylint: disable=bare-except - except: # nosec - pass # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.1/howto/static-files/ diff --git a/lifecycle/gunicorn.conf.py b/lifecycle/gunicorn.conf.py index fa0c25ec5..22cfa2707 100644 --- a/lifecycle/gunicorn.conf.py +++ b/lifecycle/gunicorn.conf.py @@ -1,11 +1,17 @@ """Gunicorn config""" import os import pwd +from hashlib import sha512 from multiprocessing import cpu_count import structlog from kubernetes.config.incluster_config import SERVICE_HOST_ENV_NAME +from authentik import get_full_version +from authentik.lib.config import CONFIG +from authentik.lib.utils.http import get_http_session +from authentik.lib.utils.reflection import get_env + bind = "127.0.0.1:8000" try: @@ -69,3 +75,31 @@ def worker_exit(server, worker): from prometheus_client import multiprocess multiprocess.mark_process_dead(worker.pid) + + +if not CONFIG.y_bool("disable_startup_analytics", False): + env = get_env() + should_send = env not in ["dev", "ci"] + if should_send: + try: + get_http_session().post( + "https://goauthentik.io/api/event", + json={ + "domain": "authentik", + "name": "pageview", + "referrer": get_full_version(), + "url": ( + f"http://localhost/{env}?utm_source={get_full_version()}&utm_medium={env}" + ), + }, + headers={ + "User-Agent": sha512(str(CONFIG.y("secret_key")).encode("ascii")).hexdigest()[ + :16 + ], + "Content-Type": "application/json", + }, + timeout=5, + ) + # pylint: disable=bare-except + except: # nosec + pass