Skip to content

Commit

Permalink
Add PagerDuty source field (PD-CEF) (#3106)
Browse files Browse the repository at this point in the history
* Add new source field to PD config
* Update documentation

Signed-off-by: Oktarian Tilney-Bassett <oktariantilneybassett@improbable.io>
  • Loading branch information
Oktarian T-B committed Oct 13, 2022
1 parent 78b5a27 commit d034f11
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/notifiers.go
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
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
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
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

0 comments on commit d034f11

Please sign in to comment.