shave a few more nanoseconds
This commit is contained in:
parent
184a20c019
commit
6622050a34
|
|
@ -82,11 +82,10 @@ func descClient(ns string) *uclient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *unifiCollector) exportClients(clients []*unifi.Client) (e []*metricExports) {
|
func (u *unifiCollector) exportClients(clients []*unifi.Client, ch chan []*metricExports) {
|
||||||
for _, c := range clients {
|
for _, c := range clients {
|
||||||
e = append(e, u.exportClient(c)...)
|
ch <- u.exportClient(c)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CollectClient exports Clients' Data
|
// CollectClient exports Clients' Data
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ type Report struct {
|
||||||
Elapsed time.Duration
|
Elapsed time.Duration
|
||||||
Start time.Time
|
Start time.Time
|
||||||
ch chan []*metricExports
|
ch chan []*metricExports
|
||||||
wait sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUnifiCollector returns a prometheus collector that will export any available
|
// NewUnifiCollector returns a prometheus collector that will export any available
|
||||||
|
|
@ -104,7 +104,7 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
var err error
|
var err error
|
||||||
r := &Report{Start: time.Now(), ch: make(chan []*metricExports)}
|
r := &Report{Start: time.Now(), ch: make(chan []*metricExports)}
|
||||||
defer func() {
|
defer func() {
|
||||||
r.wait.Wait()
|
r.wg.Wait()
|
||||||
close(r.ch)
|
close(r.ch)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -116,22 +116,23 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
|
|
||||||
go u.exportMetrics(ch, r)
|
go u.exportMetrics(ch, r)
|
||||||
|
|
||||||
r.wait.Add(2)
|
r.wg.Add(len(r.Metrics.Clients) + len(r.Metrics.Sites))
|
||||||
go func() { r.ch <- u.exportClients(r.Metrics.Clients) }()
|
go u.exportClients(r.Metrics.Clients, r.ch)
|
||||||
go func() { r.ch <- u.exportSites(r.Metrics.Sites) }()
|
go u.exportSites(r.Metrics.Sites, r.ch)
|
||||||
|
|
||||||
if r.Metrics.Devices == nil {
|
if r.Metrics.Devices == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.wait.Add(4)
|
r.wg.Add(len(r.Metrics.Devices.UAPs) + len(r.Metrics.Devices.USGs) + len(r.Metrics.Devices.USWs) + len(r.Metrics.Devices.UDMs))
|
||||||
go func() { r.ch <- u.exportUAPs(r.Metrics.Devices.UAPs) }()
|
go u.exportUAPs(r.Metrics.Devices.UAPs, r.ch)
|
||||||
go func() { r.ch <- u.exportUSGs(r.Metrics.Devices.USGs) }()
|
go u.exportUSGs(r.Metrics.Devices.USGs, r.ch)
|
||||||
go func() { r.ch <- u.exportUSWs(r.Metrics.Devices.USWs) }()
|
go u.exportUSWs(r.Metrics.Devices.USWs, r.ch)
|
||||||
go func() { r.ch <- u.exportUDMs(r.Metrics.Devices.UDMs) }()
|
go u.exportUDMs(r.Metrics.Devices.UDMs, r.ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call this once (at least as-is). It sets all the counters and runs the logging function.
|
// This is closely tied to the method above with a sync.WaitGroup.
|
||||||
|
// This method runs in a go routine and exits when the channel closes.
|
||||||
func (u *unifiCollector) exportMetrics(ch chan<- prometheus.Metric, r *Report) {
|
func (u *unifiCollector) exportMetrics(ch chan<- prometheus.Metric, r *Report) {
|
||||||
descs := make(map[*prometheus.Desc]bool) // used as a counter
|
descs := make(map[*prometheus.Desc]bool) // used as a counter
|
||||||
for newMetrics := range r.ch {
|
for newMetrics := range r.ch {
|
||||||
|
|
@ -154,7 +155,7 @@ func (u *unifiCollector) exportMetrics(ch chan<- prometheus.Metric, r *Report) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.wait.Done()
|
r.wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.Config.LoggingFn == nil {
|
if u.Config.LoggingFn == nil {
|
||||||
|
|
|
||||||
|
|
@ -67,9 +67,9 @@ func descSite(ns string) *site {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *unifiCollector) exportSites(sites unifi.Sites) (e []*metricExports) {
|
func (u *unifiCollector) exportSites(sites unifi.Sites, ch chan []*metricExports) (e []*metricExports) {
|
||||||
for _, s := range sites {
|
for _, s := range sites {
|
||||||
e = append(e, u.exportSite(s)...)
|
ch <- u.exportSite(s)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -102,11 +102,10 @@ func descUAP(ns string) *uap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *unifiCollector) exportUAPs(uaps []*unifi.UAP) (e []*metricExports) {
|
func (u *unifiCollector) exportUAPs(uaps []*unifi.UAP, ch chan []*metricExports) {
|
||||||
for _, a := range uaps {
|
for _, a := range uaps {
|
||||||
e = append(e, u.exportUAP(a)...)
|
ch <- u.exportUAP(a)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// exportUAP exports Access Point Data
|
// exportUAP exports Access Point Data
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,10 @@ func descUDM(ns string) *udm {
|
||||||
return &udm{}
|
return &udm{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *unifiCollector) exportUDMs(udms []*unifi.UDM) (e []*metricExports) {
|
func (u *unifiCollector) exportUDMs(udms []*unifi.UDM, ch chan []*metricExports) {
|
||||||
for _, d := range udms {
|
for _, d := range udms {
|
||||||
e = append(e, u.exportUDM(d)...)
|
ch <- u.exportUDM(d)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// exportUDM exports UniFi Dream Machine (and Pro) Data
|
// exportUDM exports UniFi Dream Machine (and Pro) Data
|
||||||
|
|
|
||||||
|
|
@ -109,11 +109,10 @@ func descUSG(ns string) *usg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *unifiCollector) exportUSGs(usgs []*unifi.USG) (e []*metricExports) {
|
func (u *unifiCollector) exportUSGs(usgs []*unifi.USG, ch chan []*metricExports) {
|
||||||
for _, sg := range usgs {
|
for _, sg := range usgs {
|
||||||
e = append(e, u.exportUSG(sg)...)
|
ch <- u.exportUSG(sg)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// exportUSG Exports Security Gateway Data
|
// exportUSG Exports Security Gateway Data
|
||||||
|
|
@ -122,6 +121,7 @@ func (u *unifiCollector) exportUSG(s *unifi.USG) []*metricExports {
|
||||||
labels := []string{s.SiteName, s.Mac, s.Model, s.Name, s.Serial, s.SiteID,
|
labels := []string{s.SiteName, s.Mac, s.Model, s.Name, s.Serial, s.SiteID,
|
||||||
s.Type, s.Version, s.DeviceID, s.IP}
|
s.Type, s.Version, s.DeviceID, s.IP}
|
||||||
labelWan := append([]string{"all"}, labels...)
|
labelWan := append([]string{"all"}, labels...)
|
||||||
|
// r.wait.Add(1) // closed by channel receiver.
|
||||||
|
|
||||||
// Gateway System Data.
|
// Gateway System Data.
|
||||||
return append([]*metricExports{
|
return append([]*metricExports{
|
||||||
|
|
|
||||||
|
|
@ -136,11 +136,10 @@ func descUSW(ns string) *usw {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *unifiCollector) exportUSWs(usws []*unifi.USW) (e []*metricExports) {
|
func (u *unifiCollector) exportUSWs(usws []*unifi.USW, ch chan []*metricExports) {
|
||||||
for _, sw := range usws {
|
for _, sw := range usws {
|
||||||
e = append(e, u.exportUSW(sw)...)
|
ch <- u.exportUSW(sw)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// exportUSW exports Network Switch Data
|
// exportUSW exports Network Switch Data
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue