|
40 | 40 | import java.util.List; |
41 | 41 | import java.util.Locale; |
42 | 42 | import java.util.Map; |
| 43 | +import org.checkerframework.checker.nullness.qual.Nullable; |
43 | 44 | import org.slf4j.LoggerFactory; |
44 | 45 |
|
45 | 46 | public class ConfigurationBuilder { |
@@ -154,11 +155,15 @@ public static Configuration create(Path agentJarPath, RpConfiguration rpConfigur |
154 | 155 | + " so no need to enable it under preview configuration"); |
155 | 156 | } |
156 | 157 |
|
157 | | - overlayEnvVars(config); |
158 | | - applySamplingPercentageRounding(config); |
| 158 | + overlayFromEnv(config); |
| 159 | + config.sampling.percentage = roundToNearest(config.sampling.percentage, true); |
| 160 | + for (SamplingOverride override : config.preview.sampling.overrides) { |
| 161 | + override.percentage = roundToNearest(override.percentage, true); |
| 162 | + } |
159 | 163 | // rp configuration should always be last (so it takes precedence) |
160 | 164 | // currently applicationinsights-rp.json is only used by Azure Spring Cloud |
161 | 165 | if (rpConfiguration != null) { |
| 166 | + overlayFromEnv(rpConfiguration); |
162 | 167 | overlayRpConfiguration(config, rpConfiguration); |
163 | 168 | } |
164 | 169 | // only set role instance to host name as a last resort |
@@ -346,22 +351,8 @@ public static void logConfigurationWarnMessages() { |
346 | 351 | } |
347 | 352 |
|
348 | 353 | // visible for testing |
349 | | - static void overlayEnvVars(Configuration config) throws IOException { |
350 | | - config.connectionString = |
351 | | - overlayWithSysPropEnvVar( |
352 | | - APPLICATIONINSIGHTS_CONNECTION_STRING_SYS, |
353 | | - APPLICATIONINSIGHTS_CONNECTION_STRING_ENV, |
354 | | - config.connectionString); |
355 | | - if (config.connectionString == null) { |
356 | | - // this is for backwards compatibility only |
357 | | - String instrumentationKey = getEnvVar(APPINSIGHTS_INSTRUMENTATIONKEY); |
358 | | - if (instrumentationKey != null) { |
359 | | - configurationLogger.warn( |
360 | | - "APPINSIGHTS_INSTRUMENTATIONKEY is only supported for backwards compatibility," |
361 | | - + " please consider using APPLICATIONINSIGHTS_CONNECTION_STRING instead"); |
362 | | - config.connectionString = "InstrumentationKey=" + instrumentationKey; |
363 | | - } |
364 | | - } |
| 354 | + static void overlayFromEnv(Configuration config) throws IOException { |
| 355 | + config.connectionString = overlayConnectionStringFromEnv(config.connectionString); |
365 | 356 |
|
366 | 357 | if (isTrimEmpty(config.role.name)) { |
367 | 358 | // only use WEBSITE_SITE_NAME as a fallback |
@@ -418,11 +409,34 @@ static void overlayEnvVars(Configuration config) throws IOException { |
418 | 409 | overlayInstrumentationEnabledEnvVars(config); |
419 | 410 | } |
420 | 411 |
|
421 | | - public static void applySamplingPercentageRounding(Configuration config) { |
422 | | - config.sampling.percentage = roundToNearest(config.sampling.percentage, true); |
423 | | - for (SamplingOverride override : config.preview.sampling.overrides) { |
424 | | - override.percentage = roundToNearest(override.percentage, true); |
| 412 | + public static void overlayFromEnv(RpConfiguration config) { |
| 413 | + config.connectionString = overlayConnectionStringFromEnv(config.connectionString); |
| 414 | + config.sampling.percentage = |
| 415 | + overlayWithEnvVar(APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE, config.sampling.percentage); |
| 416 | + } |
| 417 | + |
| 418 | + @Nullable |
| 419 | + private static String overlayConnectionStringFromEnv(String connectionString) { |
| 420 | + String value = |
| 421 | + overlayWithSysPropEnvVar( |
| 422 | + APPLICATIONINSIGHTS_CONNECTION_STRING_SYS, |
| 423 | + APPLICATIONINSIGHTS_CONNECTION_STRING_ENV, |
| 424 | + connectionString); |
| 425 | + |
| 426 | + if (value != null) { |
| 427 | + return value; |
| 428 | + } |
| 429 | + |
| 430 | + // this is for backwards compatibility only |
| 431 | + String instrumentationKey = getEnvVar(APPINSIGHTS_INSTRUMENTATIONKEY); |
| 432 | + if (instrumentationKey != null) { |
| 433 | + configurationLogger.warn( |
| 434 | + "APPINSIGHTS_INSTRUMENTATIONKEY is only supported for backwards compatibility," |
| 435 | + + " please consider using APPLICATIONINSIGHTS_CONNECTION_STRING instead"); |
| 436 | + return "InstrumentationKey=" + instrumentationKey; |
425 | 437 | } |
| 438 | + |
| 439 | + return null; |
426 | 440 | } |
427 | 441 |
|
428 | 442 | // visible for testing |
|
0 commit comments