-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchange_failure_rate.py
More file actions
executable file
·700 lines (594 loc) · 24.2 KB
/
change_failure_rate.py
File metadata and controls
executable file
·700 lines (594 loc) · 24.2 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#!/usr/bin/env python3
"""
Change Failure Rate Calculator
Measures the percentage of deployments causing failures in production
"""
import json
import logging
import os
import sys
from dataclasses import dataclass
from datetime import datetime, timedelta
from enum import Enum
from typing import Dict, List, Optional, Tuple
import boto3
import click
import gitlab
import pandas as pd
import requests
import yaml
from dateutil.parser import parse
from github import Github
from tabulate import tabulate
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
class DeploymentStatus(Enum):
"""Deployment status types"""
SUCCESS = "success"
FAILURE = "failure"
ROLLBACK = "rollback"
UNKNOWN = "unknown"
@dataclass
class Deployment:
"""Deployment data structure"""
id: str
timestamp: datetime
service: str
environment: str
status: DeploymentStatus
version: str
duration_minutes: Optional[float] = None
error_message: Optional[str] = None
rollback_of: Optional[str] = None
class DeploymentSource:
"""Base class for deployment data sources"""
def get_deployments(
self, start_date: datetime, end_date: datetime
) -> List[Deployment]:
"""Fetch deployments from source"""
raise NotImplementedError
class GitHubDeploymentSource(DeploymentSource):
"""Fetch deployment data from GitHub"""
def __init__(self, token: str, organization: str, repositories: List[str]):
self.github = Github(token)
self.organization = organization
self.repositories = repositories
def get_deployments(
self, start_date: datetime, end_date: datetime
) -> List[Deployment]:
"""Fetch deployments from GitHub API"""
deployments = []
for repo_name in self.repositories:
try:
repo = self.github.get_repo(f"{self.organization}/{repo_name}")
for deployment in repo.get_deployments():
if deployment.created_at < start_date:
break
if deployment.created_at <= end_date:
# Get deployment statuses
statuses = list(deployment.get_statuses())
if statuses:
latest_status = statuses[0]
status = self._map_github_status(latest_status.state)
# Calculate duration if completed
duration = None
if len(statuses) > 1:
duration = (
statuses[0].created_at - statuses[-1].created_at
).total_seconds() / 60
deployments.append(
Deployment(
id=f"{repo_name}_{deployment.id}",
timestamp=deployment.created_at,
service=repo_name,
environment=deployment.environment,
status=status,
version=deployment.sha[:7],
duration_minutes=duration,
error_message=(
latest_status.description
if status == DeploymentStatus.FAILURE
else None
),
)
)
except Exception as e:
logger.error(f"Error fetching deployments for {repo_name}: {e}")
return deployments
def _map_github_status(self, state: str) -> DeploymentStatus:
"""Map GitHub deployment state to our status"""
mapping = {
"success": DeploymentStatus.SUCCESS,
"failure": DeploymentStatus.FAILURE,
"error": DeploymentStatus.FAILURE,
"inactive": DeploymentStatus.ROLLBACK,
}
return mapping.get(state, DeploymentStatus.UNKNOWN)
class JenkinsDeploymentSource(DeploymentSource):
"""Fetch deployment data from Jenkins"""
def __init__(self, url: str, username: str, token: str, job_names: List[str]):
self.url = url.rstrip("/")
self.auth = (username, token)
self.job_names = job_names
def get_deployments(
self, start_date: datetime, end_date: datetime
) -> List[Deployment]:
"""Fetch deployments from Jenkins API"""
deployments = []
for job_name in self.job_names:
try:
# Get job builds
job_url = f"{self.url}/job/{job_name}/api/json?tree=builds[number,timestamp,result,duration,actions[parameters[name,value]]]"
response = requests.get(job_url, auth=self.auth)
response.raise_for_status()
job_data = response.json()
for build in job_data.get("builds", []):
build_time = datetime.fromtimestamp(build["timestamp"] / 1000)
if build_time < start_date:
break
if build_time <= end_date and build["result"]:
# Extract parameters
params = self._extract_parameters(build)
status = self._map_jenkins_result(build["result"])
deployments.append(
Deployment(
id=f"{job_name}_{build['number']}",
timestamp=build_time,
service=params.get("service", job_name),
environment=params.get("environment", "production"),
status=status,
version=params.get("version", str(build["number"])),
duration_minutes=(
build["duration"] / 60000
if build["duration"]
else None
),
)
)
except Exception as e:
logger.error(f"Error fetching deployments for {job_name}: {e}")
return deployments
def _extract_parameters(self, build: Dict) -> Dict:
"""Extract build parameters"""
params = {}
for action in build.get("actions", []):
if "parameters" in action:
for param in action["parameters"]:
params[param["name"]] = param.get("value")
return params
def _map_jenkins_result(self, result: str) -> DeploymentStatus:
"""Map Jenkins build result to our status"""
mapping = {
"SUCCESS": DeploymentStatus.SUCCESS,
"FAILURE": DeploymentStatus.FAILURE,
"UNSTABLE": DeploymentStatus.FAILURE,
"ABORTED": DeploymentStatus.FAILURE,
}
return mapping.get(result, DeploymentStatus.UNKNOWN)
class DynamoDBDeploymentSource(DeploymentSource):
"""Fetch deployment data from DynamoDB"""
def __init__(
self, table_name: str, profile: Optional[str] = None, region: str = "us-east-1"
):
if profile:
session = boto3.Session(profile_name=profile, region_name=region)
self.dynamodb = session.resource("dynamodb")
else:
self.dynamodb = boto3.resource("dynamodb", region_name=region)
self.table = self.dynamodb.Table(table_name)
def get_deployments(
self, start_date: datetime, end_date: datetime
) -> List[Deployment]:
"""Fetch deployments from DynamoDB"""
deployments = []
# Scan table (consider using query if you have proper indexes)
response = self.table.scan()
for item in response["Items"]:
timestamp = (
parse(item["timestamp"])
if isinstance(item["timestamp"], str)
else item["timestamp"]
)
if start_date <= timestamp <= end_date:
deployments.append(
Deployment(
id=item.get("deployment_id", item.get("id")),
timestamp=timestamp,
service=item.get("service", "unknown"),
environment=item.get("environment", "production"),
status=DeploymentStatus(item.get("status", "unknown")),
version=item.get("version", "unknown"),
duration_minutes=(
float(item["duration_minutes"])
if "duration_minutes" in item
else None
),
error_message=item.get("error_message"),
rollback_of=item.get("rollback_of"),
)
)
# Handle pagination
while "LastEvaluatedKey" in response:
response = self.table.scan(ExclusiveStartKey=response["LastEvaluatedKey"])
for item in response["Items"]:
timestamp = (
parse(item["timestamp"])
if isinstance(item["timestamp"], str)
else item["timestamp"]
)
if start_date <= timestamp <= end_date:
deployments.append(
Deployment(
id=item.get("deployment_id", item.get("id")),
timestamp=timestamp,
service=item.get("service", "unknown"),
environment=item.get("environment", "production"),
status=DeploymentStatus(item.get("status", "unknown")),
version=item.get("version", "unknown"),
duration_minutes=(
float(item["duration_minutes"])
if "duration_minutes" in item
else None
),
error_message=item.get("error_message"),
rollback_of=item.get("rollback_of"),
)
)
return deployments
class ChangeFailureRateCalculator:
"""Calculate change failure rate metrics"""
def __init__(self, config: Dict):
self.config = config
self.source = self._initialize_source()
def _initialize_source(self) -> DeploymentSource:
"""Initialize the appropriate deployment source"""
source_type = self.config.get("deployment_source", "github")
if source_type == "github":
return GitHubDeploymentSource(
token=os.environ.get("GITHUB_TOKEN")
or self.config.get("github", {}).get("token"),
organization=self.config.get("github", {}).get("organization"),
repositories=self.config.get("github", {}).get("repositories", []),
)
elif source_type == "jenkins":
jenkins_config = self.config.get("jenkins", {})
return JenkinsDeploymentSource(
url=jenkins_config.get("url"),
username=jenkins_config.get("username"),
token=os.environ.get("JENKINS_TOKEN") or jenkins_config.get("token"),
job_names=jenkins_config.get("jobs", []),
)
elif source_type == "dynamodb":
dynamodb_config = self.config.get("dynamodb", {})
return DynamoDBDeploymentSource(
table_name=dynamodb_config.get("table_name", "DeploymentStatus"),
profile=dynamodb_config.get("profile"),
region=dynamodb_config.get("region", "us-east-1"),
)
else:
raise ValueError(f"Unsupported deployment source: {source_type}")
def calculate(self) -> Dict:
"""Calculate change failure rate metrics"""
# Parse time range
start_date, end_date = self._parse_time_range()
logger.info(f"Calculating change failure rate from {start_date} to {end_date}")
# Fetch deployments
deployments = self.source.get_deployments(start_date, end_date)
if not deployments:
logger.warning("No deployments found in the specified time range")
return self._empty_results(start_date, end_date)
# Filter by environment if specified
environments = self.config.get("environments", [])
if environments:
deployments = [d for d in deployments if d.environment in environments]
# Calculate metrics
total_deployments = len(deployments)
failed_deployments = [
d for d in deployments if d.status == DeploymentStatus.FAILURE
]
rollback_deployments = [
d for d in deployments if d.status == DeploymentStatus.ROLLBACK
]
# Change failure rate calculation
failure_count = len(failed_deployments) + len(rollback_deployments)
change_failure_rate = (
(failure_count / total_deployments * 100) if total_deployments > 0 else 0
)
# Group by service
df = pd.DataFrame(
[
{
"service": d.service,
"environment": d.environment,
"status": d.status.value,
"timestamp": d.timestamp,
"duration_minutes": d.duration_minutes,
}
for d in deployments
]
)
by_service = (
df.groupby("service")
.agg(
{
"status": [
lambda x: len(x), # total
lambda x: sum(
1 for s in x if s in ["failure", "rollback"]
), # failures
lambda x: (
(
sum(1 for s in x if s in ["failure", "rollback"])
/ len(x)
* 100
)
if len(x) > 0
else 0
), # rate
]
}
)
.round(2)
)
by_service.columns = ["total", "failures", "failure_rate"]
by_service_dict = by_service.to_dict("index")
# Weekly trend
df["week"] = pd.to_datetime(df["timestamp"]).dt.isocalendar().week
weekly_stats = (
df.groupby("week")
.agg(
{
"status": [
lambda x: len(x),
lambda x: sum(1 for s in x if s in ["failure", "rollback"]),
lambda x: (
(
sum(1 for s in x if s in ["failure", "rollback"])
/ len(x)
* 100
)
if len(x) > 0
else 0
),
]
}
)
.round(2)
)
weekly_stats.columns = ["total", "failures", "failure_rate"]
weekly_trend = weekly_stats.to_dict("index")
# Determine performance level
performance_level = self._determine_performance_level(change_failure_rate)
# Find recent failures
recent_failures = sorted(
[d for d in failed_deployments + rollback_deployments],
key=lambda x: x.timestamp,
reverse=True,
)[:10]
return {
"metric": "change_failure_rate",
"period": {"start": start_date.isoformat(), "end": end_date.isoformat()},
"results": {
"total_deployments": total_deployments,
"failed_deployments": len(failed_deployments),
"rollback_deployments": len(rollback_deployments),
"change_failure_rate": change_failure_rate,
"performance_level": performance_level,
"by_service": by_service_dict,
"weekly_trend": weekly_trend,
"recent_failures": [
{
"id": f.id,
"service": f.service,
"timestamp": f.timestamp.isoformat(),
"error_message": f.error_message,
}
for f in recent_failures
],
},
}
def _parse_time_range(self) -> Tuple[datetime, datetime]:
"""Parse time range from config"""
time_range = self.config.get("time_range", {})
# Parse end date
end_date_str = time_range.get("end_date", "now")
if end_date_str == "now":
end_date = datetime.now()
else:
end_date = parse(end_date_str)
# Parse start date
start_date_str = time_range.get("start_date", "30d")
if start_date_str.endswith("d"):
days = int(start_date_str[:-1])
start_date = end_date - timedelta(days=days)
else:
start_date = parse(start_date_str)
return start_date, end_date
def _determine_performance_level(self, rate: float) -> str:
"""Determine DORA performance level based on change failure rate"""
if rate <= 15:
return "Elite/High/Medium"
else:
return "Low"
def _empty_results(self, start_date: datetime, end_date: datetime) -> Dict:
"""Return empty results structure"""
return {
"metric": "change_failure_rate",
"period": {"start": start_date.isoformat(), "end": end_date.isoformat()},
"results": {
"total_deployments": 0,
"failed_deployments": 0,
"rollback_deployments": 0,
"change_failure_rate": 0,
"performance_level": "No data",
"by_service": {},
},
}
class ChangeFailureRateReporter:
"""Generate change failure rate reports"""
@staticmethod
def generate_report(metrics: Dict):
"""Generate human-readable report"""
results = metrics["results"]
print("\n" + "=" * 70)
print("CHANGE FAILURE RATE REPORT")
print("=" * 70)
print(f"\nPeriod: {metrics['period']['start']} to {metrics['period']['end']}")
print(f"\nSummary:")
print(f" Total Deployments: {results['total_deployments']}")
print(f" Failed Deployments: {results['failed_deployments']}")
print(f" Rollback Deployments: {results['rollback_deployments']}")
print(f" Change Failure Rate: {results['change_failure_rate']:.1f}%")
print(f" Performance Level: {results['performance_level']}")
if results.get("by_service"):
print(f"\nChange Failure Rate by Service:")
service_data = []
for service, stats in results["by_service"].items():
service_data.append(
[
service[:30],
int(stats["total"]),
int(stats["failures"]),
f"{stats['failure_rate']:.1f}%",
]
)
print(
tabulate(
service_data,
headers=["Service", "Total", "Failures", "Rate"],
tablefmt="grid",
)
)
if results.get("weekly_trend"):
print(f"\nWeekly Trend:")
trend_data = []
for week, stats in sorted(results["weekly_trend"].items()):
trend_data.append(
[
f"Week {week}",
int(stats["total"]),
int(stats["failures"]),
f"{stats['failure_rate']:.1f}%",
]
)
print(
tabulate(
trend_data,
headers=["Week", "Total", "Failures", "Rate"],
tablefmt="grid",
)
)
if results.get("recent_failures"):
print(f"\nRecent Failures (Last 5):")
failure_data = []
for failure in results["recent_failures"][:5]:
failure_data.append(
[
failure["id"][:20],
failure["service"][:20],
failure["timestamp"][:19],
(
failure.get("error_message", "No error message")[:40]
+ "..."
if failure.get("error_message")
and len(failure.get("error_message", "")) > 40
else failure.get("error_message", "No error message")
),
]
)
print(
tabulate(
failure_data,
headers=["ID", "Service", "Time", "Error"],
tablefmt="grid",
)
)
print("\nPerformance Level Guide:")
print(" Elite/High/Medium: 0-15%")
print(" Low: 16-100%")
print("\n" + "=" * 70)
@staticmethod
def export_json(metrics: Dict, output_file: Optional[str] = None):
"""Export metrics as JSON"""
json_str = json.dumps(metrics, indent=2, default=str)
if output_file:
with open(output_file, "w") as f:
f.write(json_str)
logger.info(f"Metrics exported to {output_file}")
else:
print(json_str)
@click.command()
@click.option("--config", default="config.yaml", help="Configuration file path")
@click.option(
"--output",
type=click.Choice(["json", "report"]),
default="report",
help="Output format",
)
@click.option("--output-file", help="Output file path (for json)")
def main(config, output, output_file):
"""Calculate change failure rate metrics"""
try:
# Load configuration
if not os.path.exists(config):
logger.error(f"Configuration file not found: {config}")
create_example_config()
sys.exit(1)
with open(config, "r") as f:
config_data = yaml.safe_load(f)
# Calculate metrics
calculator = ChangeFailureRateCalculator(config_data)
metrics = calculator.calculate()
# Output results
if output == "report":
ChangeFailureRateReporter.generate_report(metrics)
elif output == "json":
ChangeFailureRateReporter.export_json(metrics, output_file)
except Exception as e:
logger.error(f"Error calculating change failure rate: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
def create_example_config():
"""Create an example configuration file"""
example_config = """# Change Failure Rate Calculator Configuration
# Deployment source: github, jenkins, dynamodb
deployment_source: github
# Time range for analysis
time_range:
start_date: "30d" # or specific date: "2024-01-01"
end_date: "now"
# Filter by environments (optional)
environments:
- production
# GitHub configuration
github:
token: ${GITHUB_TOKEN} # Set via environment variable
organization: your-org
repositories:
- api-service
- web-frontend
# Jenkins configuration (if deployment_source is jenkins)
jenkins:
url: https://jenkins.example.com
username: ${JENKINS_USER}
token: ${JENKINS_TOKEN}
jobs:
- deploy-production
- deploy-staging
# DynamoDB configuration (if deployment_source is dynamodb)
dynamodb:
table_name: DeploymentStatus
profile: default # AWS profile (optional)
region: us-east-1
"""
with open("config.yaml.example", "w") as f:
f.write(example_config)
logger.info(
"Created config.yaml.example - please copy to config.yaml and update settings"
)
if __name__ == "__main__":
main()