forked from omergertel/pyformance
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #3 from Lightricks/feature/events
Events: support events.
- Loading branch information
Showing
24 changed files
with
625 additions
and
216 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
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.4" | ||
__version__ = "1.0.0" |
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,19 @@ | ||
class MarkInt: | ||
""" | ||
Mark metric value as and integer. | ||
Reporters such as influx require consistent data types for metrics and require you | ||
to mark integer values with an "i" suffix. This is here to let Influx know it should | ||
do so for the value it's initialized with. | ||
""" | ||
def __init__(self, value): | ||
self.value = int(value) | ||
|
||
def __str__(self): | ||
return str(self.value) | ||
|
||
def __repr__(self): | ||
return f"MarkInt({self.value})" | ||
|
||
def __eq__(self, other): | ||
return isinstance(other, MarkInt) and other.value == self.value |
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,59 @@ | ||
import copy | ||
from dataclasses import dataclass | ||
from threading import Lock | ||
from typing import Any, Dict | ||
|
||
from .base_metric import BaseMetric | ||
|
||
|
||
@dataclass | ||
class EventPoint: | ||
time: int | ||
values: Dict[str, Any] | ||
|
||
|
||
class Event(BaseMetric): | ||
""" | ||
Report events as specific data points in specific timestamps | ||
This meter is outside of DropWizard's models and is here to support a specific use case of | ||
infrequently running cron like operations that trigger once in a while, do a bunch of work | ||
and dump the metric results for a single timestamp. Unlike all the other meter types, this one | ||
doesn't repeat itself if no activity occurs leading you to think everything is running | ||
constantly and producing data when it is not. | ||
The closest you can get to the same effect without this class is by using a Gauge, setting the | ||
value, invoking report_now, than clearing it right after. | ||
Since those operations above are not within a lock shared by scheduled reporters , it can still | ||
report the gauge twice. | ||
Additionally when using gauges you don't have any control over the name of the field writen to | ||
(just metric name and tags), and can't write a bunch of | ||
values at once but resort to writing values to separate Gauges which will make the lack of | ||
lock condition more likely to be an issue. | ||
Another problem that will pop in such usage is that the metric will still be written, it will | ||
just be written with the initial value of 0, so you won't be able to tell when was the last | ||
successful run with ease. | ||
""" | ||
|
||
def __init__(self, clock, key, tags=None): | ||
super(Event, self).__init__(key, tags) | ||
self.lock = Lock() | ||
self.points = [] | ||
self.clock = clock | ||
|
||
def add(self, values: Dict[str, Any]): | ||
with self.lock: | ||
self.points.append(EventPoint( | ||
time=self.clock.time(), | ||
values=values | ||
)) | ||
|
||
def clear(self): | ||
with self.lock: | ||
self.points = [] | ||
|
||
def get_events(self): | ||
with self.lock: | ||
return copy.copy(self.points) |
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
Oops, something went wrong.