Skip to content

Commit

Permalink
test config
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Nov 21, 2024
1 parent ea19db1 commit 8f66b8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
9 changes: 2 additions & 7 deletions mongo/datadog_checks/mongo/collectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, check, tags):
self.gauge = self.check.gauge
self.base_tags = tags
self.metrics_to_collect = self.check.metrics_to_collect
self._collection_interval = None
self._collector_key = (self.__class__.__name__,)

def collect(self, api):
Expand Down Expand Up @@ -141,16 +142,10 @@ def collection_interval_checker(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
current_time = time.time()
# Check if _collection_interval and _last_collection_timestamp exist
# If not, run the function to collect the metrics
if not hasattr(self, '_collection_interval'):
self.set_last_collection_timestamp(current_time)
return func(self, *args, **kwargs)
# If _collection_interval not set or set to the check default, call the function to collect the metrics
if (
self._collection_interval is None
or self._collection_interval <= 0 # Ensure the interval is valid
or self._collection_interval == self.check._config.min_collection_interval # Check default
or self._collection_interval <= self.check._config.min_collection_interval # Ensure the interval is valid
):
self.set_last_collection_timestamp(current_time)
return func(self, *args, **kwargs)
Expand Down
8 changes: 4 additions & 4 deletions mongo/datadog_checks/mongo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ def metrics_collection_interval(self):
'''
return {
# $collStats and $indexStats are collected on every check run but they can get expensive on large databases
'collection': self._metrics_collection_interval.get('collection', self.min_collection_interval),
'collections_indexes_stats': self._metrics_collection_interval.get(
'collections_indexes_stats', self.min_collection_interval
'collection': int(self._metrics_collection_interval.get('collection', self.min_collection_interval)),
'collections_indexes_stats': int(
self._metrics_collection_interval.get('collections_indexes_stats', self.min_collection_interval)
),
# $shardDataDistribution stats are collected every 5 minutes by default due to the high resource usage
'sharded_data_distribution': self._metrics_collection_interval.get('sharded_data_distribution', 300),
'sharded_data_distribution': int(self._metrics_collection_interval.get('sharded_data_distribution', 300)),
}
30 changes: 30 additions & 0 deletions mongo/tests/test_unit_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,33 @@ def test_amazon_docdb_cloud_metadata(instance_integration_cluster, aws_cloud_met
assert aws['cluster_identifier'] == aws_cloud_metadata['cluster_identifier']
else:
assert aws['cluster_identifier'] == instance_integration_cluster['cluster_name']


@pytest.mark.parametrize(
'metrics_collection_interval, expected_metrics_collection_interval',
[
pytest.param(
{}, {'collection': 15, 'collections_indexes_stats': 15, 'sharded_data_distribution': 300}, id='default'
),
pytest.param(
{
'collection': '60',
'collections_indexes_stats': '30',
'sharded_data_distribution': '600',
},
{'collection': 60, 'collections_indexes_stats': 30, 'sharded_data_distribution': 600},
id='custom',
),
pytest.param(
{
'collection': 60,
},
{'collection': 60, 'collections_indexes_stats': 15, 'sharded_data_distribution': 300},
id='partial',
),
],
)
def test_metrics_collection_interval(instance, metrics_collection_interval, expected_metrics_collection_interval):
instance['metrics_collection_interval'] = metrics_collection_interval
config = MongoConfig(instance, mock.Mock(), {})
assert config.metrics_collection_interval == expected_metrics_collection_interval

0 comments on commit 8f66b8f

Please sign in to comment.