Last active
August 29, 2023 16:55
-
-
Save alileza/23d3a28a341d98b7775cabd14ac24fb3 to your computer and use it in GitHub Desktop.
Add env on grafana dashboard for all prometheus query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
# Create an argument parser | |
parser = argparse.ArgumentParser(description="Modify JSON file by adding environment variable") | |
# Add arguments | |
parser.add_argument("input_file", help="Path to the input JSON file") | |
parser.add_argument("output_file", help="Path to the output JSON file") | |
# Parse the command-line arguments | |
args = parser.parse_args() | |
# The environment variable value | |
env_value = "$env" | |
# Open the input and output files | |
with open(args.input_file, "r") as input_f, open(args.output_file, "w") as output_f: | |
# Loop through the lines of the input file | |
for line in input_f: | |
if "expr" in line: | |
modified_line = line.replace("{", '{env="' + env_value + '", ') | |
output_f.write(modified_line) | |
else: | |
output_f.write(line) | |
print("Modified lines have been written to", args.output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @alileza , this script is really helpful and I started to use it immediately :). I would like to propose one change to change
count
in replace method. Since our expressions can be multiline and can consists several queries in one expression we may need to replace multiple times. I just tested in several dashboards and it is working