Documentation ¶
Overview ¶
Package metrics hold constants and utilities for instrumenting elasticsearch-asg with Prometheus metrics.
Index ¶
- Constants
- Variables
- func AWSStatusCode(err interface{}) string
- func ElasticsearchStatusCode(err interface{}) string
- func InstrumentAWS(h *aws.Handlers, reg prometheus.Registerer, namespace string, ...) error
- func InstrumentHTTP(base *http.Client, reg prometheus.Registerer, namespace string, ...) (*http.Client, error)
- func MustRegisterOnce(cs ...prometheus.Collector)
- type VecTimer
Constants ¶
const ( // LabelMethod is the Prometheus label name for HTTP method. LabelMethod = "method" // LabelStatusCode is the Prometheus label name for HTTP status codes. LabelStatusCode = "code" // LabelStatus is the Prometheus label name for the status of a process // such as "success" or "error". LabelStatus = "status" // LabelService is the Prometheus label name for AWS API names. LabelService = "service" // LabelOperation is the Prometheus label name for operations within // an AWS API. LabelOperation = "operation" // LabelOperation is the Prometheus label name for the AWS region label. LabelRegion = "region" // LabelEvent is used by InstrumentHTTP() to describe the different stages of // an HTTP connection (DNS resolution, TLS handshake, etc). LabelEvent = "event" )
Variables ¶
var DefaultObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
DefaultObjectives are default objectives for Prometheus Summary metrics.
Functions ¶
func AWSStatusCode ¶
func AWSStatusCode(err interface{}) string
AWSStatusCode returns the HTTP status code from an AWS client error as a string. It's used for labels in Prometheus metrics. The err parameter can be of type *http.Response, awserr.RequestFailure, awserr.BatchedErrors (containing at least one awserr.RequestFailure), or int (indicating the HTTP status code). Returns 0 if err is not one of those types.
func ElasticsearchStatusCode ¶
func ElasticsearchStatusCode(err interface{}) string
ElasticsearchStatusCode returns the HTTP status code from an Elasticsearch client error as a string. It's used for labels in Prometheus metrics. The err parameter can be of type *http.Response, *elastic.Error, elastic.Error, or int (indicating the HTTP status code). Returns 0 if err is not one of those types. Based on elastic.IsStatusCode (https://github.com/olivere/elastic/blob/release-branch.v7/errors.go#L151)
func InstrumentAWS ¶
func InstrumentAWS(h *aws.Handlers, reg prometheus.Registerer, namespace string, constLabels map[string]string) error
InstrumentAWS adds Prometheus metrics to an AWS client or session.
A Gauge is observed for in-flight requests with labels for AWS service, operation name, and HTTP method labels.
A duration histogram is observed for each AWS API request with labels for AWS service, operation name, HTTP method, and returned HTTP status code.
IF the AWS clients retry on error, each attempt counts as a separate sample.
Example:
sess := session.Must(session.NewSession()) InstrumentAWS(&sess.Handlers, prometheus.DefaultRegisterer, "", nil)
func InstrumentHTTP ¶
func InstrumentHTTP(base *http.Client, reg prometheus.Registerer, namespace string, constLabels map[string]string) (*http.Client, error)
InstrumentHTTP returns a new HTTP client instrumented with Prometheus metrics.
If the base Client is nil, http.DefaultClient is used.
A Gauge is observed for in-flight requests with a label for HTTP method.
Histograms of duration are observed for DNS lookup, TLS negotiation, and overall request duration, with labels for HTTP method and returned HTTP status code.
Example:
client := InstrumentHTTP(nil, prometheus.DefaultRegisterer, "", nil)
func MustRegisterOnce ¶
func MustRegisterOnce(cs ...prometheus.Collector)
MustRegisterOnce registers a set of Prometheus Collectors with the default Registerer, ignoring AlreadyRegisteredErrors. Other errors cause a panic.
Types ¶
type VecTimer ¶
type VecTimer struct {
// contains filtered or unexported fields
}
VecTimer is a helper type to time functions. It is similar to prometheus.Timer, but takes a prometheus.ObserverVec, and can add labels to it when the VecTimer is observed. Use NewVecTimer to create new instances.
func NewVecTimer ¶
func NewVecTimer(v prometheus.ObserverVec) *VecTimer
NewVecTimer creates a new VecTimer. The provided ObserverVec is used to observe a duration in seconds. Timer is usually used to time a function call in the following way:
func TimeMe() { timer := NewVecTimer(myHistogramVec) // Do actual work. timer.ObserveDurationWithLabelValues("label1", "label2") }
func (*VecTimer) ObserveErr ¶
ObserveErr sets a label equal to LabelStatus based on the err value and records the duration passed since the VecTimer was created. The observed duration is also returned. Never do `defer time.ObserveErr(err)`, instead do `defer func() { timer.ObserveErr(err) }()`.
Note that this method is only guaranteed to never observe negative durations if used with Go1.9+.
func (*VecTimer) ObserveWith ¶
func (t *VecTimer) ObserveWith(labels prometheus.Labels) time.Duration
ObserveWith records the duration passed since the VecTimer was created. It derives an Observer from the ObserverVec passed during construction from the provided labels. It calls the Observe method of the Observer with the duration in seconds as an argument. The observed duration is also returned.
Note that this method is only guaranteed to never observe negative durations if used with Go1.9+.
func (*VecTimer) ObserveWithLabelValues ¶
ObserveWithLabelValues records the duration passed since the VecTimer was created. It derives an Observer from the ObserverVec passed during construction from the provided labels. It calls the Observe method of the Observer with the duration in seconds as an argument. The observed duration is also returned.
Note that this method is only guaranteed to never observe negative durations if used with Go1.9+.