From 99f34b3af762bb304997572caa143ec453ffba25 Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Fri, 22 Dec 2023 16:14:22 +0100 Subject: [PATCH 01/12] Add onfiguration option to override Host header. --- CHANGELOG.md | 1 + .../otlpmetricgrpc/internal/oconf/options.go | 8 ++++++++ .../otlpmetricgrpc/internal/otest/collector.go | 13 ++++++++++--- .../otlp/otlpmetric/otlpmetrichttp/client.go | 5 +++++ .../otlp/otlpmetric/otlpmetrichttp/client_test.go | 15 ++++++++++++++- .../otlp/otlpmetric/otlpmetrichttp/config.go | 6 ++++++ .../otlpmetrichttp/internal/oconf/options.go | 8 ++++++++ .../otlpmetrichttp/internal/otest/collector.go | 13 ++++++++++--- .../otlptracegrpc/internal/otlpconfig/options.go | 8 ++++++++ exporters/otlp/otlptrace/otlptracehttp/client.go | 4 ++++ .../otlp/otlptrace/otlptracehttp/client_test.go | 11 +++++++++++ .../otlptracehttp/internal/otlpconfig/options.go | 10 +++++++++- .../otlptracehttp/mock_collector_test.go | 12 ++++++++++-- exporters/otlp/otlptrace/otlptracehttp/options.go | 6 ++++++ .../shared/otlp/otlpmetric/oconf/options.go.tmpl | 8 ++++++++ .../otlp/otlpmetric/otest/collector.go.tmpl | 13 ++++++++++--- .../otlp/otlptrace/otlpconfig/options.go.tmpl | 8 ++++++++ 17 files changed, 136 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c283b9cbebb..25a7a5eeba8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ 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 (#4777) ### Changed diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go index a85f2712224..6060899c9e2 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go @@ -56,6 +56,7 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string + HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -331,6 +332,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 diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go index f08fbd5c5f7..b4bc6bc7313 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go @@ -197,9 +197,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 @@ -299,6 +300,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)) } @@ -334,6 +339,8 @@ func (c *HTTPCollector) record(r *http.Request) ExportResult { } c.headersMu.Unlock() + c.hostHeader = r.Host + if c.resultCh != nil { return <-c.resultCh } diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go index 73463c91d5f..6523982ca77 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go @@ -83,6 +83,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 { @@ -99,6 +100,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, diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go index a4ead01c1f1..04f815cca27 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -123,6 +123,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) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go index 6eae3e1bbda..0b4f12a8e33 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go @@ -149,6 +149,12 @@ 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 59468b9a5ed..185807f0dde 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go @@ -56,6 +56,7 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string + HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -331,6 +332,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 diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go index 6398f8ba5ba..c4f96433e0f 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go @@ -197,9 +197,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 @@ -299,6 +300,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)) } @@ -334,6 +339,8 @@ func (c *HTTPCollector) record(r *http.Request) ExportResult { } c.headersMu.Unlock() + c.hostHeader = r.Host + if c.resultCh != nil { return <-c.resultCh } diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go index dddb1f334de..370ae769b2b 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go @@ -49,6 +49,7 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string + HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -317,6 +318,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 diff --git a/exporters/otlp/otlptrace/otlptracehttp/client.go b/exporters/otlp/otlptrace/otlptracehttp/client.go index 3b5f3839f27..007a4f08e42 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client.go @@ -228,6 +228,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: diff --git a/exporters/otlp/otlptrace/otlptracehttp/client_test.go b/exporters/otlp/otlptrace/otlptracehttp/client_test.go index 63a4cd4f207..f146fb2ae7c 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client_test.go @@ -47,6 +47,8 @@ var ( customUserAgentHeader = map[string]string{ "user-agent": "custome-user-agent", } + + customHostName = "test-host-name" ) func TestEndToEnd(t *testing.T) { @@ -156,6 +158,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 { diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go index 8401bd7f155..41b175e7230 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig" +package otlpconfig import ( "crypto/tls" @@ -49,6 +49,7 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string + HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -317,6 +318,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 diff --git a/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go b/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go index 2b87215d183..cae09ac1188 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/mock_collector_test.go @@ -50,8 +50,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 { @@ -95,6 +96,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, } @@ -215,6 +221,7 @@ type mockCollectorConfig struct { Delay <-chan struct{} WithTLS bool ExpectedHeaders map[string]string + ExpectedHostHeader string } func (c *mockCollectorConfig) fillInDefaults() { @@ -238,6 +245,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)) diff --git a/exporters/otlp/otlptrace/otlptracehttp/options.go b/exporters/otlp/otlptrace/otlptracehttp/options.go index e3ed6494c5d..459053baad1 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/options.go @@ -100,6 +100,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. +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 c4f9d0830f4..4878eb87e52 100644 --- a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl +++ b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl @@ -56,6 +56,7 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string + HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -331,6 +332,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 diff --git a/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl b/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl index fba237e68fc..aff6fad65e1 100644 --- a/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl +++ b/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl @@ -197,9 +197,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 @@ -299,6 +300,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)) } @@ -334,6 +339,8 @@ func (c *HTTPCollector) record(r *http.Request) ExportResult { } c.headersMu.Unlock() + c.hostHeader = r.Host + if c.resultCh != nil { return <-c.resultCh } diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl index 9270e506a9c..bef29cd739a 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl @@ -49,6 +49,7 @@ type ( Insecure bool TLSCfg *tls.Config Headers map[string]string + HostHeader string Compression Compression Timeout time.Duration URLPath string @@ -317,6 +318,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 From ccbd51b197de35c8345ad39bfc348d1d5f65ed56 Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Thu, 4 Jan 2024 16:34:49 +0100 Subject: [PATCH 02/12] Fix linter --- .../otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go | 2 +- .../otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go index b4bc6bc7313..8cc131676e7 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go @@ -301,7 +301,7 @@ func (c *HTTPCollector) Headers() map[string][]string { } func (c *HTTPCollector) HostHeader() string { - return c.hostHeader + return c.hostHeader } func (c *HTTPCollector) handler(w http.ResponseWriter, r *http.Request) { diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go index c4f96433e0f..f143b2f2a8e 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go @@ -301,7 +301,7 @@ func (c *HTTPCollector) Headers() map[string][]string { } func (c *HTTPCollector) HostHeader() string { - return c.hostHeader + return c.hostHeader } func (c *HTTPCollector) handler(w http.ResponseWriter, r *http.Request) { From d4f00880f8b5e3540dd5cbc7db1142db39798bfe Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Thu, 4 Jan 2024 16:37:54 +0100 Subject: [PATCH 03/12] Fix linter --- internal/shared/otlp/otlpmetric/otest/collector.go.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl b/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl index aff6fad65e1..d15c895c052 100644 --- a/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl +++ b/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl @@ -42,9 +42,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" - "{{ .oconfImportPath }}" collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" mpb "go.opentelemetry.io/proto/otlp/metrics/v1" + "{{ .oconfImportPath }}" ) // Collector is the collection target a Client sends metric uploads to. @@ -301,7 +301,7 @@ func (c *HTTPCollector) Headers() map[string][]string { } func (c *HTTPCollector) HostHeader() string { - return c.hostHeader + return c.hostHeader } func (c *HTTPCollector) handler(w http.ResponseWriter, r *http.Request) { From f802723d1b92a48f44498365b6832ceb0e9d5aa4 Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Mon, 8 Jan 2024 13:35:05 +0100 Subject: [PATCH 04/12] fix linter: make precommit --- .../otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go | 2 +- .../otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go index 8cc131676e7..b5649fcc8cb 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go @@ -42,9 +42,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf" collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" mpb "go.opentelemetry.io/proto/otlp/metrics/v1" + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf" ) // Collector is the collection target a Client sends metric uploads to. diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go index f143b2f2a8e..8f8cb92491c 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go @@ -42,9 +42,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" mpb "go.opentelemetry.io/proto/otlp/metrics/v1" + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" ) // Collector is the collection target a Client sends metric uploads to. From 590b3ea852764f83a19f9ed5b75ccc0e5d11640a Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova <109675364+enuret@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:38:36 +0100 Subject: [PATCH 05/12] Update CHANGELOG.md Co-authored-by: Damien Mathieu <42@dmathieu.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed807eede7..c1ba3498c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ 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 (#4777) +- Add `WithHostHeader` config option to override Host header in opentelemetry requests (#4780) ### Changed From 4b00ca12d76e1f54ac770f4b51f608db0f9a3d70 Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova <109675364+enuret@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:38:48 +0100 Subject: [PATCH 06/12] Update exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go Co-authored-by: Damien Mathieu <42@dmathieu.com> --- exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go index 04f815cca27..b3dc928a6d6 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, From d6ee1261393000fe633bcb270b95aa79c0b6a1c7 Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Mon, 8 Jan 2024 13:59:30 +0100 Subject: [PATCH 07/12] options tests --- .../otlpmetricgrpc/internal/oconf/options_test.go | 9 +++++++++ .../otlpmetrichttp/internal/oconf/options_test.go | 9 +++++++++ .../otlptracegrpc/internal/otlpconfig/options_test.go | 11 +++++++++++ .../otlptracehttp/internal/otlpconfig/options_test.go | 11 +++++++++++ .../shared/otlp/otlpmetric/oconf/options_test.go.tmpl | 9 +++++++++ .../otlp/otlptrace/otlpconfig/options_test.go.tmpl | 11 +++++++++++ 6 files changed, 60 insertions(+) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go index 64457efbf08..7dc95d15fc3 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go @@ -303,6 +303,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 { diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go index 1da4a6f2f63..bb533690473 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go @@ -303,6 +303,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 { diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go index 567f12f1018..8c58344d8cb 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go @@ -300,6 +300,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", diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go index 4c7a525a996..a2037f9b453 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go @@ -300,6 +300,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", diff --git a/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl b/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl index 7c52efc51d9..578ca89cd5d 100644 --- a/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl +++ b/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl @@ -303,6 +303,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 { diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl index b83891a89f6..b9d94cafff1 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl @@ -300,6 +300,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", From 78fa53c8aa117d61a921622945cc32f2fe9492bf Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Mon, 8 Jan 2024 14:22:20 +0100 Subject: [PATCH 08/12] gofmt --- .../otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go index b5649fcc8cb..8cc131676e7 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/otest/collector.go @@ -42,9 +42,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf" collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" mpb "go.opentelemetry.io/proto/otlp/metrics/v1" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf" ) // Collector is the collection target a Client sends metric uploads to. From 0c612a8260276933bfa5653e509726c872d2743e Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Mon, 8 Jan 2024 15:24:19 +0100 Subject: [PATCH 09/12] shared file for lint --- internal/shared/otlp/otlpmetric/otest/collector.go.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl b/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl index d15c895c052..083a05011c7 100644 --- a/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl +++ b/internal/shared/otlp/otlpmetric/otest/collector.go.tmpl @@ -42,9 +42,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" + "{{ .oconfImportPath }}" collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" mpb "go.opentelemetry.io/proto/otlp/metrics/v1" - "{{ .oconfImportPath }}" ) // Collector is the collection target a Client sends metric uploads to. From ece66a661601827fec2f34abf77791a9bb0d8e9f Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Mon, 8 Jan 2024 15:52:01 +0100 Subject: [PATCH 10/12] Lint for config.go --- exporters/otlp/otlpmetric/otlpmetrichttp/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go index 0b4f12a8e33..7a3b2ddde3c 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go @@ -150,7 +150,7 @@ func WithHeaders(headers map[string]string) Option { } // WithHostHeader overrides Host header in HTTP requests. -// If unset, the target Endpoint's host will be used as Host header +// If unset, the target Endpoint's host will be used as Host header. func WithHostHeader(host string) Option { return wrappedOption{oconf.WithHostHeader(host)} } From 2fbec025f2aed9a823b842228fce21c49cbc477c Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Mon, 8 Jan 2024 16:02:50 +0100 Subject: [PATCH 11/12] run precommit --- .../otlpmetric/otlpmetrichttp/internal/otest/collector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go index 8f8cb92491c..870f2f76b56 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package otest // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest" +package otest import ( "bytes" @@ -42,9 +42,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" mpb "go.opentelemetry.io/proto/otlp/metrics/v1" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" ) // Collector is the collection target a Client sends metric uploads to. From e18a523fcc5fc19cd02817decfb79d7f5806d36a Mon Sep 17 00:00:00 2001 From: Elena Nuretdinova Date: Fri, 12 Jan 2024 16:41:38 +0100 Subject: [PATCH 12/12] Add comment & fix lint --- exporters/otlp/otlpmetric/otlpmetrichttp/config.go | 9 +++++++++ .../otlpmetrichttp/internal/otest/collector.go | 2 +- .../otlptracehttp/internal/otlpconfig/options.go | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go index 7a3b2ddde3c..e735dacd8ef 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go @@ -150,6 +150,15 @@ func WithHeaders(headers map[string]string) Option { } // 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") +// // If unset, the target Endpoint's host will be used as Host header. func WithHostHeader(host string) Option { return wrappedOption{oconf.WithHostHeader(host)} diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go index 870f2f76b56..f143b2f2a8e 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest/collector.go @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package otest +package otest // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest" import ( "bytes" diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go index 41b175e7230..460c71dadb9 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package otlpconfig +package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig" import ( "crypto/tls"