Skip to content

Commit

Permalink
Log warning when failing to parse openmetrics response
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevon committed May 3, 2024
1 parent a300869 commit 7ba302a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class TrackingIterator:
def __init__(self, iterator):
self.iterator = iterator
self.current = None

def __iter__(self):
return self

def __next__(self):
self.current = next(self.iterator)
return self.current
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ...utils.http import RequestsWrapper
from .. import AgentCheck
from ..libs.prometheus import text_fd_to_metric_families
from ..libs.tracking_iterator import TrackingIterator

try:
import datadog_agent
Expand Down Expand Up @@ -463,7 +464,7 @@ def parse_metric_family(self, response, scraper_config):
if scraper_config['_text_filter_blacklist']:
input_gen = self._text_filter_input(input_gen, scraper_config)

for metric in text_fd_to_metric_families(input_gen):
for metric in self._text_fd_to_metric_families(input_gen):
self._send_telemetry_counter(
self.TELEMETRY_COUNTER_METRICS_INPUT_COUNT, len(metric.samples), scraper_config
)
Expand All @@ -480,6 +481,14 @@ def parse_metric_family(self, response, scraper_config):
metric.name = self._remove_metric_prefix(metric.name, scraper_config)
yield metric

def _text_fd_to_metric_families(self, input_gen):
iterator = TrackingIterator(input_gen)
try:
yield from text_fd_to_metric_families(iterator)
except Exception as e:
self.log.warning("Failed to parse metric response '%s': %s", iterator.current, str(e))
raise

def _text_filter_input(self, input_gen, scraper_config):
"""
Filters out the text input line by line to avoid parsing and processing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ...utils.prometheus import metrics_pb2
from .. import AgentCheck
from ..libs.prometheus import text_fd_to_metric_families
from ..libs.tracking_iterator import TrackingIterator

if PY3:
long = int
Expand Down Expand Up @@ -219,7 +220,7 @@ def parse_metric_family(self, response):

obj_map = {} # map of the types of each metrics
obj_help = {} # help for the metrics
for metric in text_fd_to_metric_families(input_gen):
for metric in self._text_fd_to_metric_families(input_gen):
metric.name = self.remove_metric_prefix(metric.name)
metric_name = "%s_bucket" % metric.name if metric.type == "histogram" else metric.name
metric_type = self.type_overrides.get(metric_name, metric.type)
Expand All @@ -244,6 +245,14 @@ def parse_metric_family(self, response):
else:
raise UnknownFormatError('Unsupported content-type provided: {}'.format(response.headers['Content-Type']))

def _text_fd_to_metric_families(self, input_gen):
iterator = TrackingIterator(input_gen)
try:
yield from text_fd_to_metric_families(iterator)
except Exception as e:
self.log.warning("Failed to parse metric response '%s': %s", iterator.current, str(e))
raise

def _text_filter_input(self, input_gen):
"""
Filters out the text input line by line to avoid parsing and processing
Expand Down

0 comments on commit 7ba302a

Please sign in to comment.