From d7ed26d09357f0bf86d9a4c77e94020a52fb07b0 Mon Sep 17 00:00:00 2001 From: Alejo Thomas Ortega Date: Mon, 7 Oct 2024 12:12:55 -0300 Subject: [PATCH] chore: enhance logging on event dispatch (#108) * chore: enhance logging on event dispatch * chore: add event.key to logs to trace them --- processor/src/logic/event-dispatcher.ts | 44 +++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/processor/src/logic/event-dispatcher.ts b/processor/src/logic/event-dispatcher.ts index 20c2ab6..6b75f34 100644 --- a/processor/src/logic/event-dispatcher.ts +++ b/processor/src/logic/event-dispatcher.ts @@ -1,4 +1,4 @@ -import { Event } from '@dcl/schemas' +import { Event, Events } from '@dcl/schemas' import { AppComponents, IEventDispatcher, IObserver } from '../types' import { UserBadge } from '@badges/common' @@ -30,7 +30,6 @@ export function createEventDispatcher({ } async function handleEvent(observer: IObserver, event: Event, userProgress: UserBadge | undefined): Promise { - const key = `${event.type}-${event.subType}` try { const result = await observer.handle(event, userProgress) return result @@ -40,31 +39,48 @@ export function createEventDispatcher({ event_sub_type: event.subType, badge_name: observer.badge.name }) - logger.error(`Failed while executing handler for badge ${observer.badge.name} for ${key}`, { - error: error.message + logger.error(`Failed while executing handler for badge ${observer.badge.name}`, { + error: error.message, + eventKey: event?.key }) - logger.debug(`Details about the error`, { stack: JSON.stringify(error.stack) }) + logger.debug(`Details about the error`, { stack: JSON.stringify(error.stack), eventKey: event?.key }) } } async function dispatch(event: Event): Promise { try { - const key = `${event.type}-${event.subType}` - logger.debug(`Dispatching event ${key}`, { event: JSON.stringify(event) }) + const observerKey = `${event.type}-${event.subType}` + const eventMetadata = event.type === Events.Type.CATALYST_DEPLOYMENT ? event.entity : event.metadata + logger.debug(`Receive event to dispatch`, { + eventKey: event.key, + eventType: event.type, + eventSubType: event.subType, + eventMetadata: JSON.stringify(eventMetadata) + }) - const list = observers.get(key) + const list = observers.get(observerKey) if (!list || list.length === 0) { - logger.debug(`No observers configured for event ${key}`) + logger.debug(`No observers configured for key ${observerKey}`) return } const badgeIds = list.map((observer) => observer.badgeId) const userAddresses = list.map((observer) => observer.getUserAddress(event)) - const userProgresses = await db.getUserProgressesForMultipleBadges(badgeIds, userAddresses) - const userProgressMap = new Map( - userProgresses.map((userProgress) => [`${userProgress.badge_id}-${userProgress.user_address}`, userProgress]) + logger.info('Dispatching event', { + eventKey: event.key, + eventType: event.type, + eventSubType: event.subType, + associatedUserAddresses: userAddresses.join(' - ') + }) + + const userProgresses: UserBadge[] = await db.getUserProgressesForMultipleBadges(badgeIds, userAddresses) + const userProgressMap: Map = new Map( + userProgresses.map((userProgress: UserBadge) => [ + `${userProgress.badge_id}-${userProgress.user_address}`, + userProgress + ]) ) const checks = list @@ -88,8 +104,8 @@ export function createEventDispatcher({ return badgesToGrant } catch (error: any) { - logger.error(`Failed while dispatching event`, { error: error.message }) - logger.debug(`Details about the error`, { stack: JSON.stringify(error.stack) }) + logger.error(`Failed while dispatching event`, { error: error.message, eventKey: event?.key }) + logger.debug(`Details about the error`, { stack: JSON.stringify(error.stack), eventKey: event?.key }) } }