Skip to content

Commit

Permalink
Correctly send timing metrics when using dogstatsd (fix schedule_dela…
Browse files Browse the repository at this point in the history
…y metric) (#19973)
  • Loading branch information
lostkamp authored Dec 15, 2021
1 parent dad2f81 commit 5d405d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions airflow/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
# specific language governing permissions and limitations
# under the License.

import datetime
import logging
import socket
import string
import time
from functools import wraps
from typing import TYPE_CHECKING, Callable, Optional, TypeVar, cast
from typing import TYPE_CHECKING, Callable, List, Optional, TypeVar, Union, cast

from airflow.configuration import conf
from airflow.exceptions import AirflowConfigException, InvalidStatsNameException
Expand Down Expand Up @@ -64,7 +65,7 @@ def gauge(cls, stat: str, value: float, rate: int = 1, delta: bool = False) -> N
"""Gauge stat"""

@classmethod
def timing(cls, stat: str, dt) -> None:
def timing(cls, stat: str, dt: Union[float, datetime.timedelta]) -> None:
"""Stats timing"""

@classmethod
Expand Down Expand Up @@ -317,10 +318,12 @@ def gauge(self, stat, value, rate=1, delta=False, tags=None):
return None

@validate_stat
def timing(self, stat, dt, tags=None):
def timing(self, stat, dt: Union[float, datetime.timedelta], tags: Optional[List[str]] = None):
"""Stats timing"""
if self.allow_list_validator.test(stat):
tags = tags or []
if isinstance(dt, datetime.timedelta):
dt = dt.total_seconds()
return self.dogstatsd.timing(metric=stat, value=dt, tags=tags)
return None

Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ def test_empty_timer(self):
self.dogstatsd_client.timed.assert_not_called()

def test_timing(self):
import datetime

self.dogstatsd.timing("dummy_timer", 123)
self.dogstatsd_client.timing.assert_called_once_with(metric='dummy_timer', value=123, tags=[])

self.dogstatsd.timing("dummy_timer", datetime.timedelta(seconds=123))
self.dogstatsd_client.timing.assert_called_with(metric='dummy_timer', value=123.0, tags=[])

def test_gauge(self):
self.dogstatsd.gauge("dummy", 123)
self.dogstatsd_client.gauge.assert_called_once_with(metric='dummy', sample_rate=1, value=123, tags=[])
Expand Down

0 comments on commit 5d405d9

Please sign in to comment.