Skip to content

Commit e975121

Browse files
authored
chore: improve log lines metadata (#608)
1 parent b17b4b5 commit e975121

4 files changed

Lines changed: 62 additions & 31 deletions

File tree

internal/alert/monitor.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,31 @@ func (m *alertMonitor) HandleAttempt(ctx context.Context, attempt DeliveryAttemp
175175
}
176176

177177
m.logger.Ctx(ctx).Audit("destination disabled",
178-
zap.String("destination_id", attempt.Destination.ID),
178+
zap.String("event_id", attempt.DeliveryEvent.Event.ID),
179179
zap.String("tenant_id", attempt.Destination.TenantID),
180-
zap.String("topic", alert.Topic),
180+
zap.String("destination_id", attempt.Destination.ID),
181+
zap.String("destination_type", attempt.Destination.Type),
181182
)
182183
}
183184

184185
// Send alert if notifier is configured
185186
if m.notifier != nil {
186187
if err := m.notifier.Notify(ctx, alert); err != nil {
187188
m.logger.Ctx(ctx).Error("failed to send alert",
188-
zap.String("destination_id", attempt.Destination.ID),
189-
zap.String("tenant_id", attempt.Destination.TenantID),
190-
zap.String("topic", alert.Topic),
191189
zap.Error(err),
190+
zap.String("event_id", attempt.DeliveryEvent.Event.ID),
191+
zap.String("tenant_id", attempt.Destination.TenantID),
192+
zap.String("destination_id", attempt.Destination.ID),
193+
zap.String("destination_type", attempt.Destination.Type),
192194
)
193195
return fmt.Errorf("failed to send alert: %w", err)
194196
}
195197

196198
m.logger.Ctx(ctx).Audit("alert sent",
197-
zap.String("destination_id", attempt.Destination.ID),
199+
zap.String("event_id", attempt.DeliveryEvent.Event.ID),
198200
zap.String("tenant_id", attempt.Destination.TenantID),
199-
zap.String("topic", alert.Topic),
201+
zap.String("destination_id", attempt.Destination.ID),
202+
zap.String("destination_type", attempt.Destination.Type),
200203
)
201204
}
202205

internal/apirouter/retry_handlers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func (h *RetryHandlers) Retry(c *gin.Context) {
7272

7373
h.logger.Ctx(c).Audit("manual retry initiated",
7474
zap.String("event_id", event.ID),
75-
zap.String("destination_id", destination.ID))
75+
zap.String("tenant_id", tenantID),
76+
zap.String("destination_id", destination.ID),
77+
zap.String("destination_type", destination.Type))
7678

7779
c.JSON(http.StatusAccepted, gin.H{
7880
"success": true,

internal/deliverymq/messagehandler.go

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ func (h *messageHandler) Handle(ctx context.Context, msg *mqs.Message) error {
144144
}
145145

146146
h.logger.Ctx(ctx).Info("processing delivery event",
147-
zap.String("delivery_event_id", deliveryEvent.ID),
148147
zap.String("event_id", deliveryEvent.Event.ID),
148+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
149+
zap.String("destination_id", deliveryEvent.DestinationID),
149150
zap.Int("attempt", deliveryEvent.Attempt))
150151

151152
// Ensure event data
@@ -198,8 +199,10 @@ func (h *messageHandler) doHandle(ctx context.Context, deliveryEvent models.Deli
198199

199200
h.logger.Ctx(ctx).Error("failed to publish event",
200201
zap.Error(err),
201-
zap.String("delivery_event_id", deliveryEvent.ID),
202-
zap.String("destination_id", destination.ID))
202+
zap.String("event_id", deliveryEvent.Event.ID),
203+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
204+
zap.String("destination_id", destination.ID),
205+
zap.String("destination_type", destination.Type))
203206
deliveryErr := &DeliveryError{err: err}
204207

205208
if h.shouldScheduleRetry(deliveryEvent, err) {
@@ -216,12 +219,18 @@ func (h *messageHandler) doHandle(ctx context.Context, deliveryEvent models.Deli
216219
if err := h.retryScheduler.Cancel(ctx, deliveryEvent.GetRetryID()); err != nil {
217220
h.logger.Ctx(ctx).Error("failed to cancel scheduled retry",
218221
zap.Error(err),
219-
zap.String("delivery_event_id", deliveryEvent.ID),
222+
zap.String("event_id", deliveryEvent.Event.ID),
223+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
224+
zap.String("destination_id", destination.ID),
225+
zap.String("destination_type", destination.Type),
220226
zap.String("retry_id", deliveryEvent.GetRetryID()))
221227
return h.logDeliveryResult(ctx, &deliveryEvent, destination, delivery, err)
222228
}
223229
logger.Audit("scheduled retry canceled",
224-
zap.String("delivery_event_id", deliveryEvent.ID),
230+
zap.String("event_id", deliveryEvent.Event.ID),
231+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
232+
zap.String("destination_id", destination.ID),
233+
zap.String("destination_type", destination.Type),
225234
zap.String("retry_id", deliveryEvent.GetRetryID()))
226235
}
227236
return h.logDeliveryResult(ctx, &deliveryEvent, destination, delivery, nil)
@@ -234,19 +243,22 @@ func (h *messageHandler) logDeliveryResult(ctx context.Context, deliveryEvent *m
234243
deliveryEvent.Delivery = delivery
235244

236245
logger.Audit("event delivered",
237-
zap.String("delivery_event_id", deliveryEvent.ID),
238-
zap.String("destination_id", deliveryEvent.DestinationID),
239246
zap.String("event_id", deliveryEvent.Event.ID),
247+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
248+
zap.String("destination_id", destination.ID),
249+
zap.String("destination_type", destination.Type),
240250
zap.String("delivery_status", deliveryEvent.Delivery.Status),
241251
zap.Int("attempt", deliveryEvent.Attempt),
242-
zap.Bool("manual", deliveryEvent.Manual),
243-
zap.String("destination_type", destination.Type))
252+
zap.Bool("manual", deliveryEvent.Manual))
244253

245254
// Publish delivery log
246255
if logErr := h.logMQ.Publish(ctx, *deliveryEvent); logErr != nil {
247256
logger.Error("failed to publish delivery log",
248257
zap.Error(logErr),
249-
zap.String("delivery_event_id", deliveryEvent.ID))
258+
zap.String("event_id", deliveryEvent.Event.ID),
259+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
260+
zap.String("destination_id", destination.ID),
261+
zap.String("destination_type", destination.Type))
250262
if err != nil {
251263
return &PostDeliveryError{err: errors.Join(err, logErr)}
252264
}
@@ -315,14 +327,18 @@ func (h *messageHandler) handleAlertAttempt(ctx context.Context, deliveryEvent *
315327
if monitorErr := h.alertMonitor.HandleAttempt(ctx, attempt); monitorErr != nil {
316328
h.logger.Ctx(ctx).Error("failed to handle alert attempt",
317329
zap.Error(monitorErr),
318-
zap.String("delivery_event_id", deliveryEvent.ID),
319-
zap.String("destination_id", destination.ID))
330+
zap.String("event_id", deliveryEvent.Event.ID),
331+
zap.String("tenant_id", destination.TenantID),
332+
zap.String("destination_id", destination.ID),
333+
zap.String("destination_type", destination.Type))
320334
return
321335
}
322336

323337
h.logger.Ctx(ctx).Info("alert attempt handled",
324-
zap.String("delivery_event_id", deliveryEvent.ID),
325-
zap.String("destination_id", destination.ID))
338+
zap.String("event_id", deliveryEvent.Event.ID),
339+
zap.String("tenant_id", destination.TenantID),
340+
zap.String("destination_id", destination.ID),
341+
zap.String("destination_type", destination.Type))
326342
}
327343

328344
func (h *messageHandler) shouldScheduleRetry(deliveryEvent models.DeliveryEvent, err error) bool {
@@ -395,14 +411,18 @@ func (h *messageHandler) scheduleRetry(ctx context.Context, deliveryEvent models
395411
if err := h.retryScheduler.Schedule(ctx, retryMessageStr, backoffDuration, scheduler.WithTaskID(deliveryEvent.GetRetryID())); err != nil {
396412
h.logger.Ctx(ctx).Error("failed to schedule retry",
397413
zap.Error(err),
398-
zap.String("delivery_event_id", deliveryEvent.ID),
414+
zap.String("event_id", deliveryEvent.Event.ID),
415+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
416+
zap.String("destination_id", deliveryEvent.DestinationID),
399417
zap.Int("attempt", deliveryEvent.Attempt),
400418
zap.Duration("backoff", backoffDuration))
401419
return err
402420
}
403421

404422
h.logger.Ctx(ctx).Audit("retry scheduled",
405-
zap.String("delivery_event_id", deliveryEvent.ID),
423+
zap.String("event_id", deliveryEvent.Event.ID),
424+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
425+
zap.String("destination_id", deliveryEvent.DestinationID),
406426
zap.Int("attempt", deliveryEvent.Attempt),
407427
zap.Duration("backoff", backoffDuration))
408428

@@ -438,8 +458,9 @@ func (h *messageHandler) ensurePublishableDestination(ctx context.Context, deliv
438458
logger := h.logger.Ctx(ctx)
439459
fields := []zap.Field{
440460
zap.Error(err),
441-
zap.String("destination_id", deliveryEvent.DestinationID),
461+
zap.String("event_id", deliveryEvent.Event.ID),
442462
zap.String("tenant_id", deliveryEvent.Event.TenantID),
463+
zap.String("destination_id", deliveryEvent.DestinationID),
443464
}
444465

445466
if errors.Is(err, models.ErrDestinationDeleted) {
@@ -452,12 +473,15 @@ func (h *messageHandler) ensurePublishableDestination(ctx context.Context, deliv
452473
}
453474
if destination == nil {
454475
h.logger.Ctx(ctx).Info("destination not found",
455-
zap.String("destination_id", deliveryEvent.DestinationID),
456-
zap.String("tenant_id", deliveryEvent.Event.TenantID))
476+
zap.String("event_id", deliveryEvent.Event.ID),
477+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
478+
zap.String("destination_id", deliveryEvent.DestinationID))
457479
return nil, models.ErrDestinationNotFound
458480
}
459481
if destination.DisabledAt != nil {
460482
h.logger.Ctx(ctx).Info("skipping disabled destination",
483+
zap.String("event_id", deliveryEvent.Event.ID),
484+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
461485
zap.String("destination_id", destination.ID),
462486
zap.String("destination_type", destination.Type),
463487
zap.Time("disabled_at", *destination.DisabledAt))

internal/publishmq/eventhandler.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (h *eventHandler) Handle(ctx context.Context, event *models.Event) (*Handle
7575
logger.Audit("processing event",
7676
zap.String("event_id", event.ID),
7777
zap.String("tenant_id", event.TenantID),
78-
zap.String("topic", event.Topic),
79-
zap.String("destination_id", event.DestinationID))
78+
zap.String("destination_id", event.DestinationID),
79+
zap.String("topic", event.Topic))
8080

8181
var matchedDestinations []models.DestinationSummary
8282
var err error
@@ -159,6 +159,8 @@ func (h *eventHandler) matchSpecificDestination(ctx context.Context, event *mode
159159
if err != nil {
160160
h.logger.Ctx(ctx).Warn("failed to retrieve destination",
161161
zap.Error(err),
162+
zap.String("event_id", event.ID),
163+
zap.String("tenant_id", event.TenantID),
162164
zap.String("destination_id", event.DestinationID))
163165
return []models.DestinationSummary{}, nil
164166
}
@@ -179,17 +181,17 @@ func (h *eventHandler) enqueueDeliveryEvent(ctx context.Context, deliveryEvent m
179181
if err := h.deliveryMQ.Publish(ctx, deliveryEvent); err != nil {
180182
h.logger.Ctx(ctx).Error("failed to enqueue delivery event",
181183
zap.Error(err),
182-
zap.String("delivery_event_id", deliveryEvent.ID),
183184
zap.String("event_id", deliveryEvent.Event.ID),
185+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
184186
zap.String("destination_id", deliveryEvent.DestinationID))
185187
deliverySpan.RecordError(err)
186188
deliverySpan.End()
187189
return err
188190
}
189191

190192
h.logger.Ctx(ctx).Audit("delivery event enqueued",
191-
zap.String("delivery_event_id", deliveryEvent.ID),
192193
zap.String("event_id", deliveryEvent.Event.ID),
194+
zap.String("tenant_id", deliveryEvent.Event.TenantID),
193195
zap.String("destination_id", deliveryEvent.DestinationID))
194196
deliverySpan.End()
195197
return nil

0 commit comments

Comments
 (0)