Skip to content

Commit

Permalink
Add metric to track collector duration
Browse files Browse the repository at this point in the history
And register Go and process collectors too.
  • Loading branch information
simonpasquier committed Jun 13, 2019
1 parent b07ab29 commit 0023c09
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
16 changes: 10 additions & 6 deletions host/host_collector.go
Expand Up @@ -37,15 +37,16 @@ func init() {

// HostCollector collects host statistics from oVirt
type HostCollector struct {
client *api.Client
metrics []prometheus.Metric
collectNetwork bool
mutex sync.Mutex
client *api.Client
collectDuration prometheus.Observer
metrics []prometheus.Metric
collectNetwork bool
mutex sync.Mutex
}

// NewCollector creates a new collector
func NewCollector(client *api.Client, collectNetwork bool) prometheus.Collector {
return &HostCollector{client: client, collectNetwork: collectNetwork}
func NewCollector(client *api.Client, collectNetwork bool, collectDuration prometheus.Observer) prometheus.Collector {
return &HostCollector{client: client, collectNetwork: collectNetwork, collectDuration: collectDuration}
}

// Collect implements Prometheus Collector interface
Expand Down Expand Up @@ -75,6 +76,9 @@ func (c *HostCollector) getMetrics() []prometheus.Metric {
}

func (c *HostCollector) retrieveMetrics() {
timer := prometheus.NewTimer(c.collectDuration)
defer timer.ObserveDuration()

h := Hosts{}
err := c.client.GetAndParse("hosts", &h)
if err != nil {
Expand Down
34 changes: 27 additions & 7 deletions main.go
Expand Up @@ -28,6 +28,15 @@ var (
withSnapshots = flag.Bool("with-snapshots", true, "Collect snapshot metrics (can be time consuming in some cases)")
withNetwork = flag.Bool("with-network", true, "Collect network metrics (can be time consuming in some cases)")
debug = flag.Bool("debug", false, "Show verbose output (e.g. body of each response received from API)")

collectorDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ovirt_collectors_duration",
Help: "Histogram of latencies for metric collectors.",
Buckets: []float64{.1, .2, .4, 1, 3, 8, 20, 60},
},
[]string{"collector"},
)
)

func init() {
Expand Down Expand Up @@ -77,8 +86,13 @@ func startServer() {
}
defer client.Close()

reg := prometheus.NewRegistry()
reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
reg.MustRegister(prometheus.NewGoCollector())
reg.MustRegister(collectorDuration)

http.HandleFunc(*metricsPath, func(w http.ResponseWriter, r *http.Request) {
handleMetricsRequest(w, r, client)
handleMetricsRequest(w, r, client, reg)
})

log.Infof("Listening for %s on %s", *metricsPath, *listenAddress)
Expand All @@ -104,13 +118,19 @@ func connectAPI() (*api.Client, error) {
return client, err
}

func handleMetricsRequest(w http.ResponseWriter, r *http.Request, client *api.Client) {
func handleMetricsRequest(w http.ResponseWriter, r *http.Request, client *api.Client, appReg *prometheus.Registry) {
reg := prometheus.NewRegistry()
reg.MustRegister(vm.NewCollector(client, *withSnapshots, *withNetwork))
reg.MustRegister(host.NewCollector(client, *withNetwork))
reg.MustRegister(storagedomain.NewCollector(client))
reg.MustRegister(vm.NewCollector(client, *withSnapshots, *withNetwork, collectorDuration.WithLabelValues("vm")))
reg.MustRegister(host.NewCollector(client, *withNetwork, collectorDuration.WithLabelValues("host")))
reg.MustRegister(storagedomain.NewCollector(client, collectorDuration.WithLabelValues("storage")))

multiRegs := prometheus.Gatherers{
reg,
appReg,
}

promhttp.HandlerFor(reg, promhttp.HandlerOpts{
promhttp.HandlerFor(multiRegs, promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, r)
ErrorHandling: promhttp.ContinueOnError,
Registry: appReg}).ServeHTTP(w, r)
}
10 changes: 7 additions & 3 deletions storagedomain/storagedomain_collector.go
Expand Up @@ -28,16 +28,20 @@ func init() {

// StorageDomainCollector collects storage domain statistics from oVirt
type StorageDomainCollector struct {
client *api.Client
client *api.Client
collectDuration prometheus.Observer
}

// NewCollector creates a new collector
func NewCollector(client *api.Client) prometheus.Collector {
return &StorageDomainCollector{client: client}
func NewCollector(client *api.Client, collectDuration prometheus.Observer) prometheus.Collector {
return &StorageDomainCollector{client: client, collectDuration: collectDuration}
}

// Collect implements Prometheus Collector interface
func (c *StorageDomainCollector) Collect(ch chan<- prometheus.Metric) {
timer := prometheus.NewTimer(c.collectDuration)
defer timer.ObserveDuration()

s := StorageDomains{}
err := c.client.GetAndParse("storagedomains", &s)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions vm/vm_collector.go
Expand Up @@ -41,20 +41,22 @@ func init() {
maxSnapshotAge = prometheus.NewDesc(prefix+"snapshot_max_age_seconds", "Age of the oldest snapshot in seconds", labelNames, nil)
minSnapshotAge = prometheus.NewDesc(prefix+"snapshot_min_age_seconds", "Age of the newest snapshot in seconds", labelNames, nil)
illegalImages = prometheus.NewDesc(prefix+"illegal_images", "Health status of the disks attatched to the VM (1 if one or more disk is in illegal state)", labelNames, nil)

}

// VMCollector collects virtual machine statistics from oVirt
type VMCollector struct {
client *api.Client
collectDuration prometheus.Observer
metrics []prometheus.Metric
collectSnapshots bool
collectNetwork bool
mutex sync.Mutex
}

// NewCollector creates a new collector
func NewCollector(client *api.Client, collectSnaphots, collectNetwork bool) prometheus.Collector {
return &VMCollector{client: client, collectSnapshots: collectSnaphots, collectNetwork: collectNetwork}
func NewCollector(client *api.Client, collectSnaphots, collectNetwork bool, collectDuration prometheus.Observer) prometheus.Collector {
return &VMCollector{client: client, collectSnapshots: collectSnaphots, collectNetwork: collectNetwork, collectDuration: collectDuration}
}

// Collect implements Prometheus Collector interface
Expand Down Expand Up @@ -84,6 +86,9 @@ func (c *VMCollector) getMetrics() []prometheus.Metric {
}

func (c *VMCollector) retrieveMetrics() {
timer := prometheus.NewTimer(c.collectDuration)
defer timer.ObserveDuration()

v := VMs{}
err := c.client.GetAndParse("vms", &v)
if err != nil {
Expand Down

0 comments on commit 0023c09

Please sign in to comment.