influxus

package module
v0.0.0-...-2a7a599 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2016 License: MIT Imports: 4 Imported by: 1

README

Influxus GoDoc

Golang Hook for the Logrus logging library, in order to output logs to InfluxDB.

Usage

Installation
go get github.com/vlad-doru/influxus
Example

import (
	"github.com/Sirupsen/logrus"
	"github.com/influxdata/influxdb/client/v2"
	"github.com/vlad-doru/influxus"
)

func init() {
	// Create the InfluxDB client.
	influxClient, err := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})
	if err != nil {
		logrus.Fatalf("Error while creating the client: %v", err)
	}
	// Create and add the hook.
	hook, err := influxus.NewHook(
		&influxus.Config{
			Client:             influxClient,
			Database:           "logrus", // DATABASE MUST BE CREATED
			DefaultMeasurement: "logrus",
			BatchSize:          1, // default is 100
			BatchInterval:      1, // default is 5 seconds
		})
	if err != nil {
		logrus.Fatalf("Error while creating the hook: %v", err)
	}
	// Add the hook to the standard logger.
	logrus.StandardLogger().Hooks.Add(hook)
}

Concepts

Concurrency

We use a non-blocking model for the hook. Each time an entry is fired, we create an InfluxDB point and put that through a buffered channel. It is then picked up by a worker goroutine that will handle the current batch and write everything once the batch hit the size or time limit.

Configuration

We chose to take in as a parameter the InfluxDB client so that we have greater flexibility over the way we connect to the Influx Server. All the defaults and configuration options can be found in the GoDoc.

Database Handling

The database should have been previously created. This is by design, as we want to avoid people generating lots of databases. When passing an empty string for the InfluxDB database name, we default to "logrus" as the database name.

Message Field

We will insert your message into InfluxDB with the field message.

TODO

  • Concurrent, non-blocking design.
  • Add unit tests
  • Set up continous integration.

Documentation

Index

Constants

View Source
const (
	// PrecisionDefault represents the default precision used for the InfluxDB points.
	PrecisionDefault = "ns"
	// DatabaseDefault is the default database that we will write to, if not specified otherwise in the Config for the hook.
	DatabaseDefault = "logrus"
	// DefaultMeasurementValue is the default measurement that we will assign to each point, unless there is a field called "measurement".
	DefaultMeasurementValue = "logrus"
	// BatchIntervalDefault represents the number of seconds that we wait for a batch to fill up.
	// After that we flush it to InfluxDB whatsoever.
	BatchIntervalDefault = 5
	// BatchSizeDefault represents the maximum size of a batch.
	BatchSizeDefault = 100
)
View Source
const (
	// LevelTag represents the name of the tag that will have the log level assigned.
	LevelTag = "level"
	// MessageField represents the name of the fields that we will assign the log message to.
	MessageField = "message"
	// MeasurementField represents the name of the field that we will get the measurement from the log fields.
	MeasurementField = "measurement"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Client             influx.Client
	Precision          string
	Database           string
	DefaultMeasurement string
	// Tags that we will extract from the log fields and set them as Influx point tags.
	Tags          []string
	BatchInterval int // seconds
	BatchSize     int
}

Config is the struct that we will use to configure our Influxus hook to Logrus.

type Hook

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

Hook represents the Logrus hook to InfluxDB.

func NewHook

func NewHook(config *Config) (*Hook, error)

NewHook generate a new InfluxDB hook based on the given configuration.

func (*Hook) Fire

func (hook *Hook) Fire(entry *logrus.Entry) (err error)

Fire represents the interface that we must have implemented for the Logrus Hook.

func (*Hook) Levels

func (hook *Hook) Levels() []logrus.Level

Levels implementation allows for level logging.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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