Skip to content

Commit

Permalink
postgres integration: fix wrong timestamp
Browse files Browse the repository at this point in the history
The timestamp derived from the last `state_change` was wrong because it didn't take into account the timezone, causing some events to be dropped during intake. Now we do an explicit conversion to a unix timestamp from the UTC Unix epoch date.
  • Loading branch information
djova committed Mar 24, 2021
1 parent 336ba69 commit 9ea8f7e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions postgres/datadog_checks/postgres/statement_samples.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import logging
import os
import re
Expand All @@ -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 = {
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9ea8f7e

Please sign in to comment.