From 5f00ebebd0c765f1acd99d9bf9013c281b5e77b7 Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Tue, 2 Jan 2024 11:58:25 +0100 Subject: [PATCH] Pass custom host header when it is set in WithHeaders() --- CHANGELOG.md | 2 +- .../otlpmetricgrpc/internal/oconf/options.go | 8 -------- exporters/otlp/otlpmetric/otlpmetrichttp/client.go | 9 ++++----- .../otlp/otlpmetric/otlpmetrichttp/client_test.go | 4 +++- exporters/otlp/otlpmetric/otlpmetrichttp/config.go | 6 ------ .../otlpmetrichttp/internal/oconf/options.go | 10 +--------- .../otlptracegrpc/internal/otlpconfig/options.go | 8 -------- exporters/otlp/otlptrace/otlptracehttp/client.go | 8 ++++---- exporters/otlp/otlptrace/otlptracehttp/client_test.go | 6 +++++- .../otlptracehttp/internal/otlpconfig/options.go | 8 -------- exporters/otlp/otlptrace/otlptracehttp/options.go | 6 ------ internal/shared/otlp/otlpmetric/oconf/options.go.tmpl | 8 -------- .../shared/otlp/otlptrace/otlpconfig/options.go.tmpl | 8 -------- 13 files changed, 18 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e81a96c7475a..c4cb1fd969c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - 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/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) ### Changed @@ -35,6 +34,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Fix `Parse` in `go.opentelemetry.io/otel/baggage` to validate member value before percent-decoding. (#4755) - Fix whitespace encoding of `Member.String` in `go.opentelemetry.io/otel/baggage`. (#4756) +- Fix processing `host` header in `WithHeaders` (#4780) ## [1.21.0/0.44.0] 2023-11-16 diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go index 6060899c9e2c..a85f27122243 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go @@ -56,7 +56,6 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string - HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -332,13 +331,6 @@ 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 diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go index 6523982ca777..5d8a503615e9 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go @@ -25,6 +25,7 @@ import ( "net/http" "net/url" "strconv" + "strings" "sync" "time" @@ -83,7 +84,6 @@ 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 { @@ -96,14 +96,13 @@ func newClient(cfg oconf.Config) (*client, error) { if n := len(cfg.Metrics.Headers); n > 0 { for k, v := range cfg.Metrics.Headers { req.Header.Set(k, v) + if strings.EqualFold(k, "host") { + req.Host = v + } } } 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, diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go index 04f815cca279..c7f4f052cfde 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go @@ -125,7 +125,9 @@ func TestConfig(t *testing.T) { t.Run("WithHostHeader", func(t *testing.T) { hostHeader := "test-host" - exp, coll := factoryFunc("", nil, WithHostHeader(hostHeader)) + key := http.CanonicalHeaderKey("host") + headers := map[string]string{key: hostHeader} + exp, coll := factoryFunc("", nil, WithHeaders(headers)) ctx := context.Background() t.Cleanup(func() { require.NoError(t, coll.Shutdown(ctx)) }) require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{})) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go index 0b4f12a8e339..6eae3e1bbdac 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go @@ -149,12 +149,6 @@ func WithHeaders(headers map[string]string) Option { return wrappedOption{oconf.WithHeaders(headers)} } -// WithHostHeader overrides Host header in HTTP requests. -// If unset, the target Endpoint's host will be used as Host header -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 diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go index 185807f0dde3..790550114548 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package oconf // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" +package oconf import ( "crypto/tls" @@ -56,7 +56,6 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string - HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -332,13 +331,6 @@ 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 diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go index 370ae769b2b9..dddb1f334dec 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go @@ -49,7 +49,6 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string - HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -318,13 +317,6 @@ 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 diff --git a/exporters/otlp/otlptrace/otlptracehttp/client.go b/exporters/otlp/otlptrace/otlptracehttp/client.go index 007a4f08e42b..4070a4b96a35 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client.go @@ -25,6 +25,7 @@ import ( "net/http" "net/url" "strconv" + "strings" "sync" "time" @@ -225,13 +226,12 @@ func (d *client) newRequest(body []byte) (request, error) { for k, v := range d.cfg.Headers { r.Header.Set(k, v) + if strings.EqualFold(k, "host") { + r.Host = v + } } 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: diff --git a/exporters/otlp/otlptrace/otlptracehttp/client_test.go b/exporters/otlp/otlptrace/otlptracehttp/client_test.go index f146fb2ae7c6..7c2c6ec444e5 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client_test.go @@ -49,6 +49,10 @@ var ( } customHostName = "test-host-name" + + customHostHeader = map[string]string{ + "host": customHostName, + } ) func TestEndToEnd(t *testing.T) { @@ -161,7 +165,7 @@ func TestEndToEnd(t *testing.T) { { name: "with custom host header", opts: []otlptracehttp.Option{ - otlptracehttp.WithHostHeader(customHostName), + otlptracehttp.WithHeaders(customHostHeader), }, mcCfg: mockCollectorConfig{ ExpectedHostHeader: customHostName, diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go index 41b175e72301..f9d268c54c38 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go @@ -49,7 +49,6 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string - HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -318,13 +317,6 @@ 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 diff --git a/exporters/otlp/otlptrace/otlptracehttp/options.go b/exporters/otlp/otlptrace/otlptracehttp/options.go index 459053baad1b..e3ed6494c5dd 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/options.go @@ -100,12 +100,6 @@ 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. -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 { diff --git a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl index 4878eb87e525..c4f9d0830f4e 100644 --- a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl +++ b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl @@ -56,7 +56,6 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string - HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -332,13 +331,6 @@ 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 diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl index bef29cd739a0..9270e506a9c6 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl @@ -49,7 +49,6 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string - HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -318,13 +317,6 @@ 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