diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java index 4f1af282503..650e74ce6fb 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java @@ -312,6 +312,8 @@ public static class PreviewConfiguration { // Note: this configuration option will be removed in 4.0.0 public boolean captureLoggingLevelAsCustomDimension; + public boolean captureLogbackCodeAttributes; + // this is to support interoperability with other systems // intentionally not allowing the removal of w3c propagator since that is key to many Azure // integrated experiences diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java index 6f8efa9c162..e6ff1e0de35 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java @@ -252,6 +252,10 @@ private static void enableInstrumentations(Configuration config, Map { String key = attributeKey.getKey(); @@ -153,6 +154,22 @@ static void setExtraAttributes(AbstractTelemetryBuilder telemetryBuilder, Attrib key.substring(LOGBACK_MDC_PREFIX.length()), String.valueOf(value)); return; } + if (SemanticAttributes.CODE_FILEPATH.getKey().equals(key)) { + telemetryBuilder.addProperty("FileName", String.valueOf(value)); + return; + } + if (SemanticAttributes.CODE_NAMESPACE.getKey().equals(key)) { + telemetryBuilder.addProperty("ClassName", String.valueOf(value)); + return; + } + if (SemanticAttributes.CODE_FUNCTION.getKey().equals(key)) { + telemetryBuilder.addProperty("MethodName", String.valueOf(value)); + return; + } + if (SemanticAttributes.CODE_LINENO.getKey().equals(key)) { + telemetryBuilder.addProperty("LineNumber", String.valueOf(value)); + return; + } if (key.startsWith(JBOSS_LOGGING_MDC_PREFIX)) { telemetryBuilder.addProperty( key.substring(JBOSS_LOGGING_MDC_PREFIX.length()), String.valueOf(value)); diff --git a/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/TraceLogBackTest.java b/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/TraceLogBackTest.java index 1dde1670038..9f0002e6476 100644 --- a/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/TraceLogBackTest.java +++ b/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/TraceLogBackTest.java @@ -30,6 +30,10 @@ abstract class TraceLogBackTest { @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); + boolean checkLogBackCodeAttributes() { + return true; + } + @Test @TargetUri("/traceLogBack") void testTraceLogBack() throws Exception { @@ -60,14 +64,42 @@ void testTraceLogBack() throws Exception { assertThat(md1.getProperties()).containsEntry("LoggerName", "smoketestapp"); assertThat(md1.getProperties()).containsKey("ThreadName"); assertThat(md1.getProperties()).containsEntry("MDC key", "MDC value"); - assertThat(md1.getProperties()).hasSize(4); + + if (checkLogBackCodeAttributes()) { + assertThat(md1.getProperties()) + .containsEntry("FileName", "SimpleTestTraceLogBackServlet.java"); + assertThat(md1.getProperties()) + .containsEntry( + "ClassName", + "com.microsoft.applicationinsights.smoketestapp.SimpleTestTraceLogBackServlet"); + assertThat(md1.getProperties()).containsEntry("MethodName", "doGet"); + assertThat(md1.getProperties()).containsEntry("LineNumber", "24"); + + assertThat(md1.getProperties()).hasSize(8); + } else { + assertThat(md1.getProperties()).hasSize(4); + } assertThat(md2.getMessage()).isEqualTo("This is logback error."); assertThat(md2.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); assertThat(md2.getProperties()).containsEntry("SourceType", "Logger"); assertThat(md2.getProperties()).containsEntry("LoggerName", "smoketestapp"); assertThat(md2.getProperties()).containsKey("ThreadName"); - assertThat(md2.getProperties()).hasSize(3); + + if (checkLogBackCodeAttributes()) { + assertThat(md2.getProperties()) + .containsEntry("FileName", "SimpleTestTraceLogBackServlet.java"); + assertThat(md2.getProperties()) + .containsEntry( + "ClassName", + "com.microsoft.applicationinsights.smoketestapp.SimpleTestTraceLogBackServlet"); + assertThat(md2.getProperties()).containsEntry("MethodName", "doGet"); + assertThat(md2.getProperties()).containsEntry("LineNumber", "26"); + + assertThat(md2.getProperties()).hasSize(7); + } else { + assertThat(md2.getProperties()).hasSize(3); + } SmokeTestExtension.assertParentChild( rd, rdEnvelope, mdEnvelope1, "GET /TraceLogBackUsingAgent/traceLogBack"); @@ -77,7 +109,7 @@ void testTraceLogBack() throws Exception { @Test @TargetUri("/traceLogBackWithException") - void testTraceLogBackWithExeption() throws Exception { + void testTraceLogBackWithException() throws Exception { List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); Envelope rdEnvelope = rdList.get(0); @@ -101,7 +133,20 @@ void testTraceLogBackWithExeption() throws Exception { assertThat(ed.getProperties()).containsEntry("LoggerName", "smoketestapp"); assertThat(ed.getProperties()).containsKey("ThreadName"); assertThat(ed.getProperties()).containsEntry("MDC key", "MDC value"); - assertThat(ed.getProperties()).hasSize(5); + + if (checkLogBackCodeAttributes()) { + assertThat(ed.getProperties()) + .containsEntry("FileName", "SimpleTestTraceLogBackWithExceptionServlet.java"); + assertThat(ed.getProperties()) + .containsEntry( + "ClassName", + "com.microsoft.applicationinsights.smoketestapp.SimpleTestTraceLogBackWithExceptionServlet"); + assertThat(ed.getProperties()).containsEntry("MethodName", "doGet"); + assertThat(ed.getProperties()).containsEntry("LineNumber", "21"); + assertThat(ed.getProperties()).hasSize(9); + } else { + assertThat(ed.getProperties()).hasSize(5); + } SmokeTestExtension.assertParentChild( rd, rdEnvelope, edEnvelope, "GET /TraceLogBackUsingAgent/traceLogBackWithException"); @@ -129,8 +174,18 @@ static class Tomcat8Java18Test extends TraceLogBackTest {} static class Tomcat8Java19Test extends TraceLogBackTest {} @Environment(WILDFLY_13_JAVA_8) - static class Wildfly13Java8Test extends TraceLogBackTest {} + static class Wildfly13Java8Test extends TraceLogBackTest { + @Override + boolean checkLogBackCodeAttributes() { + return false; + } + } @Environment(WILDFLY_13_JAVA_8_OPENJ9) - static class Wildfly13Java8OpenJ9Test extends TraceLogBackTest {} + static class Wildfly13Java8OpenJ9Test extends TraceLogBackTest { + @Override + boolean checkLogBackCodeAttributes() { + return false; + } + } } diff --git a/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/resources/applicationinsights.json b/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/resources/applicationinsights.json index 96680a4fd60..85e82dbd77e 100644 --- a/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/resources/applicationinsights.json +++ b/smoke-tests/apps/TraceLogBackUsingAgent/src/smokeTest/resources/applicationinsights.json @@ -10,5 +10,8 @@ "logging": { "level": "warn" } + }, + "preview": { + "captureLogbackCodeAttributes": true } }