-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: re-enable the logging based on X-Request-Id header (#211)
Signed-off-by: Fabio Pinheiro <fabiomgpinheiro@gmail.com> Signed-off-by: mineme0110 <shailesh.patil@iohk.io>
- Loading branch information
1 parent
a325878
commit e117cda
Showing
8 changed files
with
98 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Logging | ||
|
||
We want traceability from HTTP calls. | ||
- Each HTTP call needs to generate a call ID (preferable on the proxy). This ID must be should be pass to the scope of the ZIO application. So every log will mention this ID. This ID must also be returned to the user as a HTTP header. | ||
- In case of errors and problem reports those IDs can be use by the user to get support. | ||
|
||
Level 3 support can be provided to user during the logging retention policies. We user need to provide the value in the header `X-Request-Id` to get this support. | ||
|
||
## Annotates | ||
|
||
We have special annotations in the log so there is traceability between the different logs. | ||
Here is the list of annotations and their meaning that we currently have: | ||
|
||
- `request-id` - Is the HTTP header `X-Request-Id` from the caller. | ||
- If this header is missing, it is create by the APISIX https://apisix.apache.org/docs/apisix/plugins/request-id/. See the configuration how to enable in the file [apisixroute.yaml](https://github.com/input-output-hk/atala-prism-mediator/blob/eb6d822f125bea7b3da8f[…]0a378/infrastructure/charts/mediator/templates/apisixroute.yaml) | ||
- `msg_sha256` - Is the Hash (sha256) of the DID Comm message | ||
|
||
## Code | ||
|
||
To have a concise code we have created a Middleware that modifies the Annotations in the Scope before each execution of that endpoint, to include the trace ID of the request. | ||
See code in file `TraceIdMiddleware` (`mediator/src/main/scala/io/iohk/atala/mediator/TraceIdMiddleware.scala`). | ||
|
||
## Logging Backend | ||
|
||
We use the Simple Logging Facade for Java (SLF4J) API to call the logging backend is determined at runtime. | ||
|
||
### Logging Pattern | ||
|
||
`%d{yyyy-MM-dd_HH:mm:ss.SSS} %highlight(%-5level) %cyan(%logger{5}@L%line:[%-4.30thread]) {%mdc} - %msg%xException%n` | ||
|
||
- `%d{yyyy-MM-dd_HH:mm:ss.SSS}` is the date/timestamp of the log in the human-readable way | ||
- `%highlight(%-5level)` the log level | ||
- `%cyan(%logger{5}@L%line:[%-4.30thread])` | ||
- `%cyan` - Is just modify the color to make easy to read | ||
- `%logger{5}` - class name that originated the log | ||
- `@L%line` - line of the code that originated the log | ||
- `%-4.30thread` - the id of the thread. The ZIO fiber name | ||
- `%mdc` - the mapped diagnostic context [MDC](https://logback.qos.ch/manual/layouts.html#mdc) | ||
- `%msg` - the log message | ||
- `%xException` - exception info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
mediator/src/main/scala/io/iohk/atala/mediator/TraceIdMiddleware.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.iohk.atala.mediator | ||
|
||
import zio.* | ||
import zio.http.* | ||
|
||
object TraceIdMiddleware { | ||
|
||
private val NAME_TRACE_ID = "requestid" | ||
|
||
private def requestId(req: Request) = ZIO.logAnnotateScoped( | ||
req.headers | ||
.find(h => h.headerName.equalsIgnoreCase("x-request-id")) | ||
.toSet | ||
.map(h => LogAnnotation(NAME_TRACE_ID, h.renderedValue)) | ||
) | ||
|
||
def addTraceId = { | ||
HandlerAspect.interceptHandler( | ||
Handler.fromFunctionZIO[Request] { request => | ||
val effect = requestId(request) | ||
effect.map(_ => (request, ())) | ||
} | ||
)(Handler.identity) | ||
} | ||
|
||
} |