|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import json |
| 5 | + |
| 6 | +usage = """Usage: mergeResourceJson.py FILE [FILE ...] |
| 7 | +
|
| 8 | +Merge the content of multiple "resources.json" files produced by the FastTimerService, |
| 9 | +and print the result to standard output. |
| 10 | +
|
| 11 | +Example: |
| 12 | + mergeResourceJson.py step*/pid*/resources.json > resources.json |
| 13 | +""" |
| 14 | + |
| 15 | +def merge_into(metrics, data, dest): |
| 16 | + dest["events"] += data["events"] |
| 17 | + for metric in metrics: |
| 18 | + dest[metric] += data[metric] |
| 19 | + |
| 20 | + |
| 21 | +if len(sys.argv) == 1: |
| 22 | + print(usage) |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | +if '-h' in sys.argv[1:] or '--help' in sys.argv[1:]: |
| 26 | + print(usage) |
| 27 | + sys.exit(0) |
| 28 | + |
| 29 | +with open(sys.argv[1]) as f: |
| 30 | + output = json.load(f) |
| 31 | + |
| 32 | +metrics = [ label for resource in output["resources"] for label in resource ] |
| 33 | + |
| 34 | +datamap = { module["type"] + '|' + module["label"] : module for module in output["modules"] } |
| 35 | + |
| 36 | +for arg in sys.argv[2:]: |
| 37 | + with open(arg) as f: |
| 38 | + input = json.load(f) |
| 39 | + |
| 40 | + if output["resources"] != input["resources"]: |
| 41 | + print("Error: input files describe different metrics") |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + if output["total"]["label"] != input["total"]["label"]: |
| 45 | + print("Warning: input files describe different process names") |
| 46 | + merge_into(metrics, input["total"], output["total"]) |
| 47 | + |
| 48 | + for module in input["modules"]: |
| 49 | + key = module["type"] + '|' + module["label"] |
| 50 | + if key in datamap: |
| 51 | + merge_into(metrics, module, datamap[key]) |
| 52 | + else: |
| 53 | + datamap[key] = module |
| 54 | + output["modules"].append(datamap[key]) |
| 55 | + |
| 56 | +json.dump(output, sys.stdout, indent = 2 ) |
| 57 | +sys.stdout.write('\n') |
0 commit comments