From 15b9aed54d7a85809025701eb3d4a2bb667afcb2 Mon Sep 17 00:00:00 2001 From: devxb Date: Mon, 3 Mar 2025 21:26:59 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20@Async=EC=97=90=EC=84=9C=20traceId?= =?UTF-8?q?=EA=B0=80=20=EC=84=B8=ED=8C=85=EB=90=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=20=EB=B2=84=EA=B7=B8=EB=A5=BC=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/redis/TransactionCommitRedisPubSubEvent.kt | 7 +------ .../guild/domain/event/GuildContributionUpdated.kt | 8 +++++++- .../kotlin/org/gitanimals/render/app/AnimationFacade.kt | 6 ++++-- .../render/domain/event/UserContributionUpdated.kt | 6 ++++++ .../kotlin/org/gitanimals/render/domain/event/Visited.kt | 3 ++- .../org/gitanimals/render/infra/VisitedEventListener.kt | 5 +++++ 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/org/gitanimals/core/redis/TransactionCommitRedisPubSubEvent.kt b/src/main/kotlin/org/gitanimals/core/redis/TransactionCommitRedisPubSubEvent.kt index 72f829ea..392c993d 100644 --- a/src/main/kotlin/org/gitanimals/core/redis/TransactionCommitRedisPubSubEvent.kt +++ b/src/main/kotlin/org/gitanimals/core/redis/TransactionCommitRedisPubSubEvent.kt @@ -1,15 +1,10 @@ package org.gitanimals.core.redis import com.fasterxml.jackson.annotation.JsonIgnore -import org.gitanimals.core.IdGenerator -import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID -import org.slf4j.MDC import org.springframework.context.ApplicationEvent abstract class TransactionCommitRedisPubSubEvent( - val traceId: String = runCatching { MDC.get(TRACE_ID) }.getOrElse { - IdGenerator.generate().toString() - }, + val traceId: String, @JsonIgnore val channel: String, source: Any, diff --git a/src/main/kotlin/org/gitanimals/guild/domain/event/GuildContributionUpdated.kt b/src/main/kotlin/org/gitanimals/guild/domain/event/GuildContributionUpdated.kt index 6d1bf2b3..cf268321 100644 --- a/src/main/kotlin/org/gitanimals/guild/domain/event/GuildContributionUpdated.kt +++ b/src/main/kotlin/org/gitanimals/guild/domain/event/GuildContributionUpdated.kt @@ -1,8 +1,11 @@ package org.gitanimals.guild.domain.event +import org.gitanimals.core.DomainEventPublisher +import org.gitanimals.core.IdGenerator +import org.gitanimals.core.filter.MDCFilter import org.gitanimals.core.redis.RedisPubSubChannel import org.gitanimals.core.redis.TransactionCommitRedisPubSubEvent -import org.gitanimals.core.DomainEventPublisher +import org.slf4j.MDC data class GuildContributionUpdated( val guildId: Long, @@ -11,6 +14,9 @@ data class GuildContributionUpdated( val contributions: Long, val updatedContributions: Long, ) : TransactionCommitRedisPubSubEvent( + traceId = runCatching { MDC.get(MDCFilter.TRACE_ID) }.getOrElse { + IdGenerator.generate().toString() + }, channel = RedisPubSubChannel.GUILD_CONTRIBUTION_UPDATED, source = DomainEventPublisher::class, ) diff --git a/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt b/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt index 2962fd56..c6125363 100644 --- a/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt +++ b/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt @@ -1,11 +1,13 @@ package org.gitanimals.render.app import org.gitanimals.core.Mode +import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID import org.gitanimals.render.domain.User import org.gitanimals.render.domain.UserService import org.gitanimals.render.domain.event.NewUserCreated import org.gitanimals.render.domain.event.Visited import org.rooftop.netx.api.SagaManager +import org.slf4j.MDC import org.springframework.context.ApplicationEventPublisher import org.springframework.stereotype.Service import org.springframework.web.client.RestClientException @@ -22,7 +24,7 @@ class AnimationFacade( return when (userService.existsByName(username)) { true -> { val svgAnimation = userService.getFarmAnimationByUsername(username) - eventPublisher.publishEvent(Visited(username)) + eventPublisher.publishEvent(Visited(username, MDC.get(TRACE_ID))) svgAnimation } @@ -38,7 +40,7 @@ class AnimationFacade( return when (userService.existsByName(username)) { true -> { val svgAnimation = userService.getLineAnimationByUsername(username, personaId, mode) - eventPublisher.publishEvent(Visited(username)) + eventPublisher.publishEvent(Visited(username, MDC.get(TRACE_ID))) svgAnimation } diff --git a/src/main/kotlin/org/gitanimals/render/domain/event/UserContributionUpdated.kt b/src/main/kotlin/org/gitanimals/render/domain/event/UserContributionUpdated.kt index 1e10462e..cacc65e6 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/event/UserContributionUpdated.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/event/UserContributionUpdated.kt @@ -1,9 +1,12 @@ package org.gitanimals.render.domain.event import org.gitanimals.core.DomainEventPublisher +import org.gitanimals.core.IdGenerator +import org.gitanimals.core.filter.MDCFilter import org.gitanimals.core.instant import org.gitanimals.core.redis.RedisPubSubChannel import org.gitanimals.core.redis.TransactionCommitRedisPubSubEvent +import org.slf4j.MDC import java.time.Instant data class UserContributionUpdated( @@ -13,6 +16,9 @@ data class UserContributionUpdated( val userContributionUpdated: Boolean = true, val contributionUpdatedAt: Instant = instant(), ) : TransactionCommitRedisPubSubEvent( + traceId = runCatching { MDC.get(MDCFilter.TRACE_ID) }.getOrElse { + IdGenerator.generate().toString() + }, channel = RedisPubSubChannel.USER_CONTRIBUTION_UPDATED, source = DomainEventPublisher::class, ) diff --git a/src/main/kotlin/org/gitanimals/render/domain/event/Visited.kt b/src/main/kotlin/org/gitanimals/render/domain/event/Visited.kt index e6649a5d..25bfc5b6 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/event/Visited.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/event/Visited.kt @@ -1,5 +1,6 @@ package org.gitanimals.render.domain.event data class Visited( - val username: String + val username: String, + val traceId: String, ) diff --git a/src/main/kotlin/org/gitanimals/render/infra/VisitedEventListener.kt b/src/main/kotlin/org/gitanimals/render/infra/VisitedEventListener.kt index c9555c8d..33e68a1c 100644 --- a/src/main/kotlin/org/gitanimals/render/infra/VisitedEventListener.kt +++ b/src/main/kotlin/org/gitanimals/render/infra/VisitedEventListener.kt @@ -1,6 +1,7 @@ package org.gitanimals.render.infra import org.gitanimals.core.IdGenerator +import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID import org.gitanimals.render.app.ContributionApi import org.gitanimals.render.app.IdentityApi import org.gitanimals.render.domain.UserService @@ -8,6 +9,7 @@ import org.gitanimals.render.domain.event.Visited import org.rooftop.netx.api.* import org.rooftop.netx.meta.SagaHandler import org.slf4j.LoggerFactory +import org.slf4j.MDC import org.springframework.context.event.EventListener import org.springframework.scheduling.annotation.Async import java.time.ZoneId @@ -26,6 +28,7 @@ class VisitedEventListener( @EventListener(Visited::class) fun increaseUserVisited(visited: Visited) { runCatching { + MDC.put(TRACE_ID, visited.traceId) val username = visited.username userService.increaseVisit(username) @@ -49,6 +52,8 @@ class VisitedEventListener( logger.info("Increase point to user. username: \"$username\", point:\"${increaseContributionCount * 100}\"") }.onFailure { logger.error("Cannot increase visit or point to user. username: \"${visited.username}\"", it) + }.also { + MDC.remove(TRACE_ID) } } }