Skip to content

Commit 13f5590

Browse files
authored
Log all topics contained in a registration request with an invalid topic type (#1834)
* log invalid topic id and type * add logging to non-legacy * better loging * use MDC * log all topics * update
1 parent 5158e54 commit 13f5590

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

common/src/main/scala/models/Registration.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ object Registration {
1616
val base = Json.format[Registration]
1717
Format(
1818
Reads { json =>
19+
def topicSummary(v: JsValue): String = {
20+
val t = (v \ "type").asOpt[String].getOrElse("unknown")
21+
val n = (v \ "name").asOpt[String].getOrElse("unknown")
22+
s"$t (id=$n)"
23+
}
24+
val all = (json \ "topics").asOpt[JsArray].toSeq.flatMap(_.value.map(topicSummary))
1925
val invalid = (json \ "topics").asOpt[JsArray].toSeq.flatMap { arr =>
2026
arr.value.flatMap { v =>
21-
if (v.validate[Topic].isError) {
22-
val topicType = (v \ "type").asOpt[String].getOrElse("unknown")
23-
val topicName = (v \ "name").asOpt[String].getOrElse("unknown")
24-
Some(s"Topic Type=$topicType (Topic Id=$topicName)")
25-
} else None
27+
if (v.validate[Topic].isError) Some(topicSummary(v)) else None
2628
}
2729
}
28-
if (invalid.nonEmpty)
29-
MDC.put("invalidTopics", invalid.mkString(". "))
30+
if (invalid.nonEmpty) {
31+
MDC.put("invalidTopics", invalid.mkString(", "))
32+
if (all.nonEmpty) MDC.put("allTopics", all.mkString(", "))
33+
}
3034
base.reads(json)
3135
},
3236
base

registration/app/registration/CustomErrorHandler.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ class CustomErrorHandler(env: Environment, config: Configuration, sourceMapper:
1414
private val logger: Logger = LoggerFactory.getLogger(this.getClass)
1515
override def onBadRequest(request: RequestHeader, message: String): Future[Result] = {
1616
val debugInfo = request.headers.get("User-Agent")
17-
Option(MDC.get("invalidTopics")) match {
18-
case Some(invalidTopics) =>
19-
logger.warn(s"Bad request: request contains invalid topic type(s). $invalidTopics. User agent = $debugInfo")
20-
MDC.remove("invalidTopics")
21-
case None =>
22-
logger.error(s"Bad request due to $message. User agent = $debugInfo")
17+
try {
18+
Option(MDC.get("invalidTopics")) match {
19+
case Some(invalidTopics) =>
20+
val allTopics = Option(MDC.get("allTopics")).getOrElse("")
21+
logger.warn(s"Bad request: invalid topic type(s): [$invalidTopics]. All topics: [$allTopics]. User agent: ${debugInfo.getOrElse("unknown")}")
22+
case None =>
23+
logger.error(s"Bad request due to $message. User agent: ${debugInfo.getOrElse("unknown")}")
24+
}
25+
} finally {
26+
MDC.remove("invalidTopics")
27+
MDC.remove("allTopics")
2328
}
2429
Future.successful(BadRequest("Bad request"))
2530
}

0 commit comments

Comments
 (0)