Skip to content

Commit 9851135

Browse files
authored
Merge pull request #409 from hookdeck/chore/event-payload-alert
chore: Include event data & metadata in alert payload
2 parents 9513a1b + 346e0d3 commit 9851135

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

internal/alert/monitor.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ func WithLogger(logger *logging.Logger) AlertOption {
7575

7676
// DeliveryAttempt represents a single delivery attempt
7777
type DeliveryAttempt struct {
78-
Success bool
79-
DeliveryEvent *models.DeliveryEvent
80-
Destination *AlertDestination
81-
Timestamp time.Time
82-
Data map[string]interface{}
78+
Success bool
79+
DeliveryEvent *models.DeliveryEvent
80+
Destination *AlertDestination
81+
Timestamp time.Time
82+
DeliveryResponse map[string]interface{}
8383
}
8484

8585
type alertMonitor struct {
@@ -147,11 +147,17 @@ func (m *alertMonitor) HandleAttempt(ctx context.Context, attempt DeliveryAttemp
147147
}
148148

149149
alert := NewConsecutiveFailureAlert(ConsecutiveFailureData{
150+
Event: AlertedEvent{
151+
ID: attempt.DeliveryEvent.Event.ID,
152+
Topic: attempt.DeliveryEvent.Event.Topic,
153+
Metadata: attempt.DeliveryEvent.Event.Metadata,
154+
Data: attempt.DeliveryEvent.Event.Data,
155+
},
150156
MaxConsecutiveFailures: m.autoDisableFailureCount,
151157
ConsecutiveFailures: count,
152158
WillDisable: m.disabler != nil && level == 100,
153159
Destination: attempt.Destination,
154-
Data: attempt.Data,
160+
DeliveryResponse: attempt.DeliveryResponse,
155161
})
156162

157163
// If we've hit 100% and have a disabler configured, disable the destination

internal/alert/monitor_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestAlertMonitor_ConsecutiveFailures_MaxFailures(t *testing.T) {
5858
Success: false,
5959
DeliveryEvent: deliveryEvent,
6060
Destination: dest,
61-
Data: map[string]interface{}{
61+
DeliveryResponse: map[string]interface{}{
6262
"status": "500",
6363
"data": map[string]any{"error": "test error"},
6464
},
@@ -79,8 +79,8 @@ func TestAlertMonitor_ConsecutiveFailures_MaxFailures(t *testing.T) {
7979
failures := alert.Data.ConsecutiveFailures
8080
require.Contains(t, []int{10, 14, 18, 20}, failures, "Alert should be sent at 50%, 66%, 90%, and 100% thresholds")
8181
require.Equal(t, dest, alert.Data.Destination)
82-
require.Equal(t, "alert.consecutive-failure", alert.Topic)
83-
require.Equal(t, attempt.Data, alert.Data.Data)
82+
require.Equal(t, "alert.consecutive_failure", alert.Topic)
83+
require.Equal(t, attempt.DeliveryResponse, alert.Data.DeliveryResponse)
8484
require.Equal(t, 20, alert.Data.MaxConsecutiveFailures)
8585
require.Equal(t, failures == 20, alert.Data.WillDisable, "WillDisable should only be true at 100% (20 failures)")
8686
}
@@ -125,7 +125,7 @@ func TestAlertMonitor_ConsecutiveFailures_Reset(t *testing.T) {
125125
Success: false,
126126
DeliveryEvent: deliveryEvent,
127127
Destination: dest,
128-
Data: map[string]interface{}{
128+
DeliveryResponse: map[string]interface{}{
129129
"status": "500",
130130
"data": map[string]any{"error": "test error"},
131131
},

internal/alert/notifier.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ func NotifierWithBearerToken(token string) NotifierOption {
4242
}
4343
}
4444

45+
type AlertedEvent struct {
46+
ID string `json:"id"`
47+
Topic string `json:"topic"` // event topic
48+
Metadata map[string]string `json:"metadata"` // event metadata
49+
Data map[string]interface{} `json:"data"` // event payload
50+
}
51+
4552
type AlertDestination struct {
4653
ID string `json:"id" redis:"id"`
4754
TenantID string `json:"tenant_id" redis:"-"`
@@ -54,11 +61,12 @@ type AlertDestination struct {
5461

5562
// ConsecutiveFailureData represents the data needed for a consecutive failure alert
5663
type ConsecutiveFailureData struct {
64+
Event AlertedEvent `json:"event"`
5765
MaxConsecutiveFailures int `json:"max_consecutive_failures"`
5866
ConsecutiveFailures int `json:"consecutive_failures"`
5967
WillDisable bool `json:"will_disable"`
6068
Destination *AlertDestination `json:"destination"`
61-
Data map[string]interface{} `json:"data"`
69+
DeliveryResponse map[string]interface{} `json:"delivery_response"`
6270
}
6371

6472
// ConsecutiveFailureAlert represents an alert for consecutive failures
@@ -77,7 +85,7 @@ func (a ConsecutiveFailureAlert) MarshalJSON() ([]byte, error) {
7785
// NewConsecutiveFailureAlert creates a new consecutive failure alert with defaults
7886
func NewConsecutiveFailureAlert(data ConsecutiveFailureData) ConsecutiveFailureAlert {
7987
return ConsecutiveFailureAlert{
80-
Topic: "alert.consecutive-failure",
88+
Topic: "alert.consecutive_failure",
8189
Timestamp: time.Now(),
8290
Data: data,
8391
}

internal/alert/notifier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAlertNotifier_Notify(t *testing.T) {
3434
err := json.NewDecoder(r.Body).Decode(&body)
3535
require.NoError(t, err)
3636

37-
assert.Equal(t, "alert.consecutive-failure", body["topic"])
37+
assert.Equal(t, "alert.consecutive_failure", body["topic"])
3838
data := body["data"].(map[string]interface{})
3939
assert.Equal(t, float64(10), data["max_consecutive_failures"])
4040
assert.Equal(t, float64(5), data["consecutive_failures"])
@@ -102,7 +102,7 @@ func TestAlertNotifier_Notify(t *testing.T) {
102102
ConsecutiveFailures: 5,
103103
WillDisable: true,
104104
Destination: dest,
105-
Data: map[string]interface{}{
105+
DeliveryResponse: map[string]interface{}{
106106
"status": "error",
107107
"data": map[string]any{"code": "ETIMEDOUT"},
108108
},

internal/deliverymq/messagehandler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ func (h *messageHandler) handleAlertAttempt(ctx context.Context, deliveryEvent *
302302
if errors.As(err, &delErr) {
303303
var pubErr *destregistry.ErrDestinationPublishAttempt
304304
if errors.As(delErr.err, &pubErr) {
305-
attempt.Data = pubErr.Data
305+
attempt.DeliveryResponse = pubErr.Data
306306
} else {
307-
attempt.Data = map[string]interface{}{
307+
attempt.DeliveryResponse = map[string]interface{}{
308308
"error": delErr.err.Error(),
309309
}
310310
}
311311
} else {
312-
attempt.Data = map[string]interface{}{
312+
attempt.DeliveryResponse = map[string]interface{}{
313313
"error": "unexpected",
314314
"message": err.Error(),
315315
}

internal/deliverymq/messagehandler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ func TestMessageHandler_PublishSuccess(t *testing.T) {
11251125
return attempt.Success && // Should be a successful attempt
11261126
attempt.Destination.ID == destination.ID && // Should have correct destination
11271127
attempt.DeliveryEvent != nil && // Should have delivery event
1128-
attempt.Data == nil // No error data for success
1128+
attempt.DeliveryResponse == nil // No error data for success
11291129
})).Return(nil)
11301130

11311131
// Setup message handler
@@ -1255,8 +1255,8 @@ func assertAlertMonitor(t *testing.T, m *mockAlertMonitor, success bool, destina
12551255
assert.NotNil(t, attempt.DeliveryEvent, "alert attempt should have delivery event")
12561256

12571257
if expectedData != nil {
1258-
assert.Equal(t, expectedData, attempt.Data, "alert attempt data should match")
1258+
assert.Equal(t, expectedData, attempt.DeliveryResponse, "alert attempt data should match")
12591259
} else {
1260-
assert.Nil(t, attempt.Data, "alert attempt should not have data")
1260+
assert.Nil(t, attempt.DeliveryResponse, "alert attempt should not have data")
12611261
}
12621262
}

0 commit comments

Comments
 (0)