graylog

package module
v0.0.0-...-0bdcec9 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: GPL-3.0 Imports: 13 Imported by: 0

README

Graylog Hook for Logrus

Use this hook to send logs to multiple Graylog servers over UDP.

====

Build Status Go Report Card codecov

Motivation

There is no way to setup a UDP load balancer in front of a Graylog server cluster due to the chunking feature, which requires all chunks of a GELF message being sent to the same Graylog server node.

Client side load balancing provided by this logrus hook could be a solution to this issue.

Algorithm

Weighted-random algorithm is used to select Graylog server node as log destination every time when the hook is fired by logrus.

Install

This package depends on

$ go get -u github.com/on99/logrus-graylog-hook

Usage

package main

import (
	"io/ioutil"
	"time"

	"github.com/sirupsen/logrus"
	graylog "github.com/ghiac/logrus-graylog-hook"
)

func main() {
	// graylog configuration
	config := graylog.NewConfig()
	config.Facility = "graylog_hook"
	config.HealthCheckInterval = 5 * time.Second // check graylog nodes health periodically
	config.StaticMeta = map[string]interface{}{  // static meta that always sent to graylog
		"go_version": "1.7.1",
	}

	// create hook
	hook := graylog.New(config)

	// set graylog nodes
	// 5/10 chances log will be sent to node-1
	// 3/10 chances log will be sent to node-2
	// 2/10 chances log will be sent to node-3
	hook.SetNodeConfigs(
		graylog.NodeConfig{
			UDPAddress:     "node-1.graylog:12201",
			HealthCheckURL: "node-1.graylog/api/system/lbstatus",
			Weight:         5,
		},
		graylog.NodeConfig{
			UDPAddress:     "node-2.graylog:12201",
			HealthCheckURL: "node-2.graylog/api/system/lbstatus",
			Weight:         3,
		},
		graylog.NodeConfig{
			UDPAddress:     "node-3.graylog:12201",
			HealthCheckURL: "node-3.graylog/api/system/lbstatus",
			Weight:         2,
		},
	)

	// start health check
	// all graylog nodes are alive by default
	hook.StartHealthCheck()

	logrus.AddHook(hook)             // add graylog hook to logrus
	logrus.SetOutput(ioutil.Discard) // discard logrus output

	// log something, enjoy a cup of coffee
	logrus.WithField("key", "value").Info("log message")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateConfig

func ValidateConfig(c Config) error

ValidateConfig validates config

Types

type Config

type Config struct {
	Hostname   string
	Facility   string
	StaticMeta map[string]interface{} // static meta always sent to graylog

	// default not compress to save cpu power as logs are most likely to send over private network
	CompressType     gelf.CompressType // default to NoCompression
	CompressionLevel int               // default to flate.NoCompression

	// health check
	HealthCheckInterval time.Duration // default to 5 seconds
}

Config stores graylog hook config

func NewConfig

func NewConfig() Config

NewConfig returns a default graylog hook config

type Hook

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

Hook implements logrus.Hook for sending log to graylog

func New

func New(config Config) *Hook

New returns a Hook with config

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

Fire implements logrus.Hook interface

func (*Hook) Levels

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

Levels implements logrus.Hook interface

func (*Hook) SetNodeConfigs

func (h *Hook) SetNodeConfigs(configs ...NodeConfig) error

SetNodeConfigs set graylog nodes with node configs given

func (*Hook) StartHealthCheck

func (h *Hook) StartHealthCheck()

StartHealthCheck checks if graylog nodes are alive periodically

type NodeConfig

type NodeConfig struct {
	UDPAddress     string
	HealthCheckURL string
	Weight         int
}

NodeConfig stores node configuration

Jump to

Keyboard shortcuts

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