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

Commit

Permalink
Try to express that counts are int, not float
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Oct 22, 2021
1 parent f362cd6 commit 8ee9d35
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions synapse/http/request_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import logging
import threading
import traceback
from typing import Dict, Set, Tuple
from typing import Dict, Mapping, Set, Tuple

from prometheus_client.core import Counter, Histogram

Expand Down Expand Up @@ -113,7 +113,7 @@
_in_flight_requests_lock = threading.Lock()


def _get_in_flight_counts() -> Dict[Tuple[str, ...], float]:
def _get_in_flight_counts() -> Mapping[Tuple[str, ...], int]:
"""Returns a count of all in flight requests by (method, server_name)"""
# Cast to a list to prevent it changing while the Prometheus
# thread is collecting metrics
Expand All @@ -126,10 +126,10 @@ def _get_in_flight_counts() -> Dict[Tuple[str, ...], float]:
# Map from (method, name) -> int, the number of in flight requests of that
# type. The key type is Tuple[str, str], but we leave the length unspecified
# for compatability with LaterGauge's annotations.
counts: Dict[Tuple[str, ...], float] = {}
counts: Dict[Tuple[str, ...], int] = {}
for rm in reqs:
key = (rm.method, rm.name)
counts[key] = counts.get(key, 0.0) + 1.0
counts[key] = counts.get(key, 0) + 1

return counts

Expand Down
10 changes: 5 additions & 5 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import platform
import threading
import time
from typing import Callable, Dict, Iterable, Optional, Tuple, Union
from typing import Callable, Dict, Iterable, Mapping, Optional, Tuple, Union

import attr
from prometheus_client import Counter, Gauge, Histogram
Expand Down Expand Up @@ -67,7 +67,7 @@ class LaterGauge:
labels = attr.ib(hash=False, type=Optional[Iterable[str]])
# callback: should either return a value (if there are no labels for this metric),
# or dict mapping from a label tuple to a value
caller = attr.ib(type=Callable[[], Union[Dict[Tuple[str, ...], float], float]])
caller = attr.ib(type=Callable[[], Union[Mapping[Tuple[str, ...], float], float]])

def collect(self):

Expand All @@ -80,11 +80,11 @@ def collect(self):
yield g
return

if isinstance(calls, dict):
if isinstance(calls, float):
g.add_metric([], calls)
else:
for k, v in calls.items():
g.add_metric(k, v)
else:
g.add_metric([], calls)

yield g

Expand Down

0 comments on commit 8ee9d35

Please sign in to comment.