influx

package module
v0.0.0-...-0171f55 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Unlicense Imports: 8 Imported by: 0

README

go-metrics-influx

Godoc

This is a reporter for the go-metrics library which will post the metrics to InfluxDB.

It is loosely based on the vrischmann/go-metrics-influxdb implementation but with the following changes:

  • Support for newer Influx DB version V1.1+.
  • Optional settings can be set by chaining setter methods.
  • Support for structured logging of errors via logrus.
  • Support for stopping reporter via context.
  • Tags can be passed as key=val pairs separated by , in the metrics name (similar to influx line protocol).

Tag pairs extraction from metrics name example:

  • Metric with name hit_counter,region=eu,customerID=15 would create hit_counter measurement with tags {"region": "eu", "customerID": "15"}.
  • Invalid tag pairs are kept in the measurement name. Metric with a name hit_counter,smth,a=b would create hit_counter,smth measurement with tags {"a": "b"}.

Usage

package main

import (
	"context"
	"time"

        influx "github.com/fln/go-metrics-influx"
        metrics "github.com/rcrowley/go-metrics"
        "github.com/sirupsen/logrus"
)


func worker() {
	c := metrics.NewCounter()
	metrics.Register("foo", c)

	for {
		c.Inc(1)
		time.Sleep(time.Second)
	}
}

func main() {
	// Create context with cancel
	ctx, stop := context.WithCancel(context.Background())

	go influx.NewReporter(
		metrics.DefaultRegistry,           // go-metrics registry
		5*time.Second,                     // report interval
		"http://user:pass@localhost:8086", // Influx URL and credentials
		"app-metrics",                     // databse name
	).
		Tags(map[string]string{"instance": "app@localhost"}).
		Context(ctx).
		Logger(logrus.WithField("thread", "go-metrics-influx")).
		Run()

	// ...
	go worker()
	// ...

	// Stop reporter goroutine after 5 minutes
	time.Sleep(5 * time.Minute)
	stop()
}

Documentation

Overview

Package influx is a go-metrics to influx DB reporter implementation.

Example
package main

import (
	"context"
	"time"

	influx "github.com/fln/go-metrics-influx"
	metrics "github.com/rcrowley/go-metrics"
	"github.com/sirupsen/logrus"
)

func worker() {
	c := metrics.NewCounter()
	metrics.Register("foo", c)

	for {
		c.Inc(1)
		time.Sleep(time.Second)
	}
}

func main() {
	// Create context with cancel callback
	ctx, stop := context.WithCancel(context.Background())

	go influx.NewReporter(
		metrics.DefaultRegistry,           // go-metrics registry
		5*time.Second,                     // report interval
		"http://user:pass@localhost:8086", // Influx URL and credentials
		"app-metrics",                     // databse name
	).
		Tags(map[string]string{"instance": "app@localhost"}).
		Context(ctx).
		Logger(logrus.WithField("thread", "go-metrics-influx")).
		Run()

	// ...
	go worker()
	// ...

	// Stop reporter goroutine after 5 minutes
	time.Sleep(5 * time.Minute)
	stop()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reporter

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

Reporter holds configuration of go-metrics influx exporter. It can be configured only be public setter methods.

func NewReporter

func NewReporter(registry metrics.Registry, interval time.Duration, url string, db string) *Reporter

NewReporter creates a new instance of influx metrcs reporter. It may be further configured with helper methods. It will not start exporting metrics until Run() is called.

func (*Reporter) Context

func (r *Reporter) Context(ctx context.Context) *Reporter

Context assigns a context to this reporter. Context is only used to stop reporter Run() method.

func (*Reporter) Logger

func (r *Reporter) Logger(log logrus.FieldLogger) *Reporter

Logger sets optional logrus logger for error reporting.

func (*Reporter) Precision

func (r *Reporter) Precision(precision string) *Reporter

Precision changes the timestamp precision used in reported data points. By default timestamps are reported with a seconds precision. Having higher than seconds precision should be useful only when export interval is less than a second.

func (*Reporter) Run

func (r *Reporter) Run()

Run starts exporting metrics to influx DB. This method will block until context associated with this reporter is stopper (of forever if contex is not set).

func (*Reporter) Tags

func (r *Reporter) Tags(tags map[string]string) *Reporter

Tags sets a set of tags that will be assiciated with each influx data point written by this exporter.

Jump to

Keyboard shortcuts

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