2022-02-02 12:11:41 +00:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"net/url"
|
2022-08-21 20:51:13 +00:00
|
|
|
"os"
|
2022-02-02 12:11:41 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discovery/consul"
|
2022-04-13 08:50:26 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discovery/dns"
|
2022-02-11 14:17:00 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2022-02-02 12:11:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains list of supported configuration settings
|
|
|
|
// for Notifier
|
|
|
|
type Config struct {
|
|
|
|
// Scheme defines the HTTP scheme for Notifier address
|
|
|
|
Scheme string `yaml:"scheme,omitempty"`
|
|
|
|
// PathPrefix is added to URL path before adding alertManagerPath value
|
|
|
|
PathPrefix string `yaml:"path_prefix,omitempty"`
|
|
|
|
|
|
|
|
// ConsulSDConfigs contains list of settings for service discovery via Consul
|
|
|
|
// see https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config
|
|
|
|
ConsulSDConfigs []consul.SDConfig `yaml:"consul_sd_configs,omitempty"`
|
2022-04-13 08:50:26 +00:00
|
|
|
// DNSSDConfigs ontains list of settings for service discovery via DNS.
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#dns_sd_config
|
|
|
|
DNSSDConfigs []dns.SDConfig `yaml:"dns_sd_configs,omitempty"`
|
|
|
|
|
2022-02-02 12:11:41 +00:00
|
|
|
// StaticConfigs contains list of static targets
|
|
|
|
StaticConfigs []StaticConfig `yaml:"static_configs,omitempty"`
|
|
|
|
|
|
|
|
// HTTPClientConfig contains HTTP configuration for Notifier clients
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2022-04-09 06:21:16 +00:00
|
|
|
// RelabelConfigs contains list of relabeling rules for entities discovered via SD
|
2022-02-02 12:11:41 +00:00
|
|
|
RelabelConfigs []promrelabel.RelabelConfig `yaml:"relabel_configs,omitempty"`
|
2022-04-09 06:21:16 +00:00
|
|
|
// AlertRelabelConfigs contains list of relabeling rules alert labels
|
|
|
|
AlertRelabelConfigs []promrelabel.RelabelConfig `yaml:"alert_relabel_configs,omitempty"`
|
2022-02-02 12:11:41 +00:00
|
|
|
// The timeout used when sending alerts.
|
2022-04-16 11:25:54 +00:00
|
|
|
Timeout *promutils.Duration `yaml:"timeout,omitempty"`
|
2022-02-02 12:11:41 +00:00
|
|
|
|
|
|
|
// Checksum stores the hash of yaml definition for the config.
|
|
|
|
// May be used to detect any changes to the config file.
|
|
|
|
Checksum string
|
|
|
|
|
|
|
|
// Catches all undefined fields and must be empty after parsing.
|
|
|
|
XXX map[string]interface{} `yaml:",inline"`
|
|
|
|
|
|
|
|
// This is set to the directory from where the config has been loaded.
|
|
|
|
baseDir string
|
|
|
|
|
|
|
|
// stores already parsed RelabelConfigs object
|
|
|
|
parsedRelabelConfigs *promrelabel.ParsedConfigs
|
2022-04-09 06:21:16 +00:00
|
|
|
// stores already parsed AlertRelabelConfigs object
|
|
|
|
parsedAlertRelabelConfigs *promrelabel.ParsedConfigs
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StaticConfig contains list of static targets in the following form:
|
2022-07-11 16:21:59 +00:00
|
|
|
//
|
|
|
|
// targets:
|
|
|
|
// [ - '<host>' ]
|
2022-02-02 12:11:41 +00:00
|
|
|
type StaticConfig struct {
|
|
|
|
Targets []string `yaml:"targets"`
|
2022-09-30 15:10:17 +00:00
|
|
|
// HTTPClientConfig contains HTTP configuration for the Targets
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
type config Config
|
|
|
|
if err := unmarshal((*config)(cfg)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cfg.Scheme == "" {
|
|
|
|
cfg.Scheme = "http"
|
|
|
|
}
|
|
|
|
if cfg.Timeout.Duration() == 0 {
|
2022-02-11 14:17:00 +00:00
|
|
|
cfg.Timeout = promutils.NewDuration(time.Second * 10)
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
rCfg, err := promrelabel.ParseRelabelConfigs(cfg.RelabelConfigs)
|
2022-02-02 12:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse relabeling config: %w", err)
|
|
|
|
}
|
|
|
|
cfg.parsedRelabelConfigs = rCfg
|
2022-12-10 10:09:21 +00:00
|
|
|
arCfg, err := promrelabel.ParseRelabelConfigs(cfg.AlertRelabelConfigs)
|
2022-04-09 06:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse alert relabeling config: %w", err)
|
|
|
|
}
|
|
|
|
cfg.parsedAlertRelabelConfigs = arCfg
|
2022-02-02 12:11:41 +00:00
|
|
|
|
|
|
|
b, err := yaml.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal configuration for checksum: %w", err)
|
|
|
|
}
|
|
|
|
h := md5.New()
|
|
|
|
h.Write(b)
|
|
|
|
cfg.Checksum = fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseConfig(path string) (*Config, error) {
|
2022-08-21 20:51:13 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2022-02-02 12:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading config file: %w", err)
|
|
|
|
}
|
|
|
|
var cfg *Config
|
|
|
|
err = yaml.Unmarshal(data, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(cfg.XXX) > 0 {
|
|
|
|
var keys []string
|
|
|
|
for k := range cfg.XXX {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown fields in %s", strings.Join(keys, ", "))
|
|
|
|
}
|
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot obtain abs path for %q: %w", path, err)
|
|
|
|
}
|
|
|
|
cfg.baseDir = filepath.Dir(absPath)
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func parseLabels(target string, metaLabels *promutils.Labels, cfg *Config) (string, *promutils.Labels, error) {
|
2022-02-02 12:11:41 +00:00
|
|
|
labels := mergeLabels(target, metaLabels, cfg)
|
2022-11-30 05:22:12 +00:00
|
|
|
labels.Labels = cfg.parsedRelabelConfigs.Apply(labels.Labels, 0)
|
|
|
|
labels.RemoveMetaLabels()
|
|
|
|
labels.Sort()
|
2022-02-02 12:11:41 +00:00
|
|
|
// Remove references to already deleted labels, so GC could clean strings for label name and label value past len(labels).
|
|
|
|
// This should reduce memory usage when relabeling creates big number of temporary labels with long names and/or values.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/825 for details.
|
2022-11-30 05:22:12 +00:00
|
|
|
labels = labels.Clone()
|
2022-02-02 12:11:41 +00:00
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
if labels.Len() == 0 {
|
2022-02-02 12:11:41 +00:00
|
|
|
return "", nil, nil
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
schemeRelabeled := labels.Get("__scheme__")
|
2022-02-02 12:11:41 +00:00
|
|
|
if len(schemeRelabeled) == 0 {
|
|
|
|
schemeRelabeled = "http"
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
addressRelabeled := labels.Get("__address__")
|
2022-02-02 12:11:41 +00:00
|
|
|
if len(addressRelabeled) == 0 {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
if strings.Contains(addressRelabeled, "/") {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
addressRelabeled = addMissingPort(schemeRelabeled, addressRelabeled)
|
2022-11-30 05:22:12 +00:00
|
|
|
alertsPathRelabeled := labels.Get("__alerts_path__")
|
2022-02-02 12:11:41 +00:00
|
|
|
if !strings.HasPrefix(alertsPathRelabeled, "/") {
|
|
|
|
alertsPathRelabeled = "/" + alertsPathRelabeled
|
|
|
|
}
|
|
|
|
u := fmt.Sprintf("%s://%s%s", schemeRelabeled, addressRelabeled, alertsPathRelabeled)
|
|
|
|
if _, err := url.Parse(u); err != nil {
|
|
|
|
return "", nil, fmt.Errorf("invalid url %q for scheme=%q (%q), target=%q, metrics_path=%q (%q): %w",
|
|
|
|
u, cfg.Scheme, schemeRelabeled, target, addressRelabeled, alertsPathRelabeled, err)
|
|
|
|
}
|
|
|
|
return u, labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMissingPort(scheme, target string) string {
|
|
|
|
if strings.Contains(target, ":") {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
if scheme == "https" {
|
|
|
|
target += ":443"
|
|
|
|
} else {
|
|
|
|
target += ":80"
|
|
|
|
}
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func mergeLabels(target string, metaLabels *promutils.Labels, cfg *Config) *promutils.Labels {
|
2022-02-02 12:11:41 +00:00
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(3 + metaLabels.Len())
|
|
|
|
m.Add("__address__", target)
|
|
|
|
m.Add("__scheme__", cfg.Scheme)
|
|
|
|
m.Add("__alerts_path__", path.Join("/", cfg.PathPrefix, alertManagerPath))
|
|
|
|
m.AddFrom(metaLabels)
|
|
|
|
return m
|
2022-02-02 12:11:41 +00:00
|
|
|
}
|