Skip to content

Commit 3435b4f

Browse files
committed
loki 대시보드 연동 추가
1 parent 4243595 commit 3435b4f

File tree

7 files changed

+60
-1
lines changed

7 files changed

+60
-1
lines changed

apis/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
from .gitlab.namespace import ns as gitlabAPI
1111
from .jenkins.namespace import ns as jenkinsAPI
1212
from .argocd.namespace import ns as argocdAPI
13+
from .grafana.namespace import ns as grafanaAPI
1314
from .index.namespace import ns as defatulIndex
1415

1516
api.add_namespace(authAPI)
1617
api.add_namespace(gitlabAPI)
1718
api.add_namespace(jenkinsAPI)
1819
api.add_namespace(argocdAPI)
20+
api.add_namespace(grafanaAPI)
1921
api.add_namespace(defatulIndex)

apis/grafana/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import namespace

apis/grafana/namespace.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask_restx import Resource, Namespace
2+
from werkzeug.utils import redirect
3+
from urllib.parse import urljoin
4+
from config.grafana_config import get_grafana_host, get_grafana_dashboard_templatepath
5+
import chevron
6+
7+
ns = Namespace('grafana', version="1.0", description='grafana controller')
8+
9+
@ns.route("/log/<string:project_name>/<string:app_name>")
10+
class Index(Resource):
11+
'''
12+
앱 배포
13+
'''
14+
@ns.doc(response={200: "success"})
15+
def get(self, project_name, app_name):
16+
17+
with open(get_grafana_dashboard_templatepath(), "r", encoding="utf-8") as f:
18+
grafana_filter = chevron.render(f.readline(), {
19+
"project_name": project_name,
20+
"app_name": f"{project_name}-{app_name}"
21+
})
22+
23+
grafana_dashboard_url = urljoin(get_grafana_host(), "/explore?orgId=1&left={}".format(grafana_filter))
24+
25+
return redirect(grafana_dashboard_url)

config/global_config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ helm:
3434
helm_gitlab_groupurl: "common/common-helm"
3535
helm_gitlab_projectname: "springboot-helm"
3636

37+
grafana:
38+
host: "https://grafana.choilab.xyz/"
39+
dashboard_filterpath: "grafana_dashboard_filter.json"
40+
3741
argocd:
3842
access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiMmZkYjcxNS1kZmRkLTQwOTgtYjk0Yy1lMDIyZTBkYTAxM2IiLCJpYXQiOjE2MjM4NDcwMTMsImlzcyI6ImFyZ29jZCIsIm5iZiI6MTYyMzg0NzAxMywic3ViIjoiY2hvaTphcGlLZXkifQ.k8K8-nsyv-oXhLu9EQQkb5XiX0jEk9hDAJL_PRmZ3YQ"
3943
host: "https://argocd.choilab.xyz/"

config/grafana_config.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import yaml
2+
import os
3+
4+
def get_grafana_host():
5+
'''
6+
리턴: 그라파나 host
7+
'''
8+
9+
with open('config/global_config.yaml', 'r') as f:
10+
config = yaml.safe_load(f)
11+
12+
return config['grafana']['host']
13+
14+
def get_grafana_dashboard_templatepath():
15+
'''
16+
리턴: 그라파나 대시보드 템플릿 경로
17+
'''
18+
19+
with open('config/global_config.yaml', 'r') as f:
20+
config = yaml.safe_load(f)
21+
22+
return os.path.join('config', config['grafana']['dashboard_filterpath'])
23+
24+

config/grafana_dashboard_filter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["now-30m","now","Loki",{"expr":"{namespace=\"{{project_name}}\", pod=~\"{{app_name}}-.*\"}"},{"mode":"Logs"},{"ui":[true,true,true,"none"]}]

templates/gitlab/application_dashboard.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
<td>
3636
<a href="/api/v1/argocd/deploy/{{application_info.deploy_url}}" target="_blank">배포 요청</a>
3737
</td>
38-
<td>todo</td>
38+
<td>
39+
<a href="/api/v1/grafana/log/{{application_info.projectname}}/{{application_info.appname}}" target="_blank">컨테이너 로그</a>
40+
</td>
3941
</tr>
4042
{% endfor %}
4143
</tbody>

0 commit comments

Comments
 (0)