Skip to content

Commit

Permalink
Make Headers Setter context default value configurable (open-telemetr…
Browse files Browse the repository at this point in the history
  • Loading branch information
kyo-ke committed Sep 12, 2024
1 parent 21208c0 commit 650620a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 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
9 changes: 8 additions & 1 deletion extension/headerssetterextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ func newHeadersSetterExtension(cfg *Config, logger *zap.Logger) (auth.Client, er
headers := make([]Header, 0, len(cfg.HeadersConfig))
for _, header := range cfg.HeadersConfig {
var s source.Source

if header.Value != nil {
s = &source.StaticSource{
Value: *header.Value,
}
} else if header.FromContext != nil {
DefaultValue := ""
if header.DefaultValue != nil {
DefaultValue = *header.DefaultValue
}

s = &source.ContextSource{
Key: *header.FromContext,
Key: *header.FromContext,
DefaultValue: DefaultValue,
}
}

Expand Down
76 changes: 76 additions & 0 deletions extension/headerssetterextension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,82 @@ var (
"header_name": "",
},
},
// Default Value test - INSERT
{
// when "TenantID" exists in the context, use the value as header_name
cfg: &Config{
HeadersConfig: []HeaderConfig{
{
Key: &header,
Action: INSERT,
FromContext: stringp("TenantID"),
},
},
},
metadata: client.NewMetadata(
map[string][]string{"TenantID": {"12345"}},
),
expectedHeaders: map[string]string{
"header_name": "12345",
},
},
{
// even when "TenantID" doesn't exist in the context, use the DefaultValue
cfg: &Config{
HeadersConfig: []HeaderConfig{
{
Key: &header,
Action: INSERT,
FromContext: stringp("TenantID"),
DefaultValue: stringp("hoge"),
},
},
},
metadata: client.NewMetadata(
map[string][]string{"foo": {"bar"}},
),
expectedHeaders: map[string]string{
"header_name": "hoge",
},
},
{
// when DefaultValue and "TenantID" exist in the context, the context takes the precedence
cfg: &Config{
HeadersConfig: []HeaderConfig{
{
Key: &header,
Action: INSERT,
FromContext: stringp("TenantID"),
DefaultValue: stringp("hoge"),
},
},
},
metadata: client.NewMetadata(
map[string][]string{"TenantID": {"12345"}},
),
expectedHeaders: map[string]string{
"header_name": "12345",
},
},
{
// when "TenantID" and DefaultValue don't exist, set the header = ""
cfg: &Config{
HeadersConfig: []HeaderConfig{
{
Key: &header,
Action: INSERT,
FromContext: stringp("TenantID"),
},
},
},
metadata: client.NewMetadata(
map[string][]string{"foo": {"bar"}},
),
expectedHeaders: map[string]string{
"header_name": "",
},
},
// Default Value test - UPSERT ... should behave as same as INSERT
}
)

Expand Down
7 changes: 4 additions & 3 deletions extension/headerssetterextension/internal/source/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ 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 {
return "", fmt.Errorf("%d source keys found in the context, can't determine which one to use", len(ss))
return ts.DefaultValue, fmt.Errorf("%d source keys found in the context, can't determine which one to use", len(ss))
}

return ss[0], nil
Expand Down

0 comments on commit 650620a

Please sign in to comment.