Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PubSub Publish with OpenTelemetry #305

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions controllers/unleash_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -101,7 +102,7 @@ type UnleashFederation struct {
func (r *UnleashReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
id := controller.ReconcileIDFromContext(ctx)

ctx, span := r.Tracer.Start(ctx, "Reconcile", trace.WithAttributes(
ctx, span := r.Tracer.Start(ctx, "Reconcile Unleash", trace.WithAttributes(
attribute.String("reconcile.id", string(id)),
attribute.String("reconcile.namespace", req.Namespace),
attribute.String("reconcile.name", req.Name),
Expand Down Expand Up @@ -344,6 +345,9 @@ func (r *UnleashReconciler) publish(ctx context.Context, unleash *unleashv1.Unle
return nil
}

ctx, span := r.Tracer.Start(ctx, "Publish Federation")
defer span.End()

log.Info("Publishing Unleash instance to federation")
// Count the number of Unleash instances published
unleashPublished.WithLabelValues("provisioned", unleashPublishMetricStatusSending).Inc()
Expand Down Expand Up @@ -709,7 +713,7 @@ func (r *UnleashReconciler) reconcileService(ctx context.Context, unleash *unlea

// testConnection will test the connection to the Unleash instance
func (r *UnleashReconciler) testConnection(unleash resources.UnleashInstance, ctx context.Context, log logr.Logger) (*unleashclient.InstanceAdminStatsResult, error) {
ctx, span := r.Tracer.Start(ctx, "TestConnection")
ctx, span := r.Tracer.Start(ctx, "Test Connection")
defer span.End()

client, err := unleash.ApiClient(ctx, r.Client, r.OperatorNamespace)
Expand All @@ -729,7 +733,7 @@ func (r *UnleashReconciler) testConnection(unleash resources.UnleashInstance, ct
return nil, err
}

span.SetAttributes(attribute.Int("http.status_code", res.StatusCode))
span.SetAttributes(semconv.HTTPStatusCodeKey.Int(res.StatusCode))

if res.StatusCode != http.StatusOK {
span.RecordError(err)
Expand Down
30 changes: 28 additions & 2 deletions pkg/federation/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"cloud.google.com/go/pubsub"
"github.com/google/uuid"
unleashv1 "github.com/nais/unleasherator/api/v1"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/proto"
"sigs.k8s.io/controller-runtime/pkg/log"
)
Expand Down Expand Up @@ -46,14 +51,35 @@ func (p *publisher) Publish(ctx context.Context, unleash *unleashv1.Unleash, api
Data: payload,
PublishTime: time.Now(),
OrderingKey: pubsubOrderingKey,
Attributes: make(map[string]string),
}
result := p.topic.Publish(ctx, msg)
_, err = result.Get(ctx)

// Set pubsub information on span
spanOpts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindProducer),
trace.WithAttributes(
semconv.MessagingSystemGCPPubsub,
semconv.MessagingDestinationName(p.topic.ID()),
),
}

// Inject trace context into message attributes
otel.GetTextMapPropagator().Inject(ctx, propagation.MapCarrier(msg.Attributes))

_, span := otel.Tracer("publish").Start(ctx, "Send PubSub", spanOpts...)
defer span.End()

msgId, err := p.topic.Publish(ctx, msg).Get(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.Error(err, "failed to publish protobuf message")
return fmt.Errorf("publish protobuf message: %w", err)
}

// Add message ID to span attributes
span.SetAttributes(semconv.MessagingMessageIDKey.String(msgId))

log.Info("published message to topic")
return nil
}
Expand Down
Loading