Categorygithub.com/dbadv/go-metrics-influx
modulepackage
1.1.1
Repository: https://github.com/dbadv/go-metrics-influx.git
Documentation: pkg.go.dev

# 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()
}

# Functions

Interval specifies how often metrics should be reported to influxdb.
Logger sets custom logrus logger for error reporting.
New creates a new instance of influx metrics reporter.
Precision changes the duration precision used in reported data points.
Retries sets retries count after write failure.
Tags sets a set of tags that will be assiciated with each influx data point written by this exporter.

# Structs

Reporter holds configuration of go-metrics influx exporter.

# Type aliases

Option allows to configure optional reporter parameters.