def group_cpu_metrics(metrics):
    """Group together each instances metrics per app/space"""
    grouped_metrics = {}
    for metric in metrics:
        grouped_metrics.setdefault(metric['space'], {}).setdefault(metric['app'], []).append(metric['value'])

    return [(app, space, metric_values,)
            for space, apps in grouped_metrics.items() for app, metric_values in apps.items()]