-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[CI] Upload average CPU utilization of CI jobs to DataDog #125771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
This script postprocesses data gathered during a CI run, computes certain metrics | ||
from them, and uploads these metrics to DataDog. | ||
|
||
This script is expected to be executed from within a GitHub Actions job. | ||
|
||
It expects the following environment variables: | ||
- DATADOG_SITE: path to the DataDog API endpoint | ||
- DATADOG_API_KEY: DataDog API token | ||
- DD_GITHUB_JOB_NAME: Name of the current GitHub Actions job | ||
|
||
And it also expects the presence of a binary called `datadog-ci` to be in PATH. | ||
It can be installed with `npm install -g @datadog/datadog-ci`. | ||
|
||
Usage: | ||
```bash | ||
$ python3 upload-build-metrics.py <path-to-CPU-usage-CSV> | ||
``` | ||
|
||
`path-to-CPU-usage-CSV` is a path to a CSV generated by the `src/ci/cpu-usage-over-time.py` script. | ||
""" | ||
import argparse | ||
import csv | ||
import os | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
from typing import List | ||
|
||
|
||
def load_cpu_usage(path: Path) -> List[float]: | ||
usage = [] | ||
with open(path) as f: | ||
reader = csv.reader(f, delimiter=',') | ||
for row in reader: | ||
# The log might contain incomplete rows or some Python exception | ||
if len(row) == 2: | ||
try: | ||
idle = float(row[1]) | ||
usage.append(100.0 - idle) | ||
except ValueError: | ||
pass | ||
return usage | ||
|
||
|
||
def upload_datadog_measure(name: str, value: float): | ||
""" | ||
Uploads a single numeric metric for the current GitHub Actions job to DataDog. | ||
""" | ||
print(f"Metric {name}: {value:.4f}") | ||
|
||
datadog_cmd = "datadog-ci" | ||
if os.getenv("GITHUB_ACTIONS") is not None and sys.platform.lower().startswith("win"): | ||
# Due to weird interaction of MSYS2 and Python, we need to use an absolute path, | ||
# and also specify the ".cmd" at the end. See https://github.com/rust-lang/rust/pull/125771. | ||
datadog_cmd = "C:\\npm\\prefix\\datadog-ci.cmd" | ||
|
||
subprocess.run([ | ||
datadog_cmd, | ||
"measure", | ||
"--level", "job", | ||
"--measures", f"{name}:{value}" | ||
], | ||
check=False | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
prog="DataDog metric uploader" | ||
) | ||
parser.add_argument("cpu-usage-history-csv") | ||
args = parser.parse_args() | ||
|
||
build_usage_csv = vars(args)["cpu-usage-history-csv"] | ||
usage_timeseries = load_cpu_usage(Path(build_usage_csv)) | ||
if len(usage_timeseries) > 0: | ||
avg_cpu_usage = sum(usage_timeseries) / len(usage_timeseries) | ||
else: | ||
avg_cpu_usage = 0 | ||
upload_datadog_measure("avg-cpu-usage", avg_cpu_usage) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.