Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy: Add type hints to pglookout [BF-1560] #106

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0d4ea2c
mypy: `pglookout/logutil.py` [BF-1560]
Mar 16, 2023
238eb76
mypy: `pglookout/pgutil.py` [BF-1560]
Mar 16, 2023
3db5684
mypy: `pglookout/current-master.py` [BF-1560]
Mar 17, 2023
2ce13ab
mypy: `pglookout/statsd.py` [BF-1560]
Mar 17, 2023
25617d1
mypy: `pglookout/common.py` [BF-1560]
Mar 16, 2023
016d14d
mypy: `pglookout/config.py` [BF-1560]
Mar 17, 2023
bf067b3
mypy: `pglookout/common_types.py` [BF-1560]
Mar 23, 2023
a462bf9
mypy: `pglookout/webserver.py` [BF-1560]
Mar 17, 2023
9b4e13f
mypy: `pglookout/cluster_monitor.py` [BF-1560]
Mar 17, 2023
45d413b
mypy: `pglookout/pglookout.py` [BF-1560]
Mar 23, 2023
6ce993e
mypy: Refactor `check_cluster_state` [BF-1560]
Mar 24, 2023
0d438e9
mypy: `test/test_pglookout.py` [BF-1560]
Mar 27, 2023
1f47155
mypy: New method to normalize config [BF-1560]
Mar 27, 2023
5d4ac6e
mypy: `version.py` [BF-1560]
Mar 27, 2023
51bb85e
mypy: `setup.py` [BF-1560]
Mar 27, 2023
e9834d4
mypy: Remove python2 dependency [BF-1560]
Mar 27, 2023
a3ad6f8
mypy: `conftest.py` [BF-1560]
Mar 27, 2023
7434873
Bump `Makefile` version to `2.1.0` [BF-1560]
Mar 27, 2023
30bcd0d
mypy: Compatible with Python 3.9 logging [BF-1560]
Mar 27, 2023
e4e771a
pylint: Enable more messages [BF-1560]
Mar 27, 2023
56c01a8
pylint: Fix broad exception raised [BF-1560]
Mar 28, 2023
09a15b1
pylint: Dev requirements are now pinned [BF-1560]
Mar 28, 2023
9a0abae
make: Dev reqs are now in isolation [BF-1560]
Apr 4, 2023
dac0aad
Merge remote-tracking branch 'origin/main' into sgiffard/BF-1560_add_…
Apr 5, 2023
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
63 changes: 53 additions & 10 deletions pglookout/statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,79 @@

https://github.com/influxdata/telegraf/tree/master/plugins/inputs/statsd
"""
from __future__ import annotations

from typing import Literal

import logging
import socket

StatsdMetricType = Literal[
b"g", # gauge
b"c", # counter
b"s", # set
b"ms", # timing
b"h", # histogram
b"d", # distribution
]
Comment on lines +19 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question Why not using enumerations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checked at static type checking time (when running mypy) and is ignored at runtime.

An enumeration is an actual runtime "thing" (type+value). I don't want to change anything in these PRs (or as little as possible).



class StatsClient:
def __init__(self, host="127.0.0.1", port=8125, tags=None):
self.log = logging.getLogger("StatsClient")
self._dest_addr = (host, port)
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._tags = tags or {}
def __init__(
self,
host: str | None = "127.0.0.1",
port: int = 8125,
tags: dict[str, str] | None = None,
) -> None:
self.log: logging.Logger = logging.getLogger("StatsClient")
self._dest_addr: tuple[str | None, int] = (host, port)
self._socket: socket.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._tags: dict[str, str] = tags or {}

def gauge(self, metric, value, tags=None):
def gauge(
self,
metric: str,
value: int | float | str,
tags: dict[str, str] | None = None,
) -> None:
self._send(metric, b"g", value, tags)

def increase(self, metric, inc_value=1, tags=None):
def increase(
self,
metric: str,
inc_value: int | float = 1,
tags: dict[str, str] | None = None,
) -> None:
self._send(metric, b"c", inc_value, tags)

def timing(self, metric, value, tags=None):
def timing(
self,
metric: str,
value: int | float,
tags: dict[str, str] | None = None,
) -> None:
self._send(metric, b"ms", value, tags)

def unexpected_exception(self, ex, where, tags=None):
def unexpected_exception(
self,
ex: Exception,
where: str,
tags: dict[str, str] | None = None,
) -> None:
all_tags = {
"exception": ex.__class__.__name__,
"where": where,
}
all_tags.update(tags or {})
self.increase("exception", tags=all_tags)

def _send(self, metric, metric_type, value, tags):
def _send(
self,
metric: str,
metric_type: StatsdMetricType,
value: int | float | str,
tags: dict[str, str] | None,
) -> None:
if None in self._dest_addr:
# stats sending is disabled
return
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ exclude = [
'pglookout/cluster_monitor.py',
'pglookout/common.py',
'pglookout/pglookout.py',
'pglookout/statsd.py',
'pglookout/version.py',
'pglookout/webserver.py',
# Tests.
Expand Down