Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: Add tags back to metrics.
Browse files Browse the repository at this point in the history
While this package does not use the officially sanctioned "markus"
metrics package, it does use the same Datadog library.

Closes #1400
  • Loading branch information
jrconlin committed Jun 26, 2020
1 parent b5f2dca commit be216ea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 53 deletions.
43 changes: 15 additions & 28 deletions autopush/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from txstatsd.client import StatsDClientProtocol, TwistedStatsDClient
from txstatsd.metrics.metrics import Metrics

import mock
import datadog
from datadog import ThreadStats

Expand Down Expand Up @@ -59,26 +60,6 @@ def timing(self, name, duration, **kwargs):
pass


class TwistedMetrics(object):
"""Twisted implementation of statsd output"""
def __init__(self, statsd_host="localhost", statsd_port=8125):
self.client = TwistedStatsDClient.create(statsd_host, statsd_port)
self._metric = Metrics(connection=self.client, namespace="autopush")

def start(self):
protocol = StatsDClientProtocol(self.client)
reactor.listenUDP(0, protocol)

def increment(self, name, count=1, **kwargs):
self._metric.increment(name, count)

def gauge(self, name, count, **kwargs):
self._metric.gauge(name, count)

def timing(self, name, duration, **kwargs):
self._metric.timing(name, duration)


def make_tags(base=None, **kwargs):
# type: (Sequence[str], **Any) -> Sequence[str]
"""Generate a list of tag values"""
Expand All @@ -89,11 +70,17 @@ def make_tags(base=None, **kwargs):

class DatadogMetrics(object):
"""DataDog Metric backend"""
def __init__(self, api_key, app_key, hostname, flush_interval=10,
namespace="autopush"):

datadog.initialize(api_key=api_key, app_key=app_key,
host_name=hostname)
def __init__(self, api_key=None, app_key=None,
hostname=None, statsd_host=None, statsd_port=None,
flush_interval=10, namespace="autopush"):

datadog.initialize(
api_key=api_key,
app_key=app_key,
host_name=hostname,
statsd_host=statsd_host,
statsd_port=statsd_port,
)
self._client = ThreadStats()
self._flush_interval = flush_interval
self._host = hostname
Expand Down Expand Up @@ -122,16 +109,16 @@ def timing(self, name, duration, **kwargs):
def from_config(conf):
# type: (AutopushConfig) -> IMetrics
"""Create an IMetrics from the given config"""
if conf.datadog_api_key:
if conf.statsd_host and not isinstance(conf.statsd_host, mock.Mock):
return DatadogMetrics(
hostname=logging.instance_id_or_hostname if conf.ami_id else
conf.hostname,
api_key=conf.datadog_api_key,
app_key=conf.datadog_app_key,
statsd_host=conf.statsd_host,
statsd_port=conf.statsd_port,
flush_interval=conf.datadog_flush_interval,
)
elif conf.statsd_host:
return TwistedMetrics(conf.statsd_host, conf.statsd_port)
else:
return SinkMetrics()

Expand Down
17 changes: 0 additions & 17 deletions autopush/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from autopush.metrics import (
IMetrics,
DatadogMetrics,
TwistedMetrics,
SinkMetrics,
periodic_reporter,
)
Expand All @@ -35,22 +34,6 @@ def test_passing(self):
assert sm.timing("test", 10) is None


class TwistedMetricsTestCase(unittest.TestCase):
@patch("autopush.metrics.reactor")
def test_basic(self, mock_reactor):
twisted.internet.base.DelayedCall.debug = True
m = TwistedMetrics('127.0.0.1')
m.start()
assert len(mock_reactor.mock_calls) > 0
m._metric = Mock()
m.increment("test", 5)
m._metric.increment.assert_called_with("test", 5)
m.gauge("connection_count", 200)
m._metric.gauge.assert_called_with("connection_count", 200)
m.timing("lifespan", 113)
m._metric.timing.assert_called_with("lifespan", 113)


class DatadogMetricsTestCase(unittest.TestCase):
@patch("autopush.metrics.datadog")
def test_basic(self, mock_dog):
Expand Down
4 changes: 1 addition & 3 deletions autopush/tests/test_z_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ def setUp(self):
patchers = [
"autopush.main.TimerService.startService",
"autopush.main.reactor",
"autopush.metrics.TwistedMetrics",
]
self.mocks = {}
for name in patchers:
Expand Down Expand Up @@ -274,7 +273,7 @@ class TestArg(AutopushConfig):
datadog_flush_interval = "datadog_flush_interval"
hostname = "hostname"
statsd_host = "statsd_host"
statsd_port = "statsd_port"
statsd_port = 8125
router_tablename = "none"
router_read_throughput = 0
router_write_throughput = 0
Expand Down Expand Up @@ -323,7 +322,6 @@ def setUp(self):
"autopush.db.preflight_check",
"autopush.main.TimerService.startService",
"autopush.main.reactor",
"autopush.metrics.TwistedMetrics",
]
self.mocks = {}
for name in patchers:
Expand Down
5 changes: 0 additions & 5 deletions docs/api/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ Implementations
:special-members: __init__
:member-order: bysource

.. autoclass:: TwistedMetrics
:members:
:special-members: __init__
:member-order: bysource

.. autoclass:: DatadogMetrics
:members:
:special-members: __init__
Expand Down

0 comments on commit be216ea

Please sign in to comment.