Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus usage instructions #35

Open
maticmeznar opened this issue Jul 28, 2016 · 2 comments
Open

Prometheus usage instructions #35

maticmeznar opened this issue Jul 28, 2016 · 2 comments

Comments

@maticmeznar
Copy link

maticmeznar commented Jul 28, 2016

How can go-metrics be used with Prometheus? The farthest I came is:

package main

import (
    "net/http"
    "time"

    "github.com/armon/go-metrics"
    "github.com/armon/go-metrics/prometheus"
)

func main() {
    sink, _ := prometheus.NewPrometheusSink()
    m, err := metrics.NewGlobal(metrics.DefaultConfig("goTest"), sink)
    if err != nil {
        panic(err)
    }

    go func() {
        for {
            m.IncrCounter([]string{"requestCounter"}, 1)
            time.Sleep(time.Second)
        }
    }()

    select {}
}

Some parts are missing like setting up an HTTP handler, HTTP server, etc ... And I don't know how to enable Prometheus server to scrape this. I've looked at the source code and I have a feeling that something is missing.

@brianhama
Copy link

Did you ever figure this out?

@maticmeznar
Copy link
Author

maticmeznar commented Apr 5, 2017

@brianhama Yes, I think go-metrics uses DefaultServeMux by default, while I was using github.com/julienschmidt/httprouter.

When I used it like this it worked, although on a dedicated port:

	// Prepare metrics
	sink, _ := prometheus.NewPrometheusSink()
	m, err := metrics.NewGlobal(metrics.DefaultConfig("apiserver"), sink)
	m.EnableHostname = false
	if err != nil {
		logger.Fatal("Unable to create metrics",
			zap.Error(err),
		)
	}

	mux := http.NewServeMux()
	mux.Handle("/metrics", prom.Handler())
	metricsServer := &http.Server{
		Addr:    ":" + strconv.Itoa(*cfgHTTPMetricsPort),
		Handler: mux,
	}

	go func() {
		if err := metricsServer.ListenAndServe(); err != nil {
			logger.Fatal("Unable to start metrics HTTP server", zap.Error(err))
		}
	}()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants