Skip to content

Commit

Permalink
default value implementation
Browse files Browse the repository at this point in the history
* default value config
* set default value in context source if metadata empty

fixes open-telemetry#34412
  • Loading branch information
f7o committed Sep 8, 2024
1 parent 0e2bea5 commit 82fd831
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 5 additions & 4 deletions extension/headerssetterextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ type Config struct {
}

type HeaderConfig struct {
Action ActionValue `mapstructure:"action"`
Key *string `mapstructure:"key"`
Value *string `mapstructure:"value"`
FromContext *string `mapstructure:"from_context"`
Action ActionValue `mapstructure:"action"`
Key *string `mapstructure:"key"`
Value *string `mapstructure:"value"`
FromContext *string `mapstructure:"from_context"`
DefaultValue *string `mapstructure:"default_value"`
}

// ActionValue is the enum to capture the four types of actions to perform on a header
Expand Down
3 changes: 2 additions & 1 deletion extension/headerssetterextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func newHeadersSetterExtension(cfg *Config, logger *zap.Logger) (auth.Client, er
}
} else if header.FromContext != nil {
s = &source.ContextSource{
Key: *header.FromContext,
Key: *header.FromContext,
DefaultValue: *header.DefaultValue,
}
}

Expand Down
5 changes: 3 additions & 2 deletions extension/headerssetterextension/internal/source/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import (
var _ Source = (*ContextSource)(nil)

type ContextSource struct {
Key string
Key string
DefaultValue string
}

func (ts *ContextSource) Get(ctx context.Context) (string, error) {
cl := client.FromContext(ctx)
ss := cl.Metadata.Get(ts.Key)

if len(ss) == 0 {
return "", nil
return ts.DefaultValue, nil
}

if len(ss) > 1 {
Expand Down

0 comments on commit 82fd831

Please sign in to comment.