-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from appsignal/implement-minutely-probes
Implement minutely probes
- Loading branch information
Showing
8 changed files
with
300 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
bump: "minor" | ||
type: "add" | ||
--- | ||
|
||
Add a minutely probes system. This can be used, alongside our metric helpers, to report metrics to AppSignal once per minute. | ||
|
||
```python | ||
from appsignal import probes, set_gauge | ||
|
||
def new_carts(previous_carts=None): | ||
current_carts = Cart.objects.all().count() | ||
|
||
if previous_carts is not None: | ||
set_gauge("new_carts", current_carts - previous_carts) | ||
|
||
return current_carts | ||
|
||
probes.register("new_carts", new_carts) | ||
``` | ||
|
||
The minutely probes system starts by default, but no probes are automatically registered. You can use the `enable_minutely_probes` configuration option to disable it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from inspect import signature | ||
from threading import Event, Lock, Thread | ||
from time import gmtime | ||
from typing import Any, Callable, Optional, TypeVar, Union, cast | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
Probe = Union[Callable[[], None], Callable[[Optional[T]], Optional[T]]] | ||
|
||
_probes: dict[str, Probe] = {} | ||
_probe_states: dict[str, Any] = {} | ||
_lock: Lock = Lock() | ||
_thread: Thread | None = None | ||
_stop_event: Event = Event() | ||
|
||
|
||
def start() -> None: | ||
global _thread | ||
if _thread is None: | ||
_thread = Thread(target=_minutely_loop, daemon=True) | ||
_thread.start() | ||
|
||
|
||
def _minutely_loop() -> None: | ||
wait_time = _initial_wait_time() | ||
|
||
while True: | ||
if _stop_event.wait(timeout=wait_time): | ||
break | ||
|
||
_run_probes() | ||
wait_time = _wait_time() | ||
|
||
|
||
def _run_probes() -> None: | ||
with _lock: | ||
for name in _probes: | ||
_run_probe(name) | ||
|
||
|
||
def _run_probe(name: str) -> None: | ||
logger = logging.getLogger("appsignal") | ||
logger.debug(f"Gathering minutely metrics with `{name}` probe") | ||
|
||
try: | ||
probe = _probes[name] | ||
|
||
if len(signature(probe).parameters) > 0: | ||
probe = cast(Callable[[Any], Any], probe) | ||
state = _probe_states.get(name) | ||
result = probe(state) | ||
_probe_states[name] = result | ||
else: | ||
probe = cast(Callable[[], None], probe) | ||
probe() | ||
|
||
except Exception as e: | ||
logger.debug(f"Error in minutely probe `{name}`: {e}") | ||
|
||
|
||
def _wait_time() -> int: | ||
return 60 - gmtime().tm_sec | ||
|
||
|
||
def _initial_wait_time() -> int: | ||
remaining_seconds = _wait_time() | ||
if remaining_seconds > 30: | ||
return remaining_seconds | ||
|
||
return remaining_seconds + 60 | ||
|
||
|
||
def register(name: str, probe: Probe) -> None: | ||
with _lock: | ||
if name in _probes: | ||
logger = logging.getLogger("appsignal") | ||
logger.debug( | ||
f"A probe with the name `{name}` is already " | ||
"registered. Overwriting the entry with the new probe." | ||
) | ||
|
||
_probes[name] = probe | ||
|
||
|
||
def unregister(name: str) -> None: | ||
with _lock: | ||
if name in _probes: | ||
del _probes[name] | ||
if name in _probe_states: | ||
del _probe_states[name] | ||
|
||
|
||
def stop() -> None: | ||
global _thread | ||
if _thread is not None: | ||
_stop_event.set() | ||
_thread.join() | ||
_thread = None | ||
_stop_event.clear() | ||
|
||
|
||
def clear() -> None: | ||
with _lock: | ||
_probes.clear() | ||
_probe_states.clear() |
Submodule diagnose
updated
3 files
+22 −18 | spec/diagnose_spec.rb | |
+1 −1 | spec/support/output_helper.rb | |
+4 −3 | spec/support/runner.rb |
Oops, something went wrong.