shave a few more nanoseconds

This commit is contained in:
davidnewhall2 2019-11-25 03:24:36 -08:00
parent 184a20c019
commit 6622050a34
7 changed files with 26 additions and 29 deletions

View File

@ -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 {
e = append(e, u.exportClient(c)...)
ch <- u.exportClient(c)
}
return
}
// CollectClient exports Clients' Data

View File

@ -54,7 +54,7 @@ type Report struct {
Elapsed time.Duration
Start time.Time
ch chan []*metricExports
wait sync.WaitGroup
wg sync.WaitGroup
}
// 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
r := &Report{Start: time.Now(), ch: make(chan []*metricExports)}
defer func() {
r.wait.Wait()
r.wg.Wait()
close(r.ch)
}()
@ -116,22 +116,23 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) {
go u.exportMetrics(ch, r)
r.wait.Add(2)
go func() { r.ch <- u.exportClients(r.Metrics.Clients) }()
go func() { r.ch <- u.exportSites(r.Metrics.Sites) }()
r.wg.Add(len(r.Metrics.Clients) + len(r.Metrics.Sites))
go u.exportClients(r.Metrics.Clients, r.ch)
go u.exportSites(r.Metrics.Sites, r.ch)
if r.Metrics.Devices == nil {
return
}
r.wait.Add(4)
go func() { r.ch <- u.exportUAPs(r.Metrics.Devices.UAPs) }()
go func() { r.ch <- u.exportUSGs(r.Metrics.Devices.USGs) }()
go func() { r.ch <- u.exportUSWs(r.Metrics.Devices.USWs) }()
go func() { r.ch <- u.exportUDMs(r.Metrics.Devices.UDMs) }()
r.wg.Add(len(r.Metrics.Devices.UAPs) + len(r.Metrics.Devices.USGs) + len(r.Metrics.Devices.USWs) + len(r.Metrics.Devices.UDMs))
go u.exportUAPs(r.Metrics.Devices.UAPs, r.ch)
go u.exportUSGs(r.Metrics.Devices.USGs, r.ch)
go u.exportUSWs(r.Metrics.Devices.USWs, r.ch)
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) {
descs := make(map[*prometheus.Desc]bool) // used as a counter
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 {

View File

@ -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 {
e = append(e, u.exportSite(s)...)
ch <- u.exportSite(s)
}
return
}

View File

@ -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 {
e = append(e, u.exportUAP(a)...)
ch <- u.exportUAP(a)
}
return
}
// exportUAP exports Access Point Data

View File

@ -11,11 +11,10 @@ func descUDM(ns string) *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 {
e = append(e, u.exportUDM(d)...)
ch <- u.exportUDM(d)
}
return
}
// exportUDM exports UniFi Dream Machine (and Pro) Data

View File

@ -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 {
e = append(e, u.exportUSG(sg)...)
ch <- u.exportUSG(sg)
}
return
}
// 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,
s.Type, s.Version, s.DeviceID, s.IP}
labelWan := append([]string{"all"}, labels...)
// r.wait.Add(1) // closed by channel receiver.
// Gateway System Data.
return append([]*metricExports{

View File

@ -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 {
e = append(e, u.exportUSW(sw)...)
ch <- u.exportUSW(sw)
}
return
}
// exportUSW exports Network Switch Data