0

I want to have traceId in keycloak logs, as you can see this is my logging configuration for keycloak:

<subsystem xmlns="urn:jboss:domain:logging:8.0">
<async-handler name="async-log-server-handler">
    <level name="ALL"/>
    <queue-length value="${env.LOGSTASH_QUEUE:1000}"/>
    <overflow-action value="discard"/>
    <subhandlers>
        <handler name="log-server-handler"/>
    </subhandlers>
</async-handler>
<console-handler name="CONSOLE">
    <formatter>
        <named-formatter name="COLOR-PATTERN"/>
    </formatter>
</console-handler>
<socket-handler name="log-server-handler" block-on-reconnect="true"
                outbound-socket-binding-ref="log-server">
    <named-formatter name="json"/>
    <protocol value="UDP"/>
</socket-handler>
<logger category="com.arjuna">
    <level name="WARN"/>
</logger>
<logger category="io.jaegertracing.Configuration">
    <level name="WARN"/>
</logger>
<logger category="org.jboss.as.config">
    <level name="INFO"/>
</logger>
<logger category="sun.rmi">
    <level name="WARN"/>
</logger>
<logger category="org.keycloak">
    <level name="INFO"/>
</logger>
<root-logger>
    <level name="${env.ROOT_LOGLEVEL:INFO}"/>
    <handlers>
        <handler name="CONSOLE"/>
        <handler name="async-log-server-handler"/>
    </handlers>
</root-logger>
<formatter name="PATTERN">
    <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n"/>
</formatter>
<formatter name="COLOR-PATTERN">
    <pattern-formatter pattern="spanId: %X{span_id} traceId: %X{trace_id} %X{trace} %X{X-B3-TraceId} %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] %s%e%n"/>
</formatter>
<formatter name="json">
    <json-formatter>
        <exception-output-type value="formatted"/>
        <key-overrides timestamp="@timestamp"/>
        <meta-data>
            <property name="appname" value="${env.LOGSTASH_APPNAME:keycloak-18}"/>
            <property name="@version" value="1"/>
        </meta-data>
    </json-formatter>
</formatter>

I also added opentelemetry-logback-1.0-1.9.2 as module to my deployment:

<module name="io.opentelemetry.opentelemetry-logback" xmlns="urn:jboss:module:1.0">
<resources>
    <resource-root path="opentelemetry-logback-1.0-1.9.2.jar"/>
    <resource-root path="opentelemetry-api-1.9.1.jar"/>
    <resource-root path="opentelemetry-context-1.9.1.jar"/>
    <resource-root path="opentelemetry-instrumentation-api-1.19.2.jar"/>
</resources>

but I couldn't see traceId in logs. i feel I am missing something or doing something wrong in my configurations.

And these are sample lines of logs:

spanId:  traceId:    2023-07-23 17:28:41,798 INFO  [org.apache.kafka.common.utils.AppInfoParser] Kafka version: 2.6.0
spanId:  traceId:    2023-07-23 17:28:41,798 INFO  [org.apache.kafka.common.utils.AppInfoParser] Kafka commitId: 62abe01bee039651
spanId:  traceId:    2023-07-23 17:28:41,799 INFO  [org.apache.kafka.common.utils.AppInfoParser] Kafka startTimeMs: 1690117121789
spanId:  traceId:    2023-07-23 17:28:42,680 INFO  [org.apache.kafka.clients.Metadata] [Producer clientId=producer-1] Cluster ID: 1ZT3YA6PROm_ptKfU-b16A
spanId:  traceId:    2023-07-23 17:28:57,100 INFO  [io.rgh.authenticator.browser.UsernamePasswordForm] ---KUPF- Validation started for username: [test-user]
spanId:  traceId:    2023-07-23 17:28:57,188 INFO  [io.rgh.utils.AuthenticatorUtil] ---KAU- Validate native password for [test-user]
spanId:  traceId:    2023-07-23 17:28:57,508 INFO  [io.rgh.authenticator.browser.UsernamePasswordForm] ---KUPF- Account is enabled for [test-user]
spanId:  traceId:    2023-07-23 17:28:57,509 INFO  [io.rgh.authenticator.browser.UsernamePasswordForm] ---KUPF- User credentials validation passed for [test-user]
spanId:  traceId:    2023-07-23 17:28:57,591 INFO  [io.rgh.provider.EventListenerProvider] ---KELP- Sending Message to Topic 'activity':
Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
  • What version of Keycloak do you use? Is it a Wildfly distribution, or a Quarkus distribution? – ahus1 Jul 23 '23 at 14:46
  • Unfortunately, Wildfly distribution yet. Keycloak version is 18.0.2 – Rasool Ghafari Jul 24 '23 at 07:55
  • I don't have experience with Wildfly, but I know that the Java Opentelemetry Agent works nicely with the Quarkus edition of Keycloak. Maybe it is worth a try. In case it doesn't work with the agent: Before I was able to see the trace IDs in the logs with the Java agent, I needed to fix it in the agent, see this PR: https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/6112 Maybe something similar is needed for those libraries as well. – ahus1 Jul 25 '23 at 20:12
  • Do you have `MDC` populated with a `trace_id`? – James R. Perkins Jul 26 '23 at 03:03
  • I don't know how to integrate `MDC` with Keycloak and couldn't find any documentation. – Rasool Ghafari Jul 26 '23 at 06:31

0 Answers0