metrics

package
v0.9.28 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: Apache-2.0 Imports: 7 Imported by: 40

README

Metrics

In metrics package you can find simple implementation of metrics engine, that is metric system agnostic. Currently available engines are:

Nice to have in future:

The example of how to write an engine lives in /metrics/metrics_test.go

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultMetrics is the generic metrics aggregator
	// needed for global variables so we can attach new metrics
	// before the engine (like prometheus) is linked
	DefaultMetrics *Metrics
	// Debug flag
	Debug bool
)

Functions

func AddEngine

func AddEngine(eng MetricsEngine) error

AddEngine adds new engine to DefaultMetrics

func Handler

func Handler() http.Handler

Handler returns an http handler from default

func Hotload

func Hotload(name string) error

Hotload links given engine to previously added metrics

Types

type Counter

type Counter interface {
	Inc()
	Add(float64)
}

Counter is an interface that generalizes a group of metrics that are calculate the number over time

type Gauge

type Gauge interface {
	Counter

	Set(float64)

	Dec()
	Sub(float64)
}

Gauge is an interface that generalizes a group of metrics that are used to report a curent level/number

type GroupCounter

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

GroupCounter is a set of Counters that within the same tags

func (*GroupCounter) Add

func (gc *GroupCounter) Add(a float64)

Add adds to counter

func (*GroupCounter) AddCounter

func (gc *GroupCounter) AddCounter(c Counter)

AddCounter adds counter

func (*GroupCounter) Inc

func (gc *GroupCounter) Inc()

Inc increases the counter

type GroupGauge

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

GroupGauge is a set of Gauges that within the same tags

func (*GroupGauge) Add

func (gg *GroupGauge) Add(a float64)

Add adds to gauge

func (*GroupGauge) AddGauge

func (gg *GroupGauge) AddGauge(g Gauge)

AddGauge adds gauge

func (*GroupGauge) Dec

func (gg *GroupGauge) Dec()

Dec decreses gauge

func (*GroupGauge) Inc

func (gg *GroupGauge) Inc()

Inc increases gauge

func (*GroupGauge) Set

func (gg *GroupGauge) Set(a float64)

Set sets gauge to given value

func (*GroupGauge) Sub

func (gg *GroupGauge) Sub(a float64)

Sub substract from gauge

type GroupObserver

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

GroupObserver is a set of Observers that within the same tags

func (*GroupObserver) AddObserver

func (g *GroupObserver) AddObserver(o Observer)

AddObserver adds observer

func (*GroupObserver) Observe

func (g *GroupObserver) Observe(f float64)

Observe calls the metrics ultimately

type GroupTagCounter

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

GroupTagCounter a group of different Counters from different metric systems

func MustNewCounterWithTags

func MustNewCounterWithTags(opts Options) *GroupTagCounter

MustNewCounterWithTags constructor with embedded panic

func NewCounterWithTags

func NewCounterWithTags(opts Options) (*GroupTagCounter, error)

NewCounterWithTags is Counter constructor for DefaultMetrics global

func (*GroupTagCounter) AddCounter

func (gtc *GroupTagCounter) AddCounter(c TagCounter)

AddCounter is appending new counter to set

func (*GroupTagCounter) WithLabels

func (gtc *GroupTagCounter) WithLabels(labels ...string) *GroupCounter

WithLabels make a group with given labels

func (*GroupTagCounter) WithTags

func (gtc *GroupTagCounter) WithTags(tags map[string]string) (*GroupCounter, error)

WithTags makes a group with given tags (label-value pairs)

type GroupTagGauge

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

GroupTagGauge a group of different Gauges from different metric systems

func MustNewGaugeWithTags

func MustNewGaugeWithTags(opts Options) *GroupTagGauge

MustNewGaugeWithTags gauge constructor with embedded error

func NewGaugeWithTags

func NewGaugeWithTags(opts Options) (*GroupTagGauge, error)

NewGaugeWithTags gauge type constructor

func (*GroupTagGauge) AddGauge

func (gtg *GroupTagGauge) AddGauge(g TagGauge)

AddGauge is appending new gauge to set

func (*GroupTagGauge) WithLabels

func (gtg *GroupTagGauge) WithLabels(labels ...string) *GroupGauge

WithLabels makes a group with given labels

func (*GroupTagGauge) WithTags

func (gtg *GroupTagGauge) WithTags(tags map[string]string) (*GroupGauge, error)

WithTags makes a group with given tags (label-value pairs)

type GroupTagHistogram

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

GroupTagHistogram a group of different Histograms from different metric systems

func MustNewHistogramWithTags

func MustNewHistogramWithTags(opts HistogramOptions) *GroupTagHistogram

MustNewHistogramWithTags constructor with embedded panic

func NewHistogramWithTags

func NewHistogramWithTags(opts HistogramOptions) (*GroupTagHistogram, error)

NewHistogramWithTags Histogram constructor for DefaultMetrics global

func (*GroupTagHistogram) AddHistogram

func (gth *GroupTagHistogram) AddHistogram(o TagObserver)

AddHistogram is appending new histogram to set

func (*GroupTagHistogram) WithLabels

func (gth *GroupTagHistogram) WithLabels(labels ...string) *GroupObserver

WithLabels make a group with given labels

func (*GroupTagHistogram) WithTags

func (gth *GroupTagHistogram) WithTags(tags map[string]string) (*GroupObserver, error)

WithTags makes a group with given tags (label-value pairs)

type HistogramBucketOptions

type HistogramBucketOptions struct {
	Type    string
	Buckets []string
}

HistogramBucketOptions an options to fine-tune histogram buckets

type HistogramOptions

type HistogramOptions struct {
	Namespace string
	Subsystem string
	Name      string
	Desc      string
	Tags      []string
	Buckets   HistogramBucketOptions
}

HistogramOptions a set of options for histogram metric

type Metrics

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

Metrics a structure that group all the defined metrics

func NewMetrics

func NewMetrics() *Metrics

NewMetrics a metrics constructor

func (*Metrics) AddEngine

func (m *Metrics) AddEngine(eng MetricsEngine) error

AddEngine adds new engine (like prometheus) to run

func (*Metrics) Handler

func (m *Metrics) Handler() http.Handler

Handler Returns metrics custom handler

func (*Metrics) Hotload

func (m *Metrics) Hotload(name string) error

Hotload loads all the previously defined metrics to given engine

func (*Metrics) MustNewCounterWithTags

func (m *Metrics) MustNewCounterWithTags(opts Options) *GroupTagCounter

MustNewCounterWithTags constructor with panic on error embedded

func (*Metrics) MustNewGaugeWithTags

func (m *Metrics) MustNewGaugeWithTags(opts Options) *GroupTagGauge

MustNewGaugeWithTags constructor with panic on error embedded

func (*Metrics) MustNewHistogramWithTags

func (m *Metrics) MustNewHistogramWithTags(opts HistogramOptions) *GroupTagHistogram

MustNewHistogramWithTags constructor with panic on error embedded

func (*Metrics) NewCounterWithTags

func (m *Metrics) NewCounterWithTags(opts Options) (*GroupTagCounter, error)

NewCounterWithTags create a group of counters from defined engines

Example
package main

import (
	"expvar"
	"log"
	"net/http"
	"strings"

	"github.com/figment-networks/indexing-engine/metrics"
)

// Dummy test engine implementation
type MetricsEngineTest struct {
}

func (m *MetricsEngineTest) Name() string {
	return "MetricsEngineTest"
}

func (m *MetricsEngineTest) NewCounterWithTags(opts metrics.Options) (metrics.TagCounter, error) {
	return NewMetricElementTestCouter(opts), nil
}

func (m *MetricsEngineTest) NewGaugeWithTags(opts metrics.Options) (metrics.TagGauge, error) {
	return nil, nil // NOOP
}

func (m *MetricsEngineTest) NewHistogramWithTags(opts metrics.HistogramOptions) (metrics.TagObserver, error) {
	return nil, nil // NOOP
}

func (m *MetricsEngineTest) Handler() http.Handler {
	return nil
}

type MetricElementTestCouter struct {
	metric *MyCounter
}

func NewMetricElementTestCouter(opts metrics.Options) *MetricElementTestCouter {
	return &MetricElementTestCouter{
		metric: &MyCounter{
			expvar.NewInt(strings.Join([]string{opts.Namespace, opts.Subsystem, opts.Name}, "_")),
		},
	}
}

func (me *MetricElementTestCouter) WithTags(map[string]string) (metrics.Counter, error) {
	// (lukanus): discard tags for the sake of simple example
	return me.metric, nil
}

func (me *MetricElementTestCouter) WithLabels(...string) metrics.Counter {
	// (lukanus): discard tags for the sake of simple example
	return me.metric
}

type MyCounter struct {
	metric *expvar.Int
}

func (mc MyCounter) Inc() {
	mc.metric.Add(1)
}
func (mc MyCounter) Add(a float64) {
	mc.metric.Add(int64(a))
}

func main() {
	// Add metrics engine of yout choice metrics global
	me := &MetricsEngineTest{}
	err := metrics.AddEngine(me)
	if err != nil {
		log.Fatal(err)
	}

	// If needed link predeclared values
	err = metrics.Hotload(me.Name())
	if err != nil {
		log.Fatal(err)
	}

	// Create counter in all registred engines
	counter, err := metrics.NewCounterWithTags(metrics.Options{Namespace: "my", Subsystem: "test", Name: "metric"})

	// Use counter
	counter.WithLabels().Add(1)
}
Output:

func (*Metrics) NewGaugeWithTags

func (m *Metrics) NewGaugeWithTags(opts Options) (*GroupTagGauge, error)

NewGaugeWithTags create a group of gauges from defined engines

func (*Metrics) NewHistogramWithTags

func (m *Metrics) NewHistogramWithTags(opts HistogramOptions) (*GroupTagHistogram, error)

NewHistogramWithTags create a group of histograms from defined engines

type MetricsEngine

type MetricsEngine interface {
	Name() string

	NewCounterWithTags(opts Options) (TagCounter, error)
	NewGaugeWithTags(opts Options) (TagGauge, error)
	NewHistogramWithTags(opts HistogramOptions) (TagObserver, error)
	Handler() http.Handler
}

MetricsEngine is an interface for metric engines

type MetricsHandler

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

MetricsHandler a way to merge http handlers

func (MetricsHandler) ServeHTTP

func (mh MetricsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP fulfills http.Handler interface

type Observer

type Observer interface {
	Observe(float64)
}

Observer is an interface that generalizes a group of metrics that are receiving a set of numeric values in time

type Options

type Options struct {
	Namespace string
	Subsystem string
	Name      string
	Desc      string
	Tags      []string
}

Options for metrics

type TagCounter

type TagCounter interface {
	WithTags(map[string]string) (Counter, error)
	WithLabels(...string) Counter
}

TagCounter interface for appending tags to metrics

type TagGauge

type TagGauge interface {
	WithTags(map[string]string) (Gauge, error)
	WithLabels(...string) Gauge
}

TagGauge interface for appending tags to gauge metrics

type TagObserver

type TagObserver interface {
	WithTags(map[string]string) (Observer, error)
	WithLabels(...string) Observer
}

TagObserver interface for appending tags to metrics

type Timer

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

Timer This one is taken from prometheus impementation to get rid out of dependancy

func NewTimer

func NewTimer(o Observer) *Timer

NewTimer is a constructor for Timer, counting from time.Now()

func (*Timer) ObserveDuration

func (t *Timer) ObserveDuration() time.Duration

ObserveDuration is commiting time duration that passed from it's creation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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