Skip to content

Commit 44722bd

Browse files
committed
Auto merge of #125771 - Kobzol:ci-datadog-metrics, r=jdno
[CI] Upload average CPU utilization of CI jobs to DataDog This PR adds a new CI step that uploads the average CPU utilization of the current GH job to Datadog. I want to add more metrics in follow-up PRs. r? `@jdno` try-job: dist-i686-msvc try-job: aarch64-apple try-job: x86_64-gnu-llvm-18
2 parents 360f7d7 + c1c0bd7 commit 44722bd

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

Diff for: .github/workflows/ci.yml

+10
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@^2.x.x
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:

Diff for: src/ci/scripts/upload-build-metrics.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 os
25+
import subprocess
26+
import sys
27+
from pathlib import Path
28+
from typing import List
29+
30+
31+
def load_cpu_usage(path: Path) -> List[float]:
32+
usage = []
33+
with open(path) as f:
34+
reader = csv.reader(f, delimiter=',')
35+
for row in reader:
36+
# The log might contain incomplete rows or some Python exception
37+
if len(row) == 2:
38+
try:
39+
idle = float(row[1])
40+
usage.append(100.0 - idle)
41+
except ValueError:
42+
pass
43+
return usage
44+
45+
46+
def upload_datadog_measure(name: str, value: float):
47+
"""
48+
Uploads a single numeric metric for the current GitHub Actions job to DataDog.
49+
"""
50+
print(f"Metric {name}: {value:.4f}")
51+
52+
datadog_cmd = "datadog-ci"
53+
if os.getenv("GITHUB_ACTIONS") is not None and sys.platform.lower().startswith("win"):
54+
# Due to weird interaction of MSYS2 and Python, we need to use an absolute path,
55+
# and also specify the ".cmd" at the end. See https://github.com/rust-lang/rust/pull/125771.
56+
datadog_cmd = "C:\\npm\\prefix\\datadog-ci.cmd"
57+
58+
subprocess.run([
59+
datadog_cmd,
60+
"measure",
61+
"--level", "job",
62+
"--measures", f"{name}:{value}"
63+
],
64+
check=False
65+
)
66+
67+
68+
if __name__ == "__main__":
69+
parser = argparse.ArgumentParser(
70+
prog="DataDog metric uploader"
71+
)
72+
parser.add_argument("cpu-usage-history-csv")
73+
args = parser.parse_args()
74+
75+
build_usage_csv = vars(args)["cpu-usage-history-csv"]
76+
usage_timeseries = load_cpu_usage(Path(build_usage_csv))
77+
if len(usage_timeseries) > 0:
78+
avg_cpu_usage = sum(usage_timeseries) / len(usage_timeseries)
79+
else:
80+
avg_cpu_usage = 0
81+
upload_datadog_measure("avg-cpu-usage", avg_cpu_usage)

0 commit comments

Comments
 (0)