1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +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
19 changed files with 327 additions and 8 deletions
@@ -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{
@@ -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})
})
}
@@ -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))}
}
@@ -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
})
}
@@ -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 {