Skip to content

Commit

Permalink
fix metric names with empty namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-mez committed Sep 11, 2020
1 parent ce734cc commit 31dc758
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,17 @@ def transform_metadata(self, metric, scraper_config):
if label_name in labels:
self.set_metadata(metadata_name, labels[label_name])

def _metric_name_with_namespace(self, metric_name, scraper_config):
namespace = scraper_config['namespace']
if not namespace:
return metric_name
return '{}.{}'.format(namespace, metric_name)

def _telemetry_metric_name_with_namespace(self, metric_name, scraper_config):
return '{}.{}.{}'.format(scraper_config['namespace'], 'telemetry', metric_name)
namespace = scraper_config['namespace']
if not namespace:
return '{}.{}'.format('telemetry', metric_name)
return '{}.{}.{}'.format(namespace, 'telemetry', metric_name)

def _send_telemetry_gauge(self, metric_name, val, scraper_config):
if scraper_config['telemetry']:
Expand Down Expand Up @@ -696,7 +705,7 @@ def poll(self, scraper_config, headers=None):

# Should we send a service check for when we make a request
health_service_check = scraper_config['health_service_check']
service_check_name = '{}{}'.format(scraper_config['namespace'], '.prometheus.health')
service_check_name = self._metric_name_with_namespace('prometheus.health', scraper_config)
service_check_tags = ['endpoint:{}'.format(endpoint)]
service_check_tags.extend(scraper_config['custom_tags'])

Expand Down Expand Up @@ -749,7 +758,7 @@ def submit_openmetric(self, metric_name, metric, scraper_config, hostname=None):
metric when sending the gauge to Datadog.
"""
if metric.type in ["gauge", "counter", "rate"]:
metric_name_with_namespace = '{}.{}'.format(scraper_config['namespace'], metric_name)
metric_name_with_namespace = self._metric_name_with_namespace(metric_name, scraper_config)
for sample in metric.samples:
val = sample[self.SAMPLE_VALUE]
if not self._is_value_valid(val):
Expand Down Expand Up @@ -809,7 +818,7 @@ def _submit_gauges_from_summary(self, metric_name, metric, scraper_config, hostn
self._submit_distribution_count(
scraper_config['send_distribution_sums_as_monotonic'],
scraper_config['send_monotonic_with_gauge'],
"{}.{}.sum".format(scraper_config['namespace'], metric_name),
"{}.sum".format(self._metric_name_with_namespace(metric_name, scraper_config)),
val,
tags=tags,
hostname=custom_hostname,
Expand All @@ -819,7 +828,7 @@ def _submit_gauges_from_summary(self, metric_name, metric, scraper_config, hostn
self._submit_distribution_count(
scraper_config['send_distribution_counts_as_monotonic'],
scraper_config['send_monotonic_with_gauge'],
"{}.{}.count".format(scraper_config['namespace'], metric_name),
"{}.count".format(self._metric_name_with_namespace(metric_name, scraper_config)),
val,
tags=tags,
hostname=custom_hostname,
Expand All @@ -840,7 +849,7 @@ def _submit_gauges_from_summary(self, metric_name, metric, scraper_config, hostn
sample[self.SAMPLE_LABELS]["quantile"] = str(float(quantile))
tags = self._metric_tags(metric_name, val, sample, scraper_config, hostname=custom_hostname)
self.gauge(
"{}.{}.quantile".format(scraper_config['namespace'], metric_name),
"{}.quantile".format(self._metric_name_with_namespace(metric_name, scraper_config)),
val,
tags=tags,
hostname=custom_hostname,
Expand All @@ -863,7 +872,7 @@ def _submit_gauges_from_histogram(self, metric_name, metric, scraper_config, hos
self._submit_distribution_count(
scraper_config['send_distribution_sums_as_monotonic'],
scraper_config['send_monotonic_with_gauge'],
"{}.{}.sum".format(scraper_config['namespace'], metric_name),
"{}.sum".format(self._metric_name_with_namespace(metric_name, scraper_config)),
val,
tags=tags,
hostname=custom_hostname,
Expand All @@ -875,7 +884,7 @@ def _submit_gauges_from_histogram(self, metric_name, metric, scraper_config, hos
self._submit_distribution_count(
scraper_config['send_distribution_counts_as_monotonic'],
scraper_config['send_monotonic_with_gauge'],
"{}.{}.count".format(scraper_config['namespace'], metric_name),
"{}.count".format(self._metric_name_with_namespace(metric_name, scraper_config)),
val,
tags=tags,
hostname=custom_hostname,
Expand All @@ -889,7 +898,7 @@ def _submit_gauges_from_histogram(self, metric_name, metric, scraper_config, hos
self._submit_distribution_count(
scraper_config['send_distribution_counts_as_monotonic'],
scraper_config['send_monotonic_with_gauge'],
"{}.{}.count".format(scraper_config['namespace'], metric_name),
"{}.count".format(self._metric_name_with_namespace(metric_name, scraper_config)),
val,
tags=tags,
hostname=custom_hostname,
Expand Down Expand Up @@ -981,7 +990,7 @@ def _submit_sample_histogram_buckets(self, metric_name, sample, scraper_config,
return
tags = self._metric_tags(metric_name, sample[self.SAMPLE_VALUE], sample, scraper_config, hostname)
self.submit_histogram_bucket(
"{}.{}".format(scraper_config['namespace'], metric_name),
self._metric_name_with_namespace(metric_name, scraper_config),
sample[self.SAMPLE_VALUE],
float(sample[self.SAMPLE_LABELS]["lower_bound"]),
float(sample[self.SAMPLE_LABELS]["le"]),
Expand Down
16 changes: 16 additions & 0 deletions datadog_checks_base/tests/test_openmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2605,3 +2605,19 @@ def test_wildcard_type_overrides(aggregator, mocked_prometheus_check, text_data)
assert len(check.config_map[FAKE_ENDPOINT]['_type_override_patterns']) == 1
assert list(check.config_map[FAKE_ENDPOINT]['_type_override_patterns'].values())[0] == 'counter'
assert len(check.config_map[FAKE_ENDPOINT]['type_overrides']) == 0


def test_empty_namespace(aggregator, mocked_prometheus_check, text_data, ref_gauge):
"""
Test that metric type is overridden correctly with wildcard.
"""
check = mocked_prometheus_check
instance = copy.deepcopy(PROMETHEUS_CHECK_INSTANCE)
instance['namespace'] = ''

config = check.get_scraper_config(instance)
config['_dry_run'] = False

check.process_metric(ref_gauge, config)

aggregator.assert_metric('process.vm.bytes', count=1)

0 comments on commit 31dc758

Please sign in to comment.