def get_aggregated_sample_values(registry, name, labels=None):
    """
    Returns the aggregated value based on cases where all the labels specified
    match. Returns 0 if no values are found.

    The reason why REGISTRY.get_sample_value is not directly used is that the
    library function performs an equality on all the labels instead of only the
    labels provided.
    """
    labels = labels or {}
    total = 0
    for metric in registry.collect():
        for n, l, value in metric.samples:
            if n == name:
                # Ensure that all labels provided match
                matching = all(k in l and v == l[k] for _, (k, v) in enumerate(labels.items()))
                if matching:
                    total += value
    return total