Skip to content

Commit

Permalink
feat: add timeout to config
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaxde committed Jan 9, 2025
1 parent c918b82 commit 0d500ee
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions metricq_source_snmp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import click
import click_log # type: ignore
import metricq
from metricq.cli import metricq_command
from metricq import Timedelta
from metricq.cli import metricq_command, DurationParam
from metricq.logging import get_logger
from click import option
from pysnmp.hlapi.asyncio import CommunityData # type: ignore
from pysnmp.hlapi.asyncio import (
ContextData,
Expand All @@ -32,6 +34,8 @@

T = TypeVar("T")

global_timeout = 1.0
TIMEOUT = DurationParam(default=Timedelta.from_s(global_timeout))

# https://stackoverflow.com/a/2135920
def split(a: list[T], n: int) -> Iterable[list[T]]:
Expand Down Expand Up @@ -76,7 +80,7 @@ async def get_one(
result: Awaitable[tuple[str, str, int, Sequence[Any]]] = await getCmd(
snmp_engine,
CommunityData(community),
UdpTransportTarget(host, timeout=1.0, retries=3),
UdpTransportTarget(host, timeout=global_timeout, retries=3),
ContextData(),
*objs,
)
Expand Down Expand Up @@ -362,7 +366,17 @@ def on_signal(self, signal: str) -> None:


@metricq_command(default_token="snmp-source")
def run(server: str, token: str) -> None:
@option(
"--timeout",
type=TIMEOUT,
default=TIMEOUT.default,
help="A timeout for the program",
show_default=True,
)
def run(server: str, token: str, timeout: Timedelta) -> None:
global global_timeout
global_timeout = timeout.s

src = SnmpSource(token=token, management_url=server)
src.run()

Expand Down

0 comments on commit 0d500ee

Please sign in to comment.