def _serialize_context(context):
    # Our sending format is made up of two messages. The first has a
    # quick to unpack set of meta data that our collector is going to
    # use for routing and stats. This is much faster than having the
    # collector decode the whole event. We're just going to use python
    # struct module to make a quick and dirty data structure
    context_dict = context.to_dict()
    for key in ('host', 'type'):
        if len(context_dict.get(key, "")) > 64:
            raise ValueError("Value too long: %r" % key)

    meta_data = struct.pack(META_STRUCT_FMT, META_STRUCT_VERSION,
                            context_dict['end'], context_dict['host'],
                            context_dict['type'])

    try:
        context_data = msgpack.packb(context_dict)
    except __HOLE__:
        try:
            # If we fail to serialize our context, we can try again with an
            # enhanced packer (it's slower though)
            context_data = msgpack.packb(context_dict,
                                         default=utils.msgpack_encode_default)
        except TypeError:
            log.exception("Serialization failure (not fatal, dropping data)")

            # One last try after dropping the body
            context_dict['body'] = None
            context_data = msgpack.packb(context_dict)

    return meta_data, context_data
dataset/ETHPy150Open rhettg/BlueOx/blueox/network.py/_serialize_context