authentik.lib.debug

 1from os import environ
 2from socket import gethostname
 3
 4from structlog.stdlib import get_logger
 5
 6from authentik import authentik_build_hash, authentik_full_version, authentik_version
 7from authentik.lib.config import CONFIG
 8
 9LOGGER = get_logger()
10
11
12def start_debug_server(port_offset: int = 0, **kwargs) -> bool:
13    """Attempt to start a debugpy server in the current process.
14    Returns true if the server was started successfully, otherwise false"""
15    if not CONFIG.get_bool("debug") and not CONFIG.get_bool("debugger"):
16        return
17    try:
18        import debugpy
19    except ImportError:
20        LOGGER.warning(
21            "Failed to import debugpy. debugpy is not included "
22            "in the default release dependencies and must be installed manually"
23        )
24        return False
25
26    listen: str = CONFIG.get("listen.debug_py", "127.0.0.1:9901")
27    host, _, port = listen.rpartition(":")
28    port = int(port) + port_offset
29    try:
30        debugpy.listen((host, port), **kwargs)  # nosec
31    except RuntimeError:
32        LOGGER.warning("Could not start debug server. Continuing without")
33        return False
34    LOGGER.debug("Starting debug server", host=host, port=port)
35    return True
36
37
38def start_pyroscope(component: str, **tags: str) -> bool:
39    """Attempt to start the pyroscope profiler in the current process.
40    Must be called *after* forking, the profiler's agent threads do not survive fork().
41    Returns true if the profiler was started, otherwise false"""
42    server = environ.get("AUTHENTIK_PYROSCOPE_HOST")
43    if not server:
44        return False
45
46    try:
47        import pyroscope
48    except ModuleNotFoundError:
49        return
50
51    LOGGER.debug("Starting pyroscope profiler", server=server, component=component)
52    pyroscope.configure(
53        application_name="authentik",
54        server_address=server,
55        tags={
56            "component": component,
57            "hostname": gethostname(),
58            "service_repository": "https://github.com/goauthentik/authentik",
59            "service_git_ref": authentik_build_hash(),
60            "authentik_version": authentik_version(),
61            "authentik_full_version": authentik_full_version(),
62            **tags,
63        },
64        mem_enabled=True,
65    )
66    return True
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
def start_debug_server(port_offset: int = 0, **kwargs) -> bool:
13def start_debug_server(port_offset: int = 0, **kwargs) -> bool:
14    """Attempt to start a debugpy server in the current process.
15    Returns true if the server was started successfully, otherwise false"""
16    if not CONFIG.get_bool("debug") and not CONFIG.get_bool("debugger"):
17        return
18    try:
19        import debugpy
20    except ImportError:
21        LOGGER.warning(
22            "Failed to import debugpy. debugpy is not included "
23            "in the default release dependencies and must be installed manually"
24        )
25        return False
26
27    listen: str = CONFIG.get("listen.debug_py", "127.0.0.1:9901")
28    host, _, port = listen.rpartition(":")
29    port = int(port) + port_offset
30    try:
31        debugpy.listen((host, port), **kwargs)  # nosec
32    except RuntimeError:
33        LOGGER.warning("Could not start debug server. Continuing without")
34        return False
35    LOGGER.debug("Starting debug server", host=host, port=port)
36    return True

Attempt to start a debugpy server in the current process. Returns true if the server was started successfully, otherwise false

def start_pyroscope(component: str, **tags: str) -> bool:
39def start_pyroscope(component: str, **tags: str) -> bool:
40    """Attempt to start the pyroscope profiler in the current process.
41    Must be called *after* forking, the profiler's agent threads do not survive fork().
42    Returns true if the profiler was started, otherwise false"""
43    server = environ.get("AUTHENTIK_PYROSCOPE_HOST")
44    if not server:
45        return False
46
47    try:
48        import pyroscope
49    except ModuleNotFoundError:
50        return
51
52    LOGGER.debug("Starting pyroscope profiler", server=server, component=component)
53    pyroscope.configure(
54        application_name="authentik",
55        server_address=server,
56        tags={
57            "component": component,
58            "hostname": gethostname(),
59            "service_repository": "https://github.com/goauthentik/authentik",
60            "service_git_ref": authentik_build_hash(),
61            "authentik_version": authentik_version(),
62            "authentik_full_version": authentik_full_version(),
63            **tags,
64        },
65        mem_enabled=True,
66    )
67    return True

Attempt to start the pyroscope profiler in the current process. Must be called after forking, the profiler's agent threads do not survive fork(). Returns true if the profiler was started, otherwise false