func StartCollector()

in pkg/metrics/metrics.go [47:95]


func StartCollector() error {
	if mcfg.enabledCollectors == "" {
		glog.Infof("No metrics collectors were enabled.")
		return nil
	}
	enabledCollectors := strings.Split(mcfg.enabledCollectors, ",")
	nc, pc, err := collector.NewNodeCollector(enabledCollectors, mcfg.procPath, mcfg.stackType)
	if err != nil {
		return err
	}
	glog.Infof("Enabled metrics collectors:")
	for n := range nc.Collectors {
		glog.Infof(" - %s", n)
	}

	registry := prometheus.NewRegistry()
	err = registry.Register(nc)
	if err != nil {
		glog.Errorf("Couldn't register collector: %v", err)
		return err
	}

	for _, c := range pc {
		registry.MustRegister(c)
	}

	gatherers := prometheus.Gatherers{
		registry,
	}

	// Delegate http serving to Prometheus client library, which will call collector.Collect.
	h := promhttp.InstrumentMetricHandler(
		registry,
		promhttp.HandlerFor(gatherers,
			promhttp.HandlerOpts{
				ErrorHandling: promhttp.ContinueOnError,
			}),
	)

	go func() {
		http.HandleFunc("/metrics", h.ServeHTTP)
		glog.Infof("Listening on %s", mcfg.listenAddress)
		err = http.ListenAndServe(mcfg.listenAddress, nil)
		if err != nil {
			glog.Errorf("Couldn't start http server- %v", err)
		}
	}()
	return nil
}