Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PagerDuty source field (PD-CEF) #3100

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type PagerdutyConfig struct {
Details map[string]string `yaml:"details,omitempty" json:"details,omitempty"`
Images []PagerdutyImage `yaml:"images,omitempty" json:"images,omitempty"`
Links []PagerdutyLink `yaml:"links,omitempty" json:"links,omitempty"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Severity string `yaml:"severity,omitempty" json:"severity,omitempty"`
Class string `yaml:"class,omitempty" json:"class,omitempty"`
Component string `yaml:"component,omitempty" json:"component,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ service_key: <tmpl_secret>
# Severity of the incident.
[ severity: <tmpl_string> | default = 'error' ]

# Unique location of the affected system.
[ source: <tmpl_string> | default = client ]

# A set of arbitrary key/value pairs that provide further detail
# about the incident.
[ details: { <string>: <tmpl_string>, ... } | default = {
Expand Down
7 changes: 6 additions & 1 deletion notify/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func (n *Notifier) notifyV2(
level.Debug(n.logger).Log("msg", "Truncated summary", "summary", summary, "key", key)
}

payloadSource := n.conf.Client
if n.conf.Source != "" {
payloadSource = n.conf.Source
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value could be handled in config/notifiers.go when unmarshaling the YAML data. This would be more explicit when users look at the /api/v2/status endpoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍


msg := &pagerDutyMessage{
Client: tmpl(n.conf.Client),
ClientURL: tmpl(n.conf.ClientURL),
Expand All @@ -219,7 +224,7 @@ func (n *Notifier) notifyV2(
Links: make([]pagerDutyLink, 0, len(n.conf.Links)),
Payload: &pagerDutyPayload{
Summary: summary,
Source: tmpl(n.conf.Client),
Source: tmpl(payloadSource),
Severity: tmpl(n.conf.Severity),
CustomDetails: details,
Class: tmpl(n.conf.Class),
Expand Down
60 changes: 60 additions & 0 deletions notify/pagerduty/pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,63 @@ func TestPagerDutyEmptySrcHref(t *testing.T) {
}...)
require.NoError(t, err)
}

func TestPagerDutySourceField(t *testing.T) {
for _, tc := range []struct {
title string
cfg *config.PagerdutyConfig

expectedSource string
}{
{
title: "check source field is backward compatible",
cfg: &config.PagerdutyConfig{
RoutingKey: config.Secret("01234567890123456789012345678901"),
Client: "alert-manager-client",
},
expectedSource: "alert-manager-client",
},
{
title: "check source field is set",
cfg: &config.PagerdutyConfig{
RoutingKey: config.Secret("01234567890123456789012345678901"),
Client: "alert-manager-client",
Source: "alert-manager-source",
},
expectedSource: "alert-manager-source",
},
} {
t.Run(tc.title, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
out := make(map[string]interface{})
err := dec.Decode(&out)
if err != nil {
panic(err)
}

payloadJSON, err := json.Marshal(out["payload"])
require.NoError(t, err)
payload := pagerDutyPayload{}
err = json.Unmarshal(payloadJSON, &payload)
require.NoError(t, err)

require.Equal(t, tc.expectedSource, payload.Source)
}))
defer srv.Close()
u, _ := url.Parse(srv.URL)

tc.cfg.URL = &config.URL{URL: u}
tc.cfg.HTTPConfig = &commoncfg.HTTPClientConfig{}
pd, err := New(tc.cfg, test.CreateTmpl(t), log.NewNopLogger())
require.NoError(t, err)

ctx := context.Background()
ctx = notify.WithGroupKey(ctx, "1")

retry, err := pd.Notify(ctx)
require.NoError(t, err)
require.Equal(t, false, retry)
})
}
}