refactor: move connection generator to root
Because we need to be able to listen to events, we need to start the connection earlier so we can register events. Change-Id: I577e036359be87d451e45b2e1012baf3c7a11edc
This commit is contained in:
parent
ca775f42d6
commit
9f73c0f66a
@ -85,12 +85,7 @@ type NovaMetadata struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// nolint:funlen
|
// nolint:funlen
|
||||||
func NewDomainStatsCollector(uri string, nova bool) (*DomainStatsCollector, error) {
|
func NewDomainStatsCollector(conn *libvirt.Connect, nova bool) (*DomainStatsCollector, error) {
|
||||||
conn, err := libvirt.NewConnect(uri)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &DomainStatsCollector{
|
return &DomainStatsCollector{
|
||||||
Connection: conn,
|
Connection: conn,
|
||||||
Nova: nova,
|
Nova: nova,
|
||||||
@ -552,7 +547,3 @@ func (c *DomainStatsCollector) getNovaMetadata(domain *libvirt.Domain) (*NovaMet
|
|||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DomainStatsCollector) Close() {
|
|
||||||
c.Connection.Close()
|
|
||||||
}
|
|
||||||
|
@ -17,13 +17,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/libvirt/libvirt-go"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/prometheus/common/log"
|
"github.com/prometheus/common/log"
|
||||||
"github.com/prometheus/common/version"
|
"github.com/prometheus/common/version"
|
||||||
"gopkg.in/alecthomas/kingpin.v2"
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
|
||||||
"github.com/vexxhost/libvirtd_exporter/collectors"
|
"opendev.org/vexxhost/libvirtd_exporter/collectors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -50,15 +51,19 @@ func main() {
|
|||||||
kingpin.HelpFlag.Short('h')
|
kingpin.HelpFlag.Short('h')
|
||||||
kingpin.Parse()
|
kingpin.Parse()
|
||||||
|
|
||||||
domainStats, err := collectors.NewDomainStatsCollector(*libvirtURI, *libvirtNova)
|
conn, err := libvirt.NewConnect(*libvirtURI)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
domainStats, err := collectors.NewDomainStatsCollector(conn, *libvirtNova)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
prometheus.MustRegister(domainStats)
|
prometheus.MustRegister(domainStats)
|
||||||
|
|
||||||
defer domainStats.Close()
|
|
||||||
|
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = w.Write([]byte(`<html>
|
_, _ = w.Write([]byte(`<html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user