ashb commented on a change in pull request #7376: [AIRFLOW-2906] Add 
datadog(dogstatsd) support to send airflow metrics
URL: https://github.com/apache/airflow/pull/7376#discussion_r378166778
 
 

 ##########
 File path: airflow/stats.py
 ##########
 @@ -135,19 +135,87 @@ def timing(self, stat, dt):
             return self.statsd.timing(stat, dt)
 
 
-Stats = DummyStatsLogger  # type: Any
+class SafeDogStatsdLogger:
 
-try:
-    if conf.getboolean('scheduler', 'statsd_on'):
-        from statsd import StatsClient
+    def __init__(self, dogstatsd_client, 
allow_list_validator=AllowListValidator()):
+        self.dogstatsd = dogstatsd_client
+        self.allow_list_validator = allow_list_validator
+
+    @validate_stat
+    def incr(self, stat, count=1, rate=1, tags=None):
+        if self.allow_list_validator.test(stat):
+            tags = tags or []
+            return self.dogstatsd.increment(metric=stat, value=count, 
tags=tags, sample_rate=rate)
+
+    @validate_stat
+    def decr(self, stat, count=1, rate=1, tags=None):
+        if self.allow_list_validator.test(stat):
+            tags = tags or []
+            return self.dogstatsd.decrement(metric=stat, value=count, 
tags=tags, sample_rate=rate)
+
+    @validate_stat
+    def gauge(self, stat, value, rate=1, delta=False, tags=None):
+        if self.allow_list_validator.test(stat):
+            tags = tags or []
+            return self.dogstatsd.gauge(metric=stat, value=value, tags=tags, 
sample_rate=rate)
 
+    @validate_stat
+    def timing(self, stat, dt, tags=None):
+        if self.allow_list_validator.test(stat):
+            tags = tags or []
+            return self.dogstatsd.timing(metric=stat, value=dt, tags=tags)
+
+
+class _Stats(type):
+    instance = None
+
+    def __getattr__(cls, name):
+        return getattr(cls.instance, name)
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(self)
+        if self.__class__.instance is None:
+            try:
+                is_datadog_enabled_defined = conf.has_option('scheduler', 
'statsd_datadog_enabled')
+                if is_datadog_enabled_defined and conf.getboolean('scheduler', 
'statsd_datadog_enabled'):
+                    self.__class__.instance = self.get_dogstatsd_logger()
+                elif conf.getboolean('scheduler', 'statsd_on'):
+                    self.__class__.instance = self.get_statsd_logger()
+                else:
+                    self.__class__.instance = DummyStatsLogger()
+            except (socket.gaierror, ImportError) as e:
+                log.warning("Could not configure StatsClient: %s, using 
DummyStatsLogger instead.", e)
 
 Review comment:
   Do you need this here too?
   
   ```suggestion
                   log.warning("Could not configure StatsClient: %s, using 
DummyStatsLogger instead.", e)
                   self.__class__.instance = DummyStatsLogger()
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to