Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 29 additions & 1 deletion pkg/middleware/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,43 @@ import (
"net/http"

"go.opentelemetry.io/otel"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

type statusRecorder struct {
http.ResponseWriter
statusCode int
}

func (r *statusRecorder) WriteHeader(code int) {
r.statusCode = code
r.ResponseWriter.WriteHeader(code)
}

func AddTracing(tracerName, spanName string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx, span := otel.Tracer(tracerName).Start(r.Context(), spanName)
defer span.End()

next.ServeHTTP(w, r.WithContext(ctx))
span.SetAttributes(
semconv.HTTPMethodKey.String(r.Method),
semconv.HTTPRouteKey.String(r.URL.Path),
semconv.HTTPURLKey.String(r.URL.String()),
semconv.HTTPHostKey.String(r.Host),
semconv.HTTPSchemeKey.String(r.URL.Scheme),
)

rec := &statusRecorder{
ResponseWriter: w,
statusCode: http.StatusOK,
}

next.ServeHTTP(rec, r.WithContext(ctx))

span.SetAttributes(
semconv.HTTPStatusCodeKey.Int(rec.statusCode),
)
}
return http.HandlerFunc(fn)
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/optimizely/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func (c *OptlyClient) SetForcedVariation(ctx context.Context, experimentKey, use
override.Messages = messages
}

span.SetAttributes(attribute.String("variationKey", override.VariationKey))
span.SetAttributes(attribute.String("experimentKey", override.ExperimentKey))

c.ForcedVariations.SetVariation(forcedVariationKey, variationKey)
return &override, nil
}
Expand Down Expand Up @@ -186,6 +189,9 @@ func (c *OptlyClient) RemoveForcedVariation(ctx context.Context, experimentKey,
override.Messages = messages
c.ForcedVariations.RemoveVariation(forcedVariationKey)

span.SetAttributes(attribute.String("variationKey", override.VariationKey))
span.SetAttributes(attribute.String("experimentKey", override.ExperimentKey))

return &override, nil
}

Expand All @@ -210,6 +216,12 @@ func (c *OptlyClient) ActivateFeature(ctx context.Context, key string, uc entiti
VariationKey: unsafeDecisionInfo.VariationKey,
}

span.SetAttributes(attribute.String("variationKey", dec.VariationKey))
span.SetAttributes(attribute.String("experimentKey", dec.ExperimentKey))
span.SetAttributes(attribute.String("featureKey", dec.FeatureKey))
span.SetAttributes(attribute.Bool("enabled", dec.Enabled))
span.SetAttributes(attribute.String("type", dec.Type))

return dec, nil
}

Expand Down Expand Up @@ -239,5 +251,10 @@ func (c *OptlyClient) ActivateExperiment(ctx context.Context, key string, uc ent
Variables: map[string]interface{}{},
}

span.SetAttributes(attribute.String("variationKey", dec.VariationKey))
span.SetAttributes(attribute.String("experimentKey", dec.ExperimentKey))
span.SetAttributes(attribute.Bool("enabled", dec.Enabled))
span.SetAttributes(attribute.String("type", dec.Type))

return dec, nil
}