Skip to content

Commit 651cead

Browse files
authored
release: 1.9.6 (#353)
2 parents 6ec9af8 + d5be93c commit 651cead

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

src/main/kotlin/org/gitanimals/core/OrchestratorExtension.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitanimals.core
22

33
import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
4+
import org.gitanimals.core.filter.MDCFilter.Companion.USER_ID
45
import org.rooftop.netx.api.Context
56
import org.rooftop.netx.api.ContextOrchestrate
67
import org.rooftop.netx.api.ContextRollback
@@ -12,7 +13,11 @@ open class TraceIdContextOrchestrator<T : Any, V : Any>(
1213
) : ContextOrchestrate<T, V> {
1314

1415
override fun orchestrate(context: Context, request: T): V {
15-
MDC.put(TRACE_ID, context.decodeContext(TRACE_ID, String::class))
16+
runCatching { context.decodeContext(TRACE_ID, String::class) }
17+
.onSuccess { MDC.put(TRACE_ID, it) }
18+
runCatching { context.decodeContext(USER_ID, String::class) }
19+
.onSuccess { MDC.put(USER_ID, it) }
20+
1621
return runCatching {
1722
orchestrate.orchestrate(context, request)
1823
}.getOrElse {
@@ -33,7 +38,11 @@ open class TraceIdContextRollback<T : Any, V : Any?>(
3338
) : ContextRollback<T, V> {
3439

3540
override fun rollback(context: Context, request: T): V {
36-
MDC.put(TRACE_ID, context.decodeContext(TRACE_ID, String::class))
41+
runCatching { context.decodeContext(TRACE_ID, String::class) }
42+
.onSuccess { MDC.put(TRACE_ID, it) }
43+
runCatching { context.decodeContext(USER_ID, String::class) }
44+
.onSuccess { MDC.put(USER_ID, it) }
45+
3746
return runCatching {
3847
rollback.rollback(context, request)
3948
}.getOrElse {

src/main/kotlin/org/gitanimals/core/auth/InternalAuth.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package org.gitanimals.core.auth
22

33
import jakarta.servlet.http.HttpServletRequest
44
import org.gitanimals.core.AUTHORIZATION_EXCEPTION
5+
import org.gitanimals.core.AuthorizationException
6+
import org.gitanimals.core.filter.MDCFilter.Companion.USER_ID
57
import org.slf4j.LoggerFactory
8+
import org.slf4j.MDC
69
import org.springframework.beans.factory.annotation.Value
710
import org.springframework.http.HttpHeaders
811
import org.springframework.stereotype.Component
@@ -37,6 +40,14 @@ class InternalAuth(
3740
}
3841

3942
fun findUserId(): Long? {
43+
val userIdInMdc = runCatching {
44+
MDC.get(USER_ID).toLong()
45+
}.getOrNull()
46+
47+
if (userIdInMdc != null) {
48+
return userIdInMdc
49+
}
50+
4051
val token: String? = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION)
4152
val iv: String? = httpServletRequest.getHeader(INTERNAL_AUTH_IV_KEY)
4253
val internalAuthSecret: String? = httpServletRequest.getHeader(INTERNAL_AUTH_SECRET_KEY)
@@ -58,6 +69,9 @@ class InternalAuth(
5869
userId = runCatching {
5970
internalAuthClient.getUserByToken(token).id.toLong()
6071
}.getOrElse {
72+
if (it is AuthorizationException) {
73+
return@getOrElse null
74+
}
6175
logger.warn("[InternalAuth] Fail to get userId by token", it)
6276
null
6377
}

src/main/kotlin/org/gitanimals/core/redis/AsyncRedisPubSubEvent.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.gitanimals.core.redis
22

33
import com.fasterxml.jackson.annotation.JsonIgnore
4+
import org.gitanimals.core.filter.MDCFilter
5+
import org.slf4j.MDC
46

57
abstract class AsyncRedisPubSubEvent(
8+
val apiUserId: String? = runCatching { MDC.get(MDCFilter.USER_ID) }.getOrNull(),
69
val traceId: String,
710
@JsonIgnore
811
val channel: String,

src/main/kotlin/org/gitanimals/core/redis/TraceableMessageListener.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.gitanimals.core.redis
22

33
import com.fasterxml.jackson.core.type.TypeReference
44
import com.fasterxml.jackson.databind.ObjectMapper
5+
import org.gitanimals.core.filter.MDCFilter
56
import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
67
import org.slf4j.LoggerFactory
78
import org.slf4j.MDC
@@ -12,17 +13,28 @@ import org.springframework.data.redis.core.StringRedisTemplate
1213
abstract class TraceableMessageListener(
1314
private val redisTemplate: StringRedisTemplate,
1415
private val objectMapper: ObjectMapper,
15-
): MessageListener {
16+
) : MessageListener {
1617

1718
private val logger = LoggerFactory.getLogger(this::class.simpleName)
1819

1920
override fun onMessage(message: Message, pattern: ByteArray?) {
2021
runCatching {
21-
val traceId: String = objectMapper.readValue(
22+
val request = objectMapper.readValue(
2223
redisTemplate.stringSerializer.deserialize(message.body),
23-
object: TypeReference<Map<String, String>>(){},
24-
)["traceId"] ?: throw IllegalArgumentException("Cannot find traceId on message: $message")
25-
MDC.put(TRACE_ID, traceId)
24+
object : TypeReference<Map<String, String>>() {},
25+
)
26+
27+
runCatching {
28+
request["traceId"] as String
29+
}.onSuccess {
30+
MDC.put(TRACE_ID, it)
31+
}
32+
runCatching {
33+
request["apiUserId"] as String
34+
}.onSuccess {
35+
MDC.put(MDCFilter.USER_ID, it)
36+
}
37+
2638
onMessage(message)
2739
}.onFailure {
2840
logger.error("Fail to listen message: $message, error: $it", it)

src/main/kotlin/org/gitanimals/core/redis/TransactionCommitRedisPubSubEvent.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.gitanimals.core.redis
22

33
import com.fasterxml.jackson.annotation.JsonIgnore
4+
import org.gitanimals.core.filter.MDCFilter
5+
import org.slf4j.MDC
46
import org.springframework.context.ApplicationEvent
57

68
abstract class TransactionCommitRedisPubSubEvent(
9+
val apiUserId: String? = runCatching { MDC.get(MDCFilter.USER_ID) }.getOrNull(),
710
val traceId: String,
811
@JsonIgnore
912
val channel: String,

src/main/kotlin/org/gitanimals/guild/app/CreateGuildFacade.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.gitanimals.guild.app
33
import org.gitanimals.core.TraceIdContextOrchestrator
44
import org.gitanimals.core.TraceIdContextRollback
55
import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
6+
import org.gitanimals.core.filter.MDCFilter.Companion.USER_ID
67
import org.gitanimals.guild.app.request.CreateGuildRequest
78
import org.gitanimals.guild.app.response.GuildResponse
89
import org.gitanimals.guild.domain.GuildService
@@ -42,6 +43,7 @@ class CreateGuildFacade(
4243
"token" to token,
4344
IDEMPOTENCY_KEY to UUID.randomUUID().toString(),
4445
TRACE_ID to MDC.get(TRACE_ID),
46+
USER_ID to MDC.get(USER_ID),
4547
),
4648
timeoutMillis = 1.minutes.inWholeMilliseconds,
4749
).decodeResultOrThrow(GuildResponse::class).also {

0 commit comments

Comments
 (0)