def calculate_sampling_decision(trace_header, recorder,
                                service_name, method, path):
    """
    Return 1 if should sample and 0 if should not.
    The sampling decision coming from ``trace_header`` always has
    the highest precedence. If the ``trace_header`` doesn't contain
    sampling decision then it checks if sampling is enabled or not
    in the recorder. If not enbaled it returns 1. Otherwise it uses
    sampling rule to decide.
    """
    if trace_header.sampled is not None and trace_header.sampled != '?':
        return trace_header.sampled
    elif not recorder.sampling:
        return 1
    elif recorder.sampler.should_trace(
        service_name=service_name,
        method=method,
        path=path,
    ):
        return 1
    else:
        return 0