diff --git a/CHANGELOG.md b/CHANGELOG.md index ec2947488c7..689ef03e18c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- Add `WithProxy` option in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4906) +- Add `WithProxy` option in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlptracehttp`. (#4906) + ### Changed - `SpanFromContext` and `SpanContextFromContext` in `go.opentelemetry.io/otel/trace` no longer make a heap allocation when the passed context has no span. (#5049) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go index 316a26bd681..b6ed9a2bb65 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go @@ -9,6 +9,7 @@ package oconf // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlp import ( "crypto/tls" "fmt" + "net/http" "net/url" "path" "strings" @@ -42,6 +43,10 @@ const ( ) type ( + // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. + // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. + HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + SignalConfig struct { Endpoint string Insecure bool @@ -56,6 +61,8 @@ type ( TemporalitySelector metric.TemporalitySelector AggregationSelector metric.AggregationSelector + + Proxy HTTPTransportProxyFunc } Config struct { @@ -360,3 +367,10 @@ func WithAggregationSelector(selector metric.AggregationSelector) GenericOption return cfg }) } + +func WithProxy(pf HTTPTransportProxyFunc) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Metrics.Proxy = pf + return cfg + }) +} diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go index fb211a215e0..a24a9544556 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options_test.go @@ -8,6 +8,8 @@ package oconf import ( "errors" + "net/http" + "net/url" "testing" "time" @@ -444,6 +446,29 @@ func TestConfigs(t *testing.T) { assert.Equal(t, metric.AggregationDrop{}, got(undefinedKind)) }, }, + + // Proxy Tests + { + name: "Test With Proxy", + opts: []GenericOption{ + WithProxy(func(r *http.Request) (*url.URL, error) { + return url.Parse("http://proxy.com") + }), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.NotNil(t, c.Metrics.Proxy) + proxyURL, err := c.Metrics.Proxy(&http.Request{}) + assert.NoError(t, err) + assert.Equal(t, "http://proxy.com", proxyURL.String()) + }, + }, + { + name: "Test Without Proxy", + opts: []GenericOption{}, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Nil(t, c.Metrics.Proxy) + }, + }, } for _, tt := range tests { diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go index 5b1e18e3f95..eeb39339d45 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go @@ -58,10 +58,17 @@ func newClient(cfg oconf.Config) (*client, error) { Transport: ourTransport, Timeout: cfg.Metrics.Timeout, } - if cfg.Metrics.TLSCfg != nil { - transport := ourTransport.Clone() - transport.TLSClientConfig = cfg.Metrics.TLSCfg - httpClient.Transport = transport + + if cfg.Metrics.TLSCfg != nil || cfg.Metrics.Proxy != nil { + clonedTransport := ourTransport.Clone() + httpClient.Transport = clonedTransport + + if cfg.Metrics.TLSCfg != nil { + clonedTransport.TLSClientConfig = cfg.Metrics.TLSCfg + } + if cfg.Metrics.Proxy != nil { + clonedTransport.Proxy = cfg.Metrics.Proxy + } } u := &url.URL{ diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go index bd5205764f7..2838fd9d011 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client_test.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/http" + "net/url" "strings" "testing" "time" @@ -227,4 +228,22 @@ func TestConfig(t *testing.T) { require.Contains(t, got, key) assert.Equal(t, got[key], []string{headers[key]}) }) + + t.Run("WithProxy", func(t *testing.T) { + headerKeySetInProxy := http.CanonicalHeaderKey("X-Using-Proxy") + headerValueSetInProxy := "true" + exp, coll := factoryFunc("", nil, WithProxy(func(r *http.Request) (*url.URL, error) { + r.Header.Set(headerKeySetInProxy, headerValueSetInProxy) + return r.URL, nil + })) + 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.Headers() + require.Contains(t, got, headerKeySetInProxy) + assert.Equal(t, got[headerKeySetInProxy], []string{headerValueSetInProxy}) + }) } diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go index 48706b68151..28b058e1634 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/config.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/config.go @@ -5,6 +5,8 @@ package otlpmetrichttp // import "go.opentelemetry.io/otel/exporters/otlp/otlpme import ( "crypto/tls" + "net/http" + "net/url" "time" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" @@ -16,6 +18,11 @@ import ( // collector. type Compression oconf.Compression +// HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. +// This type is compatible with http.Transport.Proxy and can be used to set a custom proxy function +// to the OTLP HTTP client. +type HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + const ( // NoCompression tells the driver to send payloads without // compression. @@ -206,3 +213,10 @@ func WithTemporalitySelector(selector metric.TemporalitySelector) Option { func WithAggregationSelector(selector metric.AggregationSelector) Option { return wrappedOption{oconf.WithAggregationSelector(selector)} } + +// WithProxy sets the Proxy function the client will use to determine the +// proxy to use for an HTTP request. If this option is not used, the client +// will use [http.ProxyFromEnvironment]. +func WithProxy(pf HTTPTransportProxyFunc) Option { + return wrappedOption{oconf.WithProxy(oconf.HTTPTransportProxyFunc(pf))} +} diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go index df64cb916de..9bbf0941f94 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go @@ -9,6 +9,7 @@ package oconf // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlp import ( "crypto/tls" "fmt" + "net/http" "net/url" "path" "strings" @@ -42,6 +43,10 @@ const ( ) type ( + // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. + // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. + HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + SignalConfig struct { Endpoint string Insecure bool @@ -56,6 +61,8 @@ type ( TemporalitySelector metric.TemporalitySelector AggregationSelector metric.AggregationSelector + + Proxy HTTPTransportProxyFunc } Config struct { @@ -360,3 +367,10 @@ func WithAggregationSelector(selector metric.AggregationSelector) GenericOption return cfg }) } + +func WithProxy(pf HTTPTransportProxyFunc) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Metrics.Proxy = pf + return cfg + }) +} diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go index 0bb67685ab1..3401f8ec524 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options_test.go @@ -8,6 +8,8 @@ package oconf import ( "errors" + "net/http" + "net/url" "testing" "time" @@ -444,6 +446,29 @@ func TestConfigs(t *testing.T) { assert.Equal(t, metric.AggregationDrop{}, got(undefinedKind)) }, }, + + // Proxy Tests + { + name: "Test With Proxy", + opts: []GenericOption{ + WithProxy(func(r *http.Request) (*url.URL, error) { + return url.Parse("http://proxy.com") + }), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.NotNil(t, c.Metrics.Proxy) + proxyURL, err := c.Metrics.Proxy(&http.Request{}) + assert.NoError(t, err) + assert.Equal(t, "http://proxy.com", proxyURL.String()) + }, + }, + { + name: "Test Without Proxy", + opts: []GenericOption{}, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Nil(t, c.Metrics.Proxy) + }, + }, } for _, tt := range tests { diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go index 6b829cf69b7..e3f7f431fe0 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go @@ -9,6 +9,7 @@ package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/ import ( "crypto/tls" "fmt" + "net/http" "net/url" "path" "strings" @@ -35,6 +36,10 @@ const ( ) type ( + // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. + // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. + HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + SignalConfig struct { Endpoint string Insecure bool @@ -46,6 +51,8 @@ type ( // gRPC configurations GRPCCredentials credentials.TransportCredentials + + Proxy HTTPTransportProxyFunc } Config struct { @@ -332,3 +339,10 @@ func WithTimeout(duration time.Duration) GenericOption { return cfg }) } + +func WithProxy(pf HTTPTransportProxyFunc) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Traces.Proxy = pf + return cfg + }) +} diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go index f782783f1d5..87fd281dfc5 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go @@ -8,6 +8,8 @@ package otlpconfig import ( "errors" + "net/http" + "net/url" "testing" "time" @@ -408,6 +410,29 @@ func TestConfigs(t *testing.T) { assert.Equal(t, c.Traces.Timeout, 5*time.Second) }, }, + + // Proxy Tests + { + name: "Test With Proxy", + opts: []GenericOption{ + WithProxy(func(r *http.Request) (*url.URL, error) { + return url.Parse("http://proxy.com") + }), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.NotNil(t, c.Traces.Proxy) + proxyURL, err := c.Traces.Proxy(&http.Request{}) + assert.NoError(t, err) + assert.Equal(t, "http://proxy.com", proxyURL.String()) + }, + }, + { + name: "Test Without Proxy", + opts: []GenericOption{}, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Nil(t, c.Traces.Proxy) + }, + }, } for _, tt := range tests { diff --git a/exporters/otlp/otlptrace/otlptracehttp/client.go b/exporters/otlp/otlptrace/otlptracehttp/client.go index d14cd51996b..1c487a09630 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client.go @@ -74,10 +74,17 @@ func NewClient(opts ...Option) otlptrace.Client { Transport: ourTransport, Timeout: cfg.Traces.Timeout, } - if cfg.Traces.TLSCfg != nil { - transport := ourTransport.Clone() - transport.TLSClientConfig = cfg.Traces.TLSCfg - httpClient.Transport = transport + + if cfg.Traces.TLSCfg != nil || cfg.Traces.Proxy != nil { + clonedTransport := ourTransport.Clone() + httpClient.Transport = clonedTransport + + if cfg.Traces.TLSCfg != nil { + clonedTransport.TLSClientConfig = cfg.Traces.TLSCfg + } + if cfg.Traces.Proxy != nil { + clonedTransport.Proxy = cfg.Traces.Proxy + } } stopCh := make(chan struct{}) diff --git a/exporters/otlp/otlptrace/otlptracehttp/client_test.go b/exporters/otlp/otlptrace/otlptracehttp/client_test.go index aa25fedb92a..0494f82c9a7 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client_test.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "net/http" + "net/url" "strings" "testing" "time" @@ -36,6 +37,10 @@ var ( customUserAgentHeader = map[string]string{ "user-agent": "custome-user-agent", } + + customProxyHeader = map[string]string{ + "header-added-via-proxy": "proxy-value", + } ) func TestEndToEnd(t *testing.T) { @@ -150,6 +155,20 @@ func TestEndToEnd(t *testing.T) { ExpectedHeaders: customUserAgentHeader, }, }, + { + name: "with custom proxy", + opts: []otlptracehttp.Option{ + otlptracehttp.WithProxy(func(r *http.Request) (*url.URL, error) { + for k, v := range customProxyHeader { + r.Header.Set(k, v) + } + return r.URL, nil + }), + }, + mcCfg: mockCollectorConfig{ + ExpectedHeaders: customProxyHeader, + }, + }, } 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 5b286a232d0..45fefc4dd8c 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go @@ -9,6 +9,7 @@ package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/ import ( "crypto/tls" "fmt" + "net/http" "net/url" "path" "strings" @@ -35,6 +36,10 @@ const ( ) type ( + // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. + // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. + HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + SignalConfig struct { Endpoint string Insecure bool @@ -46,6 +51,8 @@ type ( // gRPC configurations GRPCCredentials credentials.TransportCredentials + + Proxy HTTPTransportProxyFunc } Config struct { @@ -332,3 +339,10 @@ func WithTimeout(duration time.Duration) GenericOption { return cfg }) } + +func WithProxy(pf HTTPTransportProxyFunc) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Traces.Proxy = pf + return cfg + }) +} diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go index 0257d8f9ffe..1df421a34dc 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go @@ -8,6 +8,8 @@ package otlpconfig import ( "errors" + "net/http" + "net/url" "testing" "time" @@ -408,6 +410,29 @@ func TestConfigs(t *testing.T) { assert.Equal(t, c.Traces.Timeout, 5*time.Second) }, }, + + // Proxy Tests + { + name: "Test With Proxy", + opts: []GenericOption{ + WithProxy(func(r *http.Request) (*url.URL, error) { + return url.Parse("http://proxy.com") + }), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.NotNil(t, c.Traces.Proxy) + proxyURL, err := c.Traces.Proxy(&http.Request{}) + assert.NoError(t, err) + assert.Equal(t, "http://proxy.com", proxyURL.String()) + }, + }, + { + name: "Test Without Proxy", + opts: []GenericOption{}, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Nil(t, c.Traces.Proxy) + }, + }, } for _, tt := range tests { diff --git a/exporters/otlp/otlptrace/otlptracehttp/options.go b/exporters/otlp/otlptrace/otlptracehttp/options.go index 37679dd373c..a3bb2b66faa 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/options.go @@ -5,6 +5,8 @@ package otlptracehttp // import "go.opentelemetry.io/otel/exporters/otlp/otlptra import ( "crypto/tls" + "net/http" + "net/url" "time" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig" @@ -15,6 +17,11 @@ import ( // collector. type Compression otlpconfig.Compression +// HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. +// This type is compatible with http.Transport.Proxy and can be used to set a custom proxy function +// to the OTLP HTTP client. +type HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + const ( // NoCompression tells the driver to send payloads without // compression. @@ -132,3 +139,10 @@ func WithTimeout(duration time.Duration) Option { func WithRetry(rc RetryConfig) Option { return wrappedOption{otlpconfig.WithRetry(retry.Config(rc))} } + +// WithProxy sets the Proxy function the client will use to determine the +// proxy to use for an HTTP request. If this option is not used, the client +// will use [http.ProxyFromEnvironment]. +func WithProxy(pf HTTPTransportProxyFunc) Option { + return wrappedOption{otlpconfig.WithProxy(otlpconfig.HTTPTransportProxyFunc(pf))} +} diff --git a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl index e7a99af8d54..071a144fdf5 100644 --- a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl +++ b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl @@ -9,6 +9,7 @@ package oconf import ( "crypto/tls" "fmt" + "net/http" "net/url" "path" "strings" @@ -42,6 +43,10 @@ const ( ) type ( + // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. + // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. + HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + SignalConfig struct { Endpoint string Insecure bool @@ -56,6 +61,8 @@ type ( TemporalitySelector metric.TemporalitySelector AggregationSelector metric.AggregationSelector + + Proxy HTTPTransportProxyFunc } Config struct { @@ -360,3 +367,10 @@ func WithAggregationSelector(selector metric.AggregationSelector) GenericOption return cfg }) } + +func WithProxy(pf HTTPTransportProxyFunc) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Metrics.Proxy = pf + return cfg + }) +} diff --git a/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl b/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl index 3bd1136e52c..f266628e2ae 100644 --- a/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl +++ b/internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl @@ -8,6 +8,8 @@ package oconf import ( "errors" + "net/http" + "net/url" "testing" "time" @@ -444,6 +446,29 @@ func TestConfigs(t *testing.T) { assert.Equal(t, metric.AggregationDrop{}, got(undefinedKind)) }, }, + + // Proxy Tests + { + name: "Test With Proxy", + opts: []GenericOption{ + WithProxy(func(r *http.Request) (*url.URL, error) { + return url.Parse("http://proxy.com") + }), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.NotNil(t, c.Metrics.Proxy) + proxyURL, err := c.Metrics.Proxy(&http.Request{}) + assert.NoError(t, err) + assert.Equal(t, "http://proxy.com", proxyURL.String()) + }, + }, + { + name: "Test Without Proxy", + opts: []GenericOption{}, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Nil(t, c.Metrics.Proxy) + }, + }, } for _, tt := range tests { diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl index 8e96ec475b2..0a64afe3ce6 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl @@ -9,6 +9,7 @@ package otlpconfig import ( "crypto/tls" "fmt" + "net/http" "net/url" "path" "strings" @@ -35,6 +36,10 @@ const ( ) type ( + // HTTPTransportProxyFunc is a function that resolves which URL to use as proxy for a given request. + // This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client. + HTTPTransportProxyFunc func(*http.Request) (*url.URL, error) + SignalConfig struct { Endpoint string Insecure bool @@ -46,6 +51,8 @@ type ( // gRPC configurations GRPCCredentials credentials.TransportCredentials + + Proxy HTTPTransportProxyFunc } Config struct { @@ -332,3 +339,10 @@ func WithTimeout(duration time.Duration) GenericOption { return cfg }) } + +func WithProxy(pf HTTPTransportProxyFunc) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Traces.Proxy = pf + return cfg + }) +} diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl index 3d1a9d460c7..2a25c103e4d 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl @@ -8,6 +8,8 @@ package otlpconfig import ( "errors" + "net/http" + "net/url" "testing" "time" @@ -408,6 +410,29 @@ func TestConfigs(t *testing.T) { assert.Equal(t, c.Traces.Timeout, 5*time.Second) }, }, + + // Proxy Tests + { + name: "Test With Proxy", + opts: []GenericOption{ + WithProxy(func(r *http.Request) (*url.URL, error) { + return url.Parse("http://proxy.com") + }), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.NotNil(t, c.Traces.Proxy) + proxyURL, err := c.Traces.Proxy(&http.Request{}) + assert.NoError(t, err) + assert.Equal(t, "http://proxy.com", proxyURL.String()) + }, + }, + { + name: "Test Without Proxy", + opts: []GenericOption{}, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Nil(t, c.Traces.Proxy) + }, + }, } for _, tt := range tests {