Tests for functionality: Config fallback to properties files
This commit is contained in:
parent
6fc69ba64e
commit
1d57a5873b
|
@ -191,11 +191,15 @@ public class Config {
|
|||
@Getter private final List<String> traceExecutors;
|
||||
|
||||
@Getter private final boolean traceAnalyticsEnabled;
|
||||
private static final Properties propertiesFromConfigFile = loadConfigurationFile();
|
||||
|
||||
// Values from an optionally provided properties file
|
||||
private static Properties propertiesFromConfigFile;
|
||||
|
||||
// Read order: System Properties -> Env Variables, [-> default value]
|
||||
// Visible for testing
|
||||
Config() {
|
||||
propertiesFromConfigFile = loadConfigurationFile();
|
||||
|
||||
runtimeId = UUID.randomUUID().toString();
|
||||
|
||||
serviceName = getSettingFromEnvironment(SERVICE_NAME, DEFAULT_SERVICE_NAME);
|
||||
|
@ -868,7 +872,7 @@ public class Config {
|
|||
* @return The {@link Properties} object. the returned instance might be empty of file does not
|
||||
* exist or if it is in a wrong format.
|
||||
*/
|
||||
private static Properties loadConfigurationFile() {
|
||||
private static synchronized Properties loadConfigurationFile() {
|
||||
Properties properties = new Properties();
|
||||
|
||||
String configurationFilePath =
|
||||
|
@ -892,7 +896,7 @@ public class Config {
|
|||
properties.load(fileReader);
|
||||
} catch (FileNotFoundException fnf) {
|
||||
log.error("Configuration file '{}' not found.", configurationFilePath);
|
||||
} catch (IOException e) {
|
||||
} catch (IOException ioe) {
|
||||
log.error(
|
||||
"Configuration file '{}' cannot be accessed or correctly parsed.", configurationFilePath);
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ import spock.lang.Specification
|
|||
import static datadog.trace.api.Config.AGENT_HOST
|
||||
import static datadog.trace.api.Config.AGENT_PORT_LEGACY
|
||||
import static datadog.trace.api.Config.AGENT_UNIX_DOMAIN_SOCKET
|
||||
import static datadog.trace.api.Config.CONFIGURATION_FILE
|
||||
import static datadog.trace.api.Config.DEFAULT_JMX_FETCH_STATSD_PORT
|
||||
import static datadog.trace.api.Config.DEFAULT_SERVICE_NAME
|
||||
import static datadog.trace.api.Config.GLOBAL_TAGS
|
||||
import static datadog.trace.api.Config.HEADER_TAGS
|
||||
import static datadog.trace.api.Config.HTTP_CLIENT_ERROR_STATUSES
|
||||
|
@ -715,4 +717,50 @@ class ConfigTest extends Specification {
|
|||
then:
|
||||
config.localRootSpanTags.get('_dd.hostname') == InetAddress.localHost.hostName
|
||||
}
|
||||
|
||||
def "verify fallback to properties file"() {
|
||||
setup:
|
||||
System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/dd-java-tracer.properties")
|
||||
|
||||
when:
|
||||
def config = new Config()
|
||||
|
||||
then:
|
||||
config.serviceName == "set-in-properties"
|
||||
}
|
||||
|
||||
def "verify fallback to properties file has lower priority then system property"() {
|
||||
setup:
|
||||
System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/dd-java-tracer.properties")
|
||||
System.setProperty(PREFIX + SERVICE_NAME, "set-in-system")
|
||||
|
||||
when:
|
||||
def config = new Config()
|
||||
|
||||
then:
|
||||
config.serviceName == "set-in-system"
|
||||
}
|
||||
|
||||
def "verify fallback to properties file has lower priority then env var"() {
|
||||
setup:
|
||||
System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/dd-java-tracer.properties")
|
||||
environmentVariables.set("DD_SERVICE_NAME", "set-in-env")
|
||||
|
||||
when:
|
||||
def config = new Config()
|
||||
|
||||
then:
|
||||
config.serviceName == "set-in-env"
|
||||
}
|
||||
|
||||
def "verify fallback to properties file that does not exist does not crash app"() {
|
||||
setup:
|
||||
System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/do-not-exist.properties")
|
||||
|
||||
when:
|
||||
def config = new Config()
|
||||
|
||||
then:
|
||||
config.serviceName == 'unnamed-java-app'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
dd.service.name=set-in-properties
|
Loading…
Reference in New Issue