Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

ClassCastException with Log4j log correlation #1436

Closed
sebright opened this issue Sep 15, 2018 · 0 comments · Fixed by #1437
Closed

ClassCastException with Log4j log correlation #1436

sebright opened this issue Sep 15, 2018 · 0 comments · Fixed by #1437

Comments

@sebright
Copy link
Contributor

I got a ClassCastException with opencensus-contrib-log-correlation-log4j2 when using it with a Log4j JsonLayout:

2018-09-14 20:59:52,690 main ERROR An exception occurred processing Appender File java.lang.ClassCastException: io.opencensus.contrib.logcorrelation.log4j2.ContextDataUtils$TraceIdToLowerBase16Formatter cannot be cast to java.lang.String
        at org.apache.logging.log4j.core.lookup.ContextMapLookup.lookup(ContextMapLookup.java:58)
        at org.apache.logging.log4j.core.lookup.Interpolator.lookup(Interpolator.java:188)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.resolveVariable(StrSubstitutor.java:1060)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:982)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:878)
        at org.apache.logging.log4j.core.lookup.StrSubstitutor.replace(StrSubstitutor.java:433)
        at org.apache.logging.log4j.core.layout.AbstractJacksonLayout.resolveAdditionalFields(AbstractJacksonLayout.java:292)
        at org.apache.logging.log4j.core.layout.AbstractJacksonLayout.wrapLogEvent(AbstractJacksonLayout.java:274)
        at org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:304)
        at org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(JsonLayout.java:291)
        at org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:255)
        at org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(JsonLayout.java:68)
        at org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:44)
        at org.apache.logging.log4j.core.layout.AbstractStringLayout.toByteArray(AbstractStringLayout.java:304)
        at org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:210)
        at org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:37)
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:177)
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170)
        at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161)
        at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
        at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
        at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
        at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
        at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
        at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
        at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
        at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2002)
        at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1974)
        at org.apache.logging.log4j.spi.AbstractLogger.warn(AbstractLogger.java:2624)
        at io.opencensus.contrib.logcorrelation.log4j.demo.OpenCensusLog4jLogCorrelationDemo.doWork(OpenCensusLog4jLogCorrelationDemo.java:60)
        at io.opencensus.contrib.logcorrelation.log4j.demo.OpenCensusLog4jLogCorrelationDemo.createSampledTrace(OpenCensusLog4jLogCorrelationDemo.java:50)
        at io.opencensus.contrib.logcorrelation.log4j.demo.OpenCensusLog4jLogCorrelationDemo.main(OpenCensusLog4jLogCorrelationDemo.java:44)

I think the issue is that the OpenCensus ContextDataInjector adds Object values to the Log4j context, but Log4j doesn't call String.valueOf on those Objects on all code paths.

@sebright sebright self-assigned this Sep 15, 2018
sebright added a commit to sebright/opencensus-java that referenced this issue Sep 15, 2018
Fixes census-instrumentation#1436.

Log4j's SortedArrayStringMap can contain Object values, but
`SortedArrayStringMap.getValue` has signature `<V> V getValue(String key)` and
unsafely casts the Objects to type `V`.  When the OpenCensus ContextDataInjector
returned a SortedArrayStringMap, and then a Log4j ContextMapLookup looked up
values as Strings to insert them into log entries
(https://github.com/apache/logging-log4j2/blob/fa27894c13c3890e4ae545f6b6365ea2e159757c/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java#L58),
it resulted in a ClassCastException.

This commit fixes the ClassCastException by only inserting String values into
the Log4j SortedArrayStringMap.
sebright added a commit that referenced this issue Sep 17, 2018
…1437)

Fixes #1436.

Log4j's SortedArrayStringMap can contain Object values, but
`SortedArrayStringMap.getValue` has signature `<V> V getValue(String key)` and
unsafely casts the Objects to type `V`.  When the OpenCensus ContextDataInjector
returned a SortedArrayStringMap, and then a Log4j ContextMapLookup looked up
values as Strings to insert them into log entries
(https://github.com/apache/logging-log4j2/blob/fa27894c13c3890e4ae545f6b6365ea2e159757c/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java#L58),
it resulted in a ClassCastException.

This commit fixes the ClassCastException by only inserting String values into
the Log4j SortedArrayStringMap.
sebright added a commit to sebright/opencensus-java that referenced this issue Sep 18, 2018
…ensus-instrumentation#1437)

Fixes census-instrumentation#1436.

Log4j's SortedArrayStringMap can contain Object values, but
`SortedArrayStringMap.getValue` has signature `<V> V getValue(String key)` and
unsafely casts the Objects to type `V`.  When the OpenCensus ContextDataInjector
returned a SortedArrayStringMap, and then a Log4j ContextMapLookup looked up
values as Strings to insert them into log entries
(https://github.com/apache/logging-log4j2/blob/fa27894c13c3890e4ae545f6b6365ea2e159757c/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java#L58),
it resulted in a ClassCastException.

This commit fixes the ClassCastException by only inserting String values into
the Log4j SortedArrayStringMap.

(cherry picked from commit 7aab20a)
sebright added a commit to sebright/opencensus-experiments that referenced this issue Sep 18, 2018
opencensus-java 0.16.1 has a fix for
census-instrumentation/opencensus-java#1436, which
affected the Log4j log correlation demo.
sebright added a commit to census-ecosystem/opencensus-experiments that referenced this issue Sep 18, 2018
opencensus-java 0.16.1 has a fix for
census-instrumentation/opencensus-java#1436, which
affected the Log4j log correlation demo.
rghetia pushed a commit to census-ecosystem/opencensus-experiments that referenced this issue Dec 12, 2018
opencensus-java 0.16.1 has a fix for
census-instrumentation/opencensus-java#1436, which
affected the Log4j log correlation demo.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant