Documentation
¶
Overview ¶
Package httpclix provides extended HTTP client functionality including load balancing, logging, and observability middleware.
This package builds on httpcli to provide production-ready HTTP clients with built-in support for request tracing, metrics, and logging.
Index ¶
- func Default(opts ...httpcli.Option) *httpcli.Client
- func DefaultMiddlewares() []httpcli.Middleware
- func Log(logger log.Logger) httpcli.Middleware
- func LogConfigToContext(ctx context.Context, logRequestBody bool, logResponseBody bool, ...) context.Context
- func LogWithOptions(logger log.Logger, opts ...LogOption) httpcli.Middleware
- func Metrics(storage *http_metrics.ClientStorage) httpcli.Middleware
- func RequestId() httpcli.Middleware
- type ClientBalancer
- func (c *ClientBalancer) Delete(method string) *httpcli.RequestBuilder
- func (c *ClientBalancer) Execute(ctx context.Context, builder *httpcli.RequestBuilder) (*httpcli.Response, error)
- func (c *ClientBalancer) Get(method string) *httpcli.RequestBuilder
- func (c *ClientBalancer) Patch(method string) *httpcli.RequestBuilder
- func (c *ClientBalancer) Post(method string) *httpcli.RequestBuilder
- func (c *ClientBalancer) Put(method string) *httpcli.RequestBuilder
- func (c *ClientBalancer) Upgrade(hosts []string)
- type ClientTracer
- type LogOption
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Default ¶
Default creates a new Client with default middlewares for observability, metrics, and request tracing.
Additional options are applied after the default middlewares.
func DefaultMiddlewares ¶ added in v1.45.0
func DefaultMiddlewares() []httpcli.Middleware
DefaultMiddlewares returns a slice of middlewares for production use, including request ID propagation, metrics collection, and distributed tracing.
func Log ¶
func Log(logger log.Logger) httpcli.Middleware
Log creates a middleware that logs HTTP requests and responses with sensible defaults (request body and response body enabled).
All logs are at Debug level unless the response has an error status code.
func LogConfigToContext ¶
func LogConfigToContext( ctx context.Context, logRequestBody bool, logResponseBody bool, opts ...LogOption, ) context.Context
LogConfigToContext creates a new context with per-request logging configuration.
This allows overriding the default log behavior for specific requests.
func LogWithOptions ¶ added in v1.66.3
func LogWithOptions(logger log.Logger, opts ...LogOption) httpcli.Middleware
LogWithOptions creates a middleware that logs HTTP requests and responses with custom configuration.
By default, no body content is logged. Use LogDump, LogHeaders, or other options to enable specific logging features.
func Metrics ¶
func Metrics(storage *http_metrics.ClientStorage) httpcli.Middleware
Metrics is a middleware that collects HTTP client metrics including request duration, status codes, and errors.
The endpoint must be set in the context using http_metrics.ClientEndpoint.
DNS lookup, connection establishment, request writing, and response reading times are also observed when available.
func RequestId ¶
func RequestId() httpcli.Middleware
RequestId is a middleware that ensures every request has a unique ID.
If a request ID already exists in the context, it is used. Otherwise, a new ID is generated and added to the request headers.
Types ¶
type ClientBalancer ¶ added in v1.45.0
ClientBalancer is an HTTP client that distributes requests across multiple hosts using round-robin load balancing.
It embeds httpcli.Client and extends it with host management functionality.
func DefaultWithBalancer ¶ added in v1.45.0
func DefaultWithBalancer(initialHosts []string, opts ...Option) *ClientBalancer
DefaultWithBalancer creates a new ClientBalancer with default middlewares and load balancing across the provided hosts.
Additional options are applied after the default middlewares.
func NewClientBalancer ¶ added in v1.45.0
func NewClientBalancer(initialHosts []string, opts ...Option) *ClientBalancer
NewClientBalancer creates a new ClientBalancer with the given initial hosts and options.
Hosts without a schema prefix automatically get http:// prepended.
func (*ClientBalancer) Delete ¶ added in v1.45.0
func (c *ClientBalancer) Delete(method string) *httpcli.RequestBuilder
Delete creates a new DELETE request builder for the given path. The path is appended to the currently selected host.
func (*ClientBalancer) Execute ¶ added in v1.45.0
func (c *ClientBalancer) Execute(ctx context.Context, builder *httpcli.RequestBuilder) (*httpcli.Response, error)
Execute sends an HTTP request using the provided builder and the load balancer.
If GlobalRequestConfig.BaseUrl is set, the request is sent to that URL. Otherwise, the next host from the round-robin balancer is selected and the request path is appended to it.
func (*ClientBalancer) Get ¶ added in v1.45.0
func (c *ClientBalancer) Get(method string) *httpcli.RequestBuilder
Get creates a new GET request builder for the given path. The path is appended to the currently selected host.
func (*ClientBalancer) Patch ¶ added in v1.45.0
func (c *ClientBalancer) Patch(method string) *httpcli.RequestBuilder
Patch creates a new PATCH request builder for the given path. The path is appended to the currently selected host.
func (*ClientBalancer) Post ¶ added in v1.45.0
func (c *ClientBalancer) Post(method string) *httpcli.RequestBuilder
Post creates a new POST request builder for the given path. The path is appended to the currently selected host.
func (*ClientBalancer) Put ¶ added in v1.45.0
func (c *ClientBalancer) Put(method string) *httpcli.RequestBuilder
Put creates a new PUT request builder for the given path. The path is appended to the currently selected host.
func (*ClientBalancer) Upgrade ¶ added in v1.45.0
func (c *ClientBalancer) Upgrade(hosts []string)
Upgrade replaces the current set of hosts with a new set.
Hosts without a schema prefix automatically get the configured schema prepended.
type ClientTracer ¶ added in v1.38.0
type ClientTracer struct {
// contains filtered or unexported fields
}
ClientTracer collects detailed timing information for HTTP client operations.
It implements the httptrace.ClientTrace interface to measure DNS lookup, connection establishment, request writing, and response reading durations.
func NewClientTracer ¶ added in v1.38.0
func NewClientTracer(clientStorage *metrics.ClientStorage, endpoint string) *ClientTracer
NewClientTracer creates a new ClientTracer for the given endpoint.
func (*ClientTracer) ClientTrace ¶ added in v1.38.0
func (cli *ClientTracer) ClientTrace() *httptrace.ClientTrace
ClientTrace returns an httptrace.ClientTrace that records timing metrics for the various phases of an HTTP request.
func (*ClientTracer) ResponseReceived ¶ added in v1.38.0
func (cli *ClientTracer) ResponseReceived()
ResponseReceived is called when the response body has been fully read.
Records the duration of response reading.
type LogOption ¶ added in v1.36.0
type LogOption func(*logConfig)
LogOption is a function that configures log behavior.
func LogCombined ¶ added in v1.66.3
LogCombined enables combined logging mode where all fields are logged in a single log entry instead of separate request and response entries.
func LogDump ¶ added in v1.36.0
LogDump enables raw HTTP request/response dumping. When enabled, the full HTTP request and/or response are logged including headers and body.
func LogHeaders ¶ added in v1.36.0
LogHeaders enables logging of HTTP request and response headers.
type Option ¶ added in v1.45.0
type Option func(c *clientBalancerOptions)
Option is a function that configures a ClientBalancer.
func WithClient ¶ added in v1.45.0
WithClient sets a custom http.Client for the ClientBalancer.
func WithClientOptions ¶ added in v1.45.0
WithClientOptions appends the given client options to the ClientBalancer.
func WithHttpsSchema ¶ added in v1.45.0
func WithHttpsSchema() Option
WithHttpsSchema configures the ClientBalancer to use https:// as the URL schema.