Skip to content

Commit 3cbe31f

Browse files
authored
chore(telemetry): fix interval set on telemetry metrics (#6025)
Includes the following changes: - An interval should only be set for gauge and rate metrics. Any interval set on count and distribution metrics will be ignored by the backend. This is not a "fix", it more of a "cleanup". - The interval set on gauge and rate metrics is incorrect. This PR ensures the correct value is used (PeriodicService.interval should be used instead of ``DD_TELEMETRY_HEARTBEAT_INTERVAL``). This is a fix for ddwaf metrics that are used internally. Since this fix does not impact users, a release note is not required. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment.
1 parent c318370 commit 3cbe31f

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

ddtrace/internal/telemetry/metrics.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ class CountMetric(Metric):
9797

9898
metric_type = TELEMETRY_METRIC_TYPE_COUNT
9999

100-
def __init__(self, namespace, name, tags, common, interval=None):
101-
super(CountMetric, self).__init__(namespace, name, tags, common, interval)
102-
self.interval = None
103-
104100
def add_point(self, value=1.0):
105101
# type: (float) -> None
106102
"""adds timestamped data point associated with a metric"""
@@ -155,10 +151,6 @@ class DistributionMetric(Metric):
155151

156152
metric_type = TELEMETRY_METRIC_TYPE_DISTRIBUTIONS
157153

158-
def __init__(self, namespace, name, tags, common, interval=None):
159-
super(DistributionMetric, self).__init__(namespace, name, tags, common, interval)
160-
self.interval = None
161-
162154
def add_point(self, value=1.0):
163155
# type: (float) -> None
164156
"""Example:

ddtrace/internal/telemetry/metrics_namespaces.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any
22
from typing import Dict
3+
from typing import Optional
34

45
from ddtrace.internal.telemetry.constants import TELEMETRY_METRIC_TYPE_COUNT
56
from ddtrace.internal.telemetry.constants import TELEMETRY_METRIC_TYPE_DISTRIBUTIONS
@@ -61,8 +62,8 @@ def validate_type_metric(metric_check, metric_to_validate):
6162
return True
6263
return False
6364

64-
def _add_metric(self, metric_type, namespace, name, value=1.0, tags={}, interval=10.0):
65-
# type: (MetricType, str,str, float, MetricTagType, float) -> None
65+
def _add_metric(self, metric_type, namespace, name, value=1.0, tags={}, interval=None):
66+
# type: (MetricType, str,str, float, MetricTagType, Optional[float]) -> None
6667
"""
6768
Telemetry Metrics are stored in DD dashboards, check the metrics in datadoghq.com/metric/explorer.
6869
The metric will store in dashboard as "dd.instrumentation_telemetry_data." + namespace + "." + name

ddtrace/internal/telemetry/writer.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from .data import get_dependencies
3636
from .data import get_host_info
3737
from .metrics import MetricTagType
38-
from .metrics import MetricType
3938
from .metrics_namespaces import MetricNamespace
4039
from .metrics_namespaces import NamespaceMetricType
4140

@@ -274,38 +273,61 @@ def add_gauge_metric(self, namespace, name, value, tags={}):
274273
"""
275274
Queues gauge metric
276275
"""
277-
self._add_metric(TELEMETRY_METRIC_TYPE_GAUGE, namespace, name, value, tags)
276+
if self.enable():
277+
with self._lock:
278+
self._namespace._add_metric(
279+
TELEMETRY_METRIC_TYPE_GAUGE,
280+
namespace,
281+
name,
282+
value,
283+
tags,
284+
interval=self.interval,
285+
)
278286

279287
def add_rate_metric(self, namespace, name, value=1.0, tags={}):
280288
# type: (str,str, float, MetricTagType) -> None
281289
"""
282290
Queues rate metric
283291
"""
284-
self._add_metric(TELEMETRY_METRIC_TYPE_RATE, namespace, name, value, tags)
292+
if self.enable():
293+
with self._lock:
294+
self._namespace._add_metric(
295+
TELEMETRY_METRIC_TYPE_RATE,
296+
namespace,
297+
name,
298+
value,
299+
tags,
300+
interval=self.interval,
301+
)
285302

286303
def add_count_metric(self, namespace, name, value=1.0, tags={}):
287304
# type: (str,str, float, MetricTagType) -> None
288305
"""
289306
Queues count metric
290307
"""
291-
self._add_metric(TELEMETRY_METRIC_TYPE_COUNT, namespace, name, value, tags)
308+
if self.enable():
309+
with self._lock:
310+
self._namespace._add_metric(
311+
TELEMETRY_METRIC_TYPE_COUNT,
312+
namespace,
313+
name,
314+
value,
315+
tags,
316+
)
292317

293318
def add_distribution_metric(self, namespace, name, value=1.0, tags={}):
294319
# type: (str,str, float, MetricTagType) -> None
295320
"""
296321
Queues distributions metric
297322
"""
298-
self._add_metric(TELEMETRY_METRIC_TYPE_DISTRIBUTIONS, namespace, name, value, tags)
299-
300-
def _add_metric(self, metric_type, namespace, name, value=1.0, tags={}):
301-
# type: (MetricType, str,str, float, MetricTagType) -> None
302-
"""
303-
Queues metric
304-
"""
305323
if self.enable():
306324
with self._lock:
307325
self._namespace._add_metric(
308-
metric_type, namespace, name, value, tags, interval=_get_heartbeat_interval_or_default()
326+
TELEMETRY_METRIC_TYPE_DISTRIBUTIONS,
327+
namespace,
328+
name,
329+
value,
330+
tags,
309331
)
310332

311333
def periodic(self):

tests/telemetry/test_telemetry_metrics.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,17 @@ def test_send_appsec_rate_metric(telemetry_metrics_writer, test_agent_metrics_se
220220
expected_series = [
221221
{
222222
"common": True,
223-
"interval": 60,
223+
"interval": 10,
224224
"metric": "test-metric",
225-
"points": [[1642544540, 0.016666666666666666]],
225+
"points": [[1642544540, 0.1]],
226226
"tags": ["hi:hello", "name:candy"],
227227
"type": "rate",
228228
},
229229
{
230230
"common": True,
231-
"interval": 60,
231+
"interval": 10,
232232
"metric": "test-metric",
233-
"points": [[1642544540, 0.03333333333333333]],
233+
"points": [[1642544540, 0.2]],
234234
"tags": [],
235235
"type": "rate",
236236
},
@@ -250,23 +250,23 @@ def test_send_appsec_gauge_metric(telemetry_metrics_writer, test_agent_metrics_s
250250
expected_series = [
251251
{
252252
"common": True,
253-
"interval": 60,
253+
"interval": 10,
254254
"metric": "test-metric",
255255
"points": [[1642544540, 5.0]],
256256
"tags": ["hi:hello", "name:candy"],
257257
"type": "gauge",
258258
},
259259
{
260260
"common": True,
261-
"interval": 60,
261+
"interval": 10,
262262
"metric": "test-metric",
263263
"points": [[1642544540, 5.0]],
264264
"tags": ["a:b"],
265265
"type": "gauge",
266266
},
267267
{
268268
"common": True,
269-
"interval": 60,
269+
"interval": 10,
270270
"metric": "test-metric",
271271
"points": [[1642544540, 6.0]],
272272
"tags": [],

0 commit comments

Comments
 (0)