Documentation
¶
Overview ¶
Package httpclient constructs HTTP clients with optional OpenTelemetry tracing instrumentation.
Clients are built with functional options:
client := httpclient.NewHTTPClient( httpclient.WithTimeout(5*time.Second), httpclient.WithTracing(true), )
Options are applied in order, so a later one overrides an earlier one. An environment-loaded Config expresses itself as Options via Config.Options, so a config-driven client is built the same way, and individual settings can still be overridden after it:
client := httpclient.NewHTTPClient(append(cfg.Options(), httpclient.WithTracing(true))...)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHTTPClient ¶
NewHTTPClient provides an HTTP client. With no options it returns a client with the package defaults; pass Config.Options to drive it from an environment-loaded Config.
func RegisterHTTPClient ¶
RegisterHTTPClient registers an *http.Client with the injector, built from the injector's *Config. Any opts are applied after the Config and so override it.
Types ¶
type Config ¶
type Config struct {
Timeout time.Duration `env:"TIMEOUT" json:"timeout" yaml:"timeout"`
MaxIdleConns int `env:"MAX_IDLE_CONNS" json:"maxIdleConns" yaml:"maxIdleConns"`
MaxIdleConnsPerHost int `env:"MAX_IDLE_CONNS_PER_HOST" json:"maxIdleConnsPerHost" yaml:"maxIdleConnsPerHost"`
EnableTracing bool `env:"ENABLE_TRACING" json:"enableTracing" yaml:"enableTracing"`
}
Config configures an HTTP client.
func (*Config) EnsureDefaults ¶
func (cfg *Config) EnsureDefaults()
EnsureDefaults sets default values for zero fields.
func (*Config) Options ¶
Options expresses the config as the equivalent list of Options, which is how a Config reaches NewHTTPClient:
client := httpclient.NewHTTPClient(cfg.Options()...)
Callers can append further Options to override individual settings. Zero-valued numeric fields yield Options that leave the package defaults in place, matching EnsureDefaults; EnableTracing is applied as given. A nil Config yields no Options.
type Option ¶
type Option func(*clientConfig)
Option customizes the HTTP client returned by NewHTTPClient. Options are applied in order, so a later Option overrides an earlier one.
func WithMaxIdleConns ¶
WithMaxIdleConns sets the transport's maximum number of idle connections across all hosts. A non-positive value leaves the default in place. It has no effect alongside WithTransport.
func WithMaxIdleConnsPerHost ¶
WithMaxIdleConnsPerHost sets the transport's maximum number of idle connections per host. A non-positive value leaves the default in place. It has no effect alongside WithTransport.
func WithTimeout ¶
WithTimeout sets the client's overall request timeout, which also bounds the dial. A non-positive duration leaves the default (defaultTimeout) in place.
func WithTracing ¶
WithTracing toggles wrapping the transport in OpenTelemetry instrumentation. Tracing is off by default.
func WithTransport ¶
func WithTransport(transport http.RoundTripper) Option
WithTransport uses the given RoundTripper as the client's base transport rather than building one, which is the seam for stubbing responses in tests or layering custom middleware. The connection-pool options are ignored when it is set; tracing, if enabled, still wraps it. A nil RoundTripper is ignored.