-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Add typing to sentry.utils.metrics, fix bug where metric tags we…
…re lost (#48512)
- Loading branch information
Showing
13 changed files
with
234 additions
and
79 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
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 |
---|---|---|
@@ -1,46 +1,69 @@ | ||
__all__ = ["DogStatsdMetricsBackend"] | ||
from typing import Any, Optional, Union | ||
|
||
from datadog import initialize, statsd | ||
|
||
from .base import MetricsBackend | ||
from .base import MetricsBackend, Tags | ||
|
||
__all__ = ["DogStatsdMetricsBackend"] | ||
|
||
|
||
class DogStatsdMetricsBackend(MetricsBackend): | ||
def __init__(self, prefix=None, **kwargs): | ||
def __init__(self, prefix: Optional[str] = None, **kwargs: Any) -> None: | ||
# TODO(dcramer): it'd be nice if the initialize call wasn't a global | ||
self.tags = kwargs.pop("tags", None) | ||
initialize(**kwargs) | ||
super().__init__(prefix=prefix) | ||
|
||
def incr(self, key, instance=None, tags=None, amount=1, sample_rate=1): | ||
if tags is None: | ||
tags = {} | ||
def incr( | ||
self, | ||
key: str, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
amount: Union[float, int] = 1, | ||
sample_rate: float = 1, | ||
) -> None: | ||
tags = dict(tags or ()) | ||
|
||
if self.tags: | ||
tags.update(self.tags) | ||
if instance: | ||
tags["instance"] = instance | ||
if tags: | ||
tags = [f"{k}:{v}" for k, v in tags.items()] | ||
statsd.increment(self._get_key(key), amount, sample_rate=sample_rate, tags=tags) | ||
|
||
def timing(self, key, value, instance=None, tags=None, sample_rate=1): | ||
if tags is None: | ||
tags = {} | ||
tags_list = [f"{k}:{v}" for k, v in tags.items()] | ||
statsd.increment(self._get_key(key), amount, sample_rate=sample_rate, tags=tags_list) | ||
|
||
def timing( | ||
self, | ||
key: str, | ||
value: float, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
sample_rate: float = 1, | ||
) -> None: | ||
tags = dict(tags or ()) | ||
|
||
if self.tags: | ||
tags.update(self.tags) | ||
if instance: | ||
tags["instance"] = instance | ||
if tags: | ||
tags = [f"{k}:{v}" for k, v in tags.items()] | ||
statsd.timing(self._get_key(key), value, sample_rate=sample_rate, tags=tags) | ||
|
||
def gauge(self, key, value, instance=None, tags=None, sample_rate=1): | ||
if tags is None: | ||
tags = {} | ||
tags_list = [f"{k}:{v}" for k, v in tags.items()] | ||
statsd.timing(self._get_key(key), value, sample_rate=sample_rate, tags=tags_list) | ||
|
||
def gauge( | ||
self, | ||
key: str, | ||
value: float, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
sample_rate: float = 1, | ||
) -> None: | ||
tags = dict(tags or ()) | ||
|
||
if self.tags: | ||
tags.update(self.tags) | ||
if instance: | ||
tags["instance"] = instance | ||
if tags: | ||
tags = [f"{k}:{v}" for k, v in tags.items()] | ||
statsd.gauge(self._get_key(key), value, sample_rate=sample_rate, tags=tags) | ||
|
||
tags_list = [f"{k}:{v}" for k, v in tags.items()] | ||
statsd.gauge(self._get_key(key), value, sample_rate=sample_rate, tags=tags_list) |
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,14 +1,37 @@ | ||
__all__ = ["DummyMetricsBackend"] | ||
from typing import Optional, Union | ||
|
||
from .base import MetricsBackend, Tags | ||
|
||
from .base import MetricsBackend | ||
__all__ = ["DummyMetricsBackend"] | ||
|
||
|
||
class DummyMetricsBackend(MetricsBackend): | ||
def incr(self, key, instance=None, tags=None, amount=1, sample_rate=1): | ||
def incr( | ||
self, | ||
key: str, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
amount: Union[float, int] = 1, | ||
sample_rate: float = 1, | ||
) -> None: | ||
pass | ||
|
||
def timing(self, key, value, instance=None, tags=None, sample_rate=1): | ||
def timing( | ||
self, | ||
key: str, | ||
value: float, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
sample_rate: float = 1, | ||
) -> None: | ||
pass | ||
|
||
def gauge(self, key, value, instance=None, tags=None, sample_rate=1): | ||
def gauge( | ||
self, | ||
key: str, | ||
value: float, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
sample_rate: float = 1, | ||
) -> None: | ||
pass |
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,18 +1,40 @@ | ||
import logging | ||
from typing import Optional, Union | ||
|
||
from .base import MetricsBackend | ||
from .base import MetricsBackend, Tags | ||
|
||
logger = logging.getLogger("sentry.metrics") | ||
|
||
|
||
class LoggingBackend(MetricsBackend): | ||
def incr(self, key, instance=None, tags=None, amount=1, sample_rate=1): | ||
def incr( | ||
self, | ||
key: str, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
amount: Union[float, int] = 1, | ||
sample_rate: float = 1, | ||
) -> None: | ||
logger.debug("%r: %+g", key, amount, extra={"instance": instance, "tags": tags or {}}) | ||
|
||
def timing(self, key, value, instance=None, tags=None, sample_rate=1): | ||
def timing( | ||
self, | ||
key: str, | ||
value: float, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
sample_rate: float = 1, | ||
) -> None: | ||
logger.debug( | ||
"%r: %g ms", key, value * 1000, extra={"instance": instance, "tags": tags or {}} | ||
) | ||
|
||
def gauge(self, key, value, instance=None, tags=None, sample_rate=1): | ||
def gauge( | ||
self, | ||
key: str, | ||
value: float, | ||
instance: Optional[str] = None, | ||
tags: Optional[Tags] = None, | ||
sample_rate: float = 1, | ||
) -> None: | ||
logger.debug("%r: %+g", key, value, extra={"instance": instance, "tags": tags or {}}) |
Oops, something went wrong.