promlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: Apache-2.0 Imports: 3 Imported by: 1

README

promlog

GoDoc Widget


forked klog hook to expose the number of log messages as Prometheus metrics:

log_messages_total{severity="ERROR"} 0
log_messages_total{severity="INFO"} 42
log_messages_total{severity="WARNING"} 0

Usage

Sample code:

package main

import (
	"net/http"
	"time"

	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/practo/klog/v2"
	"github.com/practo/promlog"
)

func main() {
	// Create the Prometheus hook:
	hook := promlog.MustNewPrometheusHook("", klog.InfoSeverityLevel)

	// Configure klog to use the Prometheus hook:
	klog.AddHook(hook)

	// Expose Prometheus metrics via HTTP, as you usually would:
	go http.ListenAndServe(":8080", promhttp.Handler())

	// Log with klog, as you usually would.
	// Every time the program generates a log message,
	// a Prometheus counter is incremented for the corresponding level.
	for {
		klog.Infof("foo")
		time.Sleep(1 * time.Second)
	}
}

Run the above program:

$ cd example && go run main.go
I0624 13:26:39.035027   51136 main.go:26] foo
I0624 13:26:40.035543   51136 main.go:26] foo
I0624 13:26:41.039174   51136 main.go:26] foo
I0624 13:26:42.039930   51136 main.go:26] foo
I0624 13:26:43.041310   51136 main.go:26] foo

Scrape the Prometheus metrics exposed by the hook:

$ curl -fsS localhost:8080 | grep log_messages
# HELP log_messages_total Total number of log messages.
# TYPE log_messages_total counter
log_messages_total{severity="ERROR"} 0
log_messages_total{severity="INFO"} 42
log_messages_total{severity="WARNING"} 0

Compile

$ go build

Test

$ go test
I0624 13:27:29.696765   51237 promlog_test.go:49] this is at info level!
W0624 13:27:29.697785   51237 promlog_test.go:55] this is at warning level!
E0624 13:27:29.698964   51237 promlog_test.go:61] this is at error level!
PASS
ok  	github.com/practo/promlog	0.237s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PrometheusHook

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

PrometheusHook exposes Prometheus counters for each of klog severity levels.

func MustNewPrometheusHook

func MustNewPrometheusHook(
	metricPrefix string, severityLevel string) *PrometheusHook

MustNewPrometheusHook creates a new instance of PrometheusHook which exposes Prometheus counters for various log levels. Contrarily to NewPrometheusHook, it does not return any error to the caller, but panics instead. Use MustNewPrometheusHook if you want a less verbose hook creation. Use NewPrometheusHook if you want more control.

func NewPrometheusHook

func NewPrometheusHook(
	metricPrefix string, severityLevel string) (*PrometheusHook, error)

NewPrometheusHook creates a new instance of PrometheusHook which exposes Prometheus counters for various severity. Contrarily to MustNewPrometheusHook, it returns an error to the caller in case of issue. Use NewPrometheusHook if you want more control. Use MustNewPrometheusHook if you want a less verbose hook creation.

func (*PrometheusHook) Fire

func (hook *PrometheusHook) Fire(s string, args ...interface{}) error

Fire increments the appropriate Prometheus counter

func (*PrometheusHook) SeverityLevel

func (hook *PrometheusHook) SeverityLevel() string

SeverityLevel can be "INFO", "WARNING", "ERROR" or "FATAL" Hook will be fired in all the cases when severity is greater than or equal to the severity level

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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