Skip to content

Commit 2dc22f4

Browse files
authored
Add a config to disable all logging appenders (#3208)
1 parent f3e3b72 commit 2dc22f4

File tree

11 files changed

+212
-0
lines changed

11 files changed

+212
-0
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public static class DatabaseMaskingConfiguration {
226226

227227
public static class LoggingInstrumentation {
228228
public String level = "INFO";
229+
public boolean enabled = true;
229230

230231
public int getSeverityThreshold() {
231232
return getSeverityThreshold(level);

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public Map<String, String> apply(ConfigProperties otelConfig) {
7070
properties.put("otel.instrumentation.log4j-appender.experimental-log-attributes", "true");
7171
properties.put("otel.instrumentation.logback-appender.experimental-log-attributes", "true");
7272

73+
// disable logging appender
74+
if (!configuration.instrumentation.logging.enabled) {
75+
properties.put("otel.instrumentation.logback-appender.enabled", "false");
76+
properties.put("otel.instrumentation.log4j-appender.enabled", "false");
77+
properties.put("otel.instrumentation.java-util-logging.enabled", "false");
78+
}
79+
7380
// custom instrumentation
7481
if (!configuration.preview.customInstrumentation.isEmpty()) {
7582
StringBuilder sb = new StringBuilder();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketestapp;
5+
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
import javax.servlet.annotation.WebServlet;
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
@WebServlet("/testDisabled")
14+
public class JavaUtilLoggingDisabledServlet extends HttpServlet {
15+
16+
private static final Logger logger = Logger.getLogger("smoketestapp");
17+
18+
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
19+
logger.log(Level.WARNING, "this message will get suppressed.");
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest;
5+
6+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
10+
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
11+
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
12+
import java.util.List;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
16+
@Environment(TOMCAT_8_JAVA_8)
17+
@UseAgent("disabled_applicationinsights.json")
18+
class JavaUtilLoggingDisabledTest {
19+
20+
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
21+
22+
@Test
23+
@TargetUri("/testDisabled")
24+
void testDisabled() throws Exception {
25+
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
26+
27+
Envelope rdEnvelope = rdList.get(0);
28+
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
29+
assertThat(rd.getName()).isEqualTo("GET /JavaUtilLogging/testDisabled");
30+
31+
assertThat(testing.mockedIngestion.getCountForType("MessageData")).isZero();
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"role": {
3+
"name": "testrolename",
4+
"instance": "testroleinstance"
5+
},
6+
"sampling": {
7+
"percentage": 100
8+
},
9+
"instrumentation": {
10+
"logging": {
11+
"enabled": "false"
12+
}
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketestapp;
5+
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import org.apache.logging.log4j.LogManager;
11+
import org.apache.logging.log4j.Logger;
12+
13+
@WebServlet("/testDisabled")
14+
public class Log4j2DisabledServlet extends HttpServlet {
15+
16+
private static final Logger logger = LogManager.getLogger("smoketestapp");
17+
18+
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
19+
logger.warn("This message will get suppressed");
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest;
5+
6+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
10+
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
11+
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
12+
import java.util.List;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
16+
@Environment(TOMCAT_8_JAVA_8)
17+
@UseAgent("disabled_applicationinsights.json")
18+
class Log4j2DisabledTest {
19+
20+
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
21+
22+
@Test
23+
@TargetUri("/testDisabled")
24+
void testDisabled() throws Exception {
25+
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
26+
27+
Envelope rdEnvelope = rdList.get(0);
28+
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
29+
assertThat(rd.getName()).isEqualTo("GET /Log4j2/testDisabled");
30+
31+
assertThat(testing.mockedIngestion.getCountForType("MessageData")).isZero();
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"role": {
3+
"name": "testrolename",
4+
"instance": "testroleinstance"
5+
},
6+
"sampling": {
7+
"percentage": 100
8+
},
9+
"instrumentation": {
10+
"logging": {
11+
"enabled": "false"
12+
}
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketestapp;
5+
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
@WebServlet("/testDisabled")
14+
public class LogbackDisabledServlet extends HttpServlet {
15+
16+
private static final Logger logger = LoggerFactory.getLogger("smoketestapp");
17+
18+
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
19+
logger.warn("This message will get suppressed");
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest;
5+
6+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
10+
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
11+
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
12+
import java.util.List;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
16+
@Environment(TOMCAT_8_JAVA_8)
17+
@UseAgent("disabled_applicationinsights.json")
18+
class LogbackDisabledTest {
19+
20+
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
21+
22+
@Test
23+
@TargetUri("/testDisabled")
24+
void testDisabled() throws Exception {
25+
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
26+
27+
Envelope rdEnvelope = rdList.get(0);
28+
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
29+
assertThat(rd.getName()).isEqualTo("GET /Logback/testDisabled");
30+
31+
assertThat(testing.mockedIngestion.getCountForType("MessageData")).isZero();
32+
}
33+
}

0 commit comments

Comments
 (0)