Skip to content

Commit

Permalink
Updating count and gauage with timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosroman committed May 3, 2024
1 parent a7e337d commit 717b7c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
50 changes: 21 additions & 29 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,6 @@ def gauge(
value, # type: float
tags=None, # type: Optional[List[str]]
sample_rate=None, # type: Optional[float]
timestamp=0, # type:int
): # type(...) -> None
"""
Record the value of a gauge, optionally setting a list of tags and a
Expand All @@ -784,7 +783,7 @@ def gauge(
>>> statsd.gauge("users.online", 123)
>>> statsd.gauge("active.connections", 1001, tags=["protocol:http"])
"""
return self._report(metric, "g", value, tags, sample_rate, timestamp)
return self._report(metric, "g", value, tags, sample_rate)

# Minimum Datadog Agent version: 7.40.0
def gauge_with_timestamp(
Expand All @@ -806,79 +805,72 @@ def gauge_with_timestamp(
"""
return self._report(metric, "g", value, tags, sample_rate, timestamp)

def increment(
def count(
self,
metric, # type: Text
value=1, # type: float
value, # type: float
tags=None, # type: Optional[List[str]]
sample_rate=None, # type: Optional[float]
): # type: (...) -> None
): # type(...) -> None
"""
Increment a counter, optionally setting a value, tags and a sample
Count tracks how many times something happened per second, tags and a sample
rate.
>>> statsd.increment("page.views")
>>> statsd.increment("files.transferred", 124)
>>> statsd.count("page.views", 123)
"""
self._report(metric, "c", value, tags, sample_rate)

# Minimum Datadog Agent version: 7.40.0
def increment_with_timestamp(
def count_with_timestamp(
self,
metric, # type: Text
value=1, # type: float
value, # type: float
timestamp=0, # type: int
tags=None, # type: Optional[List[str]]
sample_rate=None, # type: Optional[float]
): # type: (...) -> None
): # type(...) -> None
"""
Increment a counter, optionally setting a value, a Unix timestamp
in seconds, tags and a sample rate.
Count how many times something happened at a given Unix timestamp in seconds,
tags and a sample rate.
Minimum Datadog Agent version: 7.40.0
>>> statsd.increment("page.views", timestamp=1713804588)
>>> statsd.increment("files.transferred", 124, timestamp=1713804588)
>>> statsd.count("files.transferred", 124, timestamp=1713804588)
"""
self._report(metric, "c", value, tags, sample_rate, timestamp)

def decrement(
def increment(
self,
metric, # type: Text
value=1, # type: float
tags=None, # type: Optional[List[str]]
sample_rate=None, # type: Optional[float]
): # type(...) -> None
"""
Decrement a counter, optionally setting a value, tags and a sample
Increment a counter, optionally setting a value, tags and a sample
rate.
>>> statsd.decrement("files.remaining")
>>> statsd.decrement("active.connections", 2)
>>> statsd.increment("page.views")
>>> statsd.increment("files.transferred", 124)
"""
metric_value = -value if value else value
self._report(metric, "c", metric_value, tags, sample_rate)
self._report(metric, "c", value, tags, sample_rate)

# Minimum Datadog Agent version: 7.40.0
def decrement_with_timestamp(
def decrement(
self,
metric, # type: Text
value=1, # type: float
timestamp=0, # type:int
tags=None, # type: Optional[List[str]]
sample_rate=None, # type: Optional[float]
): # type(...) -> None
"""
Decrement a counter, optionally setting a value, a Unix timestamp
in seconds, tags and a sample rate.
Minimum Datadog Agent version: 7.40.0
Decrement a counter, optionally setting a value, tags and a sample
rate.
>>> statsd.decrement("files.remaining")
>>> statsd.decrement("active.connections", 2)
"""
metric_value = -value if value else value
self._report(metric, "c", metric_value, tags, sample_rate, timestamp)
self._report(metric, "c", metric_value, tags, sample_rate)

def histogram(
self,
Expand Down
15 changes: 10 additions & 5 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,23 @@ def test_counter(self):
self.statsd.flush()
self.assert_equal_telemetry('page.views:-12|c\n', self.recv(2))

def test_counter_with_ts(self):
self.statsd.increment_with_timestamp("page.views", timestamp=1066)
def test_count(self):
self.statsd.count('page.views', 11)
self.statsd.flush()
self.assert_equal_telemetry('page.views:11|c\n', self.recv(2))

def test_count_with_ts(self):
self.statsd.count_with_timestamp("page.views", 1, timestamp=1066)
self.statsd.flush()
self.assert_equal_telemetry("page.views:1|c|T1066\n", self.recv(2))

self.statsd._reset_telemetry()
self.statsd.increment_with_timestamp("page.views", 11, timestamp=2121)
self.statsd.count_with_timestamp("page.views", 11, timestamp=2121)
self.statsd.flush()
self.assert_equal_telemetry("page.views:11|c|T2121\n", self.recv(2))

def test_counter_with_invalid_ts_should_be_ignored(self):
self.statsd.increment_with_timestamp("page.views", timestamp=-1066)
def test_count_with_invalid_ts_should_be_ignored(self):
self.statsd.count_with_timestamp("page.views", 1, timestamp=-1066)
self.statsd.flush()
self.assert_equal_telemetry("page.views:1|c\n", self.recv(2))

Expand Down

0 comments on commit 717b7c7

Please sign in to comment.