Skip to content

Commit

Permalink
fix kafka producer spans (#1428)
Browse files Browse the repository at this point in the history
  • Loading branch information
puckpuck authored Mar 2, 2024
1 parent 06f020c commit b7c7891
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/accountingservice/kafka/trace_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type OTelInterceptor struct {
// headers with the span data.
func NewOTelInterceptor(groupID string) *OTelInterceptor {
oi := OTelInterceptor{}
oi.tracer = otel.Tracer("github.com/open-telemetry/opentelemetry-demo/accountingservice/sarama")
oi.tracer = otel.Tracer("accountingservice")

oi.fixedAttrs = []attribute.KeyValue{
semconv.MessagingSystemKafka,
Expand Down
1 change: 0 additions & 1 deletion src/checkoutservice/kafka/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func CreateKafkaProducer(brokers []string, log *logrus.Logger) (sarama.AsyncProd
saramaConfig.Version = ProtocolVersion
// So we can know the partition and offset of messages.
saramaConfig.Producer.Return.Successes = true
saramaConfig.Producer.Interceptors = []sarama.ProducerInterceptor{NewOTelInterceptor()}

producer, err := sarama.NewAsyncProducer(brokers, saramaConfig)
if err != nil {
Expand Down
60 changes: 0 additions & 60 deletions src/checkoutservice/kafka/trace_interceptor.go

This file was deleted.

35 changes: 33 additions & 2 deletions src/checkoutservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -310,7 +311,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq

// send to kafka only if kafka broker address is set
if cs.kafkaBrokerSvcAddr != "" {
cs.sendToPostProcessor(orderResult)
cs.sendToPostProcessor(ctx, orderResult)
}

resp := &pb.PlaceOrderResponse{Order: orderResult}
Expand Down Expand Up @@ -473,7 +474,7 @@ func (cs *checkoutService) shipOrder(ctx context.Context, address *pb.Address, i
return resp.GetTrackingId(), nil
}

func (cs *checkoutService) sendToPostProcessor(result *pb.OrderResult) {
func (cs *checkoutService) sendToPostProcessor(ctx context.Context, result *pb.OrderResult) {
message, err := proto.Marshal(result)
if err != nil {
log.Errorf("Failed to marshal message to protobuf: %+v", err)
Expand All @@ -485,7 +486,37 @@ func (cs *checkoutService) sendToPostProcessor(result *pb.OrderResult) {
Value: sarama.ByteEncoder(message),
}

// Inject tracing info into message
span := createProducerSpan(ctx, &msg)
defer span.End()

cs.KafkaProducerClient.Input() <- &msg
successMsg := <-cs.KafkaProducerClient.Successes()
log.Infof("Successful to write message. offset: %v", successMsg.Offset)
}

func createProducerSpan(ctx context.Context, msg *sarama.ProducerMessage) trace.Span {
spanContext, span := tracer.Start(
ctx,
fmt.Sprintf("%s publish", msg.Topic),
trace.WithSpanKind(trace.SpanKindProducer),
trace.WithAttributes(
semconv.PeerService("kafka"),
semconv.NetworkTransportTCP,
semconv.MessagingSystemKafka,
semconv.MessagingDestinationName(msg.Topic),
semconv.MessagingOperationPublish,
semconv.MessagingKafkaDestinationPartition(int(msg.Partition)),
),
)

carrier := propagation.MapCarrier{}
propagator := otel.GetTextMapPropagator()
propagator.Inject(spanContext, carrier)

for key, value := range carrier {
msg.Headers = append(msg.Headers, sarama.RecordHeader{Key: []byte(key), Value: []byte(value)})
}

return span
}

0 comments on commit b7c7891

Please sign in to comment.