Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 15db8b7

Browse files
authored
Correctly initialise the synapse_user_logins metric. (#10677)
Fix a bug where the prometheus metrics for SSO logins wouldn't be initialised until the first user logged in with a given auth provider.
1 parent 86415f1 commit 15db8b7

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

changelog.d/10677.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug which caused the `synapse_user_logins_total` Prometheus metric not to be correctly initialised on restart.

synapse/handlers/register.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@
5656
)
5757

5858

59+
def init_counters_for_auth_provider(auth_provider_id: str) -> None:
60+
"""Ensure the prometheus counters for the given auth provider are initialised
61+
62+
This fixes a problem where the counters are not reported for a given auth provider
63+
until the user first logs in/registers.
64+
"""
65+
for is_guest in (True, False):
66+
login_counter.labels(guest=is_guest, auth_provider=auth_provider_id)
67+
for shadow_banned in (True, False):
68+
registration_counter.labels(
69+
guest=is_guest,
70+
shadow_banned=shadow_banned,
71+
auth_provider=auth_provider_id,
72+
)
73+
74+
5975
class LoginDict(TypedDict):
6076
device_id: str
6177
access_token: str
@@ -96,6 +112,8 @@ def __init__(self, hs: "HomeServer"):
96112
self.session_lifetime = hs.config.session_lifetime
97113
self.access_token_lifetime = hs.config.access_token_lifetime
98114

115+
init_counters_for_auth_provider("")
116+
99117
async def check_username(
100118
self,
101119
localpart: str,

synapse/handlers/sso.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from synapse.api.constants import LoginType
3838
from synapse.api.errors import Codes, NotFoundError, RedirectException, SynapseError
3939
from synapse.config.sso import SsoAttributeRequirement
40+
from synapse.handlers.register import init_counters_for_auth_provider
4041
from synapse.handlers.ui_auth import UIAuthSessionDataConstants
4142
from synapse.http import get_request_user_agent
4243
from synapse.http.server import respond_with_html, respond_with_redirect
@@ -213,6 +214,7 @@ def register_identity_provider(self, p: SsoIdentityProvider):
213214
p_id = p.idp_id
214215
assert p_id not in self._identity_providers
215216
self._identity_providers[p_id] = p
217+
init_counters_for_auth_provider(p_id)
216218

217219
def get_identity_providers(self) -> Mapping[str, SsoIdentityProvider]:
218220
"""Get the configured identity providers"""

synapse/rest/client/login.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ def __init__(self, hs: "HomeServer"):
104104
burst_count=self.hs.config.rc_login_account.burst_count,
105105
)
106106

107+
# ensure the CAS/SAML/OIDC handlers are loaded on this worker instance.
108+
# The reason for this is to ensure that the auth_provider_ids are registered
109+
# with SsoHandler, which in turn ensures that the login/registration prometheus
110+
# counters are initialised for the auth_provider_ids.
111+
_load_sso_handlers(hs)
112+
107113
def on_GET(self, request: SynapseRequest):
108114
flows = []
109115
if self.jwt_enabled:
@@ -499,12 +505,7 @@ class SsoRedirectServlet(RestServlet):
499505
def __init__(self, hs: "HomeServer"):
500506
# make sure that the relevant handlers are instantiated, so that they
501507
# register themselves with the main SSOHandler.
502-
if hs.config.cas_enabled:
503-
hs.get_cas_handler()
504-
if hs.config.saml2_enabled:
505-
hs.get_saml_handler()
506-
if hs.config.oidc_enabled:
507-
hs.get_oidc_handler()
508+
_load_sso_handlers(hs)
508509
self._sso_handler = hs.get_sso_handler()
509510
self._msc2858_enabled = hs.config.experimental.msc2858_enabled
510511
self._public_baseurl = hs.config.public_baseurl
@@ -598,3 +599,19 @@ def register_servlets(hs, http_server):
598599
SsoRedirectServlet(hs).register(http_server)
599600
if hs.config.cas_enabled:
600601
CasTicketServlet(hs).register(http_server)
602+
603+
604+
def _load_sso_handlers(hs: "HomeServer"):
605+
"""Ensure that the SSO handlers are loaded, if they are enabled by configuration.
606+
607+
This is mostly useful to ensure that the CAS/SAML/OIDC handlers register themselves
608+
with the main SsoHandler.
609+
610+
It's safe to call this multiple times.
611+
"""
612+
if hs.config.cas.cas_enabled:
613+
hs.get_cas_handler()
614+
if hs.config.saml2.saml2_enabled:
615+
hs.get_saml_handler()
616+
if hs.config.oidc.oidc_enabled:
617+
hs.get_oidc_handler()

0 commit comments

Comments
 (0)