def _sample_injector_factory(sample):
    """Factory for message mutators which inject the sample metadata

    Parameters
    ----------
    sample : dict
        The sample metadata

    Returns
    -------
    _inject_sample_md : func
        The message mutator

    """

    def _inject_sample_md(msg):
        if msg.command == "open_run":
            # No keys in metadata_kw are allows to collide with sample keys.
            if set(sample) & set(msg.kwargs):
                raise ValueError(
                    "These keys in metadata_kw are illegal "
                    "because they are always in sample: "
                    "{}".format(set(sample) & set(msg.kwargs))
                )

            msg.kwargs.update(sample)
        return msg

    return _inject_sample_md