Skip to content

Commit

Permalink
SQLServer: Add lookback_window param. (#18979)
Browse files Browse the repository at this point in the history
* Ad lookback_window param.

* Changelog.

* Run linter.

* Add config param to spec.

* Match config spec.

* Fix type.

* Sync models.

* Remove default value, since there is none.

* Change default value.

* Add example val.
  • Loading branch information
amw-zero authored Nov 6, 2024
1 parent 166798a commit 260ee86
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sqlserver/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,15 @@ files:
example: 250
display_default: 250
hidden: true
- name: lookback_window
description: |
Queries whose last execution completed after the lookback window are excluded from
metrics collection. Set a longer lookback window (in seconds) to capture infrequently
run queries.
value:
type: integer
display_default: 2 * collection_interval
example: 600
- name: procedure_metrics
description: Configure collection of procedure metrics
options:
Expand Down
7 changes: 7 additions & 0 deletions sqlserver/changelog.d/18979.added
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Add lookback_window config parameter to query_metrics.

The current lookback window defaults to 2 times the collection interval, and is not able to be overridden.
This means that infrequently-run queries are unlikely to have metrics captured for them. One common
use case that falls into this bucket is ETL queries which can run hourly or even daily. These have
a very small chance of having metrics captured for them. In that case, we will support setting a lookback
window that will include such queries.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class QueryMetrics(BaseModel):
dm_exec_query_stats_row_limit: Optional[int] = None
enabled: Optional[bool] = None
enforce_collection_interval_deadline: Optional[bool] = None
lookback_window: Optional[int] = None
max_queries: Optional[int] = None
samples_per_hour_per_query: Optional[int] = None

Expand Down
7 changes: 7 additions & 0 deletions sqlserver/datadog_checks/sqlserver/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ instances:
#
# samples_per_hour_per_query: 4

## @param lookback_window - integer - optional - default: 2 * collection_interval
## Queries whose last execution completed after the lookback window are excluded from
## metrics collection. Set a longer lookback window (in seconds) to capture infrequently
## run queries.
#
# lookback_window: 600

## Configure collection of procedure metrics
#
# procedure_metrics:
Expand Down
5 changes: 5 additions & 0 deletions sqlserver/datadog_checks/sqlserver/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,17 @@ def _get_statement_metrics_query_cached(self, cursor):
)
return self._statement_metrics_query

def lookback_window(self):
return self._config.statement_metrics_config.get('lookback_window', None)

@tracked_method(agent_check_getter=agent_check_getter, track_result_length=True)
def _load_raw_query_metrics_rows(self, cursor):
self.log.debug("collecting sql server statement metrics")
statement_metrics_query = self._get_statement_metrics_query_cached(cursor)
now = time.time()
query_interval = self.collection_interval * 2
if self.lookback_window():
query_interval = self.lookback_window()
if self._last_stats_query_time:
query_interval = max(query_interval, now - self._last_stats_query_time)
self._last_stats_query_time = now
Expand Down
10 changes: 10 additions & 0 deletions sqlserver/tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,16 @@ def test_metrics_lookback_multiplier(instance_docker):
mock_cursor.execute.assert_called_with(ANY, (6,))


@pytest.mark.unit
def test_metrics_lookback_window_config(instance_docker):
instance_docker['query_metrics'] = {'lookback_window': 86400}
check = SQLServer(CHECK_NAME, {}, [instance_docker])
_, mock_cursor = _mock_database_list()

check.statement_metrics._load_raw_query_metrics_rows(mock_cursor)
mock_cursor.execute.assert_called_with(ANY, (86400,))


@pytest.mark.flaky
@pytest.mark.integration
@pytest.mark.usefixtures('dd_environment')
Expand Down

0 comments on commit 260ee86

Please sign in to comment.