-
Notifications
You must be signed in to change notification settings - Fork 0
317 lines (270 loc) · 9.68 KB
/
monitoring-workflow.yml
File metadata and controls
317 lines (270 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
name: Monitoring Workflow
on:
workflow_run:
workflows: ["Advanced Test Automation", "Coverage Optimization"]
types: [completed]
schedule:
- cron: '0 0 * * *' # 毎日午前0時
jobs:
monitor:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install prometheus-client grafana-api
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: test-results
path: test-results
- name: Parse test results
run: |
python -c """
import json
from datetime import datetime
import os
# テスト結果の解析
test_results = []
for file in os.listdir('test-results'):
if file.endswith('.json'):
with open(f'test-results/{file}') as f:
result = json.load(f)
test_results.append({
'timestamp': datetime.now().isoformat(),
'workflow': file.replace('.json', ''),
'status': result.get('status', 'unknown'),
'duration': result.get('duration', 0),
'tests': result.get('tests', 0),
'passed': result.get('passed', 0),
'failed': result.get('failed', 0)
})
# 結果をJSONに保存
with open('monitoring-results.json', 'w') as f:
json.dump(test_results, f)
"""
- name: Upload monitoring data
uses: actions/upload-artifact@v3
with:
name: monitoring-data
path: monitoring-results.json
- name: Generate metrics
run: |
python -c """
import json
from prometheus_client import Gauge, start_http_server
import time
# Prometheusメトリクスの設定
test_status = Gauge('test_status', 'Test status', ['workflow', 'status'])
test_duration = Gauge('test_duration', 'Test duration', ['workflow'])
test_count = Gauge('test_count', 'Number of tests', ['workflow', 'result'])
# メトリクスサーバーの起動
start_http_server(8000)
# メトリクスの更新
with open('monitoring-results.json') as f:
results = json.load(f)
for result in results:
workflow = result['workflow']
status = result['status']
duration = result['duration']
test_status.labels(workflow=workflow, status=status).set(1)
test_duration.labels(workflow=workflow).set(duration)
test_count.labels(workflow=workflow, result='passed').set(result['passed'])
test_count.labels(workflow=workflow, result='failed').set(result['failed'])
# メトリクスサーバーの継続稼働
while True:
time.sleep(60)
"""
- name: Send to Grafana
run: |
python -c """
import json
import requests
from datetime import datetime
# Grafana設定
grafana_url = os.getenv('GRAFANA_URL')
api_key = os.getenv('GRAFANA_API_KEY')
# データの送信
with open('monitoring-results.json') as f:
results = json.load(f)
for result in results:
payload = {
'timestamp': result['timestamp'],
'workflow': result['workflow'],
'status': result['status'],
'duration': result['duration'],
'tests': result['tests'],
'passed': result['passed'],
'failed': result['failed']
}
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
response = requests.post(
f'{grafana_url}/api/datasources/proxy/1/api/v1/import',
headers=headers,
json=payload
)
if response.status_code != 200:
print(f'Error sending data to Grafana: {response.text}')
"""
- name: Generate dashboard
run: |
python -c """
import json
from datetime import datetime
import os
# ダッシュボードの生成
dashboard = {
'title': 'CI/CD Monitoring Dashboard',
'panels': [
{
'title': 'Test Status',
'type': 'stat',
'targets': [
{
'expr': 'test_status',
'legendFormat': '{{workflow}} - {{status}}'
}
]
},
{
'title': 'Test Duration',
'type': 'graph',
'targets': [
{
'expr': 'test_duration',
'legendFormat': '{{workflow}}'
}
]
},
{
'title': 'Test Results',
'type': 'gauge',
'targets': [
{
'expr': 'test_count',
'legendFormat': '{{workflow}} - {{result}}'
}
]
}
]
}
# ダッシュボードの保存
with open('dashboard.json', 'w') as f:
json.dump(dashboard, f)
"""
- name: Upload dashboard
run: |
python -c """
import json
import requests
# ダッシュボードのアップロード
with open('dashboard.json') as f:
dashboard = json.load(f)
grafana_url = os.getenv('GRAFANA_URL')
api_key = os.getenv('GRAFANA_API_KEY')
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
response = requests.post(
f'{grafana_url}/api/dashboards/db',
headers=headers,
json={'dashboard': dashboard}
)
if response.status_code != 200:
print(f'Error uploading dashboard: {response.text}')
"""
- name: Generate alerts
run: |
python -c """
import json
import requests
# アラートルールの生成
alerts = {
'rules': [
{
'name': 'Test Failure Alert',
'condition': 'test_status{status="failed"} > 0',
'for': '5m',
'labels': {
'severity': 'critical'
},
'annotations': {
'summary': 'Test failures detected',
'description': 'Tests are failing in the CI/CD pipeline'
}
},
{
'name': 'Performance Alert',
'condition': 'test_duration > 300',
'for': '10m',
'labels': {
'severity': 'warning'
},
'annotations': {
'summary': 'Performance degradation',
'description': 'Test execution time is above threshold'
}
}
]
}
# アラートルールの保存
with open('alerts.json', 'w') as f:
json.dump(alerts, f)
"""
- name: Send alerts
run: |
python -c """
import json
import requests
# アラートの送信
with open('alerts.json') as f:
alerts = json.load(f)
grafana_url = os.getenv('GRAFANA_URL')
api_key = os.getenv('GRAFANA_API_KEY')
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
for alert in alerts['rules']:
response = requests.post(
f'{grafana_url}/api/alerts/rules',
headers=headers,
json=alert
)
if response.status_code != 200:
print(f'Error creating alert: {response.text}')
"""
- name: Generate report
run: |
python -c """
import json
from datetime import datetime
# モニタリングレポートの生成
with open('monitoring-results.json') as f:
results = json.load(f)
with open('monitoring-report.md', 'w') as f:
f.write('# CI/CD Monitoring Report\n\n')
f.write(f'Generated at: {datetime.now().isoformat()}\n\n')
for result in results:
f.write(f'## {result["workflow"]}\n\n')
f.write(f'- Status: {result["status"]}\n')
f.write(f'- Duration: {result["duration"]}s\n')
f.write(f'- Tests: {result["tests"]}\n')
f.write(f'- Passed: {result["passed"]}\n')
f.write(f'- Failed: {result["failed"]}\n')
"""
- name: Upload report
uses: actions/upload-artifact@v3
with:
name: monitoring-report
path: monitoring-report.md