Skip to content

Instantly share code, notes, and snippets.

@lixianyang
Created January 16, 2019 06:01
Show Gist options
  • Select an option

  • Save lixianyang/fd5c40fcc47de33bb99878271c81d94a to your computer and use it in GitHub Desktop.

Select an option

Save lixianyang/fd5c40fcc47de33bb99878271c81d94a to your computer and use it in GitHub Desktop.
prometheus metrics to markdown help
# coding: utf-8
import sys
import requests
def get_metrics_string(url):
""" get metrics content """
r = requests.get(url)
return r.content
def parse_metrics_help(string):
metrics = []
lines = string.split("\n")
for line in lines:
if line.startswith("# HELP"):
words = line.split()
metrics.append((words[2], " ".join(words[3:])))
return metrics
def save_metrics_help(metrics, name):
lines = [
"| metric | help |",
"| ------ | ---- |",
]
for metric in metrics:
lines.append("| %s | %s |" % (metric[0], metric[1]))
lines.append("")
with open(name, "w+") as f:
f.write("\n".join(lines))
def main():
if len(sys.argv) != 3:
print("Invalid arguments")
return
url, name = sys.argv[1:]
string = get_metrics_string(url)
metrics = parse_metrics_help(string)
save_metrics_help(metrics, name)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment