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

Add env vars for disabling instrumentation #1495

Merged
merged 2 commits into from
Feb 17, 2021
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 @@ -74,25 +74,20 @@ public class ConfigurationBuilder {
public static Configuration create(Path agentJarPath) throws IOException {
Configuration config = loadConfigurationFile(agentJarPath);
overlayEnvVars(config);

return config;
}

private static void loadLogCaptureEnvVar(Configuration config) {
Map<String, Object> logging = config.instrumentation.get("logging");
if (logging == null) {
logging = new HashMap<>();
config.instrumentation.put("logging", logging);
}

final String loggingEnvVar = overlayWithEnvVar(APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL, (String)null);
String loggingEnvVar = getEnvVar(APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL);
if (loggingEnvVar != null) {
logging.put("level", loggingEnvVar);
config.instrumentation
.computeIfAbsent("logging", k -> new HashMap<>())
.put("level", loggingEnvVar);
}
}

private static void loadJmxMetricsEnvVar(Configuration config) throws IOException {
String jmxMetricsEnvVarJson = overlayWithEnvVar(APPLICATIONINSIGHTS_JMX_METRICS, (String)null);
String jmxMetricsEnvVarJson = getEnvVar(APPLICATIONINSIGHTS_JMX_METRICS);

// JmxMetrics env variable has higher precedence over jmxMetrics config from applicationinsights.json
if (jmxMetricsEnvVarJson != null && !jmxMetricsEnvVarJson.isEmpty()) {
Expand Down Expand Up @@ -131,13 +126,33 @@ private static boolean jmxMetricExisted(List<Configuration.JmxMetric> jmxMetrics
return false;
}

private static void loadInstrumentationEnabledEnvVars(Configuration config) {
loadInstrumentationEnabledEnvVar(config, "micrometer", "APPLICATIONINSIGHTS_INSTRUMENTATION_MICROMETER_ENABLED");
loadInstrumentationEnabledEnvVar(config, "jdbc", "APPLICATIONINSIGHTS_INSTRUMENTATION_JDBC_ENABLED");
loadInstrumentationEnabledEnvVar(config, "redis", "APPLICATIONINSIGHTS_INSTRUMENTATION_REDIS_ENABLED");
loadInstrumentationEnabledEnvVar(config, "kafka", "APPLICATIONINSIGHTS_INSTRUMENTATION_KAFKA_ENABLED");
loadInstrumentationEnabledEnvVar(config, "jms", "APPLICATIONINSIGHTS_INSTRUMENTATION_JMS_ENABLED");
loadInstrumentationEnabledEnvVar(config, "mongo", "APPLICATIONINSIGHTS_INSTRUMENTATION_MONGO_ENABLED");
loadInstrumentationEnabledEnvVar(config, "cassandra", "APPLICATIONINSIGHTS_INSTRUMENTATION_CASSANDRA_ENABLED");
loadInstrumentationEnabledEnvVar(config, "spring-scheduling", "APPLICATIONINSIGHTS_INSTRUMENTATION_SPRING_SCHEDULING_ENABLED");
}

private static void loadInstrumentationEnabledEnvVar(Configuration config, String instrumentationName, String envVarName) {
String instrumentationEnabledEnvVar = getEnvVar(envVarName);
if (instrumentationEnabledEnvVar != null) {
Map<String, Object> properties = config.instrumentation.computeIfAbsent(instrumentationName, k -> new HashMap<>());
// intentionally allowing NumberFormatException to bubble up as invalid configuration and prevent agent from starting
properties.put("enabled", Boolean.parseBoolean(instrumentationEnabledEnvVar));
}
}

private static Configuration loadConfigurationFile(Path agentJarPath) throws IOException {
if (DiagnosticsHelper.isAnyCodelessAttach()) {
// codeless attach only supports configuration via environment variables (for now at least)
return new Configuration();
}

String configPathStr = getEnvVarOrProperty(APPLICATIONINSIGHTS_CONFIGURATION_FILE, "applicationinsights.configuration.file");
String configPathStr = getConfigPath();
if (configPathStr != null) {
Path configPath = agentJarPath.resolveSibling(configPathStr);
if (Files.exists(configPath)) {
Expand Down Expand Up @@ -168,17 +183,6 @@ public static void logConfigurationMessages() {
}
}

// never returns empty string (empty string is normalized to null)
private static String getEnvVar(String name) {
return trimAndEmptyToNull(System.getenv(name));
}

// never returns empty string (empty string is normalized to null)
private static String getEnvVarOrProperty(String envVarName, String propertyName) {
String value = trimAndEmptyToNull(System.getenv(envVarName));
return value != null ? value : trimAndEmptyToNull(System.getProperty(propertyName));
}

public static void overlayEnvVars(Configuration config) throws IOException {
config.connectionString = overlayWithEnvVar(APPLICATIONINSIGHTS_CONNECTION_STRING, config.connectionString);
if (config.connectionString == null) {
Expand All @@ -192,13 +196,13 @@ public static void overlayEnvVars(Configuration config) throws IOException {

if (isTrimEmpty(config.role.name)) {
// only use WEBSITE_SITE_NAME as a fallback
config.role.name = getEnv(WEBSITE_SITE_NAME);
config.role.name = getEnvVar(WEBSITE_SITE_NAME);
}
config.role.name = overlayWithEnvVar(APPLICATIONINSIGHTS_ROLE_NAME, config.role.name);

if (isTrimEmpty(config.role.instance)) {
// only use WEBSITE_INSTANCE_ID as a fallback
config.role.instance = getEnv(WEBSITE_INSTANCE_ID);
config.role.instance = getEnvVar(WEBSITE_INSTANCE_ID);
}
config.role.instance = overlayWithEnvVar(APPLICATIONINSIGHTS_ROLE_INSTANCE, config.role.instance);

Expand All @@ -210,37 +214,47 @@ public static void overlayEnvVars(Configuration config) throws IOException {
loadJmxMetricsEnvVar(config);

addDefaultJmxMetricsIfNotPresent(config);
}

static String overlayWithEnvVar(String name, String defaultValue) {
String value = getEnv(name);
if (value != null && !value.isEmpty()) {
return value;
}

return defaultValue;
loadInstrumentationEnabledEnvVars(config);
}

static Double overlayWithEnvVar(String name, Double defaultValue) {
String value = getEnv(name);
if (value != null && !value.isEmpty()) {
return Double.parseDouble(value);
private static String getConfigPath() {
String value = getEnvVar(APPLICATIONINSIGHTS_CONFIGURATION_FILE);
if (value != null) {
return value;
}

return defaultValue;
// intentionally not checking system properties for other system properties
// with the intention to keep configuration paths minimal to help with supportability
return trimAndEmptyToNull(System.getProperty("applicationinsights.configuration.file"));
}

private static String getEnv(String name) {
String value = System.getenv(name);
private static String getWebsiteSiteNameEnvVar() {
String value = getEnvVar(WEBSITE_SITE_NAME);
// TODO is the best way to identify running as Azure Functions worker?
// TODO is this the correct way to match role name from Azure Functions IIS host?
if (name.equals("WEBSITE_SITE_NAME") && "java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
// special case for Azure Functions
value = value.toLowerCase(Locale.ENGLISH);
return value.toLowerCase(Locale.ENGLISH);
}
return value;
}

static String overlayWithEnvVar(String name, String defaultValue) {
String value = getEnvVar(name);
return value != null ? value : defaultValue;
}

static double overlayWithEnvVar(String name, double defaultValue) {
String value = getEnvVar(name);
// intentionally allowing NumberFormatException to bubble up as invalid configuration and prevent agent from starting
return value != null ? Double.parseDouble(value) : defaultValue;
}

// never returns empty string (empty string is normalized to null)
private static String getEnvVar(String name) {
return trimAndEmptyToNull(System.getenv(name));
}

// visible for testing
static String trimAndEmptyToNull(String str) {
if (str == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -279,6 +280,27 @@ public void shouldOverrideJmxMetrics() throws IOException {
assertEquals(configuration.jmxMetrics.get(2).name, "Current Thread Count");
}

@Test
public void shouldOverrideInstrumentationEnabled() throws IOException {
shouldOverrideInstrumentationEnable("micrometer");
shouldOverrideInstrumentationEnable("jdbc");
shouldOverrideInstrumentationEnable("redis");
shouldOverrideInstrumentationEnable("kafka");
shouldOverrideInstrumentationEnable("jms");
shouldOverrideInstrumentationEnable("mongo");
shouldOverrideInstrumentationEnable("cassandra");
shouldOverrideInstrumentationEnable("spring-scheduling");
}

private void shouldOverrideInstrumentationEnable(String instrumentationName) throws IOException {
envVars.set("APPLICATIONINSIGHTS_INSTRUMENTATION_" + instrumentationName.replace('-', '_').toUpperCase(Locale.ROOT) + "_ENABLED", "false");

Configuration configuration = loadConfiguration();
ConfigurationBuilder.overlayEnvVars(configuration);

assertEquals(false, configuration.instrumentation.get(instrumentationName).get("enabled"));
}

@Test(expected = JsonDataException.class)
public void shouldNotParseFaultyJson() throws IOException {
Configuration configuration = loadConfiguration("applicationinsights_faulty.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,18 @@ private static void start(Instrumentation instrumentation) throws Exception {
if (!isInstrumentationEnabled(config, "kafka")) {
properties.put("otel.instrumentation.kafka.enabled", "false");
}
if (!isInstrumentationEnabled(config, "jms")) {
properties.put("otel.instrumentation.jms.enabled", "false");
}
if (!isInstrumentationEnabled(config, "mongo")) {
properties.put("otel.instrumentation.mongo.enabled", "false");
}
if (!isInstrumentationEnabled(config, "cassandra")) {
properties.put("otel.instrumentation.cassandra.enabled", "false");
}
if (!isInstrumentationEnabled(config, "spring-scheduling")) {
properties.put("otel.instrumentation.spring-scheduling.enabled", "false");
}
if (!config.preview.openTelemetryApiSupport) {
properties.put("otel.instrumentation.opentelemetry-api.enabled", "false");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"cassandra": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"jdbc": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"jms": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"kafka": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"micrometer": {
"reportingIntervalSeconds": 5,
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"mongo": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"redis": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"connectionString": "InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/",
"instrumentation": {
"spring-scheduling": {
"enabled": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.microsoft.applicationinsights.smoketest;

import java.util.List;

import com.microsoft.applicationinsights.internal.schemav2.Data;
import com.microsoft.applicationinsights.internal.schemav2.Envelope;
import com.microsoft.applicationinsights.internal.schemav2.RemoteDependencyData;
import com.microsoft.applicationinsights.internal.schemav2.RequestData;
import org.junit.*;

import static org.junit.Assert.*;

@UseAgent("disabled_redis")
@WithDependencyContainers(@DependencyContainer(value="redis", portMapping="6379"))
public class JedisDisabledTest extends AiSmokeTest {

@Test
@TargetUri("/index.jsp")
public void doCalcSendsRequestDataAndMetricData() throws Exception {
List<Envelope> rdList = mockedIngestion.waitForItems("RequestData", 1);
Envelope rdEnvelope = rdList.get(0);
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();

assertTrue(rd.getSuccess());
assertEquals("/CachingCalculator/index.jsp", rd.getName());
assertEquals("200", rd.getResponseCode());

// sleep a bit and make sure no jedis dependencies are reported
Thread.sleep(5000);
assertEquals(0, mockedIngestion.getCountForType("RemoteDependencyData"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@UseAgent
@WithDependencyContainers(@DependencyContainer(value="redis", portMapping="6379"))
public class SampleTestWithDependencyContainer extends AiSmokeTest {
public class JedisTest extends AiSmokeTest {

@Test
@TargetUri("/index.jsp")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.microsoft.applicationinsights.smoketestapp;

import java.util.List;

import com.microsoft.applicationinsights.internal.schemav2.Data;
import com.microsoft.applicationinsights.internal.schemav2.Envelope;
import com.microsoft.applicationinsights.internal.schemav2.RequestData;
import com.microsoft.applicationinsights.smoketest.AiSmokeTest;
import com.microsoft.applicationinsights.smoketest.DependencyContainer;
import com.microsoft.applicationinsights.smoketest.TargetUri;
import com.microsoft.applicationinsights.smoketest.UseAgent;
import com.microsoft.applicationinsights.smoketest.WithDependencyContainers;
import org.junit.*;

import static org.junit.Assert.*;

@UseAgent("disabled_cassandra")
@WithDependencyContainers(
@DependencyContainer(
value = "cassandra:3",
portMapping = "9042",
hostnameEnvironmentVariable = "CASSANDRA")
)
public class CassandraDisabledTest extends AiSmokeTest {

@Test
@TargetUri("/cassandra")
public void cassandra() throws Exception {
List<Envelope> rdList = mockedIngestion.waitForItems("RequestData", 1);
Envelope rdEnvelope = rdList.get(0);
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();

assertTrue(rd.getSuccess());
assertEquals("HTTP GET", rd.getName());
assertEquals("200", rd.getResponseCode());

// sleep a bit and make sure no cassandra dependencies are reported
Thread.sleep(5000);
assertEquals(0, mockedIngestion.getCountForType("RemoteDependencyData"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
portMapping = "9042",
hostnameEnvironmentVariable = "CASSANDRA")
)
public class CassandraSmokeTest extends AiSmokeTest {
public class CassandraTest extends AiSmokeTest {

@Test
@TargetUri("/cassandra")
Expand Down
2 changes: 2 additions & 0 deletions test/smoke/testApps/JMS/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ dependencies {

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.7.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '2.1.7.RELEASE'

compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.7'
}
Loading