-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(checkoutservice): add producer interceptor for tracing
- Loading branch information
1 parent
0d7e94b
commit b1dead7
Showing
4 changed files
with
64 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package kafka | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/propagation" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/IBM/sarama" | ||
) | ||
|
||
type OTelInterceptor struct { | ||
tracer trace.Tracer | ||
fixedAttrs []attribute.KeyValue | ||
} | ||
|
||
// NewOTelInterceptor processes span for intercepted messages and add some | ||
// headers with the span data. | ||
func NewOTelInterceptor() *OTelInterceptor { | ||
oi := OTelInterceptor{} | ||
oi.tracer = otel.Tracer("github.com/open-telemetry/opentelemetry-demo/checkoutservice/sarama") | ||
|
||
oi.fixedAttrs = []attribute.KeyValue{ | ||
semconv.MessagingSystemKafka, | ||
semconv.MessagingOperationPublish, | ||
semconv.NetworkTransportTCP, | ||
} | ||
return &oi | ||
} | ||
|
||
func (oi *OTelInterceptor) OnSend(msg *sarama.ProducerMessage) { | ||
spanContext, span := oi.tracer.Start( | ||
context.Background(), | ||
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)), | ||
), | ||
) | ||
defer span.End() | ||
|
||
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)}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters