Skip to content

Commit

Permalink
Pass custom host header when it is set in WithHeaders()
Browse files Browse the repository at this point in the history
  • Loading branch information
enuret committed Jan 2, 2024
1 parent 214528e commit 16a4d10
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 73 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
Expand Down
6 changes: 0 additions & 6 deletions exporters/otlp/otlpmetric/otlpmetrichttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/otlptrace/otlptracehttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion exporters/otlp/otlptrace/otlptracehttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var (
}

customHostName = "test-host-name"

customHostHeader = map[string]string{
"host": customHostName,
}
)

func TestEndToEnd(t *testing.T) {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -49,7 +49,6 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions exporters/otlp/otlptrace/otlptracehttp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions internal/shared/otlp/otlpmetric/oconf/options.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type (
Insecure bool
TLSCfg *tls.Config
Headers map[string]string
HostHeader string
Compression Compression
Timeout time.Duration
URLPath string
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16a4d10

Please sign in to comment.