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

Add WithHostHeader config option to override Host header in opentelemetry requests #4780

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ See our [versioning policy](VERSIONING.md) for more information about these stab
The package contains semantic conventions from the `v1.24.0` version of the OpenTelemetry Semantic Conventions. (#4770)
- Add `WithResourceAsConstantLabels` option to apply resource attributes for every metric emitted by the Prometheus exporter. (#4733)
- Experimental cardinality limiting is added to the metric SDK.
See [metric documentation](./sdk/metric/internal/x/README.md#cardinality-limit) for more information about this feature and how to enable it. (#4457)
See [metric documentation](./sdk/metric/EXPERIMENTAL.md#cardinality-limit) for more information about this feature and how to enable it. (#4457)
- Add `WithHostHeader` config option to override Host header in opentelemetry requests (#4780
- Add `NewMemberRaw` and `NewKeyValuePropertyRaw` in `go.opentelemetry.io/otel/baggage`. (#4804)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -340,6 +341,13 @@ func WithHeaders(headers map[string]string) GenericOption {
})
}

func WithHostHeader(host string) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Metrics.HostHeader = host
return cfg
})
}

func WithTimeout(duration time.Duration) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Metrics.Timeout = duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, map[string]string{"m1": "mv1"}, c.Metrics.Headers)
},
},
{
name: "Test WithHeaders",
opts: []GenericOption{
WithHostHeader("th"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "th", c.Metrics.HostHeader)
},
},

// Compression Tests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@
type HTTPCollector struct {
plainTextResponse bool

headersMu sync.Mutex
headers http.Header
storage *Storage
headersMu sync.Mutex
headers http.Header
hostHeader string
storage *Storage

resultCh <-chan ExportResult
listener net.Listener
Expand Down Expand Up @@ -288,6 +289,10 @@
return c.headers.Clone()
}

func (c *HTTPCollector) HostHeader() string {
return c.hostHeader

Check warning on line 293 in exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go#L292-L293

Added lines #L292 - L293 were not covered by tests
}

func (c *HTTPCollector) handler(w http.ResponseWriter, r *http.Request) {
c.respond(w, c.record(r))
}
Expand Down Expand Up @@ -323,6 +328,8 @@
}
c.headersMu.Unlock()

c.hostHeader = r.Host

Check warning on line 331 in exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go#L331

Added line #L331 was not covered by tests

if c.resultCh != nil {
return <-c.resultCh
}
Expand Down
5 changes: 5 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func newClient(cfg oconf.Config) (*client, error) {
if cfg.Metrics.Insecure {
u.Scheme = "http"
}

// Body is set when this is cloned during upload.
req, err := http.NewRequest(http.MethodPost, u.String(), http.NoBody)
if err != nil {
Expand All @@ -88,6 +89,10 @@ func newClient(cfg oconf.Config) (*client, error) {
}
req.Header.Set("Content-Type", "application/x-protobuf")

if cfg.Metrics.HostHeader != "" {
req.Host = cfg.Metrics.HostHeader
}

return &client{
compression: Compression(cfg.Metrics.Compression),
req: req,
Expand Down
13 changes: 13 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ func TestConfig(t *testing.T) {
assert.Equal(t, got[key], []string{headers[key]})
})

t.Run("WithHostHeader", func(t *testing.T) {
hostHeader := "test-host"
exp, coll := factoryFunc("", nil, WithHostHeader(hostHeader))
ctx := context.Background()
t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) })
require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{}))
// Ensure everything is flushed.
require.NoError(t, exp.Shutdown(ctx))

got := coll.HostHeader()
assert.Equal(t, got, hostHeader)
})

t.Run("WithTimeout", func(t *testing.T) {
// Do not send on rCh so the Collector never responds to the client.
rCh := make(chan otest.ExportResult)
Expand Down
15 changes: 15 additions & 0 deletions exporters/otlp/otlpmetric/otlpmetrichttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ func WithHeaders(headers map[string]string) Option {
return wrappedOption{oconf.WithHeaders(headers)}
}

// WithHostHeader overrides Host header in HTTP requests.
//
// The example of the use case is using load balancer like envoy between
// the otel instrumentation library and collector. Typically
// the call via envoy's route is based on the host header. Example of the usage
//
// config.WithEndpoint("myenvoyhost")
//
// .WithHostHeader("mytargetotelcollectorhost")
Comment on lines +167 to +169
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look correctly formatted to be rendered as code nor does it look valid. WithHostHeader is an argument passed to a config new function, this makes it look like a method being called.

//
// If unset, the target Endpoint's host will be used as Host header.
enuret marked this conversation as resolved.
Show resolved Hide resolved
func WithHostHeader(host string) Option {
return wrappedOption{oconf.WithHostHeader(host)}
}

// 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -340,6 +341,13 @@ func WithHeaders(headers map[string]string) GenericOption {
})
}

func WithHostHeader(host string) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Metrics.HostHeader = host
return cfg
})
}

func WithTimeout(duration time.Duration) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Metrics.Timeout = duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, map[string]string{"m1": "mv1"}, c.Metrics.Headers)
},
},
{
name: "Test WithHeaders",
opts: []GenericOption{
WithHostHeader("th"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "th", c.Metrics.HostHeader)
},
},

// Compression Tests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ func (e *HTTPResponseError) Unwrap() error { return e.Err }
type HTTPCollector struct {
plainTextResponse bool

headersMu sync.Mutex
headers http.Header
storage *Storage
headersMu sync.Mutex
headers http.Header
hostHeader string
storage *Storage

resultCh <-chan ExportResult
listener net.Listener
Expand Down Expand Up @@ -288,6 +289,10 @@ func (c *HTTPCollector) Headers() map[string][]string {
return c.headers.Clone()
}

func (c *HTTPCollector) HostHeader() string {
return c.hostHeader
}

func (c *HTTPCollector) handler(w http.ResponseWriter, r *http.Request) {
c.respond(w, c.record(r))
}
Expand Down Expand Up @@ -323,6 +328,8 @@ func (c *HTTPCollector) record(r *http.Request) ExportResult {
}
c.headersMu.Unlock()

c.hostHeader = r.Host

if c.resultCh != nil {
return <-c.resultCh
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -326,6 +327,13 @@ func WithHeaders(headers map[string]string) GenericOption {
})
}

func WithHostHeader(host string) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.HostHeader = host
return cfg
})
}

func WithTimeout(duration time.Duration) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.Timeout = duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ func TestConfigs(t *testing.T) {
},
},

// Host header test
{
name: "Test With Host Header",
opts: []GenericOption{
WithHostHeader("th"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "th", c.Traces.HostHeader)
},
},

// Compression Tests
{
name: "Test With Compression",
Expand Down
4 changes: 4 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func (d *client) newRequest(body []byte) (request, error) {
}
r.Header.Set("Content-Type", contentTypeProto)

if d.cfg.HostHeader != "" {
r.Host = d.cfg.HostHeader
}

req := request{Request: r}
switch Compression(d.cfg.Compression) {
case NoCompression:
Expand Down
11 changes: 11 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
customUserAgentHeader = map[string]string{
"user-agent": "custome-user-agent",
}

customHostName = "test-host-name"
)

func TestEndToEnd(t *testing.T) {
Expand Down Expand Up @@ -150,6 +152,15 @@ func TestEndToEnd(t *testing.T) {
ExpectedHeaders: customUserAgentHeader,
},
},
{
name: "with custom host header",
opts: []otlptracehttp.Option{
otlptracehttp.WithHostHeader(customHostName),
},
mcCfg: mockCollectorConfig{
ExpectedHostHeader: customHostName,
},
},
}

for _, tc := range tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -326,6 +327,13 @@ func WithHeaders(headers map[string]string) GenericOption {
})
}

func WithHostHeader(host string) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.HostHeader = host
return cfg
})
}

func WithTimeout(duration time.Duration) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.Timeout = duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ func TestConfigs(t *testing.T) {
},
},

// Host header test
{
name: "Test With Host Header",
opts: []GenericOption{
WithHostHeader("th"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "th", c.Traces.HostHeader)
},
},

// Compression Tests
{
name: "Test With Compression",
Expand Down
12 changes: 10 additions & 2 deletions exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type mockCollector struct {
partial *collectortracepb.ExportTracePartialSuccess
delay <-chan struct{}

clientTLSConfig *tls.Config
expectedHeaders map[string]string
clientTLSConfig *tls.Config
expectedHeaders map[string]string
expectedHostHeader string
}

func (c *mockCollector) Stop() error {
Expand Down Expand Up @@ -84,6 +85,11 @@ func (c *mockCollector) serveTraces(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}
if c.expectedHostHeader != "" && c.expectedHostHeader != r.Host {
w.WriteHeader(http.StatusBadRequest)
return
}

response := collectortracepb.ExportTraceServiceResponse{
PartialSuccess: c.partial,
}
Expand Down Expand Up @@ -204,6 +210,7 @@ type mockCollectorConfig struct {
Delay <-chan struct{}
WithTLS bool
ExpectedHeaders map[string]string
ExpectedHostHeader string
}

func (c *mockCollectorConfig) fillInDefaults() {
Expand All @@ -227,6 +234,7 @@ func runMockCollector(t *testing.T, cfg mockCollectorConfig) *mockCollector {
partial: cfg.Partial,
delay: cfg.Delay,
expectedHeaders: cfg.ExpectedHeaders,
expectedHostHeader: cfg.ExpectedHostHeader,
}
mux := http.NewServeMux()
mux.Handle(cfg.TracesURLPath, http.HandlerFunc(m.serveTraces))
Expand Down
6 changes: 6 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func WithHeaders(headers map[string]string) Option {
return wrappedOption{otlpconfig.WithHeaders(headers)}
}

// WithHostHeader allows one to tell the driver to override HTTP host header.
// If value is unset Endpoint's host is used as Host header.
Comment on lines +121 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment differs from the metric one. They should be unified.

func WithHostHeader(host string) Option {
return wrappedOption{otlpconfig.WithHostHeader(host)}
}

// WithTimeout tells the driver the max waiting time for the backend to process
// each spans batch. If unset, the default will be 10 seconds.
func WithTimeout(duration time.Duration) Option {
Expand Down
Loading
Loading