1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-11-24 08:22:25 +02:00

otlptracehttp, otlpmetrichttp: Add WithProxy option (#4906)

This commit is contained in:
Mickael Alliel 2024-03-12 12:04:06 +02:00 committed by GitHub
parent 76921e9020
commit 9a515ceb74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 327 additions and 8 deletions

View File

@ -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)

View File

@ -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
})
}

View File

@ -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 {

View File

@ -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{

View File

@ -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})
})
}

View File

@ -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))}
}

View File

@ -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
})
}

View File

@ -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 {

View File

@ -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
})
}

View File

@ -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 {

View File

@ -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{})

View File

@ -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 {

View File

@ -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
})
}

View File

@ -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 {

View File

@ -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))}
}

View File

@ -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
})
}

View File

@ -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 {

View File

@ -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
})
}

View File

@ -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 {