|
| 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 | +} |
0 commit comments