Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use seconds for schedule_delay metric when using dogstatsd #19973

Merged
merged 1 commit into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions airflow/stats.py
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
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