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

Option to export logback code attributes #2518

Merged
merged 7 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ private static void enableInstrumentations(Configuration config, Map<String, Str
System.setProperty(
"otel.instrumentation.spring-scheduling.experimental-span-attributes", "true");
}
if (config.preview.captureLogbackCodeAttributes) {
properties.put(
"otel.instrumentation.logback-appender.experimental.capture-code-attributes", "true");
}
if (config.preview.instrumentation.akka.enabled) {
properties.put("otel.instrumentation.akka-actor.enabled", "true");
properties.put("otel.instrumentation.akka-http.enabled", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ private static void setItemCount(
private static final String LOGBACK_MDC_PREFIX = "logback.mdc.";
private static final String JBOSS_LOGGING_MDC_PREFIX = "jboss-logmanager.mdc.";

static void setExtraAttributes(AbstractTelemetryBuilder telemetryBuilder, Attributes attributes) {
private static void setExtraAttributes(
AbstractTelemetryBuilder telemetryBuilder, Attributes attributes) {
attributes.forEach(
(attributeKey, value) -> {
String key = attributeKey.getKey();
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand All @@ -77,7 +109,7 @@ void testTraceLogBack() throws Exception {

@Test
@TargetUri("/traceLogBackWithException")
void testTraceLogBackWithExeption() throws Exception {
void testTraceLogBackWithException() throws Exception {
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);

Envelope rdEnvelope = rdList.get(0);
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"logging": {
"level": "warn"
}
},
"preview": {
"captureLogbackCodeAttributes": true
}
}