elastictransport

package
v8.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 27 Imported by: 1,976

Documentation

Overview

Package elastictransport provides the transport layer for the Elastic clients.

The default HTTP transport of the client is http.Transport; use the Transport option to customize it.

The package will automatically retry requests on network-related errors, and on specific response status codes (by default 502, 503, 504). Use the RetryOnStatus option to customize the list. The transport will not retry a timeout network error, unless enabled by setting EnableRetryOnTimeout to true.

Use the MaxRetries option to configure the number of retries, and set DisableRetry to true to disable the retry behaviour altogether.

By default, the retry will be performed without any delay; to configure a backoff interval, implement the RetryBackoff option function; see an example in the package unit tests for information.

When multiple addresses are passed in configuration, the package will use them in a round-robin fashion, and will keep track of live and dead nodes. The status of dead nodes is checked periodically.

To customize the node selection behaviour, provide a Selector implementation in the configuration. To replace the connection pool entirely, provide a custom ConnectionPool implementation via the ConnectionPoolFunc option.

The package defines the Logger interface for logging information about request and response. It comes with several bundled loggers for logging in text and JSON.

Use the EnableDebugLogger option to enable the debugging logger for connection management.

Use the EnableMetrics option to enable metric collection and export.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Client represents the HTTP client.

func New

func New(cfg Config) (*Client, error)

New creates new transport client.

http.DefaultTransport will be used if no transport is passed in the configuration.

func (*Client) DiscoverNodes

func (c *Client) DiscoverNodes() error

DiscoverNodes reloads the client connections by fetching information from the cluster.

func (*Client) InstrumentationEnabled added in v8.4.0

func (c *Client) InstrumentationEnabled() Instrumentation

func (*Client) Metrics

func (c *Client) Metrics() (Metrics, error)

Metrics returns the transport metrics.

func (*Client) Perform

func (c *Client) Perform(req *http.Request) (*http.Response, error)

Perform executes the request and returns a response or error.

func (*Client) URLs

func (c *Client) URLs() []*url.URL

URLs returns a list of transport URLs.

type ColorLogger

type ColorLogger struct {
	Output             io.Writer
	EnableRequestBody  bool
	EnableResponseBody bool
}

ColorLogger prints the log message in a terminal-optimized plain text.

func (*ColorLogger) LogRoundTrip

func (l *ColorLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error

LogRoundTrip prints the information about request and response.

func (*ColorLogger) RequestBodyEnabled

func (l *ColorLogger) RequestBodyEnabled() bool

RequestBodyEnabled returns true when the request body should be logged.

func (*ColorLogger) ResponseBodyEnabled

func (l *ColorLogger) ResponseBodyEnabled() bool

ResponseBodyEnabled returns true when the response body should be logged.

type Config

type Config struct {
	UserAgent string

	URLs         []*url.URL
	Username     string
	Password     string
	APIKey       string
	ServiceToken string

	Header http.Header
	CACert []byte

	// DisableRetry disables retrying requests.
	//
	// If DisableRetry is true, then RetryOnStatus, RetryOnError, MaxRetries, and RetryBackoff will be ignored.
	DisableRetry bool

	// RetryOnStatus holds an optional list of HTTP response status codes that should trigger a retry.
	//
	// If RetryOnStatus is nil, then the defaults will be used:
	// 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout).
	RetryOnStatus []int

	// RetryOnError holds an optional function that will be called when a request fails due to an
	// HTTP transport error, to indicate whether the request should be retried, e.g. timeouts.
	RetryOnError func(*http.Request, error) bool
	MaxRetries   int
	RetryBackoff func(attempt int) time.Duration

	CompressRequestBody      bool
	CompressRequestBodyLevel int
	// If PoolCompressor is true, a sync.Pool based gzip writer is used. Should be enabled with CompressRequestBody.
	PoolCompressor bool

	EnableMetrics     bool
	EnableDebugLogger bool

	Instrumentation Instrumentation

	DiscoverNodesInterval time.Duration

	Transport http.RoundTripper
	Logger    Logger
	Selector  Selector

	ConnectionPoolFunc func([]*Connection, Selector) ConnectionPool

	CertificateFingerprint string
}

Config represents the configuration of HTTP client.

type Connection

type Connection struct {
	sync.Mutex

	URL       *url.URL
	IsDead    bool
	DeadSince time.Time
	Failures  int

	ID         string
	Name       string
	Roles      []string
	Attributes map[string]interface{}
}

Connection represents a connection to a node.

func (*Connection) String

func (c *Connection) String() string

String returns a readable connection representation.

type ConnectionMetric

type ConnectionMetric struct {
	URL       string     `json:"url"`
	Failures  int        `json:"failures,omitempty"`
	IsDead    bool       `json:"dead,omitempty"`
	DeadSince *time.Time `json:"dead_since,omitempty"`

	Meta struct {
		ID    string   `json:"id"`
		Name  string   `json:"name"`
		Roles []string `json:"roles"`
	} `json:"meta"`
}

ConnectionMetric represents metric information for a connection.

func (ConnectionMetric) String

func (cm ConnectionMetric) String() string

String returns the connection information as a string.

type ConnectionPool

type ConnectionPool interface {
	Next() (*Connection, error)  // Next returns the next available connection.
	OnSuccess(*Connection) error // OnSuccess reports that the connection was successful.
	OnFailure(*Connection) error // OnFailure reports that the connection failed.
	URLs() []*url.URL            // URLs returns the list of URLs of available connections.
}

ConnectionPool defines the interface for the connection pool.

func NewConnectionPool

func NewConnectionPool(conns []*Connection, selector Selector) (ConnectionPool, error)

NewConnectionPool creates and returns a default connection pool.

type CurlLogger

type CurlLogger struct {
	Output             io.Writer
	EnableRequestBody  bool
	EnableResponseBody bool
}

CurlLogger prints the log message as a runnable curl command.

func (*CurlLogger) LogRoundTrip

func (l *CurlLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error

LogRoundTrip prints the information about request and response.

func (*CurlLogger) RequestBodyEnabled

func (l *CurlLogger) RequestBodyEnabled() bool

RequestBodyEnabled returns true when the request body should be logged.

func (*CurlLogger) ResponseBodyEnabled

func (l *CurlLogger) ResponseBodyEnabled() bool

ResponseBodyEnabled returns true when the response body should be logged.

type DebuggingLogger

type DebuggingLogger interface {
	Log(a ...interface{}) error
	Logf(format string, a ...interface{}) error
}

DebuggingLogger defines the interface for a debugging logger.

type Discoverable

type Discoverable interface {
	DiscoverNodes() error
}

Discoverable defines the interface for transports supporting node discovery.

type ElasticsearchOpenTelemetry added in v8.4.0

type ElasticsearchOpenTelemetry struct {
	// contains filtered or unexported fields
}

func NewOtelInstrumentation added in v8.4.0

func NewOtelInstrumentation(provider trace.TracerProvider, captureSearchBody bool, version string) *ElasticsearchOpenTelemetry

NewOtelInstrumentation returns a new instrument for Open Telemetry traces If no provider is passed, the instrumentation will fall back to the global otel provider. captureSearchBody sets the query capture behavior for search endpoints. version should be set to the version provided by the caller.

func (ElasticsearchOpenTelemetry) AfterRequest added in v8.4.0

func (i ElasticsearchOpenTelemetry) AfterRequest(req *http.Request, system, endpoint string)

AfterRequest enrich the span with the available data from the request.

func (ElasticsearchOpenTelemetry) AfterResponse added in v8.4.0

func (i ElasticsearchOpenTelemetry) AfterResponse(ctx context.Context, res *http.Response)

AfterResponse enric the span with the cluster id and node name if the query was executed on Elastic Cloud.

func (ElasticsearchOpenTelemetry) BeforeRequest added in v8.4.0

func (i ElasticsearchOpenTelemetry) BeforeRequest(req *http.Request, endpoint string)

BeforeRequest noop for interface.

func (ElasticsearchOpenTelemetry) Close added in v8.4.0

Close call for the end of the span, preferably defered by the client once started.

func (ElasticsearchOpenTelemetry) RecordError added in v8.4.0

func (i ElasticsearchOpenTelemetry) RecordError(ctx context.Context, err error)

RecordError sets any provided error as an OTel error in the active span.

func (ElasticsearchOpenTelemetry) RecordPathPart added in v8.4.0

func (i ElasticsearchOpenTelemetry) RecordPathPart(ctx context.Context, pathPart, value string)

RecordPathPart sets the couple for a specific path part. An index placed in the path would translate to `db.elasticsearch.path_parts.index`.

func (ElasticsearchOpenTelemetry) RecordRequestBody added in v8.4.0

func (i ElasticsearchOpenTelemetry) RecordRequestBody(ctx context.Context, endpoint string, query io.Reader) io.ReadCloser

RecordRequestBody add the db.statement attributes only for search endpoints. Returns a new reader if the query has been recorded, nil otherwise.

func (ElasticsearchOpenTelemetry) Start added in v8.4.0

Start begins a new span in the given context with the provided name. Span will always have a kind set to trace.SpanKindClient. The context span aware is returned for use within the client.

type Instrumentation added in v8.4.0

type Instrumentation interface {
	// Start creates the span before building the request, returned context will be propagated to the request by the client.
	Start(ctx context.Context, name string) context.Context

	// Close will be called once the client has returned.
	Close(ctx context.Context)

	// RecordError propagates an error.
	RecordError(ctx context.Context, err error)

	// RecordPathPart provides the path variables, called once per variable in the url.
	RecordPathPart(ctx context.Context, pathPart, value string)

	// RecordRequestBody provides the endpoint name as well as the current request payload.
	RecordRequestBody(ctx context.Context, endpoint string, query io.Reader) io.ReadCloser

	// BeforeRequest provides the request and endpoint name, called before sending to the server.
	BeforeRequest(req *http.Request, endpoint string)

	// AfterRequest provides the request, system used (e.g. elasticsearch) and endpoint name.
	// Called after the request has been enhanced with the information from the transport and sent to the server.
	AfterRequest(req *http.Request, system, endpoint string)

	// AfterResponse provides the response.
	AfterResponse(ctx context.Context, res *http.Response)
}

Instrumentation defines the interface the client uses to propagate information about the requests. Each method is called with the current context or request for propagation.

type Instrumented added in v8.4.0

type Instrumented interface {
	InstrumentationEnabled() Instrumentation
}

Instrumented allows to retrieve the current transport Instrumentation

type Interface

type Interface interface {
	Perform(*http.Request) (*http.Response, error)
}

Interface defines the interface for HTTP client.

type JSONLogger

type JSONLogger struct {
	Output             io.Writer
	EnableRequestBody  bool
	EnableResponseBody bool
}

JSONLogger prints the log message as JSON.

func (*JSONLogger) LogRoundTrip

func (l *JSONLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error

LogRoundTrip prints the information about request and response.

func (*JSONLogger) RequestBodyEnabled

func (l *JSONLogger) RequestBodyEnabled() bool

RequestBodyEnabled returns true when the request body should be logged.

func (*JSONLogger) ResponseBodyEnabled

func (l *JSONLogger) ResponseBodyEnabled() bool

ResponseBodyEnabled returns true when the response body should be logged.

type Logger

type Logger interface {
	// LogRoundTrip should not modify the request or response, except for consuming and closing the body.
	// Implementations have to check for nil values in request and response.
	LogRoundTrip(*http.Request, *http.Response, error, time.Time, time.Duration) error
	// RequestBodyEnabled makes the client pass a copy of request body to the logger.
	RequestBodyEnabled() bool
	// ResponseBodyEnabled makes the client pass a copy of response body to the logger.
	ResponseBodyEnabled() bool
}

Logger defines an interface for logging request and response.

type Measurable

type Measurable interface {
	Metrics() (Metrics, error)
}

Measurable defines the interface for transports supporting metrics.

type Metrics

type Metrics struct {
	Requests  int         `json:"requests"`
	Failures  int         `json:"failures"`
	Responses map[int]int `json:"responses"`

	Connections []fmt.Stringer `json:"connections"`
}

Metrics represents the transport metrics.

func (Metrics) String

func (m Metrics) String() string

String returns the metrics as a string.

type Selector

type Selector interface {
	Select([]*Connection) (*Connection, error)
}

Selector defines the interface for selecting connections from the pool.

type TextLogger

type TextLogger struct {
	Output             io.Writer
	EnableRequestBody  bool
	EnableResponseBody bool
}

TextLogger prints the log message in plain text.

func (*TextLogger) LogRoundTrip

func (l *TextLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error

LogRoundTrip prints the information about request and response.

func (*TextLogger) RequestBodyEnabled

func (l *TextLogger) RequestBodyEnabled() bool

RequestBodyEnabled returns true when the request body should be logged.

func (*TextLogger) ResponseBodyEnabled

func (l *TextLogger) ResponseBodyEnabled() bool

ResponseBodyEnabled returns true when the response body should be logged.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL