You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +02:00
otlptracehttp: Add WithHTTPClient option (#6751)
Follows https://github.com/open-telemetry/opentelemetry-go/pull/6688 Towards (for OTLP trace exporter): - https://github.com/open-telemetry/opentelemetry-go/issues/4536 - https://github.com/open-telemetry/opentelemetry-go/issues/5129 - https://github.com/open-telemetry/opentelemetry-go/issues/2632
This commit is contained in:
@@ -15,8 +15,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
The package contains semantic conventions from the `v1.31.0` version of the OpenTelemetry Semantic Conventions.
|
||||
See the [migration documentation](./semconv/v1.31.0/MIGRATION.md) for information on how to upgrade from `go.opentelemetry.io/otel/semconv/v1.30.0`(#6479)
|
||||
- Add `Recording`, `Scope`, and `Record` types in `go.opentelemetry.io/otel/log/logtest`. (#6507)
|
||||
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6688)
|
||||
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#6751)
|
||||
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#6752)
|
||||
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6688)
|
||||
|
||||
### Removed
|
||||
|
||||
|
@@ -53,7 +53,9 @@ type (
|
||||
// gRPC configurations
|
||||
GRPCCredentials credentials.TransportCredentials
|
||||
|
||||
// HTTP configurations
|
||||
Proxy HTTPTransportProxyFunc
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
Config struct {
|
||||
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
||||
func WithHTTPClient(c *http.Client) GenericOption {
|
||||
return newGenericOption(func(cfg Config) Config {
|
||||
cfg.Traces.HTTPClient = c
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
|
||||
assert.Nil(t, c.Traces.Proxy)
|
||||
},
|
||||
},
|
||||
|
||||
// HTTP Client Tests
|
||||
{
|
||||
name: "Test With HTTP Client",
|
||||
opts: []GenericOption{
|
||||
WithHTTPClient(http.DefaultClient),
|
||||
},
|
||||
asserts: func(t *testing.T, c *Config, grpcOption bool) {
|
||||
assert.Equal(t, http.DefaultClient, c.Traces.HTTPClient)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test Without HTTP Client",
|
||||
opts: []GenericOption{},
|
||||
asserts: func(t *testing.T, c *Config, grpcOption bool) {
|
||||
assert.Nil(t, c.Traces.HTTPClient)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
@@ -71,7 +71,10 @@ var _ otlptrace.Client = (*client)(nil)
|
||||
func NewClient(opts ...Option) otlptrace.Client {
|
||||
cfg := otlpconfig.NewHTTPConfig(asHTTPOptions(opts)...)
|
||||
|
||||
httpClient := &http.Client{
|
||||
httpClient := cfg.Traces.HTTPClient
|
||||
|
||||
if httpClient == nil {
|
||||
httpClient = &http.Client{
|
||||
Transport: ourTransport,
|
||||
Timeout: cfg.Traces.Timeout,
|
||||
}
|
||||
@@ -87,6 +90,7 @@ func NewClient(opts ...Option) otlptrace.Client {
|
||||
clonedTransport.Proxy = cfg.Traces.Proxy
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
return &client{
|
||||
|
@@ -53,7 +53,9 @@ type (
|
||||
// gRPC configurations
|
||||
GRPCCredentials credentials.TransportCredentials
|
||||
|
||||
// HTTP configurations
|
||||
Proxy HTTPTransportProxyFunc
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
Config struct {
|
||||
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
||||
func WithHTTPClient(c *http.Client) GenericOption {
|
||||
return newGenericOption(func(cfg Config) Config {
|
||||
cfg.Traces.HTTPClient = c
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
|
||||
assert.Nil(t, c.Traces.Proxy)
|
||||
},
|
||||
},
|
||||
|
||||
// HTTP Client Tests
|
||||
{
|
||||
name: "Test With HTTP Client",
|
||||
opts: []GenericOption{
|
||||
WithHTTPClient(http.DefaultClient),
|
||||
},
|
||||
asserts: func(t *testing.T, c *Config, grpcOption bool) {
|
||||
assert.Equal(t, http.DefaultClient, c.Traces.HTTPClient)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test Without HTTP Client",
|
||||
opts: []GenericOption{},
|
||||
asserts: func(t *testing.T, c *Config, grpcOption bool) {
|
||||
assert.Nil(t, c.Traces.HTTPClient)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
@@ -153,3 +153,19 @@ func WithRetry(rc RetryConfig) Option {
|
||||
func WithProxy(pf HTTPTransportProxyFunc) Option {
|
||||
return wrappedOption{otlpconfig.WithProxy(otlpconfig.HTTPTransportProxyFunc(pf))}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets the HTTP client to used by the exporter.
|
||||
//
|
||||
// This option will take precedence over [WithProxy], [WithTimeout],
|
||||
// [WithTLSClientConfig] options as well as OTEL_EXPORTER_OTLP_CERTIFICATE,
|
||||
// OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE, OTEL_EXPORTER_OTLP_TIMEOUT,
|
||||
// OTEL_EXPORTER_OTLP_TRACES_TIMEOUT environment variables.
|
||||
//
|
||||
// Timeout and all other fields of the passed [http.Client] are left intact.
|
||||
//
|
||||
// Be aware that passing an HTTP client with transport like
|
||||
// [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.NewTransport] can
|
||||
// cause the client to be instrumented twice and cause infinite recursion.
|
||||
func WithHTTPClient(c *http.Client) Option {
|
||||
return wrappedOption{otlpconfig.WithHTTPClient(c)}
|
||||
}
|
||||
|
@@ -53,7 +53,9 @@ type (
|
||||
// gRPC configurations
|
||||
GRPCCredentials credentials.TransportCredentials
|
||||
|
||||
// HTTP configurations
|
||||
Proxy HTTPTransportProxyFunc
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
Config struct {
|
||||
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
||||
func WithHTTPClient(c *http.Client) GenericOption {
|
||||
return newGenericOption(func(cfg Config) Config {
|
||||
cfg.Traces.HTTPClient = c
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
|
||||
assert.Nil(t, c.Traces.Proxy)
|
||||
},
|
||||
},
|
||||
|
||||
// HTTP Client Tests
|
||||
{
|
||||
name: "Test With HTTP Client",
|
||||
opts: []GenericOption{
|
||||
WithHTTPClient(http.DefaultClient),
|
||||
},
|
||||
asserts: func(t *testing.T, c *Config, grpcOption bool) {
|
||||
assert.Equal(t, http.DefaultClient, c.Traces.HTTPClient)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test Without HTTP Client",
|
||||
opts: []GenericOption{},
|
||||
asserts: func(t *testing.T, c *Config, grpcOption bool) {
|
||||
assert.Nil(t, c.Traces.HTTPClient)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
Reference in New Issue
Block a user