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) #3106

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 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 Expand Up @@ -249,6 +250,9 @@ func (c *PagerdutyConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
if c.Details == nil {
c.Details = make(map[string]string)
}
if c.Source == "" {
c.Source = c.Client
}
for k, v := range DefaultPagerdutyDetails {
if _, ok := c.Details[k]; !ok {
c.Details[k] = v
Expand Down
36 changes: 36 additions & 0 deletions config/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -146,6 +148,40 @@ details:
}
}

func TestPagerDutySource(t *testing.T) {
for _, tc := range []struct {
title string
in string

expectedSource string
}{
{
title: "check source field is backward compatible",
in: `
routing_key: 'xyz'
client: 'alert-manager-client'
`,
expectedSource: "alert-manager-client",
},
{
title: "check source field is set",
in: `
routing_key: 'xyz'
client: 'alert-manager-client'
source: 'alert-manager-source'
`,
expectedSource: "alert-manager-source",
},
} {
t.Run(tc.title, func(t *testing.T) {
var cfg PagerdutyConfig
err := yaml.UnmarshalStrict([]byte(tc.in), &cfg)
require.NoError(t, err)
require.Equal(t, tc.expectedSource, cfg.Source)
})
}
}

func TestWebhookURLIsPresent(t *testing.T) {
in := `{}`
var cfg WebhookConfig
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
2 changes: 1 addition & 1 deletion notify/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (n *Notifier) notifyV2(
Links: make([]pagerDutyLink, 0, len(n.conf.Links)),
Payload: &pagerDutyPayload{
Summary: summary,
Source: tmpl(n.conf.Client),
Source: tmpl(n.conf.Source),
Severity: tmpl(n.conf.Severity),
CustomDetails: details,
Class: tmpl(n.conf.Class),
Expand Down