Skip to content

Commit 4a0a1df

Browse files
authored
Fix json polling (#1436)
1 parent 5ff7d04 commit 4a0a1df

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/configuration/ConfigurationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private static String getEnvVarOrProperty(String envVarName, String propertyName
176176
return value != null ? value : trimAndEmptyToNull(System.getProperty(propertyName));
177177
}
178178

179-
static void overlayEnvVars(Configuration config) throws IOException {
179+
public static void overlayEnvVars(Configuration config) throws IOException {
180180
config.connectionString = overlayWithEnvVar(APPLICATIONINSIGHTS_CONNECTION_STRING, config.connectionString);
181181
if (config.connectionString == null) {
182182
// this is for backwards compatibility only

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class JsonConfigPolling implements Runnable {
4848
private volatile double lastReadSamplingPercentage;
4949
private static final Logger logger = LoggerFactory.getLogger(JsonConfigPolling.class);
5050

51-
private JsonConfigPolling(Path path, long lastModifiedTime, double lastReadSamplingPercentage) {
51+
// visible for testing
52+
JsonConfigPolling(Path path, long lastModifiedTime, double lastReadSamplingPercentage) {
5253
this.path = path;
5354
this.lastModifiedTime = lastModifiedTime;
5455
this.lastReadSamplingPercentage = lastReadSamplingPercentage;
@@ -80,16 +81,14 @@ public void run() {
8081
if (lastModifiedTime != fileTime.toMillis()) {
8182
lastModifiedTime = fileTime.toMillis();
8283
Configuration configuration = ConfigurationBuilder.loadJsonConfigFile(path);
84+
// important to overlay env vars here, so that we don't overwrite the value set by env var
85+
ConfigurationBuilder.overlayEnvVars(configuration);
8386

84-
// TODO only want to update connectionString with value from configuration file if original value
85-
// is from configuration file (not if original value is from APPLICATIONINSIGHTS_CONNECTION_STRING env var)
8687
if (!configuration.connectionString.equals(TelemetryConfiguration.getActive().getConnectionString())) {
8788
logger.debug("Connection string from the JSON config file is overriding the previously configured connection string.");
8889
TelemetryConfiguration.getActive().setConnectionString(configuration.connectionString);
8990
}
9091

91-
// TODO only want to update sampling percentage with value from configuration file if original value
92-
// is from configuration file (not if original value is from APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE env var)
9392
if (configuration.sampling.percentage != lastReadSamplingPercentage) {
9493
logger.debug("Updating sampling percentage from {} to {}", lastReadSamplingPercentage, configuration.sampling.percentage);
9594
double roundedSamplingPercentage = SamplingPercentage.roundToNearest(configuration.sampling.percentage);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* ApplicationInsights-Java
3+
* Copyright (c) Microsoft Corporation
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
8+
* software and associated documentation files (the ""Software""), to deal in the Software
9+
* without restriction, including without limitation the rights to use, copy, modify, merge,
10+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11+
* persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
* DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package com.microsoft.applicationinsights.agent.internal;
23+
24+
import java.io.File;
25+
import java.nio.file.Path;
26+
27+
import com.google.common.io.Resources;
28+
import com.microsoft.applicationinsights.TelemetryConfiguration;
29+
import com.microsoft.applicationinsights.agent.internal.sampling.SamplingPercentage;
30+
import io.opentelemetry.sdk.OpenTelemetrySdk;
31+
import io.opentelemetry.sdk.trace.config.TraceConfig;
32+
import org.junit.*;
33+
import org.junit.contrib.java.lang.system.*;
34+
35+
import static org.junit.Assert.*;
36+
37+
public class JsonConfigPollingTest {
38+
39+
@Rule
40+
public EnvironmentVariables envVars = new EnvironmentVariables();
41+
42+
@AfterClass
43+
public static void tearDown() {
44+
// need to reset trace config back to default (with default sampler)
45+
// otherwise tests run after this can fail
46+
OpenTelemetrySdk.getGlobalTracerManagement().updateActiveTraceConfig(TraceConfig.getDefault());
47+
}
48+
49+
@Test
50+
public void shouldUpdate() {
51+
// given
52+
TelemetryConfiguration.getActive().setConnectionString("InstrumentationKey=00000000-0000-0000-0000-000000000000");
53+
Global.setSamplingPercentage(SamplingPercentage.roundToNearest(90));
54+
55+
// when
56+
Path path = new File(Resources.getResource("applicationinsights.json").getPath()).toPath();
57+
new JsonConfigPolling(path, 0, 90).run();
58+
59+
// then
60+
assertEquals("InstrumentationKey=11111111-1111-1111-1111-111111111111", TelemetryConfiguration.getActive().getConnectionString());
61+
assertEquals(Global.getSamplingPercentage(), 10, 0);
62+
}
63+
64+
@Test
65+
public void shouldNotUpdate() {
66+
// given
67+
TelemetryConfiguration.getActive().setConnectionString("InstrumentationKey=00000000-0000-0000-0000-000000000000");
68+
Global.setSamplingPercentage(SamplingPercentage.roundToNearest(90));
69+
envVars.set("APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=00000000-0000-0000-0000-000000000000");
70+
envVars.set("APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE", "90");
71+
72+
// when
73+
Path path = new File(Resources.getResource("applicationinsights.json").getPath()).toPath();
74+
new JsonConfigPolling(path, 0, 90).run();
75+
76+
// then
77+
// FIXME uncomment this after https://github.com/microsoft/ApplicationInsights-Java/pull/1431 is merged
78+
// assertEquals("InstrumentationKey=00000000-0000-0000-0000-000000000000", TelemetryConfiguration.getActive().getConnectionString());
79+
assertEquals(Global.getSamplingPercentage(), SamplingPercentage.roundToNearest(90), 0);
80+
}
81+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"connectionString": "InstrumentationKey=11111111-1111-1111-1111-111111111111",
3+
"sampling": {
4+
"percentage": 10
5+
}
6+
}

0 commit comments

Comments
 (0)