Skip to content

Commit f7acf06

Browse files
committed
Upload average CPU consumption of CI jobs to DataDog
1 parent 7d97c59 commit f7acf06

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ jobs:
212212
# erroring about invalid credentials instead.
213213
if: github.event_name == 'push' || env.DEPLOY == '1' || env.DEPLOY_ALT == '1'
214214

215+
- name: upload job metrics to DataDog
216+
if: needs.calculate_matrix.outputs.run_type != 'pr'
217+
env:
218+
DATADOG_SITE: datadoghq.com
219+
DATADOG_API_KEY: ${{ secrets.DATADOG_API_KEY }}
220+
DD_GITHUB_JOB_NAME: ${{ matrix.name }}
221+
run: |
222+
npm install -g @datadog/datadog-ci
223+
python3 src/ci/scripts/upload-build-metrics.py build/cpu-usage.csv
224+
215225
# This job isused to tell bors the final status of the build, as there is no practical way to detect
216226
# when a workflow is successful listening to webhooks only in our current bors implementation (homu).
217227
outcome:
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
This script postprocesses data gathered during a CI run, computes certain metrics
3+
from them, and uploads these metrics to DataDog.
4+
5+
This script is expected to be executed from within a GitHub Actions job.
6+
7+
It expects the following environment variables:
8+
- DATADOG_SITE: path to the DataDog API endpoint
9+
- DATADOG_API_KEY: DataDog API token
10+
- DD_GITHUB_JOB_NAME: Name of the current GitHub Actions job
11+
12+
And it also expects the presence of a binary called `datadog-ci` to be in PATH.
13+
It can be installed with `npm install -g @datadog/datadog-ci`.
14+
15+
Usage:
16+
```bash
17+
$ python3 upload-build-metrics.py <path-to-CPU-usage-CSV>
18+
```
19+
20+
`path-to-CPU-usage-CSV` is a path to a CSV generated by the `src/ci/cpu-usage-over-time.py` script.
21+
"""
22+
import argparse
23+
import csv
24+
import subprocess
25+
from pathlib import Path
26+
from typing import List
27+
28+
29+
def load_cpu_usage(path: Path) -> List[float]:
30+
usage = []
31+
with open(path) as f:
32+
reader = csv.reader(f, delimiter=',')
33+
for row in reader:
34+
# The log might contain incomplete rows or some Python exception
35+
if len(row) == 2:
36+
try:
37+
idle = float(row[1])
38+
usage.append(100.0 - idle)
39+
except ValueError:
40+
pass
41+
return usage
42+
43+
44+
def upload_datadog_measure(name: str, value: float):
45+
"""
46+
Uploads a single numeric metric for the current GitHub Actions job to DataDog.
47+
"""
48+
subprocess.run([
49+
"datadog-ci",
50+
"measure",
51+
"--level", "job",
52+
"--measures", f"{name}:{value}"
53+
],
54+
check=False
55+
)
56+
57+
58+
if __name__ == "__main__":
59+
parser = argparse.ArgumentParser(
60+
prog="DataDog metric uploader"
61+
)
62+
parser.add_argument("cpu-usage-history-csv")
63+
args = parser.parse_args()
64+
65+
build_usage_csv = vars(args)["cpu-usage-history-csv"]
66+
usage_timeseries = load_cpu_usage(Path(build_usage_csv))
67+
if len(usage_timeseries) > 0:
68+
avg_cpu_usage = sum(usage_timeseries) / len(usage_timeseries)
69+
else:
70+
avg_cpu_usage = 0
71+
upload_datadog_measure("avg-cpu-usage", avg_cpu_usage)

0 commit comments

Comments
 (0)