forked from mtusi2026/mtusi2026lab6
feat: add OpenTelemetry instrumentation
This commit is contained in:
@@ -11,7 +11,12 @@ import (
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/outbox"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/outbox"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -103,6 +108,10 @@ func isBusyGroup(err error) bool {
|
||||
}
|
||||
|
||||
func (b *Broker) PublishConfirmed(ctx context.Context, payload []byte) (string, error) {
|
||||
ctx, span := otel.Tracer("redisstream").Start(ctx, "redis XADD", trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system", "redis"), attribute.String("db.operation.name", "XADD"),
|
||||
attribute.String("messaging.destination.name", b.config.Stream)))
|
||||
defer span.End()
|
||||
envelope, err := DecodeEnvelope(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -117,12 +126,19 @@ func (b *Broker) PublishConfirmed(ctx context.Context, payload []byte) (string,
|
||||
Values: map[string]any{payloadField: string(payload)},
|
||||
}).Result()
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, "XADD failed")
|
||||
return "", fmt.Errorf("publish redis stream job: %w", err)
|
||||
}
|
||||
span.SetAttributes(attribute.String("messaging.message.id", id))
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (b *Broker) Read(ctx context.Context) ([]Delivery, error) {
|
||||
ctx, span := otel.Tracer("redisstream").Start(ctx, "redis XREADGROUP", trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system", "redis"), attribute.String("db.operation.name", "XREADGROUP"),
|
||||
attribute.String("messaging.destination.name", b.config.Stream)))
|
||||
defer span.End()
|
||||
streams, err := b.client.XReadGroup(ctx, &redis.XReadGroupArgs{
|
||||
Group: b.config.Group,
|
||||
Consumer: b.config.Consumer,
|
||||
@@ -134,6 +150,8 @@ func (b *Broker) Read(ctx context.Context) ([]Delivery, error) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, "XREADGROUP failed")
|
||||
return nil, fmt.Errorf("read redis stream jobs: %w", err)
|
||||
}
|
||||
return b.deliveries(streams)
|
||||
|
||||
Reference in New Issue
Block a user