Skip to content

Commit d33abd2

Browse files
authored
Prevent leak of tags across check runs (DataDog#20859)
* Add logging and stop storing all tags * unit test * add changelog
1 parent 7709840 commit d33abd2

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

tibco_ems/changelog.d/20859.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix leak of tags across check runs.

tibco_ems/datadog_checks/tibco_ems/tibco_ems.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def __init__(self, name, init_config, instances):
3131
script_path = self.instance.get('script_path')
3232
server_string = CONNECTION_STRING.format(host, port)
3333
self.tags = self.instance.get('tags', [])
34-
self.parsed_data = {}
3534

3635
self.cmd = tibemsadmin_cmd + [
3736
'-server',
@@ -55,18 +54,20 @@ def check(self, _):
5554
sections = self._section_output(cleaned_data)
5655

5756
# Parse the output
57+
parsed_data = {}
5858
for command, section in sections.items():
5959
pattern = SHOW_METRIC_DATA[command]['regex']
6060
if command == 'show server':
61-
self.parsed_data[command] = self._parse_show_server(section, pattern)
61+
parsed_data[command] = self._parse_show_server(section, pattern)
6262
else:
6363
try:
64-
self.parsed_data[command] = self._parse_factory(section, pattern)
64+
parsed_data[command] = self._parse_factory(section, pattern)
6565
except Exception as e:
6666
self.log.error('Error parsing command %s: %s', command, e)
6767
continue
6868

69-
for command, metric_info in self.parsed_data.items():
69+
for command, metric_info in parsed_data.items():
70+
self.log.debug("Processing output from %s command", command)
7071
metric_keys = SHOW_METRIC_DATA[command]['metric_keys']
7172
tag_keys = SHOW_METRIC_DATA[command]['tags']
7273
metric_prefix = SHOW_METRIC_DATA[command]['metric_prefix']
@@ -205,11 +206,14 @@ def sanitize_name(name):
205206
return data
206207

207208
def _submit_metrics_factory(self, prefix, metric_data, metric_names, tag_keys):
209+
self.log.debug("Submitting %s metrics (%s tags) for %s", len(metric_names), len(tag_keys), prefix)
208210
tags = []
209211
for key in tag_keys:
210212
if prefix == 'server':
211213
# Add server tags to all metrics
212-
self.tags.append(f"server_{key}:{metric_data[key]}")
214+
server_tag = f"server_{key}:{metric_data[key]}"
215+
if server_tag not in self.tags:
216+
self.tags.append(server_tag)
213217
else:
214218
if metric_data.get(key):
215219
tags.append(f"{key}:{metric_data[key]}")

tibco_ems/tests/test_unit.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
# All rights reserved
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44

5-
from typing import Any, Callable, Dict # noqa: F401
65
from unittest.mock import MagicMock
76

87
import pytest
98

10-
from datadog_checks.base import AgentCheck # noqa: F401
11-
from datadog_checks.base.stubs.aggregator import AggregatorStub # noqa: F401
129
from datadog_checks.dev.utils import get_metadata_metrics
1310
from datadog_checks.tibco_ems import TibcoEMSCheck
1411

@@ -152,3 +149,17 @@ def test_parse_factory(data, regex, expected_result):
152149
result = check._parse_factory(data.decode('utf-8'), regex)
153150

154151
assert result == expected_result
152+
153+
154+
def test_base_tags(dd_run_check, instance):
155+
check = TibcoEMSCheck('tibco_ems', {}, [instance])
156+
check.run_tibco_command = MagicMock(return_value=mock_output('show_all'))
157+
dd_run_check(check)
158+
assert len(check.tags) == 3
159+
160+
dd_run_check(check)
161+
dd_run_check(check)
162+
dd_run_check(check)
163+
164+
# assert the lenght of tags does not grow indefinitely
165+
assert len(check.tags) == 3

0 commit comments

Comments
 (0)