Skip to content

Commit

Permalink
Registration of custom function to map container name to prometheus l…
Browse files Browse the repository at this point in the history
…abels
  • Loading branch information
jimmidyson committed Oct 7, 2015
1 parent 8dcf46b commit 5db9f86
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ func main() {
mux := http.DefaultServeMux

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

cadvisorHttp.RegisterPrometheusHandler(mux, containerManager, *prometheusEndpoint, nil)

// Start the manager.
if err := containerManager.Start(); err != nil {
glog.Fatalf("Failed to start container manager: %v", err)
Expand Down
12 changes: 7 additions & 5 deletions http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm, prometheusEndpoint string) error {
func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm string) error {
// Basic health handler.
if err := healthz.RegisterHandler(mux); err != nil {
return fmt.Errorf("failed to register healthz handler: %s", err)
Expand Down Expand Up @@ -85,13 +85,15 @@ func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAut
}
}

collector := metrics.NewPrometheusCollector(containerManager)
prometheus.MustRegister(collector)
http.Handle(prometheusEndpoint, prometheus.Handler())

return nil
}

func RegisterPrometheusHandler(mux httpMux.Mux, containerManager manager.Manager, prometheusEndpoint string, containerNameToLabelsFunc metrics.ContainerNameToLabelsFunc) {
collector := metrics.NewPrometheusCollector(containerManager, containerNameToLabelsFunc)
prometheus.MustRegister(collector)
mux.Handle(prometheusEndpoint, prometheus.Handler())
}

func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) {
err := static.HandleRequest(w, r.URL)
if err != nil {
Expand Down
22 changes: 17 additions & 5 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,21 @@ func (cm *containerMetric) desc(baseLabels []string) *prometheus.Desc {
return prometheus.NewDesc(cm.name, cm.help, append(baseLabels, cm.extraLabels...), nil)
}

type ContainerNameToLabelsFunc func(containerName string) map[string]string

// PrometheusCollector implements prometheus.Collector.
type PrometheusCollector struct {
infoProvider infoProvider
errors prometheus.Gauge
containerMetrics []containerMetric
infoProvider infoProvider
errors prometheus.Gauge
containerMetrics []containerMetric
containerNameToLabels ContainerNameToLabelsFunc
}

// NewPrometheusCollector returns a new PrometheusCollector.
func NewPrometheusCollector(infoProvider infoProvider) *PrometheusCollector {
func NewPrometheusCollector(infoProvider infoProvider, f ContainerNameToLabelsFunc) *PrometheusCollector {
c := &PrometheusCollector{
infoProvider: infoProvider,
infoProvider: infoProvider,
containerNameToLabels: f,
errors: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "container",
Name: "scrape_error",
Expand Down Expand Up @@ -500,6 +504,14 @@ func (c *PrometheusCollector) collectContainersInfo(ch chan<- prometheus.Metric)
}
baseLabelValues := []string{id, name, image}[:len(baseLabels)]

if c.containerNameToLabels != nil {
newLabels := c.containerNameToLabels(name)
for k, v := range newLabels {
baseLabels = append(baseLabels, k)
baseLabelValues = append(baseLabelValues, v)
}
}

// Container spec
desc := prometheus.NewDesc("container_start_time_seconds", "Start time of the container since unix epoch in seconds.", baseLabels, nil)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(container.Spec.CreationTime.Unix()), baseLabelValues...)
Expand Down
2 changes: 1 addition & 1 deletion metrics/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (p testSubcontainersInfoProvider) SubcontainersInfo(string, *info.Container
}

func TestPrometheusCollector(t *testing.T) {
prometheus.MustRegister(NewPrometheusCollector(testSubcontainersInfoProvider{}))
prometheus.MustRegister(NewPrometheusCollector(testSubcontainersInfoProvider{}, nil))

rw := httptest.NewRecorder()
prometheus.Handler().ServeHTTP(rw, &http.Request{})
Expand Down

0 comments on commit 5db9f86

Please sign in to comment.