|
| 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 | +importargparse |
| 23 | +importcsv |
| 24 | +importos |
| 25 | +importsubprocess |
| 26 | +importsys |
| 27 | +frompathlibimportPath |
| 28 | +fromtypingimportList |
| 29 | + |
| 30 | + |
| 31 | +defload_cpu_usage(path: Path) ->List[float]: |
| 32 | +usage= [] |
| 33 | +withopen(path) asf: |
| 34 | +reader=csv.reader(f, delimiter=',') |
| 35 | +forrowinreader: |
| 36 | +# The log might contain incomplete rows or some Python exception |
| 37 | +iflen(row) ==2: |
| 38 | +try: |
| 39 | +idle=float(row[1]) |
| 40 | +usage.append(100.0-idle) |
| 41 | +exceptValueError: |
| 42 | +pass |
| 43 | +returnusage |
| 44 | + |
| 45 | + |
| 46 | +defupload_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 | +ifos.getenv("GITHUB_ACTIONS") isnotNoneandsys.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 | +iflen(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