Skip to content

Commit 822d60b

Browse files
Provide cadvisor stats as prometheus metrics
1 parent 5eeb6fd commit 822d60b

File tree

3 files changed

+376
-2
lines changed

3 files changed

+376
-2
lines changed

cadvisor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var httpAuthRealm = flag.String("http_auth_realm", "localhost", "HTTP auth realm
4343
var httpDigestFile = flag.String("http_digest_file", "", "HTTP digest file for the web UI")
4444
var httpDigestRealm = flag.String("http_digest_realm", "localhost", "HTTP digest file for the web UI")
4545

46+
var prometheusEndpoint = flag.String("prometheus_endpoint", "/metrics", "Endpoint to expose Prometheus metrics on")
47+
4648
func main() {
4749
defer glog.Flush()
4850
flag.Parse()
@@ -72,7 +74,7 @@ func main() {
7274
mux := http.DefaultServeMux
7375

7476
// Register all HTTP handlers.
75-
err = cadvisorHttp.RegisterHandlers(mux, containerManager, *httpAuthFile, *httpAuthRealm, *httpDigestFile, *httpDigestRealm)
77+
err = cadvisorHttp.RegisterHandlers(mux, containerManager, *httpAuthFile, *httpAuthRealm, *httpDigestFile, *httpDigestRealm, *prometheusEndpoint)
7678
if err != nil {
7779
glog.Fatalf("Failed to register HTTP handlers: %v", err)
7880
}

http/handlers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ import (
2424
"github.com/google/cadvisor/healthz"
2525
httpMux "github.com/google/cadvisor/http/mux"
2626
"github.com/google/cadvisor/manager"
27+
"github.com/google/cadvisor/metrics"
2728
"github.com/google/cadvisor/pages"
2829
"github.com/google/cadvisor/pages/static"
2930
"github.com/google/cadvisor/validate"
31+
"github.com/prometheus/client_golang/prometheus"
3032
)
3133

32-
func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm string) error {
34+
func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm, prometheusEndpoint string) error {
3335
// Basic health handler.
3436
if err := healthz.RegisterHandler(mux); err != nil {
3537
return fmt.Errorf("failed to register healthz handler: %s", err)
@@ -83,6 +85,10 @@ func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAut
8385
}
8486
}
8587

88+
collector := metrics.NewPrometheusCollector(containerManager)
89+
prometheus.MustRegister(collector)
90+
http.Handle(prometheusEndpoint, prometheus.Handler())
91+
8692
return nil
8793
}
8894

0 commit comments

Comments
 (0)