metrics

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 10 Imported by: 0

README

InfluxDB Metrics Reporter

Go Report Card Software License Issues Stars GoDoc

Package metrics is based on github.com/rcrowley/go-metrics and loosely inspired by github.com/vrischmann/go-metrics-influxdb.

Why?

Why write a new package if there already is a influxDB reporter?

The main difference between this package and vrischmann/go-metrics-influxdb is the API. This API is based on the go-metrics api and separates the registry (built into the Reporter) from the measurement/metrics. This enables a global Reporter and the creation of independant metrics distributed across the codebase without passing the Reporter around. A reporter can however be injected into a metric if needed.

With vrischmann/go-metrics-influxdb a reporter can only handle one measurement, meaning a new reporter is needed per measurement. This entails more goroutines and more calls to the influxDB in larger applications with multiple measurements.

Metrics collection has been optimized in regards to memory allocations to be fast and have less impact on the GC.

Usage

Global Reporter, metrics accross the application
func main() {
	// Initialize reporter and set it as default (global).
	rep := metrics.NewReporter("http://localhost:8086", "metrics", metrics.Interval(1*time.Second))
	metrics.SetDefaultReporter(rep)

	// Start reporting all registered or to be registered metrics.
	go rep.Run()
}

This creates a new reporter, sets it as the default reporter and starts it. The reporter will then start a Timer with given interval (default: 10 seconds). It will collect and report data from all metrics registered to the default reporter.

To register a metric use the following code anywhere in your server:

func registerGauge() {
	// Create and register a new gauge metric.
	metric := metrics.NewGauge("MetricName", metrics.WithMeasurement("MeasurementName"))
}

To update the data of the metric:

// Update the gauge metric value.
metric.Update(n)

For working code see the simple-gauge example.

Without Global Reporter

Some might prefer to inject the reporter with the metric instead of using a global reporter. This is supported with the WithReporter option when creating a metric:

rep := metrics.NewReporter("http://localhost:8086", "metrics")

// inject the reporter to be registered to:
metric := metrics.NewGauge("MetricName", metrics.WithMeasurement("MeasurementName"), metrics.WithReporter(rep))

For working code see the multiple-reporters example.

⚠ When creating multiple repoters use the Registry option to supply a new registry to each reporter. If multiple reporters are created without this option, they will all use the default registry: all reporters will report data from all the metrics.

Metrics

New metrics can be created with the metrics.NewXY functions.

For more information on the different metric types see go-metrics. If a go-metrics metric is not implemented here, please open an issue.

Documentation

Overview

Package metrics is based on github.com/rcrowley/go-metrics and loosely inspired by github.com/vrischmann/go-metrics-influxdb. The main difference of this package and `vrischmann/go-metrics-influxdb` is the API. The API is based on the `go-metrics` api and separates the registry (built into the Reporter) from the measurement/metrics. This enables a global Reporter and the creation of independent metrics distributed across the codebase without passing the Reporter around. A reporter can however be injected into a metrics if needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureGCStats added in v0.1.3

func CaptureGCStats(d time.Duration)

CaptureGCStats starts capturing GC stats on the default reporter.

func CaptureMemStats added in v0.1.3

func CaptureMemStats(d time.Duration)

CaptureMemStats starts capturing Memory stats on the default reporter.

func Register

func Register(name string, metric Metric, options ...Option) error

Register registers a custom metric to the default reporter. Data points from a registered metric will be collected via the AddPoints endpoint and then sent to influxDB.

This function is only needed for custom metrics. Functions creating a metric in this package register themselves.

func SetDefaultReporter

func SetDefaultReporter(reporter Reporter)

SetDefaultReporter sets the default reporter to be used for all metrics. It can be overwritten per Measurement.

Types

type Counter

type Counter interface {
	metrics.Counter
}

Counter implements go-metrics.Counter and possibly adds a bit functionality.

func NewCounter

func NewCounter(name string, options ...Option) Counter

NewCounter creates a new counter or retrieves an existing counter with the same name.

type Gauge

type Gauge interface {
	metrics.Gauge
}

Gauge implements go-metrics.Gauge and possibly adds a bit functionality.

func NewGauge

func NewGauge(name string, options ...Option) Gauge

NewGauge creates a new gauge or retrieves an existing gauge with the same name.

type GaugeFloat64

type GaugeFloat64 interface {
	metrics.GaugeFloat64
}

GaugeFloat64 implements go-metrics.GaugeFloat64 and possibly adds a bit functionality.

func NewGaugeFloat64

func NewGaugeFloat64(name string, options ...Option) GaugeFloat64

NewGaugeFloat64 creates a new gauge with float64 or retrieves an existing gauge with the same name.

type Histogram

type Histogram interface {
	metrics.Histogram
}

Histogram implements go-metrics.Histogram and possibly adds a bit functionality.

func NewHistogram

func NewHistogram(name string, options ...Option) Histogram

NewHistogram creates a new histogram or retrieves an existing histogram with the same name. By default, this creates a uniform sample with a reservoir size of 100. Provide a different metric via the WithMetric option: e.g. WithMetric(metrics.NewHistogram(metrics.NewUniformSample(100)))

type Meter

type Meter interface {
	metrics.Meter
}

Meter implements go-metrics.Meter and possibly adds a bit functionality.

func NewMeter

func NewMeter(name string, options ...Option) Meter

NewMeter creates a new meter or retrieves an existing meter with the same name.

type Metric

type Metric interface {
	AddPoints(pts []client.Point) []client.Point
}

Metric defines an interface to be implemented for custom metrics.

For a metric to be used, it needs to be registered. If no registry was provided to the Reporter, it uses the default registry and can be registered with `metrics.Register(name, metric)`. If a custom registry was provided to the Reporter, either register it to that registry or use the Reporter.Register function to register it.

type Option

type Option func(s *baseMetric)

Option defines an option to be used when creating a new measurement.

func WithMeasurement

func WithMeasurement(m string) Option

WithMeasurement provides a ifluxDB measurement name to the metric.

func WithMetric

func WithMetric(m interface{}) Option

WithMetric injects a github.com/rcrowley/go-metrics metric instead of creating a new one.

func WithReporter

func WithReporter(r Reporter) Option

WithReporter sets a reporter to use. If not set the global reporter will be used that can be set via `metrics.SetDefaultReporter`.

func WithTags

func WithTags(t map[string]string) Option

WithTags adds tags to the metric. The tags given to the reporter are being extended and overwritten (if same key).

type Reporter

type Reporter interface {
	Run()
	Register(name string, metric Metric) error
	Get(name string) (Metric, bool)
	Tags() map[string]string
	Stop()
}

Reporter defines a metrics reporter. It is responsible for connection handling and sending data to it. It also holds the metrics registry all the metrics get registered to. Implementing the Reporter interface is useful for testing or changing the way the reporter behaves.

func NewReporter

func NewReporter(influxURL, database string, options ...ReporterOption) Reporter

NewReporter creates a new reporter which holds the influxDB connection and sends data to it.

type ReporterOption

type ReporterOption func(r *reporter)

ReporterOption defines an option to be used when creating a reporter.

func Align

func Align() ReporterOption

Align enables aligning the reported time to the interval.

func Auth

func Auth(user, pass string) ReporterOption

Auth sets user and password for the influxDB connection.

func Interval

func Interval(d time.Duration) ReporterOption

Interval overwrites the default interval of 10 seconds. The interval is used to send the data e.g. every 10 seconds.

func Registry

func Registry(reg metrics.Registry) ReporterOption

Registry uses the given metric registry instead of the global default registry.

func Tags

func Tags(tags map[string]string) ReporterOption

Tags allows to add some tags to be added to all metrics sent by this reporter.

func WithGCStats added in v0.1.3

func WithGCStats() ReporterOption

WithGCStats enables collection of GC stats for this reporter.

func WithMemStats added in v0.1.3

func WithMemStats() ReporterOption

WithMemStats enables collection of memory stats for this reporter.

type Timer

type Timer interface {
	metrics.Timer

	TimeThis() func()
}

Timer implements go-metrics.Timer and possibly adds a bit functionality.

func NewTimer

func NewTimer(name string, options ...Option) Timer

NewTimer creates a new timer or retrieves an existing timer with the same name.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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