private TypedMap flattenContextHierarchy()

in metrics-core/src/main/java/software/amazon/swage/metrics/record/cloudwatch/DimensionMapper.java [163:185]


    private TypedMap flattenContextHierarchy(MetricRecorder.RecorderContext context) {
        if (context.parent() == null) {
            return context.attributes();
        }

        MetricRecorder.RecorderContext parent = context;
        ImmutableTypedMap.Builder attributes = ImmutableTypedMap.Builder.from(TypedMap.empty());
        Stack<MetricRecorder.RecorderContext> parents = new Stack<>();
        while(parent != null) {
            parents.push(parent);
            parent = parent.parent();
        }

        //Add the all of the attributes starting with the oldest parent and progressing down the hierarchy. 
        //If there are name conflicts between a parent and one of its children, this order will result in the 
        //value from the parent being overridden. 
        while(!parents.empty()) {
            MetricRecorder.RecorderContext current = parents.pop();
            current.attributes().forEach(entry -> attributes.add(entry.getKey(), entry.getValue()));
        }

        return attributes.build();
    }