Skip to content

Commit

Permalink
fix type, lint, flake issues
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavsingh committed Aug 11, 2024
1 parent e94154d commit 626a461
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions proxy/http/server/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""
import os
import glob
from typing import TYPE_CHECKING, Any, Dict, List, Tuple
from typing import TYPE_CHECKING, Any, Dict, List, Tuple, Generator
from pathlib import Path
from urllib.parse import parse_qs, urlparse
from multiprocessing.synchronize import Lock
Expand Down Expand Up @@ -46,19 +46,20 @@
% text_(DEFAULT_METRICS_URL_PATH),
)


class MetricsStorage:

def __init__(self, lock: Lock) -> None:
self._lock = lock

def get_counter(self, name: str, default: float = 0.0) -> float:
def get_counter(self, name: str) -> float:
with self._lock:
return self._get_counter(name, default)
return self._get_counter(name)

Check warning on line 57 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L57

Added line #L57 was not covered by tests

def _get_counter(self, name: str, default: float = 0.0) -> float:
def _get_counter(self, name: str) -> float:
path = os.path.join(DEFAULT_METRICS_DIRECTORY_PATH, f'{name}.counter')

Check warning on line 60 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L60

Added line #L60 was not covered by tests
if not os.path.exists(path):
return default
return 0
return float(Path(path).read_text(encoding='utf-8').strip())

Check warning on line 63 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L62-L63

Added lines #L62 - L63 were not covered by tests

def incr_counter(self, name: str, by: float = 1.0) -> None:
Expand All @@ -70,14 +71,14 @@ def _incr_counter(self, name: str, by: float = 1.0) -> None:
path = os.path.join(DEFAULT_METRICS_DIRECTORY_PATH, f'{name}.counter')
Path(path).write_text(str(current + by), encoding='utf-8')

Check warning on line 72 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L70-L72

Added lines #L70 - L72 were not covered by tests

def get_gauge(self, name: str, default: float = 0.0) -> float:
def get_gauge(self, name: str) -> float:
with self._lock:
return self._get_gauge(name, default)
return self._get_gauge(name)

Check warning on line 76 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L76

Added line #L76 was not covered by tests

def _get_gauge(self, name: str, default: float = 0.0) -> float:
def _get_gauge(self, name: str) -> float:
path = os.path.join(DEFAULT_METRICS_DIRECTORY_PATH, f'{name}.gauge')

Check warning on line 79 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L79

Added line #L79 was not covered by tests
if not os.path.exists(path):
return default
return 0
return float(Path(path).read_text(encoding='utf-8').strip())

Check warning on line 82 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L81-L82

Added lines #L81 - L82 were not covered by tests

def set_gauge(self, name: str, value: float) -> None:
Expand All @@ -93,14 +94,15 @@ def _set_gauge(self, name: str, value: float) -> None:

def get_collector(metrics_lock: Lock) -> 'Collector':
# pylint: disable=import-outside-toplevel
from prometheus_client.core import Metric
from prometheus_client.registry import Collector

Check warning on line 98 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L97-L98

Added lines #L97 - L98 were not covered by tests

class MetricsCollector(Collector):

def __init__(self, metrics_lock: Lock) -> None:
self.storage = MetricsStorage(metrics_lock)

Check warning on line 103 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L102-L103

Added lines #L102 - L103 were not covered by tests

def collect(self):
def collect(self) -> Generator[Metric, None, None]:

Check warning on line 105 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L105

Added line #L105 was not covered by tests
"""Serves from aggregates metrics managed by MetricsEventSubscriber."""
# pylint: disable=import-outside-toplevel
from prometheus_client.core import CounterMetricFamily

Check warning on line 108 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L108

Added line #L108 was not covered by tests
Expand Down Expand Up @@ -177,7 +179,6 @@ class MetricsWebServerPlugin(HttpWebServerBasePlugin):
def __init__(self, *args: Any, **kwargs: Any) -> None:
# pylint: disable=import-outside-toplevel
from prometheus_client.core import CollectorRegistry

Check warning on line 181 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L181

Added line #L181 was not covered by tests
from prometheus_client.registry import Collector

super().__init__(*args, **kwargs)
self.registry = CollectorRegistry()
Expand Down Expand Up @@ -205,7 +206,8 @@ def handle_request(self, request: HttpParser) -> None:
# pylint: disable=import-outside-toplevel
from prometheus_client.exposition import _bake_output

Check warning on line 207 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L207

Added line #L207 was not covered by tests

status, headers, output = _bake_output(
# flake8: noqa
status, headers, output = _bake_output( # type: ignore[no-untyped-call]

Check warning on line 210 in proxy/http/server/metrics.py

View check run for this annotation

Codecov / codecov/patch

proxy/http/server/metrics.py#L210

Added line #L210 was not covered by tests
self.registry,
(
request.header(b'Accept').decode()
Expand Down

0 comments on commit 626a461

Please sign in to comment.