Exemplo n.º 1
0
func (prod *Kafka) send(msg core.Message) {
	// If we have not yet connected or the connection dropped: connect.
	if prod.client == nil || prod.client.Closed() {
		if prod.producer != nil {
			prod.producer.Close()
			prod.producer = nil
		}

		var err error
		prod.client, err = kafka.NewClient(prod.servers, prod.config)
		if err != nil {
			Log.Error.Print("Kafka client error:", err)
			return // ### return, connection failed ###
		}
	}

	// Make sure we have a producer up and running
	if prod.producer == nil {
		var err error
		prod.producer, err = kafka.NewAsyncProducerFromClient(prod.client)
		if err != nil {
			Log.Error.Print("Kafka producer error:", err)
			return // ### return, connection failed ###
		}
	}

	if prod.client != nil && prod.producer != nil {
		msg.Data, msg.StreamID = prod.ProducerBase.Format(msg)

		// Send message
		topic, topicMapped := prod.topic[msg.StreamID]
		if !topicMapped {
			// Use wildcard fallback or stream name if not set
			topic, topicMapped = prod.topic[core.WildcardStreamID]
			if !topicMapped {
				topic = core.StreamTypes.GetStreamName(msg.StreamID)
			}
		}

		prod.producer.Input() <- &kafka.ProducerMessage{
			Topic: topic,
			Key:   nil,
			Value: kafka.ByteEncoder(msg.Data),
		}

		// Check for errors
		select {
		case err := <-prod.producer.Errors():
			Log.Error.Print("Kafka producer error:", err)
		default:
		}
	}
}
Exemplo n.º 2
0
func (z *zipkin) pub(s *zipkincore.Span, pr sarama.SyncProducer) {
	t := thrift.NewTMemoryBuffer()
	p := thrift.NewTBinaryProtocolTransport(t)

	if err := s.Write(p); err != nil {
		return
	}

	m := &sarama.ProducerMessage{
		Topic: z.opts.Topic,
		Key:   nil,
		Value: sarama.ByteEncoder(t.Buffer.Bytes()),
	}

	pr.SendMessage(m)
}
Exemplo n.º 3
0
// Fire - to implement logrus.Hook interface
func (kC kafkaConn) Fire(entry *logrus.Entry) error {
	body, err := entry.Reader()
	if err != nil {
		return err
	}

	// Construct message to send to Kafka
	msg := sarama.ProducerMessage{
		Topic: kC.topic,
		Value: sarama.ByteEncoder(body.Bytes()),
	}

	// Attempt sending the message to Kafka
	_, _, err = kC.producer.SendMessage(&msg)
	if err != nil {
		return fmt.Errorf("Error sending event to Kafka - %v", err)
	}
	return nil
}