diff --git a/config/notifiers.go b/config/notifiers.go index f8896436..fc6356c0 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -22,6 +22,7 @@ import ( "github.com/pkg/errors" commoncfg "github.com/prometheus/common/config" + "github.com/prometheus/common/model" ) var ( @@ -456,18 +457,18 @@ type OpsGenieConfig struct { HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"` - APIKey Secret `yaml:"api_key,omitempty" json:"api_key,omitempty"` - APIURL *URL `yaml:"api_url,omitempty" json:"api_url,omitempty"` - Message string `yaml:"message,omitempty" json:"message,omitempty"` - Description string `yaml:"description,omitempty" json:"description,omitempty"` - Source string `yaml:"source,omitempty" json:"source,omitempty"` - AllLabelsAsDetails bool `yaml:"all_labels_as_details,omitempty" json:"all_labels_as_details,omitempty"` - LabelsAsDetails []string `yaml:"labels_as_details,omitempty" json:"labels_as_details,omitempty"` - Details map[string]string `yaml:"details,omitempty" json:"details,omitempty"` - Responders []OpsGenieConfigResponder `yaml:"responders,omitempty" json:"responders,omitempty"` - Tags string `yaml:"tags,omitempty" json:"tags,omitempty"` - Note string `yaml:"note,omitempty" json:"note,omitempty"` - Priority string `yaml:"priority,omitempty" json:"priority,omitempty"` + APIKey Secret `yaml:"api_key,omitempty" json:"api_key,omitempty"` + APIURL *URL `yaml:"api_url,omitempty" json:"api_url,omitempty"` + Message string `yaml:"message,omitempty" json:"message,omitempty"` + Description string `yaml:"description,omitempty" json:"description,omitempty"` + Source string `yaml:"source,omitempty" json:"source,omitempty"` + Details map[string]string `yaml:"details,omitempty" json:"details,omitempty"` + DetailsLabels []string `yaml:"details_labels,omitempty" json:"details_labels,omitempty"` + DetailsLabelsAll bool `yaml:"-" json:"-"` + Responders []OpsGenieConfigResponder `yaml:"responders,omitempty" json:"responders,omitempty"` + Tags string `yaml:"tags,omitempty" json:"tags,omitempty"` + Note string `yaml:"note,omitempty" json:"note,omitempty"` + Priority string `yaml:"priority,omitempty" json:"priority,omitempty"` } const opsgenieValidTypesRe = `^(team|user|escalation|schedule)$` @@ -493,6 +494,21 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error } } + if c.DetailsLabels != nil && c.Details != nil { + return errors.Errorf("OpsGenieConfig can only contain details or details_labels, but both fields were provided") + } + + for _, l := range c.DetailsLabels { + if l == "..." { + c.DetailsLabelsAll = true + } else { + labelName := model.LabelName(l) + if !labelName.IsValid() { + return errors.Errorf("invalid label name %q in details_labels list", l) + } + } + } + return nil } diff --git a/notify/opsgenie/opsgenie.go b/notify/opsgenie/opsgenie.go index dcb5d1e9..6584bf96 100644 --- a/notify/opsgenie/opsgenie.go +++ b/notify/opsgenie/opsgenie.go @@ -122,18 +122,20 @@ func (n *Notifier) createRequest(ctx context.Context, as ...*types.Alert) (*http var details map[string]string - if n.conf.AllLabelsAsDetails { + if n.conf.DetailsLabelsAll { details = make(map[string]string, len(data.CommonLabels)) for k, v := range data.CommonLabels { details[k] = v } - } else if len(n.conf.LabelsAsDetails) > 0 { - details = make(map[string]string, len(n.conf.LabelsAsDetails)) - for _, k := range n.conf.LabelsAsDetails { - if v, ok := data.CommonLabels[k]; ok { - details[k] = v + } else if len(n.conf.DetailsLabels) > 0 { + details = make(map[string]string, len(n.conf.DetailsLabels)) + for _, k := range n.conf.DetailsLabels { + label := string(k) + + if v, ok := data.CommonLabels[label]; ok { + details[label] = v } else { - details[k] = "N/A" + details[label] = "N/A" } } } else {