from twisted.internet import reactor
from txstatsd.client import StatsDClientProtocol, TwistedStatsDClient
from txstatsd.metrics.metrics import Metrics

import datadog
from datadog import ThreadStats
from datadog.util.hostname import get_hostname


class TwistedMetrics(object):
    def __init__(self, statsd_host="localhost", statsd_port=8125):
        self.client = TwistedStatsDClient(statsd_host, statsd_port)
        self._metric = Metrics(connection=self.client, namespace="pushgo")

    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):
        self._metric.timing(name, duration)


class DatadogMetrics(object):
    def __init__(self, api_key, app_key, flush_interval=10):
        datadog.initialize(api_key=api_key, app_key=app_key)
        self._client = ThreadStats()
        self._flush_interval = flush_interval

        self._host = get_hostname()

    def start(self):
        self._client.start(flush_interval=self._flush_interval,
                           roll_up_interval=self._flush_interval)

    def increment(self, name, count=1, **kwargs):
        self._client.increment(name, count, host=self._host, **kwargs)

    def gauge(self, name, count, **kwargs):
        self._client.gauge(name, count, host=self._host, **kwargs)

    def timing(self, name, duration, **kwargs):
        self._client.timing(name, value=duration, host=self._host, **kwargs)

@@ -949,19 +949,59 @@
 erval=10
+,%0A                 namespace=%22autopush%22
 ):%0A
+%0A
         
@@ -1136,17 +1136,16 @@
 nterval%0A
-%0A
         
@@ -1171,16 +1171,136 @@
 stname()
+%0A        self._namespace = namespace%0A%0A    def _prefix_name(self, name):%0A        return %22%25s.%25s%22 %25 (self._namespace, name)
 %0A%0A    de
@@ -1522,28 +1522,47 @@
 t.increment(
+self._prefix_name(
 name
+)
 , count, hos
@@ -1566,32 +1566,63 @@
 host=self._host,
+%0A                              
  **kwargs)%0A%0A    
@@ -1684,28 +1684,47 @@
 lient.gauge(
-name
+self._prefix_name(name)
 , count, hos
@@ -1728,32 +1728,59 @@
 host=self._host,
+%0A                          
  **kwargs)%0A%0A    
@@ -1855,29 +1855,76 @@
 ing(
-name, value=duration,
+self._prefix_name(name), value=duration,%0A                           
  hos

add namespace option