func (this *KafkaAvroDecoder) Decode(bytes []byte) (interface{}, error) {
	if bytes == nil {
		return nil, nil
	} else {
		if bytes[0] != 0 {
			return nil, errors.New("Unknown magic byte!")
		}
		id := int32(binary.BigEndian.Uint32(bytes[1:]))
		schema, err := this.schemaRegistry.GetByID(id)
		if err != nil {
			return nil, err
		}

		if schema.Type() == avro.Bytes {
			return bytes[5:], nil
		} else {
			reader := avro.NewGenericDatumReader()
			reader.SetSchema(schema)
			value := avro.NewGenericRecord(schema)
			err := reader.Read(value, avro.NewBinaryDecoder(bytes[5:]))

			return value, err
		}
	}
}
Esempio n. 2
0
func (a *KafkaAvroAdapter) formatMessage(message *router.Message) (*sarama.ProducerMessage, error) {
	var encoder sarama.Encoder
	var containerName string
	var env docker.Env = message.Container.Config.Env

	if env != nil {
		containerName = env.Get("MARATHON_APP_ID")
	}
	if containerName == "" {
		containerName = message.Container.Name
	}
	host := env.Get("HOST")
	if host == "" {
		host = getLocalIP()
	}

	record := avro.NewGenericRecord(a.schema)
	time, _ := json.Marshal(iso8601.Time(message.Time))
	record.Set("timestamp", string(time))
	record.Set("container_name", containerName)
	record.Set("host", host)
	record.Set("source", message.Source)
	record.Set("line", message.Data)

	b, err := a.registry.Encode(record)
	if err != nil {
		return nil, err
	}
	debug(b)

	encoder = sarama.ByteEncoder(b)

	return &sarama.ProducerMessage{
		Topic: a.topic,
		Value: encoder,
	}, nil
}