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

Fix type errors introduced by new annotations in the Prometheus Client library. #11832

Merged
merged 7 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11832.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix type errors introduced by new annotations in the Prometheus Client library.
12 changes: 6 additions & 6 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def init_counters_for_auth_provider(auth_provider_id: str) -> None:
until the user first logs in/registers.
"""
for is_guest in (True, False):
login_counter.labels(guest=is_guest, auth_provider=auth_provider_id)
login_counter.labels(guest=str(is_guest), auth_provider=auth_provider_id)
for shadow_banned in (True, False):
registration_counter.labels(
guest=is_guest,
shadow_banned=shadow_banned,
guest=str(is_guest),
shadow_banned=str(shadow_banned),
auth_provider=auth_provider_id,
)

Expand Down Expand Up @@ -341,8 +341,8 @@ async def register_user(
fail_count += 1

registration_counter.labels(
guest=make_guest,
shadow_banned=shadow_banned,
guest=str(make_guest),
shadow_banned=str(shadow_banned),
auth_provider=(auth_provider_id or ""),
).inc()

Expand Down Expand Up @@ -775,7 +775,7 @@ async def register_device(
)

login_counter.labels(
guest=is_guest,
guest=str(is_guest),
auth_provider=(auth_provider_id or ""),
).inc()

Expand Down
10 changes: 9 additions & 1 deletion synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Type,
TypeVar,
Union,
cast,
)

import attr
Expand Down Expand Up @@ -60,14 +61,21 @@
HAVE_PROC_SELF_STAT = os.path.exists("/proc/self/stat")


class RegistryProxy:
class _RegistryProxy:
@staticmethod
def collect() -> Iterable[Metric]:
for metric in REGISTRY.collect():
if not metric.name.startswith("__"):
yield metric


# A little bit nasty, but collect() above is static so a Protocol doesn't work.
# _RegistryProxy matches the signature of a CollectorRegistry instance enough
# for it to be usable in the contexts in which we use it.
# TODO Do something nicer about this.
RegistryProxy = cast(CollectorRegistry, _RegistryProxy)


@attr.s(slots=True, hash=True, auto_attribs=True)
class LaterGauge:

Expand Down
4 changes: 2 additions & 2 deletions synapse/metrics/_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def _maybe_gc() -> None:

_last_gc[i] = end

gc_time.labels(i).observe(end - start)
gc_unreachable.labels(i).set(unreachable)
gc_time.labels(str(i)).observe(end - start)
gc_unreachable.labels(str(i)).set(unreachable)

gc_task = task.LoopingCall(_maybe_gc)
gc_task.start(0.1)
Expand Down
4 changes: 2 additions & 2 deletions synapse/replication/http/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ async def send_request(*, instance_name="master", **kwargs):
# We convert to SynapseError as we know that it was a SynapseError
# on the main process that we should send to the client. (And
# importantly, not stack traces everywhere)
_outgoing_request_counter.labels(cls.NAME, e.code).inc()
_outgoing_request_counter.labels(cls.NAME, str(e.code)).inc()
raise e.to_synapse_error()
except Exception as e:
_outgoing_request_counter.labels(cls.NAME, "ERR").inc()
raise SynapseError(502, "Failed to talk to main process") from e

_outgoing_request_counter.labels(cls.NAME, 200).inc()
_outgoing_request_counter.labels(cls.NAME, "200").inc()
return result

return send_request
Expand Down
2 changes: 1 addition & 1 deletion synapse/replication/tcp/external_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def get(self, cache_name: str, key: str) -> Optional[Any]:

logger.debug("Got cache result %s %s: %r", cache_name, key, result)

get_counter.labels(cache_name, result is not None).inc()
get_counter.labels(cache_name, str(result is not None)).inc()

if not result:
return None
Expand Down