Skip to content

Commit 5cd1611

Browse files
authored
otlptracehttp: Add WithHTTPClient option (#6751)
Follows #6688 Towards (for OTLP trace exporter): - #4536 - #5129 - #2632
1 parent b665425 commit 5cd1611

File tree

9 files changed

+118
-16
lines changed

9 files changed

+118
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1515
The package contains semantic conventions from the `v1.31.0` version of the OpenTelemetry Semantic Conventions.
1616
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)
1717
- Add `Recording`, `Scope`, and `Record` types in `go.opentelemetry.io/otel/log/logtest`. (#6507)
18-
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6688)
18+
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#6751)
1919
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#6752)
20+
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6688)
2021

2122
### Removed
2223

exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options_test.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporters/otlp/otlptrace/otlptracehttp/client.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,24 @@ var _ otlptrace.Client = (*client)(nil)
7171
func NewClient(opts ...Option) otlptrace.Client {
7272
cfg := otlpconfig.NewHTTPConfig(asHTTPOptions(opts)...)
7373

74-
httpClient := &http.Client{
75-
Transport: ourTransport,
76-
Timeout: cfg.Traces.Timeout,
77-
}
78-
79-
if cfg.Traces.TLSCfg != nil || cfg.Traces.Proxy != nil {
80-
clonedTransport := ourTransport.Clone()
81-
httpClient.Transport = clonedTransport
74+
httpClient := cfg.Traces.HTTPClient
8275

83-
if cfg.Traces.TLSCfg != nil {
84-
clonedTransport.TLSClientConfig = cfg.Traces.TLSCfg
76+
if httpClient == nil {
77+
httpClient = &http.Client{
78+
Transport: ourTransport,
79+
Timeout: cfg.Traces.Timeout,
8580
}
86-
if cfg.Traces.Proxy != nil {
87-
clonedTransport.Proxy = cfg.Traces.Proxy
81+
82+
if cfg.Traces.TLSCfg != nil || cfg.Traces.Proxy != nil {
83+
clonedTransport := ourTransport.Clone()
84+
httpClient.Transport = clonedTransport
85+
86+
if cfg.Traces.TLSCfg != nil {
87+
clonedTransport.TLSClientConfig = cfg.Traces.TLSCfg
88+
}
89+
if cfg.Traces.Proxy != nil {
90+
clonedTransport.Proxy = cfg.Traces.Proxy
91+
}
8892
}
8993
}
9094

exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporters/otlp/otlptrace/otlptracehttp/options.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,19 @@ func WithRetry(rc RetryConfig) Option {
153153
func WithProxy(pf HTTPTransportProxyFunc) Option {
154154
return wrappedOption{otlpconfig.WithProxy(otlpconfig.HTTPTransportProxyFunc(pf))}
155155
}
156+
157+
// WithHTTPClient sets the HTTP client to used by the exporter.
158+
//
159+
// This option will take precedence over [WithProxy], [WithTimeout],
160+
// [WithTLSClientConfig] options as well as OTEL_EXPORTER_OTLP_CERTIFICATE,
161+
// OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE, OTEL_EXPORTER_OTLP_TIMEOUT,
162+
// OTEL_EXPORTER_OTLP_TRACES_TIMEOUT environment variables.
163+
//
164+
// Timeout and all other fields of the passed [http.Client] are left intact.
165+
//
166+
// Be aware that passing an HTTP client with transport like
167+
// [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.NewTransport] can
168+
// cause the client to be instrumented twice and cause infinite recursion.
169+
func WithHTTPClient(c *http.Client) Option {
170+
return wrappedOption{otlpconfig.WithHTTPClient(c)}
171+
}

internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ type (
5353
// gRPC configurations
5454
GRPCCredentials credentials.TransportCredentials
5555

56-
Proxy HTTPTransportProxyFunc
56+
// HTTP configurations
57+
Proxy HTTPTransportProxyFunc
58+
HTTPClient *http.Client
5759
}
5860

5961
Config struct {
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
350352
return cfg
351353
})
352354
}
355+
356+
func WithHTTPClient(c *http.Client) GenericOption {
357+
return newGenericOption(func(cfg Config) Config {
358+
cfg.Traces.HTTPClient = c
359+
return cfg
360+
})
361+
}

internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
483483
assert.Nil(t, c.Traces.Proxy)
484484
},
485485
},
486+
487+
// HTTP Client Tests
488+
{
489+
name: "Test With HTTP Client",
490+
opts: []GenericOption{
491+
WithHTTPClient(http.DefaultClient),
492+
},
493+
asserts: func(t *testing.T, c *Config, grpcOption bool) {
494+
assert.Equal(t, http.DefaultClient, c.Traces.HTTPClient)
495+
},
496+
},
497+
{
498+
name: "Test Without HTTP Client",
499+
opts: []GenericOption{},
500+
asserts: func(t *testing.T, c *Config, grpcOption bool) {
501+
assert.Nil(t, c.Traces.HTTPClient)
502+
},
503+
},
486504
}
487505

488506
for _, tt := range tests {

0 commit comments

Comments
 (0)