open-telemetry / open-telemetry/opentelemetry-java-instrumentation

OpenTelemetry Log4j appender does not handle non string values

Open
#8,354 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug contribution welcome
Dominant language
Java
Stars
2.6k
Forks
1.2k
Avg merge
2d 18h
Merged PRs (30d)
228

Description

When the Log4j LogEvent context data contains non string values e.g string arrays or java maps, the resulting attributes are incorrect.

Create a class extending ObjectThreadContextMap and set it as the default using the log4j system property log4j2.threadContextMap

Add the following code:

public class MyObjectContextMap implements ReadOnlyThreadContextMap, ObjectThreadContextMap {
//Implementation methods
}

In the logging code add the following

MyObjectContextMap map = (MyObjectContextMap)ThreadContext.getThreadContextMap();
map.putValue("numbers", new String[]{"one", "two", "three"});
HashMap testmap = new HashMap();
testmap.put("fn", "firstname");
testmap.put("ln", "lastname");
testmap.put(age, 25);
map.put("testmap", testmap);

logger.log(Level.TRACE, "test message");

What did you expect to see?
I would expect that the nested value and the array value would be included in attributes properly.

What did you see instead?
I saw the .toString() representations of the array and map. So numbers showed as an attribute with value [Ljava.lang.String and testmap showed up as a single value with the map values as {fn=firstname, ln=lastname}.

What version are you using?
1.23.0-alpha
Log4j: 2.17.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by locating the OpenTelemetry Log4j appender and its handling of ThreadContext.getThreadContextMap(), then reproduce the example with the log4j2.threadContextMap system property. Done means array and map context values become proper nested attributes instead of Java toString representations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.