Skip to content

Commit cc743ef

Browse files
author
Hamed Maleki
committed
Add parallel mechanism to probe.go
1 parent aeea6c3 commit cc743ef

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

pkg/probe/probe.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"net/http"
3838
"net/url"
3939
"strings"
40+
"sync"
4041

4142
"github.com/prometheus/client_golang/prometheus"
4243

@@ -48,7 +49,10 @@ import (
4849
type Collector struct {
4950
metrics []prometheus.Metric
5051
}
51-
52+
type probeResult struct {
53+
metrics []prometheus.Metric
54+
ok bool
55+
}
5256
type TargetMetadata struct {
5357
VersionMajor int
5458
VersionMinor int
@@ -124,9 +128,9 @@ func (p *Collector) Probe(ctx context.Context, target map[string]string, hc *htt
124128
includedProbes := savedConfig.AuthKeys[config.Target(u.String())].Probes.Include
125129
excludedProbes := savedConfig.AuthKeys[config.Target(u.String())].Probes.Exclude
126130

127-
// TODO: Make parallel
128-
success := true
129-
for _, aProbe := range []probeDetailedFunc{
131+
var wg sync.WaitGroup
132+
var mu sync.Mutex
133+
allProbes := []probeDetailedFunc{
130134
// Always keep probeSystemTime on top of the list to have the probe processed first.
131135
// Therefore time returned is more accurate when integrated in Prometheus because
132136
// timestamp for the metrics probe, in Prometheus, is obtained from the query time, not the reply time.
@@ -171,7 +175,10 @@ func (p *Collector) Probe(ctx context.Context, target map[string]string, hc *htt
171175
{"Wifi/ManagedAP", probeWifiManagedAP},
172176
{"Switch/ManagedSwitch", probeManagedSwitch},
173177
{"OSPF/Neighbors", probeOSPFNeighbors},
174-
} {
178+
}
179+
success := true
180+
results := make(chan probeResult, len(allProbes))
181+
for _, aProbe := range allProbes {
175182
wanted := false
176183

177184
if len(includedProbes) == 0 {
@@ -197,14 +204,32 @@ func (p *Collector) Probe(ctx context.Context, target map[string]string, hc *htt
197204
if !wanted {
198205
continue
199206
}
207+
wg.Add(1)
208+
go func(probe probeDetailedFunc) {
209+
defer wg.Done()
210+
m, ok := aProbe.function(c, meta)
211+
results <- probeResult{
212+
metrics: m,
213+
ok: ok,
214+
}
215+
}(aProbe)
200216

201-
m, ok := aProbe.function(c, meta)
202-
if !ok {
217+
}
218+
go func() {
219+
wg.Wait()
220+
close(results)
221+
}()
222+
for res := range results {
223+
mu.Lock()
224+
p.metrics = append(p.metrics, res.metrics...)
225+
mu.Unlock()
226+
227+
if !res.ok {
228+
mu.Lock()
203229
success = false
230+
mu.Unlock()
204231
}
205-
p.metrics = append(p.metrics, m...)
206232
}
207-
208233
return success, nil
209234
}
210235

0 commit comments

Comments
 (0)