Skip to content

Commit 83678ae

Browse files
committed
Add logging for recipient to contacts expansion
The most relevant case where this is interesting is when a schedule expands to no contacts at the time of a notification.
1 parent 27c1180 commit 83678ae

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

internal/incident/incident.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,11 +648,20 @@ func (i *Incident) getRecipientsChannel(t time.Time) rule.ContactChannels {
648648
}
649649

650650
if i.IsNotifiable(state.Role) {
651-
for _, contact := range r.GetContactsAt(t) {
652-
if contactChs[contact] == nil {
653-
contactChs[contact] = make(map[int64]bool)
654-
contactChs[contact][contact.DefaultChannelID] = true
651+
contacts := r.GetContactsAt(t)
652+
if len(contacts) > 0 {
653+
i.logger.Debugw("Expanded recipient to contacts",
654+
zap.Object("recipient", r),
655+
zap.Objects("contacts", contacts))
656+
657+
for _, contact := range contacts {
658+
if contactChs[contact] == nil {
659+
contactChs[contact] = make(map[int64]bool)
660+
contactChs[contact][contact.DefaultChannelID] = true
661+
}
655662
}
663+
} else {
664+
i.logger.Warnw("Recipient expanded to no contacts", zap.Object("recipient", r))
656665
}
657666
}
658667
}

internal/recipient/contact.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package recipient
22

33
import (
44
"database/sql"
5+
"go.uber.org/zap/zapcore"
56
"time"
67
)
78

@@ -21,6 +22,14 @@ func (c *Contact) GetContactsAt(t time.Time) []*Contact {
2122
return []*Contact{c}
2223
}
2324

25+
// MarshalLogObject implements the zapcore.ObjectMarshaler interface.
26+
func (c *Contact) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
27+
// Use contact_id as key so that the type is explicit if logged as the Recipient interface.
28+
encoder.AddInt64("contact_id", c.ID)
29+
encoder.AddString("name", c.FullName)
30+
return nil
31+
}
32+
2433
var _ Recipient = (*Contact)(nil)
2534

2635
type Address struct {

internal/recipient/group.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package recipient
22

3-
import "time"
3+
import (
4+
"go.uber.org/zap/zapcore"
5+
"time"
6+
)
47

58
type Group struct {
69
ID int64 `db:"id"`
@@ -21,4 +24,12 @@ func (g *Group) String() string {
2124
return g.Name
2225
}
2326

27+
// MarshalLogObject implements the zapcore.ObjectMarshaler interface.
28+
func (g *Group) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
29+
// Use contact_id as key so that the type is explicit if logged as the Recipient interface.
30+
encoder.AddInt64("group_id", g.ID)
31+
encoder.AddString("name", g.Name)
32+
return nil
33+
}
34+
2435
var _ Recipient = (*Group)(nil)

internal/recipient/recipient.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type Recipient interface {
1212
fmt.Stringer
13+
zapcore.ObjectMarshaler
1314

1415
GetContactsAt(t time.Time) []*Contact
1516
}

internal/recipient/schedule.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func (s *Schedule) RefreshRotations() {
2323
s.rotationResolver.update(s.Rotations)
2424
}
2525

26+
// MarshalLogObject implements the zapcore.ObjectMarshaler interface.
27+
func (s *Schedule) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
28+
// Use schedule_id as key so that the type is explicit if logged as the Recipient interface.
29+
encoder.AddInt64("schedule_id", s.ID)
30+
encoder.AddString("name", s.Name)
31+
return nil
32+
}
33+
2634
type Rotation struct {
2735
ID int64 `db:"id"`
2836
ScheduleID int64 `db:"schedule_id"`

0 commit comments

Comments
 (0)