influx

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2022 License: Unlicense Imports: 13 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 v2.0+.
  • Optional settings can be set by using variadic function parameters.
  • 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"
	"sync"
	"time"

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

func worker() {
	c := metrics.NewCounter()
	if err := metrics.Register("foo", c); err != nil {
		// Handle err.
	}

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

func Example() {
	ctx, stop := context.WithCancel(context.Background())

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		influx.New(
			metrics.DefaultRegistry, // go-metrics registry
			"http://localhost:8086", // influx URL
			"user:pass",             // influx token
			"org",                   // org name
			"bucket",                // bucket name
			influx.Tags(map[string]string{"instance": "app@localhost"}),
			influx.Logger(logrus.WithField("thread", "go-metrics-influx")),
		).Run(ctx)
	}()

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

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

	// Wait for graceful shutdown.
	wg.Wait()
}

Documentation

Overview

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

Example
package main

import (
	"context"
	"sync"
	"time"

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

func worker() {
	c := metrics.NewCounter()
	if err := metrics.Register("foo", c); err != nil {
		// Handle err.
	}

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

func main() {
	ctx, stop := context.WithCancel(context.Background())

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		influx.New(
			metrics.DefaultRegistry, // go-metrics registry
			"http://localhost:8086", // influx URL
			"user:pass",             // influx token
			"org",                   // org name
			"bucket",                // bucket name
			influx.Tags(map[string]string{"instance": "app@localhost"}),
			influx.Logger(logrus.WithField("thread", "go-metrics-influx")),
		).Run(ctx)
	}()

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

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

	// Wait for graceful shutdown.
	wg.Wait()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Reporter)

Option allows to configure optional reporter parameters.

func Interval

func Interval(intv time.Duration) Option

Interval specifies how often metrics should be reported to influxdb. By default it will be done every 10 seconds.

func Logger

func Logger(log logrus.FieldLogger) Option

Logger sets custom logrus logger for error reporting.

func Precision

func Precision(prec time.Duration) Option

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

func Retries added in v1.1.0

func Retries(retries uint) Option

Retries sets retries count after write failure. By default 3 retries are done.

func Tags

func Tags(tags map[string]string) Option

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

type Reporter

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

Reporter holds configuration of go-metrics influx exporter. It should only be created using New function.

func New

func New(
	reg metrics.Registry,
	url string,
	token string,
	org string,
	bucket string,
	opts ...Option,
) *Reporter

New creates a new instance of influx metrics reporter. Variadic function parameters can be used to further configure reporter. It will not start exporting metrics until Run() is called.

func (*Reporter) Run

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

Run starts exporting metrics to influx DB. This method will block until context is cancelled. After context is closed, reporter client will be closed as well.

Jump to

Keyboard shortcuts

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