metrics

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2017 License: MIT Imports: 5 Imported by: 3

README

Metrics Build Status

Go Report Card Coverage Status license

Package to register metrics into Prometheus from Go

Package usage

Include this package into your project with:

go get github.com/Travix-International/go-metrics

Usage

Note: please refer to the Prometheus documentation for the exact details on each counter and on how to choose the counters that best fit your use cases.

// Initialization
metrics := metrics.NewMetrics("namespace", logger.New())

// Example: simple count
metrics.Count("searchcache", "hit", "SearchCache cache hits")

// Example: count with labels
metrics.CountLabels("service", "get_liveness_request_duration_milliseconds", 
	"Response times for GET requests to liveness in milliseconds.",
	[]string{"status", "method"}, []string{strconv.Itoa(ww.status), r.Method})

// Example: increase counter by 5
metrics.IncreaseCounter("kpimetrics", "fullflowpublishing_errors_count", 
	"total number of errors while publishing fares to Datastore.", 5)

// Example: histogram of processing times
func UseHistogram(req *someRequest) someResponse {
		histogram := metrics.AddHistogram("datastore", "getroutefare_request_duration_milliseconds", 
			"Response times for getRouteFare requests to Datastore in milliseconds.")
		
		start := time.Now()
		defer func() {
			histogram.RecordTimeElapsed(start)
		}()

		// Do work that runs for some time

		return myResponse
	}

Dependencies

Known limitations

Histograms

When adding a histogram, the library will create both a summary and histogram. The summary is currently hard-coded to use the following percentiles: 0.5 / 0.75 / 0.9 / 0.95 / 0.99 / 0.999. This should cover most use cases. As for the histogram, this is currently using the default buckets from the Prometheus library, which will not fit all use cases. It will work correctly for simple network traffic, but not for cases which typically have a higher value (more than one second). The buckets are currently not configurable.

license

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultObjectives added in v1.3.0

func DefaultObjectives() map[float64]float64

DefaultObjectives returns a default map of quantiles to be used in summaries.

Types

type HistogramVec added in v1.2.0

type HistogramVec struct {
	Key         string
	Labels      []string
	LabelValues []string
	// contains filtered or unexported fields
}

HistogramVec wraps prometheus.HistogramVec

func (*HistogramVec) Observe added in v1.2.0

func (vec *HistogramVec) Observe(value float64)

Observe adds the specified value to the histogram.

func (*HistogramVec) RecordDuration added in v1.2.0

func (vec *HistogramVec) RecordDuration(start time.Time, unit time.Duration)

RecordDuration adds the elapsed time since the specified start to the histogram in the specified unit of time.

func (*HistogramVec) RecordTimeElapsed added in v1.2.0

func (vec *HistogramVec) RecordTimeElapsed(start time.Time)

RecordTimeElapsed adds the elapsed time since the specified start to the histogram in seconds.

type Metrics

type Metrics struct {
	Namespace     string
	Counters      map[string]prometheus.Counter
	CounterVecs   map[string]*prometheus.CounterVec
	Summaries     map[string]prometheus.Summary
	SummaryVecs   map[string]*prometheus.SummaryVec
	Histograms    map[string]prometheus.Histogram
	HistogramVecs map[string]*prometheus.HistogramVec
	Gauges        map[string]prometheus.Gauge
	Logger        *logger.Logger
	// contains filtered or unexported fields
}

Metrics provides a set of convenience functions that wrap Prometheus

func NewMetrics

func NewMetrics(namespace string, logger *logger.Logger) *Metrics

NewMetrics will instantiate a new Metrics wrapper object

func (*Metrics) AddHistogram

func (m *Metrics) AddHistogram(subsystem, name, help string) *MetricsHistogram

AddHistogram returns the MetricsHistogram for the specified subsystem and name.

func (*Metrics) AddHistogramVec added in v1.2.0

func (m *Metrics) AddHistogramVec(subsystem, name, help string, labels, labelValues []string) *HistogramVec

AddHistogramVec returns the HistogramVec for the specified subsystem and name.

func (*Metrics) AddHistogramVecWithCustomBuckets added in v1.2.0

func (m *Metrics) AddHistogramVecWithCustomBuckets(subsystem, name, help string, labels, labelValues []string,
	buckets []float64) *HistogramVec

AddHistogramVecWithCustomBuckets returns the HistogramVec for the specified subsystem and name with the specified buckets.

func (*Metrics) AddHistogramWithCustomBuckets

func (m *Metrics) AddHistogramWithCustomBuckets(subsystem, name, help string, buckets []float64) *MetricsHistogram

AddHistogramWithCustomBuckets returns the MetricsHistogram for the specified subsystem and name with the specified buckets.

func (*Metrics) AddSummaryVec added in v1.3.0

func (m *Metrics) AddSummaryVec(subsystem, name, help string, labels, labelValues []string) *SummaryVec

AddSummaryVec returns the SummaryVec for the specified subsystem and name.

func (*Metrics) AddSummaryVecWithCustomObjectives added in v1.3.0

func (m *Metrics) AddSummaryVecWithCustomObjectives(subsystem, name, help string, labels, labelValues []string,
	objectives map[float64]float64) *SummaryVec

AddSummaryVecWithCustomObjectives returns the SummaryVec for the specified subsystem and name with the specified objectives.

func (*Metrics) Count

func (m *Metrics) Count(subsystem, name, help string)

Count increases the counter for the specified subsystem and name.

func (*Metrics) CountLabels

func (m *Metrics) CountLabels(subsystem, name, help string, labels, values []string)

CountLabels increases the counter for the specified subsystem and name and adds the specified labels with values.

func (*Metrics) IncreaseCounter

func (m *Metrics) IncreaseCounter(subsystem, name, help string, increment int)

IncreaseCounter increases the counter for the specified subsystem and name with the specified increment.

func (*Metrics) SetGauge

func (m *Metrics) SetGauge(value float64, subsystem, name, help string)

SetGauge sets the gauge value for the specified subsystem and name.

type MetricsHistogram

type MetricsHistogram struct {
	Key string
	// contains filtered or unexported fields
}

MetricsHistogram combines a histogram and summary

func (*MetricsHistogram) Observe

func (histogram *MetricsHistogram) Observe(value float64)

Observe adds the specified value to the histogram.

func (*MetricsHistogram) RecordDuration

func (histogram *MetricsHistogram) RecordDuration(start time.Time, unit time.Duration)

RecordDuration adds the elapsed time since the specified start to the histogram in the specified unit of time and to the linked summary in milliseconds.

func (*MetricsHistogram) RecordTimeElapsed

func (histogram *MetricsHistogram) RecordTimeElapsed(start time.Time)

RecordTimeElapsed adds the elapsed time since the specified start to the histogram in seconds and to the linked summary in milliseconds.

type SummaryVec added in v1.3.0

type SummaryVec struct {
	Key         string
	Labels      []string
	LabelValues []string
	// contains filtered or unexported fields
}

SummaryVec wraps prometheus.SummaryVec

func (*SummaryVec) RecordDuration added in v1.5.0

func (vec *SummaryVec) RecordDuration(start time.Time, unit time.Duration)

RecordDuration adds the elapsed time since the specified start to the histogram in the specified unit of time.

func (*SummaryVec) RecordTimeElapsed added in v1.3.0

func (vec *SummaryVec) RecordTimeElapsed(start time.Time)

RecordTimeElapsed adds the elapsed time since the specified start to the summary in milliseconds.

Jump to

Keyboard shortcuts

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