-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Support for multiple multiple folders, python client
Modified: Internal logic for creating/updating dashboards Deleted: Removed old logic as it is not flexible for automation
- Loading branch information
Vishnu Challa
committed
Sep 29, 2023
1 parent
c536c10
commit 1cac256
Showing
27 changed files
with
5,270 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,6 @@ defaults: | |
run: | ||
shell: bash | ||
|
||
env: | ||
# Space separated list as a string of all dashboard json files in "rendered" to load | ||
DASHBOARDS: "kube-burner.json" | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
|
@@ -25,13 +21,10 @@ jobs: | |
# The secret GRAFANA_URL must be set with the format http://username:[email protected] without a trailing / | ||
- name: Import dashboards to grafana | ||
run: > | ||
dashboard_list=($(echo $DASHBOARDS)); | ||
for path in "${dashboard_list[@]}"; do | ||
full_path="rendered/${path}"; | ||
echo "Importing ${full_path}"; | ||
dashboard=$(cat ${full_path}); | ||
for t in rendered/**/*.json; do | ||
echo "Importing ${t}"; | ||
dashboard=$(cat ${t}); | ||
echo "{\"dashboard\": ${dashboard}, \"overwrite\": true}" | | ||
curl -k -Ss -XPOST -H "Content-Type: application/json" -H "Accept: application/json" -d@- | ||
"${{ secrets.GRAFANA_URL }}/api/dashboards/db" -o /dev/null; | ||
done | ||
done |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM ubuntu | ||
|
||
WORKDIR /performance-dashboards | ||
ARG DEBIAN_FRONTEND=noninteractive | ||
|
||
# Install necessary libraries for subsequent commands | ||
RUN apt-get update && apt-get install -y podman dumb-init python3.6 python3-distutils python3-pip python3-apt | ||
|
||
COPY . . | ||
RUN chmod -R 775 /performance-dashboards | ||
|
||
# Install dependencies | ||
RUN python3 -m pip install --upgrade pip | ||
RUN pip install -r requirements.txt | ||
|
||
# Cleanup the installation remainings | ||
RUN apt-get clean autoclean && \ | ||
apt-get autoremove --yes && \ | ||
rm -rf /var/lib/{apt,dpkg,cache,log}/ | ||
|
||
# Start the command | ||
ENTRYPOINT ["/usr/bin/dumb-init", "--"] | ||
CMD ["python3", "dittybopper/syncer/entrypoint.py"] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import json | ||
import logging | ||
import os | ||
import requests | ||
import uuid | ||
import time | ||
from collections import defaultdict | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
class GrafanaOperations: | ||
""" | ||
This class is responsible for Grafana operations | ||
""" | ||
def __init__(self, grafana_url: str, input_directory: str): | ||
self.grafana_url = grafana_url | ||
self.input_directory = input_directory | ||
self.dashboards = defaultdict(list) | ||
self.folder_map = dict() | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def fetch_all_dashboards(self): | ||
""" | ||
This method fetches all rendered dashboards | ||
:return: | ||
""" | ||
self.get_all_folders() | ||
self.folder_map['General'] = None | ||
for root, _, files in os.walk(self.input_directory): | ||
folder_name = os.path.basename(root) | ||
json_files = [os.path.join(root, filename) for filename in files if filename.endswith(".json")] | ||
folder_name = "General" if (folder_name == "") else folder_name | ||
if folder_name in self.folder_map: | ||
folder_id = self.folder_map[folder_name] | ||
else: | ||
folder_id = self.create_folder(folder_name) | ||
self.dashboards[folder_id].extend(json_files) | ||
|
||
def get_all_folders(self): | ||
""" | ||
This method gets the entire list of folders in grafana | ||
:return: | ||
""" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
try: | ||
response = requests.get( | ||
f"{self.grafana_url}/api/folders", | ||
headers=headers, | ||
) | ||
response_json = response.json() | ||
self.folder_map = {each_folder['title']: each_folder['id'] for each_folder in response_json} | ||
except requests.exceptions.RequestException as e: | ||
raise Exception(f"Error listing folders. Message: {e}") | ||
|
||
def create_folder(self, folder_name): | ||
""" | ||
This method creates a folder in grafana | ||
:return: | ||
""" | ||
uid = str(uuid.uuid4()) | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
try: | ||
response = requests.post( | ||
f"{self.grafana_url}/api/folders", | ||
headers=headers, | ||
json={ | ||
"title": folder_name, | ||
"uid": uid, | ||
}, | ||
) | ||
response_json = response.json() | ||
self.folder_map[folder_name] = id | ||
return response_json['id'] | ||
|
||
except requests.exceptions.RequestException as e: | ||
raise Exception(f"Error creating folder with name:'{self.folder_name}' and uid:'{uid}'. Message: {e}") | ||
|
||
def read_dashboard_json(self, json_file): | ||
""" | ||
This method reads dashboard from json file | ||
:return: | ||
""" | ||
with open(json_file, 'r') as f: | ||
return json.load(f) | ||
|
||
def create_dashboards(self): | ||
""" | ||
This method creates/updates dashboard with new json | ||
:return: | ||
""" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
for folder_id, files in self.dashboards.items(): | ||
for json_file in set(files): | ||
dashboard_json = self.read_dashboard_json(json_file) | ||
try: | ||
response = requests.post( | ||
f"{self.grafana_url}/api/dashboards/db", | ||
headers=headers, | ||
json={ | ||
"dashboard": dashboard_json, | ||
"folderId": folder_id, | ||
"overwrite": True, | ||
}, | ||
) | ||
if response.status_code == 200: | ||
self.logger.info(f"Dashboard '{dashboard_json['title']}' created successfully in folder '{folder_id}'") | ||
else: | ||
raise Exception( | ||
f"Failed to create dashboard '{dashboard_json['title']}' in folder '{folder_id}'. Status code: {response.status_code}. Message: {response.text}") | ||
|
||
except requests.exceptions.RequestException as e: | ||
raise Exception(f"Error creating dashboard '{dashboard_json['title']}' in folder '{folder_id}'. Message: {e}") | ||
|
||
if __name__ == '__main__': | ||
grafana_operations = GrafanaOperations(os.environ.get("GRAFANA_URL"), os.environ.get("INPUT_DIR")) | ||
grafana_operations.fetch_all_dashboards() | ||
grafana_operations.create_dashboards() | ||
while True: | ||
time.sleep(60) |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests==2.26.0 | ||
|
This file contains 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
2 changes: 1 addition & 1 deletion
2
templates/kube-burner.jsonnet → templates/CPT/kube-burner.jsonnet
This file contains 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
2 changes: 1 addition & 1 deletion
2
templates/api-performance-overview.jsonnet → .../General/api-performance-overview.jsonnet
This file contains 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
2 changes: 1 addition & 1 deletion
2
templates/cilium-k8s-perf.jsonnet → templates/General/cilium-k8s-perf.jsonnet
This file contains 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
2 changes: 1 addition & 1 deletion
2
templates/etcd-on-cluster-dashboard.jsonnet → ...General/etcd-on-cluster-dashboard.jsonnet
This file contains 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
2 changes: 1 addition & 1 deletion
2
templates/hypershift-performance.jsonnet → ...es/General/hypershift-performance.jsonnet
This file contains 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
Oops, something went wrong.