Giter VIP home page Giter VIP logo

promrus's Introduction

promrus

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

log_messages{level="debug"}
log_messages{level="info"}
log_messages{level="warning"}
log_messages{level="error"}

Usage

Sample code:

package main

import (
	"net/http"
	"time"

	"github.com/prometheus/client_golang/prometheus/promhttp"
	log "github.com/sirupsen/logrus"
	"github.com/weaveworks/promrus"
)

func main() {
	// Create the Prometheus hook:
	hook := promrus.MustNewPrometheusHook()

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

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

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

Run the above program:

$ go get -u github.com/golang/dep/cmd/dep
$ dep ensure
$ go run main.go
INFO[0000] foo
INFO[0001] foo
INFO[0002] foo
[...]
INFO[0042] foo

Scrape the Prometheus metrics exposed by the hook:

$ curl -fsS localhost:8080 | grep log_messages
# HELP log_messages Total number of log messages.
# TYPE log_messages counter
log_messages{level="debug"} 0
log_messages{level="error"} 0
log_messages{level="info"} 42
log_messages{level="warning"} 0

Setup development environment

$ go get github.com/golang/dep/cmd/dep
$ dep ensure

Compile

$ go build

Test

$ go test
DEBU[0000] this is at debug level!
INFO[0000] this is at info level!
WARN[0000] this is at warning level!
ERRO[0000] this is at error level!
PASS
ok  	github.com/weaveworks/promrus	0.011s

Getting Help

If you have any questions about, feedback for or problems with promrus:

Weaveworks follows the CNCF Code of Conduct. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting a Weaveworks project maintainer, or Alexis Richardson ([email protected]).

Your feedback is always welcome!

promrus's People

Contributors

bboreham avatar heppu avatar marccarre avatar rade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

promrus's Issues

Error "duplicate metrics collector registration attempted" on multiple initialisations

When creating the same hook multiple times, the counter vector for the hook fails to be registered by Prometheus, which then fails with:

error duplicate metrics collector registration attempted

I might be better for promrus to:

  • be able to safely to initialise the hook multiple times (in order to tolerate user errors and/or resets), OR
  • be able to unregister the hook.

Initially raised by @bboreham here:

Please add to the documentation on this function that it cannot be called more than once, or bracket this section with a test to avoid causing an error duplicate metrics collector registration attempted (and adding multiple instances of the same hook below)

Stop unregistering the log_messages counter

If there is another library in your program trying to use a counter named log_messages, it's unpredictable which one wins.

Maybe some kind of namespacing in the name should be added, but from the Prometheus docs it seems that the AlreadyRegisteredError is designed for the case above.

Add MustNew constructor

Typical usage is:

hook, err := promrus.NewPrometheusHook()
if err != nil {
    log.Fatalf("...", err) // or panic("...")
}

so it would make sense to wrap this up in a MustNewPrometheusHook function to keep things DRY.

Allow configuring custom log levels

Would you accept a PR with a wrapper that allow configuring the supported log levels and doesn’t break the existing signature of NewPrometheusHook ?

New Trace log level in logrus

Hello, I'm just opening this issue to let you know, the next release of logrus (v1.2.0) will add a new trace level named Trace below Debug.
Here is the PR for reference sirupsen/logrus#844
You may want to take into account this new level.

`TestRegisteringHookMultipleTimesShouldBeSafe` failure on CPU-constrained systems

It was reported in a Debian bug that TestRegisteringHookMultipleTimesShouldBeSafe fails when the tests are run on a system with only one or two CPUs, or alternatively on non-amd64 architectures with slower CPUs. The test failure output is at the end of this issue.

The cause appears to be that the metrics server started as a goroutine in httpServePrometheusMetrics() isn't fully ready to serve requests by the time the test code attempts to make a request. As a work around for the time being, I've introduced a quarter second sleep before returning from httpServePrometheusMetrics() (which is probably overkill) that fixes this test failure.

=== RUN   TestRegisteringHookMultipleTimesShouldBeSafe
    promrus_test.go:103: 
        	Error Trace:	/build/golang-github-weaveworks-promrus-1.2.0+git20210208.77c195c/_build/src/github.com/weaveworks/promrus/promrus_test.go:103
        	            				/build/golang-github-weaveworks-promrus-1.2.0+git20210208.77c195c/_build/src/github.com/weaveworks/promrus/promrus_test.go:29
        	Error:      	Expected nil, but got: &url.Error{Op:"Get", URL:"http://localhost:8080/metrics", Err:(*net.OpError)(0xc000025360)}
        	Test:       	TestRegisteringHookMultipleTimesShouldBeSafe
--- FAIL: TestRegisteringHookMultipleTimesShouldBeSafe (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x8acf6a]

goroutine 8 [running]:
testing.tRunner.func1.2({0x9157c0, 0xdaaef0})
	/usr/lib/go-1.21/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
	/usr/lib/go-1.21/src/testing/testing.go:1548 +0x397
panic({0x9157c0?, 0xdaaef0?})
	/usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
github.com/weaveworks/promrus_test.httpGetMetrics(0x27?)
	/build/golang-github-weaveworks-promrus-1.2.0+git20210208.77c195c/_build/src/github.com/weaveworks/promrus/promrus_test.go:104 +0xca
github.com/weaveworks/promrus_test.TestRegisteringHookMultipleTimesShouldBeSafe(0x0?)
	/build/golang-github-weaveworks-promrus-1.2.0+git20210208.77c195c/_build/src/github.com/weaveworks/promrus/promrus_test.go:29 +0x98
testing.tRunner(0xc000106ea0, 0x9eb450)
	/usr/lib/go-1.21/src/testing/testing.go:1595 +0xff
created by testing.(*T).Run in goroutine 1
	/usr/lib/go-1.21/src/testing/testing.go:1648 +0x3ad
FAIL	github.com/weaveworks/promrus	0.023s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.