2020-04-24 16:50:21 +02:00
|
|
|
package gce
|
|
|
|
|
|
|
|
import (
|
2021-06-25 11:10:20 +02:00
|
|
|
"flag"
|
2020-04-24 16:50:21 +02:00
|
|
|
"fmt"
|
2021-06-25 11:10:20 +02:00
|
|
|
"time"
|
2022-11-30 06:22:12 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-04-24 16:50:21 +02:00
|
|
|
)
|
|
|
|
|
2021-06-25 11:10:20 +02:00
|
|
|
// SDCheckInterval defines interval for targets refresh.
|
|
|
|
var SDCheckInterval = flag.Duration("promscrape.gceSDCheckInterval", time.Minute, "Interval for checking for changes in gce. "+
|
|
|
|
"This works only if gce_sd_configs is configured in '-promscrape.config' file. "+
|
2022-08-15 00:40:20 +02:00
|
|
|
"See https://docs.victoriametrics.com/sd_configs.html#gce_sd_configs for details")
|
2021-06-25 11:10:20 +02:00
|
|
|
|
2020-04-24 16:50:21 +02:00
|
|
|
// SDConfig represents service discovery config for gce.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#gce_sd_config
|
|
|
|
type SDConfig struct {
|
2020-04-27 10:44:28 +02:00
|
|
|
Project string `yaml:"project"`
|
|
|
|
Zone ZoneYAML `yaml:"zone"`
|
2020-11-13 15:17:03 +01:00
|
|
|
Filter string `yaml:"filter,omitempty"`
|
2020-04-24 16:50:21 +02:00
|
|
|
// RefreshInterval time.Duration `yaml:"refresh_interval"`
|
|
|
|
// refresh_interval is obtained from `-promscrape.gceSDCheckInterval` command-line option.
|
2020-11-13 15:17:03 +01:00
|
|
|
Port *int `yaml:"port,omitempty"`
|
|
|
|
TagSeparator *string `yaml:"tag_separator,omitempty"`
|
2020-04-24 16:50:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 10:44:28 +02:00
|
|
|
// ZoneYAML holds info about zones.
|
|
|
|
type ZoneYAML struct {
|
2022-05-06 23:02:54 +02:00
|
|
|
Zones []string
|
2020-04-27 10:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler
|
|
|
|
func (z *ZoneYAML) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := unmarshal(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var zones []string
|
2020-04-27 18:28:10 +02:00
|
|
|
switch t := v.(type) {
|
2020-04-27 10:44:28 +02:00
|
|
|
case string:
|
2020-04-27 18:28:10 +02:00
|
|
|
zones = []string{t}
|
2020-04-27 10:44:28 +02:00
|
|
|
case []interface{}:
|
2020-04-27 18:28:10 +02:00
|
|
|
for _, vv := range t {
|
2020-04-27 10:44:28 +02:00
|
|
|
zone, ok := vv.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected zone type detected: %T; contents: %#v", vv, vv)
|
|
|
|
}
|
|
|
|
zones = append(zones, zone)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected type unmarshaled for ZoneYAML: %T; contents: %#v", v, v)
|
|
|
|
}
|
2022-05-06 23:02:54 +02:00
|
|
|
z.Zones = zones
|
2020-04-27 10:44:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:39:23 +01:00
|
|
|
// MarshalYAML implements yaml.Marshaler
|
|
|
|
func (z ZoneYAML) MarshalYAML() (interface{}, error) {
|
2022-05-06 23:02:54 +02:00
|
|
|
return z.Zones, nil
|
2022-02-11 23:39:23 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 16:50:21 +02:00
|
|
|
// GetLabels returns gce labels according to sdc.
|
2023-09-01 09:34:16 +02:00
|
|
|
func (sdc *SDConfig) GetLabels(_ string) ([]*promutils.Labels, error) {
|
2020-04-24 16:50:21 +02:00
|
|
|
cfg, err := getAPIConfig(sdc)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
2020-04-24 16:50:21 +02:00
|
|
|
}
|
2020-05-06 14:00:02 +02:00
|
|
|
ms := getInstancesLabels(cfg)
|
2020-04-24 16:50:21 +02:00
|
|
|
return ms, nil
|
|
|
|
}
|
2021-03-01 13:13:56 +01:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
2023-07-27 23:47:53 +02:00
|
|
|
v := configMap.Delete(sdc)
|
|
|
|
if v != nil {
|
|
|
|
cfg := v.(*apiConfig)
|
|
|
|
cfg.client.CloseIdleConnections()
|
|
|
|
}
|
2021-03-01 13:13:56 +01:00
|
|
|
}
|