Add a function to remove duplicate HELP/TYPE text from output

Ocassionally this exporter produces output which is not compatible
with the telegraf prometheus input plugin. This change adds a filter
to remove duplicated HELP and TYPE texts from the output

eg:
021-09-13T19:40:00Z E! [inputs.prometheus] Error in plugin: error
reading metrics for http://10.96.22.16:9103/metrics: reading text
format failed: text format parsing error in line 46: second HELP
line for metric name "openstack_services_neutron_neutron_sriov_nic_agent"

Change-Id: I8c4c7739be86207de04bca4c1f9718794f4dda5f
This commit is contained in:
Steven Fitzpatrick 2021-09-15 20:30:32 +00:00
parent 27f9516182
commit 857090bc7c
1 changed files with 17 additions and 2 deletions

View File

@ -41,7 +41,6 @@ collectors = []
class ForkingHTTPServer(ForkingMixIn, HTTPServer):
pass
class OpenstackExporterHandler(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
@ -54,7 +53,7 @@ class OpenstackExporterHandler(BaseHTTPRequestHandler):
try:
stats = collector.get_stats()
if stats is not None:
output = output + stats
output = output + remove_duplicate_help_text(stats)
except BaseException as inst:
logger.warning(
'Could not get stats for collector {}.'
@ -86,6 +85,22 @@ class OpenstackExporterHandler(BaseHTTPRequestHandler):
def handler(*args, **kwargs):
OpenstackExporterHandler(*args, **kwargs)
def remove_duplicate_help_text(stats):
""" Ocassionally this exporter produces output which is not compatible
with the telegraf prometheus input plugin. This function is a filter
to remove duplicated # HELP and # TYPE texts from the output """
seen = set()
clean_stats = []
for line in stats.decode().splitlines():
if line not in seen:
if line.startswith('#'):
seen.add(line)
clean_stats.append(line)
return '\n'.join(clean_stats).encode() + b'\n'
if __name__ == '__main__':
parser = argparse.ArgumentParser(