1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00
Robert Pająk
2025-05-13 21:06:01 +02:00
committed by GitHub
parent b66542529a
commit 5cd1611cda
9 changed files with 118 additions and 16 deletions

View File

@@ -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. 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) 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 `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/otlpmetric/otlpmetrichttp`. (#6752)
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6688)
### Removed ### Removed

View File

@@ -53,7 +53,9 @@ type (
// gRPC configurations // gRPC configurations
GRPCCredentials credentials.TransportCredentials GRPCCredentials credentials.TransportCredentials
Proxy HTTPTransportProxyFunc // HTTP configurations
Proxy HTTPTransportProxyFunc
HTTPClient *http.Client
} }
Config struct { Config struct {
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
return cfg return cfg
}) })
} }
func WithHTTPClient(c *http.Client) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.HTTPClient = c
return cfg
})
}

View File

@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
assert.Nil(t, c.Traces.Proxy) 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 { for _, tt := range tests {

View File

@@ -71,20 +71,24 @@ var _ otlptrace.Client = (*client)(nil)
func NewClient(opts ...Option) otlptrace.Client { func NewClient(opts ...Option) otlptrace.Client {
cfg := otlpconfig.NewHTTPConfig(asHTTPOptions(opts)...) cfg := otlpconfig.NewHTTPConfig(asHTTPOptions(opts)...)
httpClient := &http.Client{ httpClient := cfg.Traces.HTTPClient
Transport: ourTransport,
Timeout: cfg.Traces.Timeout,
}
if cfg.Traces.TLSCfg != nil || cfg.Traces.Proxy != nil { if httpClient == nil {
clonedTransport := ourTransport.Clone() httpClient = &http.Client{
httpClient.Transport = clonedTransport Transport: ourTransport,
Timeout: cfg.Traces.Timeout,
if cfg.Traces.TLSCfg != nil {
clonedTransport.TLSClientConfig = cfg.Traces.TLSCfg
} }
if cfg.Traces.Proxy != nil {
clonedTransport.Proxy = cfg.Traces.Proxy 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
}
} }
} }

View File

@@ -53,7 +53,9 @@ type (
// gRPC configurations // gRPC configurations
GRPCCredentials credentials.TransportCredentials GRPCCredentials credentials.TransportCredentials
Proxy HTTPTransportProxyFunc // HTTP configurations
Proxy HTTPTransportProxyFunc
HTTPClient *http.Client
} }
Config struct { Config struct {
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
return cfg return cfg
}) })
} }
func WithHTTPClient(c *http.Client) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.HTTPClient = c
return cfg
})
}

View File

@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
assert.Nil(t, c.Traces.Proxy) 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 { for _, tt := range tests {

View File

@@ -153,3 +153,19 @@ func WithRetry(rc RetryConfig) Option {
func WithProxy(pf HTTPTransportProxyFunc) Option { func WithProxy(pf HTTPTransportProxyFunc) Option {
return wrappedOption{otlpconfig.WithProxy(otlpconfig.HTTPTransportProxyFunc(pf))} 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)}
}

View File

@@ -53,7 +53,9 @@ type (
// gRPC configurations // gRPC configurations
GRPCCredentials credentials.TransportCredentials GRPCCredentials credentials.TransportCredentials
Proxy HTTPTransportProxyFunc // HTTP configurations
Proxy HTTPTransportProxyFunc
HTTPClient *http.Client
} }
Config struct { Config struct {
@@ -350,3 +352,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
return cfg return cfg
}) })
} }
func WithHTTPClient(c *http.Client) GenericOption {
return newGenericOption(func(cfg Config) Config {
cfg.Traces.HTTPClient = c
return cfg
})
}

View File

@@ -483,6 +483,24 @@ func TestConfigs(t *testing.T) {
assert.Nil(t, c.Traces.Proxy) 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 { for _, tt := range tests {