Skip to content

Commit

Permalink
chore: rename constructor method (#237)
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan authored Jun 9, 2023
1 parent ea2233c commit b54f2c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ const (
EventPropertyVariant = "feature_flag.variant"
)

type hook struct {
// traceHook is the hook implementation for OTel traces
type traceHook struct {
setErrorStatus bool
openfeature.UnimplementedHook
}

// NewHook return a reference to a new instance of the OpenTelemetry Hook
func NewHook(opts ...Options) *hook {
h := &hook{}
// Deprecated: please use NewTracesHook instead
func NewHook(opts ...Options) *traceHook {
return NewTracesHook(opts...)
}

// NewTracesHook return a reference to a new instance of the OpenTelemetry Hook
func NewTracesHook(opts ...Options) *traceHook {
h := &traceHook{}

for _, opt := range opts {
opt(h)
Expand All @@ -33,7 +40,7 @@ func NewHook(opts ...Options) *hook {
}

// After sets the feature_flag event and associated attributes on the span stored in the context
func (h *hook) After(ctx context.Context, hookContext openfeature.HookContext, flagEvaluationDetails openfeature.InterfaceEvaluationDetails, hookHints openfeature.HookHints) error {
func (h *traceHook) After(ctx context.Context, hookContext openfeature.HookContext, flagEvaluationDetails openfeature.InterfaceEvaluationDetails, hookHints openfeature.HookHints) error {
span := trace.SpanFromContext(ctx)
span.AddEvent(EventName, trace.WithAttributes(
attribute.String(EventPropertyFlagKey, hookContext.FlagKey()),
Expand All @@ -44,7 +51,7 @@ func (h *hook) After(ctx context.Context, hookContext openfeature.HookContext, f
}

// Error records the given error against the span and sets the span to an error status
func (h *hook) Error(ctx context.Context, hookContext openfeature.HookContext, err error, hookHints openfeature.HookHints) {
func (h *traceHook) Error(ctx context.Context, hookContext openfeature.HookContext, err error, hookHints openfeature.HookHints) {
span := trace.SpanFromContext(ctx)

if h.setErrorStatus {
Expand All @@ -57,13 +64,13 @@ func (h *hook) Error(ctx context.Context, hookContext openfeature.HookContext, e

// Options of the hook

type Options func(*hook)
type Options func(*traceHook)

// WithErrorStatusEnabled enable setting span status to codes.Error in case of an error. Default behavior is disabled
func WithErrorStatusEnabled() Options {
return func(h *hook) {
return func(h *traceHook) {
h.setErrorStatus = true
}
}

var _ openfeature.Hook = &hook{}
var _ openfeature.Hook = &traceHook{}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestHookMethods(t *testing.T) {
)
otel.SetTracerProvider(tp)
ctx, span := otel.Tracer("test-tracer").Start(context.Background(), "Run")
hook := otelHook.NewHook()
hook := otelHook.NewTracesHook()
err := hook.After(
ctx,
openfeature.NewHookContext(
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestHookMethods(t *testing.T) {
)
otel.SetTracerProvider(tp)
ctx, span := otel.Tracer("test-tracer").Start(context.Background(), "Run")
hook := otelHook.NewHook()
hook := otelHook.NewTracesHook()
err := errors.New("a terrible error")
hook.Error(ctx, openfeature.HookContext{}, err, openfeature.HookHints{})
span.End()
Expand Down Expand Up @@ -128,8 +128,8 @@ func TestHookMethods(t *testing.T) {
otel.SetTracerProvider(tp)
ctx, span := otel.Tracer("test-tracer").Start(context.Background(), "Run")

// build hook with option WithErrorStatusEnabled
hook := otelHook.NewHook(otelHook.WithErrorStatusEnabled())
// build traceHook with option WithErrorStatusEnabled
hook := otelHook.NewTracesHook(otelHook.WithErrorStatusEnabled())

err := errors.New("a terrible error")
hook.Error(ctx, openfeature.HookContext{}, err, openfeature.HookHints{})
Expand All @@ -152,7 +152,7 @@ func TestHookMethods(t *testing.T) {
providerName := "provider-name"
variant := "variant"
ctx := context.Background()
hook := otelHook.NewHook()
hook := otelHook.NewTracesHook()

err := hook.After(
ctx,
Expand Down

0 comments on commit b54f2c5

Please sign in to comment.