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

Prevent aggregate_last_24_hours option from affecting global state #16033

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions snowflake/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

***Fixed***:

* Fixed bug where setting the `aggregate_last_24_hours` option to `true` would not be honored when other instances had it set it to `false` (the default) ([#16033](https://github.com/DataDog/integrations-core/pull/16033))
alopezz marked this conversation as resolved.
Show resolved Hide resolved

## 5.0.0 / 2023-08-10 / Agent 7.48.0

***Changed***:
Expand Down
20 changes: 15 additions & 5 deletions snowflake/datadog_checks/snowflake/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,26 @@ def __init__(self, *args, **kwargs):
)
self.metric_queries = []
self.errors = []

# Collect queries corresponding to groups provided in the config
for mgroup in self._config.metric_groups:
try:
if not self._config.aggregate_last_24_hours:
for query in range(len(metric_groups[mgroup])):
metric_groups[mgroup][query]['query'] = metric_groups[mgroup][query]['query'].replace(
'DATEADD(hour, -24, current_timestamp())', 'date_trunc(day, current_date)'
)
self.metric_queries.extend(metric_groups[mgroup])
except KeyError:
self.errors.append(mgroup)
continue

if not self._config.aggregate_last_24_hours:
# Modify queries to use legacy time aggregation behavior
self.metric_queries = [
{
**query,
'query': query['query'].replace(
'DATEADD(hour, -24, current_timestamp())', 'date_trunc(day, current_date)'
),
}
for query in self.metric_queries
]

if self.errors:
self.log.warning(
Expand Down
1 change: 1 addition & 0 deletions snowflake/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'role': 'ACCOUNTADMIN',
'disable_generic_tags': True,
'login_timeout': 3,
'aggregate_last_24_hours': True,
}

OAUTH_INSTANCE = {
Expand Down
31 changes: 31 additions & 0 deletions snowflake/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,34 @@ def test_aggregate_last_24_hours_queries(aggregate_last_24_hours, expected_query

# Only one query configured
assert check.metric_queries[0]['query'] == expected_query


def test_aggregate_last_24_hours_queries_multiple_instances():
# This test checks that the `aggregate_last_24_hours` setting doesn't leak into other instances.
inst_default = {
'metric_groups': ['snowflake.replication'],
'username': 'user',
'password': 'password',
'account': 'account',
'role': 'role',
}

inst_last_24_hours = dict(inst_default)

inst_default['aggregate_last_24_hours'] = False
inst_last_24_hours['aggregate_last_24_hours'] = True

check_default = SnowflakeCheck(CHECK_NAME, {}, [inst_default])
check_last_24_hours = SnowflakeCheck(CHECK_NAME, {}, [inst_last_24_hours])

assert check_default.metric_queries[0]['query'] == (
'select database_name, avg(credits_used), sum(credits_used), '
'avg(bytes_transferred), sum(bytes_transferred) from replication_usage_history '
'where start_time >= date_trunc(day, current_date) group by 1;'
)

assert check_last_24_hours.metric_queries[0]['query'] == (
'select database_name, avg(credits_used), sum(credits_used), '
'avg(bytes_transferred), sum(bytes_transferred) from replication_usage_history '
'where start_time >= DATEADD(hour, -24, current_timestamp()) group by 1;'
)