示例#1
0
文件: __init__.py 项目: adieu/heka-py
    def __init__(self):

        from heka.streams import DebugCaptureStream
        from heka.encoders import JSONEncoder

        self.stream = DebugCaptureStream()
        self.encoder = JSONEncoder()
示例#2
0
    def test_hmac_signer_sha1(self):
        hmac_signer = {'signer': 'vic',
                       'key_version': 1,
                       'hash_function': 'SHA1',
                       'key': 'some_key'}

        enc = JSONEncoder(hmac_signer)

        bytes = enc.encode(SAMPLE_MSG)
        header, message = decode_message(bytes)

        e1 = hmac.new(hmac_signer['key'],
                      enc.msg_to_payload(SAMPLE_MSG), sha1).digest()
        eq_(header.hmac, e1)
示例#3
0
    def test_uuid_becomes_base64(self):
        # Check that message decoding works symmetrically

        enc = JSONEncoder()
        heka_jdata = """{"uuid":"VTSIOf61R1yNDapwB8NzMw==","timestamp":1366901022982316037,"type":"TEST","logger":"GoSpec","severity":6,"payload":"Test Payload","env_version":"0.8","pid":16611,"hostname":"victorng-MacBookAir","fields":[{"name":"foo","value_type":"STRING","representation":"","value_string":["bar"]}]}"""
        heka_msg = enc.decode(heka_jdata)
        heka_bytes = enc.encode(heka_msg)
        h, m = decode_message(heka_bytes)

        eq_(m, heka_msg)

        # Now double check that the msg going to json is the same
        py_bytes = enc.encode(m)
        eq_(json.loads(py_bytes[8:]), json.loads(heka_jdata))
        eq_(base64.b64encode(heka_msg.uuid), json.loads(heka_jdata)['uuid'])
示例#4
0
    def test_json_encoding(self):
        enc = JSONEncoder()

        bytes = enc.encode(SAMPLE_MSG)

        eq_(ord(bytes[0]), RECORD_SEPARATOR)
        header_len = ord(bytes[1])
        header = bytes[2:2+header_len]

        # Now double check the header
        h = Header()
        h.ParseFromString(header)
        eq_(h.message_encoding, Header.MessageEncoding.Value('JSON'))
        eq_(h.message_length, len(enc.msg_to_payload(SAMPLE_MSG)))

        eq_(ord(bytes[header_len+2]), 0x1f)
        json_data = bytes[header_len+3:]
        eq_(len(json_data), h.message_length)

        # Check that the messages are the same object (minus UUID)
        eq_(enc.decode(json_data), SAMPLE_MSG)
示例#5
0
文件: __init__.py 项目: adieu/heka-py
class DebugCaptureSender(object):
    def __init__(self):

        from heka.streams import DebugCaptureStream
        from heka.encoders import JSONEncoder

        self.stream = DebugCaptureStream()
        self.encoder = JSONEncoder()

    def send_message(self, msg):
        self.stream.write(self.encoder.encode(msg))

    def __getattr__(self, key):
        return getattr(self.stream, key)
示例#6
0
            f.value_type = Field.BOOL
            f.value_bool.append(bool(v))
        elif isinstance(v, basestring):
            f.value_type = Field.STRING
            f.value_string.append(v)
        elif isinstance(v, types.DictType):
            msg.fields.remove(f)
            _flatten_fields(msg, v, prefix=full_name)
        else:
            msg = "Unexpected value type : [%s][%s]" % (type(v), v)
            raise ValueError(msg)


ENCODERS = {
    Header.MessageEncoding.Value('PROTOCOL_BUFFER'): ProtobufEncoder(),
    Header.MessageEncoding.Value('JSON'): JSONEncoder()
}


def decode_message(bytes):
    """
    return header and message object
    """
    header_len = ord(bytes[1])
    header_bytes = bytes[2:2 + header_len]

    # Now double check the header
    h = Header()
    h.ParseFromString(header_bytes)
    encoder = ENCODERS[h.message_encoding]
    return h, encoder.decode(bytes[header_len + 3:])