Skip to content

Commit

Permalink
Postgres cross-org telemetry (#18758)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethsamuel authored Oct 3, 2024
1 parent 721ee62 commit 797d480
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/18758.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Postgres cross-org telemetry metrics.
10 changes: 10 additions & 0 deletions datadog_checks_base/datadog_checks/base/stubs/datadog_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self):
self._process_start_time = 0
self._external_tags = []
self._host_tags = "{}"
self._sent_telemetry = defaultdict(list)

def get_default_config(self):
return {'enable_metadata_collection': True, 'disable_unsafe_yaml': True}
Expand Down Expand Up @@ -84,6 +85,12 @@ def assert_external_tags_count(self, count):
count, tags_count, repr(self._external_tags)
)

def assert_telemetry(self, check_name, metric_name, metric_type, metric_value):
values = self._sent_telemetry[(check_name, metric_name, metric_type)]
assert metric_value in values, 'Expected value {} for check {}, metric {}, type {}. Found {}.'.format(
metric_value, check_name, metric_name, metric_type, values
)

def get_hostname(self):
return self._hostname

Expand Down Expand Up @@ -152,6 +159,9 @@ def obfuscate_mongodb_string(self, command):
# Passthrough stub: obfuscation implementation is in Go code.
return command

def emit_agent_telemetry(self, check_name, metric_name, metric_value, metric_type):
self._sent_telemetry[(check_name, metric_name, metric_type)].append(metric_value)


# Use the stub as a singleton
datadog_agent = DatadogAgentStub()
1 change: 1 addition & 0 deletions postgres/changelog.d/18758.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Postgres cross-org telemetry metrics.
22 changes: 22 additions & 0 deletions postgres/datadog_checks/postgres/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ def report_postgres_metadata(self):

# Tuned from experiments on staging, we may want to make this dynamic based on schema size in the future
chunk_size = 50
total_tables = 0
start_time = time.time()

for database in schema_metadata:
dbname = database["name"]
Expand Down Expand Up @@ -332,11 +334,31 @@ def report_postgres_metadata(self):

if buffer_column_count >= 100_000:
self._flush_schema(base_event, database, schema, tables_buffer)
total_tables += len(tables_buffer)
tables_buffer = []
buffer_column_count = 0

if len(tables_buffer) > 0:
self._flush_schema(base_event, database, schema, tables_buffer)
total_tables += len(tables_buffer)
elapsed_ms = (time.time() - start_time) * 1000
self._check.histogram(
"dd.postgres.schema.time",
elapsed_ms,
tags=self._check.tags,
hostname=self._check.resolved_hostname,
raw=True,
)
self._check.gauge(
"dd.postgres.schema.tables_count",
total_tables,
tags=self._check.tags,
hostname=self._check.resolved_hostname,
raw=True,
)
datadog_agent.emit_agent_telemetry("postgres", "schema_tables_elapsed_ms", elapsed_ms, "gauge")
datadog_agent.emit_agent_telemetry("postgres", "schema_tables_count", total_tables, "gauge")

self._is_schemas_collection_in_progress = False

def _should_collect_metadata(self, name, metadata_type):
Expand Down
2 changes: 2 additions & 0 deletions postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ def _collect_metric_autodiscovery(self, instance_tags, scopes, scope_type):
hostname=self.resolved_hostname,
raw=True,
)
telemetry_metric = scope_type.replace("_", "", 1) # remove the first underscore to match telemetry convention
datadog_agent.emit_agent_telemetry("postgres", f"{telemetry_metric}_ms", elapsed_ms, "histogram")
if elapsed_ms > self._config.min_collection_interval * 1000:
self.record_warning(
DatabaseConfigurationError.autodiscovered_metrics_exceeds_collection_interval,
Expand Down
31 changes: 31 additions & 0 deletions postgres/datadog_checks/postgres/statement_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,18 @@ def _report_check_hist_metrics(self, start_time, row_len, method_name):
hostname=self._check.resolved_hostname,
raw=True,
)
datadog_agent.emit_agent_telemetry(
"postgres",
f"{method_name}_ms",
(time.time() - start_time) * 1000,
"histogram",
)
datadog_agent.emit_agent_telemetry(
"postgres",
f"{method_name}_count",
row_len,
"histogram",
)

def run_job(self):
# do not emit any dd.internal metrics for DBM specific check code
Expand Down Expand Up @@ -478,6 +490,13 @@ def _collect_statement_samples(self):
tags=self.tags,
raw=True,
)
datadog_agent.emit_agent_telemetry(
"postgres",
"collect_activity_snapshot_ms",
(time.time() - start_time) * 1000,
"histogram",
)

elapsed_ms = (time.time() - start_time) * 1000
self._check.histogram(
"dd.postgres.collect_statement_samples.time",
Expand Down Expand Up @@ -514,6 +533,18 @@ def _collect_statement_samples(self):
hostname=self._check.resolved_hostname,
raw=True,
)
datadog_agent.emit_agent_telemetry(
"postgres",
"collect_statement_samples_ms",
elapsed_ms,
"histogram",
)
datadog_agent.emit_agent_telemetry(
"postgres",
"collect_statement_samples_count",
submitted_count,
"gauge",
)

@staticmethod
def _to_active_session(row, track_activity_query_size):
Expand Down

0 comments on commit 797d480

Please sign in to comment.