ApplicationInsights-Java icon indicating copy to clipboard operation
ApplicationInsights-Java copied to clipboard

Unable to propagate UserId via MDC

Open MatthiasMpo opened this issue 2 years ago • 4 comments

Expected behavior

When I insert the userId into the logging context via MDC, I would expect that it would be mapped correctly.

Actual behavior

UserId is only visible as custom properties.

To Reproduce

// does not work
MDC.put("enduser.id", userId)
// does work
Span.current().setAttribute("enduser.id", userId)

System information

Please provide the following information:

  • SDK Version: 3.4.15
  • OS type and version: Windows
  • Application Server type and version (if applicable):
  • Using spring-boot? Yes (3.1.2)
  • Additional relevant libraries (with version, if applicable):

image

MatthiasMpo avatar Aug 17 '23 10:08 MatthiasMpo

hi @MatthiasMpo! MDC only applies to logs

trask avatar Aug 18 '23 15:08 trask

hi @MatthiasMpo! MDC only applies to logs

Hi @trask, maybe I was not clear. I set enduser.id via MDC and Span Attribute. After that only my requests and Dependencies had the correct user. Traces did not. I would expect that the MDC properties are also able to map to Application Insights "default" properties. Or is there another way to set the User Id for Traces in Java? In all my dotnet application it works via ITelemetryInitializer.

MatthiasMpo avatar Aug 23 '23 06:08 MatthiasMpo

does this work for other MDC attributes, e.g. xyz? just trying to understand if it's something specific to enduser.id or not, thx

trask avatar Aug 23 '23 17:08 trask

I also tried it with user_agent.original (SemanticConventions). It was respected in Request via Span, but not with MDC. If I use xyz I see in traces and request xyz as customDimensions.

I found enduser.id in the Microsoft Docu for Application Insights in Java: https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-add-modify?tabs=java#set-the-user-id-or-authenticated-user-id

MatthiasMpo avatar Aug 29 '23 16:08 MatthiasMpo