Skip to content

Commit

Permalink
add client headers provider func
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingentile committed Feb 25, 2025
1 parent 8f4a5c6 commit a0e1bf9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
5 changes: 5 additions & 0 deletions exporters/otlp/otlplog/otlploghttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func newHTTPClient(cfg config) (*client, error) {
userAgent := "OTel Go OTLP over HTTP/protobuf logs exporter/" + Version()
req.Header.Set("User-Agent", userAgent)

headers := cfg.headersProvider.Value()
for k, v := range headers {
req.Header.Set(k, v)
}

if n := len(cfg.headers.Value); n > 0 {
for k, v := range cfg.headers.Value {
req.Header.Set(k, v)
Expand Down
43 changes: 29 additions & 14 deletions exporters/otlp/otlplog/otlploghttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (

// Default values.
var (
defaultEndpoint = "localhost:4318"
defaultPath = "/v1/logs"
defaultTimeout = 10 * time.Second
defaultProxy HTTPTransportProxyFunc = http.ProxyFromEnvironment
defaultRetryCfg = retry.DefaultConfig
defaultEndpoint = "localhost:4318"
defaultPath = "/v1/logs"
defaultTimeout = 10 * time.Second
defaultProxy HTTPTransportProxyFunc = http.ProxyFromEnvironment
defaultRetryCfg = retry.DefaultConfig
defaultHeadersProvider HeadersProviderFunc = func() map[string]string { return map[string]string{} }
)

// Environment variable keys.
Expand Down Expand Up @@ -85,15 +86,16 @@ type fnOpt func(config) config
func (f fnOpt) applyHTTPOption(c config) config { return f(c) }

type config struct {
endpoint setting[string]
path setting[string]
insecure setting[bool]
tlsCfg setting[*tls.Config]
headers setting[map[string]string]
compression setting[Compression]
timeout setting[time.Duration]
proxy setting[HTTPTransportProxyFunc]
retryCfg setting[retry.Config]
endpoint setting[string]
path setting[string]
insecure setting[bool]
tlsCfg setting[*tls.Config]
headers setting[map[string]string]
headersProvider setting[HeadersProviderFunc]
compression setting[Compression]
timeout setting[time.Duration]
proxy setting[HTTPTransportProxyFunc]
retryCfg setting[retry.Config]
}

func newConfig(options []Option) config {
Expand All @@ -117,6 +119,9 @@ func newConfig(options []Option) config {
c.tlsCfg = c.tlsCfg.Resolve(
loadEnvTLS[*tls.Config](),
)
c.headersProvider = c.headersProvider.Resolve(
fallback[HeadersProviderFunc](defaultHeadersProvider),
)
c.headers = c.headers.Resolve(
getenv[map[string]string](envHeaders, convHeaders),
)
Expand Down Expand Up @@ -286,6 +291,16 @@ func WithHeaders(headers map[string]string) Option {
})
}

type HeadersProviderFunc func() map[string]string

// WithHeadersProvider will be called to send the provided headers with each HTTP requests.
func WithHeadersProvider(providerFunc HeadersProviderFunc) Option {
return fnOpt(func(c config) config {
c.headersProvider = newSetting(providerFunc)
return c
})
}

// WithTimeout sets the max amount of time an Exporter will attempt an export.
//
// This takes precedence over any retry settings defined by WithRetry. Once
Expand Down
17 changes: 16 additions & 1 deletion exporters/otlp/otlplog/otlploghttp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestNewConfig(t *testing.T) {
WithHeaders(headers),
WithTimeout(time.Second),
WithRetry(RetryConfig(rc)),
// Do not test WithProxy. Requires func comparison.
// Do not test WithProxy or WithHeaderProvider. Requires func comparison.
},
want: config{
endpoint: newSetting("test"),
Expand Down Expand Up @@ -395,6 +395,9 @@ func TestNewConfig(t *testing.T) {
// Cannot compare funcs, see TestWithProxy.
c.proxy = setting[HTTPTransportProxyFunc]{}

// Cannot compare funcs, see TestWithHeaderProvider
c.headersProvider = setting[HeadersProviderFunc]{}

assert.Equal(t, tc.want, c)

for _, errMsg := range tc.errs {
Expand Down Expand Up @@ -436,3 +439,15 @@ func TestWithProxy(t *testing.T) {
assert.True(t, c.proxy.Set)
assert.NotNil(t, c.proxy.Value)
}

func TestWithHeaderProvider(t *testing.T) {
headers := map[string]string{"a": "A"}
provider := func() map[string]string {
return map[string]string{"b": "B"}
}
opts := []Option{WithHeaders(headers), WithHeadersProvider(provider)}
c := newConfig(opts)

assert.True(t, c.headersProvider.Set)
assert.NotNil(t, c.headersProvider.Value)
}

0 comments on commit a0e1bf9

Please sign in to comment.