36 lines
767 B
Go
36 lines
767 B
Go
package kafka
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/IBM/sarama"
|
|
"go.opentelemetry.io/otel"
|
|
otelcodes "go.opentelemetry.io/otel/codes"
|
|
)
|
|
|
|
func NewProducer() *Producer {
|
|
return &Producer{}
|
|
}
|
|
|
|
type Producer struct {
|
|
}
|
|
|
|
func (c *Producer) Produce(ctx context.Context, data Topic) {
|
|
marshal, _ := json.Marshal(data)
|
|
msg := &sarama.ProducerMessage{
|
|
Topic: data.Name(),
|
|
Value: sarama.ByteEncoder(marshal),
|
|
Headers: NewCarrier().Inject(ctx),
|
|
}
|
|
client.producer.Input() <- msg
|
|
}
|
|
|
|
func producerError() {
|
|
for err := range client.producer.Errors() {
|
|
ctx := NewCarrier().ExtractProducer(err.Msg.Headers)
|
|
_, span := otel.Tracer("common.db.kafka").Start(ctx, "kafka.producer.error")
|
|
span.SetStatus(otelcodes.Error, err.Error())
|
|
span.End()
|
|
}
|
|
}
|