Skip to content

Commit 7c7bb5a

Browse files
committed
add notify info for pagerduty
1 parent 7a8e6f9 commit 7c7bb5a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

notify.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Config struct {
2323
Platform Platform
2424
Token string
2525
Channel string
26+
Source string
27+
Severity string
2628
}
2729

2830
func NewNotify(config *Config) *Notify {
@@ -66,6 +68,8 @@ func (n *Notify) sendSlackNotify(msg string) error {
6668
func (n *Notify) sendPagerdutyNotify(msg string) error {
6769
app := pagerduty.New(pagerduty.Options{
6870
Token: n.config.Token,
71+
Source: n.config.Source,
72+
Severity: n.config.Severity,
6973
})
7074
err := app.Send(msg)
7175
return err

notify_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ func TestNotify_Send(t *testing.T) {
3636
args{msg: "test case"},
3737
},
3838
{
39-
"test pagerduty notify",
39+
"test pagerduty severity is null",
4040
fields{config: &Config{
4141
Platform: Platform("pagerduty"),
4242
Token: os.Getenv("PAGERDUTY_TOKEN"),
43+
Source: "api-test",
44+
Severity: "",
4345
}},
4446
args{msg: "test pagerduty"},
4547
},
48+
{
49+
"test pagerduty severity is error",
50+
fields{config: &Config{
51+
Platform: Platform("pagerduty"),
52+
Token: os.Getenv("PAGERDUTY_TOKEN"),
53+
Source: "api-test",
54+
Severity: "error",
55+
}},
56+
args{msg: "test pagerduty is error"},
57+
},
4658
}
4759
for _, tt := range tests {
4860
t.Run(tt.name, func(t *testing.T) {

pagerduty/notify.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const (
1515
)
1616

1717
type Options struct {
18-
Token string `json:"token"`
19-
Text string `json:"text"`
18+
Token string `json:"token"`
19+
Source string `json:"source"`
20+
Severity string `json:"severity"`
21+
Text string `json:"text"`
2022
}
2123

2224
type pagerduty struct {
@@ -59,8 +61,8 @@ func (c *client) Send(message string) error {
5961
pdOpt := &pagerduty{
6062
Payload: payload{
6163
Summary: message,
62-
Source: "monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003",
63-
Severity: "info",
64+
Source: c.opt.Source,
65+
Severity: c.opt.Severity,
6466
},
6567
RoutingKey: c.opt.Token,
6668
EventAction: "trigger",
@@ -93,8 +95,12 @@ func (c *client) Send(message string) error {
9395
}
9496

9597
func (c *client) check(msg string) error {
96-
if c.opt.Token == "" {
97-
return errors.New("missing auth token")
98+
if c.opt.Token == "" || c.opt.Source == "" {
99+
return errors.New("missing config")
100+
}
101+
102+
if c.opt.Severity == "" {
103+
c.opt.Severity = "critical"
98104
}
99105

100106
if msg == "" {

0 commit comments

Comments
 (0)