lib/promscrape/discovery/gce: discover per-zone instances for gce_sd_config in parallel. This should reduce discovery latency

This commit is contained in:
Aliaksandr Valialkin 2020-05-06 15:00:02 +03:00
parent 8665c2edb1
commit 9f39e618ed
2 changed files with 32 additions and 19 deletions

View File

@ -53,9 +53,6 @@ func GetLabels(sdc *SDConfig) ([]map[string]string, error) {
if err != nil {
return nil, fmt.Errorf("cannot get API config: %s", err)
}
ms, err := getInstancesLabels(cfg)
if err != nil {
return nil, fmt.Errorf("error when fetching instances data from GCE: %s", err)
}
ms := getInstancesLabels(cfg)
return ms, nil
}

View File

@ -6,32 +6,48 @@ import (
"net/http"
"strings"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
)
// getInstancesLabels returns labels for gce instances obtained from the given cfg
func getInstancesLabels(cfg *apiConfig) ([]map[string]string, error) {
insts, err := getInstances(cfg)
if err != nil {
return nil, err
}
func getInstancesLabels(cfg *apiConfig) []map[string]string {
insts := getInstances(cfg)
var ms []map[string]string
for _, inst := range insts {
ms = inst.appendTargetLabels(ms, cfg.project, cfg.tagSeparator, cfg.port)
}
return ms, nil
return ms
}
func getInstances(cfg *apiConfig) ([]Instance, error) {
var insts []Instance
for _, zone := range cfg.zones {
zoneInsts, err := getInstancesForProjectAndZone(cfg.client, cfg.project, zone, cfg.filter)
if err != nil {
return nil, err
}
insts = append(insts, zoneInsts...)
func getInstances(cfg *apiConfig) []Instance {
// Collect instances for each zone in parallel
type result struct {
zone string
insts []Instance
err error
}
return insts, nil
ch := make(chan result, len(cfg.zones))
for _, zone := range cfg.zones {
go func(zone string) {
insts, err := getInstancesForProjectAndZone(cfg.client, cfg.project, zone, cfg.filter)
ch <- result{
zone: zone,
insts: insts,
err: err,
}
}(zone)
}
var insts []Instance
for range cfg.zones {
r := <-ch
if r.err != nil {
logger.Errorf("cannot collect instances from zone %q: %s", r.zone, r.err)
continue
}
insts = append(insts, r.insts...)
}
return insts
}
func getInstancesForProjectAndZone(client *http.Client, project, zone, filter string) ([]Instance, error) {