|
| 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