Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument SLF4J's addKeyValue(key, value) method from fluent API #4085

Open
evkaky opened this issue Feb 17, 2025 · 0 comments
Open

Instrument SLF4J's addKeyValue(key, value) method from fluent API #4085

evkaky opened this issue Feb 17, 2025 · 0 comments

Comments

@evkaky
Copy link

evkaky commented Feb 17, 2025

At our project we have a bunch of azure function written in java. Those functions are connected to the application insight through the following env variables

APPLICATIONINSIGHTS_CONNECTION_STRING=...
APPLICATIONINSIGHTS_ENABLE_AGENT=true

In java code we use SLF4J and Logback libraries for logging. We often need to attach some metadata to our logs so it appears in customDimensions field in the traces table in application insights.
So far we have been doing this by using MDC from SLF4J, so the code is something alone these lines

MDC.put("key1", "val1");
log.info("my log message");
MDC.remove("key1");

The code above makes "my log message" having "key1" field in customDimensions field in traces table in application insights. Fine. That means that MDC.put() has been properly instrumented.
But recently we discovered more convenient way to attach those additional fields to the log message.
SLF4J provides fluent api https://www.slf4j.org/manual.html#fluent which exposes addKeyValue(key, val) method for doing that (instead of putting those key+val in MDC)
So the code above can be rewritten as following:

log.atInfo()
    .addKeyValue("key1", "val1")
    .log("my log message");

Looks much cleaner, doesn't it?

Unfortunately, in this case key1="val1" entry does not appear in the customDimensions of this log message in traces table in the application insights. That means fwi_java SDK does not instrument this addKeyValue() as it does for MDC.put().

From my point of view making .addKeyValue(key, val) instrumented similarly to MDC.put(key, val) would be the right move. They both have very similar semantics, but .addKeyValue(key, val) makes the code much cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant