diff --git a/postgres/datadog_checks/postgres/statement_samples.py b/postgres/datadog_checks/postgres/statement_samples.py index 02160082fae71..6efcd60a73462 100644 --- a/postgres/datadog_checks/postgres/statement_samples.py +++ b/postgres/datadog_checks/postgres/statement_samples.py @@ -1,3 +1,4 @@ +import datetime import logging import os import re @@ -22,6 +23,8 @@ VALID_EXPLAIN_STATEMENTS = frozenset({'select', 'table', 'delete', 'insert', 'replace', 'update'}) +unix_epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc) + # columns from pg_stat_activity which correspond to attributes common to all databases and are therefore stored in # under other standard keys pg_stat_activity_sample_exclude_keys = { @@ -343,12 +346,11 @@ def _collect_plan_for_statement(self, row): }, 'postgres': {k: v for k, v in row.items() if k not in pg_stat_activity_sample_exclude_keys}, } + event['timestamp'] = time.time() * 1000 if row['state'] in {'idle', 'idle in transaction'}: if row['state_change'] and row['query_start']: event['duration'] = (row['state_change'] - row['query_start']).total_seconds() * 1e9 - event['timestamp'] = time.mktime(row['state_change'].timetuple()) * 1000 - else: - event['timestamp'] = time.time() * 1000 + event['timestamp'] = (row['state_change'] - unix_epoch).total_seconds() * 1000 return event def _explain_pg_stat_activity(self, rows):