ashb commented on a change in pull request #12353:
URL: https://github.com/apache/airflow/pull/12353#discussion_r524279665



##########
File path: airflow/stats.py
##########
@@ -72,20 +73,71 @@ def timer(cls, *args, **kwargs) -> TimerProtocol:
         """Timer metric that can be cancelled"""
 
 
-class DummyTimer:
-    """No-op timer"""
+class Timer:
+    """
+    Timer that records duration, and optional sends to statsd backend.
+
+    This class lets us have an accurate timer with the logic in one place (so
+    that we don't use datetime math for duration -- it is error prone).
+
+    Example usage:
+
+    .. code-block:: python
+
+        with Stats.timer() as t:
+            # Something to time
+            frob_the_foos()
+
+        log.info("Frobbing the foos took %.2f", t.duration)
+
+    To send a metric:
+
+    .. code-block:: python
+
+        with Stats.timer("foos.frob"):
+            # Something to time
+            frob_the_foos()
+
+
+    Or both:
+
+    .. code-block:: python
+
+        with Stats.timer("foos.frob") as t:
+            # Something to time
+            frob_the_foos()
+
+        log.info("Frobbing the foos took %.2f", t.duration)
+
+    """
+
+    # pystatsd and dogstatsd both have a timer class, but present different API
+    # so we can't use this as a mixin on those, instead this class is contains 
the "real" timer
+
+    _start_time: Optional[int]
+    duration: Optional[int]
+    ms: Optional[int]
+    _send: bool = False

Review comment:
       Done.




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to