Skip to content

Commit 3c49393

Browse files
committed
fix: prevented from sending status change on service start if there was no last known status
1 parent 774aee8 commit 3c49393

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

pkg/monitor/watcher.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func (w *watcher) run(ctx context.Context) {
4646
if len(incidents) > 0 && incidents[0].EndTS == nil {
4747
w.incident = incidents[0]
4848
}
49+
if lastResponse, err := w.store.LastResponse(ctx, w.host.ID); err == nil && lastResponse != nil {
50+
w.status = lastResponse.StatusType
51+
}
4952

5053
log.Printf("[INFO] %s: new watcher", w.host.String())
5154

@@ -96,7 +99,7 @@ func (w *watcher) validate(resp *types.HttpResponse) {
9699
if w.successCount >= w.host.SuccessThreshold {
97100
newStatus := types.UP
98101
if w.status != types.Unknown && w.status != types.UP {
99-
if err := w.notify.Set(w.host.Alerts, newStatus, w.host.String()); err != nil {
102+
if err := w.notify.Send(w.host, newStatus); err != nil {
100103
log.Print(err)
101104
}
102105

@@ -122,9 +125,10 @@ func (w *watcher) validate(resp *types.HttpResponse) {
122125
if w.failureCount >= w.host.FailureThreshold {
123126
newStatus := types.DOWN
124127
if w.status != types.Unknown && w.status != types.DOWN {
125-
if err := w.notify.Set(w.host.Alerts, newStatus, w.host.String()); err != nil {
128+
if err := w.notify.Send(w.host, newStatus); err != nil {
126129
log.Print(err)
127130
}
131+
128132
if w.incident == nil {
129133
w.incident = &types.Incident{
130134
Details: types.IncidentDetails{

store/bolt.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ func (b *Bolt) FindResponses(ctx context.Context, hostID string) ([]*types.HttpR
8383

8484
return res, err
8585
}
86+
func (b *Bolt) LastResponse(ctx context.Context, hostID string) (*types.HttpResponse, error) {
87+
var res *types.HttpResponse
88+
89+
err := b.conn.View(func(tx *bolt.Tx) error {
90+
bucket := tx.Bucket([]byte(hostID))
91+
if bucket == nil {
92+
return nil
93+
}
94+
c := bucket.Cursor()
95+
k, v := c.Last()
96+
if k == nil {
97+
return nil
98+
}
99+
var r types.HttpResponse
100+
if err := json.Unmarshal(v, &r); err != nil {
101+
return err
102+
}
103+
res = &r
104+
return nil
105+
})
106+
107+
return res, err
108+
}
86109

87110
func (b *Bolt) Hosts(ctx context.Context) ([]string, error) {
88111
keys := []string{}

store/memory.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ func (m *Memory) FindResponses(ctx context.Context, hostID string) ([]*types.Htt
7373

7474
return res, nil
7575
}
76+
func (m *Memory) LastResponse(ctx context.Context, hostID string) (*types.HttpResponse, error) {
77+
m.RLock()
78+
defer m.RUnlock()
79+
80+
if _, ok := m.history[hostID]; !ok {
81+
return nil, nil
82+
}
83+
84+
var last *types.HttpResponse
85+
for _, r := range m.history[hostID] {
86+
if last == nil || r.Timestamp.After(last.Timestamp) {
87+
last = r
88+
}
89+
}
90+
91+
return last, nil
92+
}
7693

7794
func (m *Memory) Hosts(ctx context.Context) ([]string, error) {
7895
m.RLock()

store/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Interface interface {
1818
AddResponse(ctx context.Context, hostID string, r *types.HttpResponse) error
1919
DeleteResponse(ctx context.Context, hostID string, keys []time.Time) error
2020
FindResponses(ctx context.Context, hostID string) ([]*types.HttpResponse, error)
21+
LastResponse(ctx context.Context, hostID string) (*types.HttpResponse, error)
2122

2223
// Hosts returns a list of all hosts that has any responses in the store.
2324
Hosts(ctx context.Context) ([]string, error)

0 commit comments

Comments
 (0)