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

Make Headers Setter context default value configurable (#35151) #35152

Closed
wants to merge 5 commits into from
Closed
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
28 changes: 28 additions & 0 deletions .chloggen/headers_setter_feat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: headerssetterextension

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Make default_value option for headerssetterextension from_context."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34412]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
This change adds support for specifying default value for Headers Setter extension from_context.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
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
Loading