Skip to content

Commit 6bdd74e

Browse files
authored
Add config_file option (#750)
closes #701
1 parent a4f6c1c commit 6bdd74e

File tree

7 files changed

+142
-7
lines changed

7 files changed

+142
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Add support for Spring's JMS flavor - instrumenting `org.springframework.jms.listener.SessionAwareMessageListener`
55
* Add support to legacy ApacheHttpClient APIs (which adds support to Axis2 configured to use ApacheHttpClient)
66
* Added support for setting `server_urls` dynamically via properties file [#723](https://github.com/elastic/apm-agent-java/issues/723)
7+
* Add [`config_file`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-config-file) option
78

89
## Bug Fixes
910
* Some JMS Consumers and Producers are filtered due to class name filtering in instrumentation matching

apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package co.elastic.apm.agent.configuration;
2626

27+
import co.elastic.apm.agent.bci.ElasticApmAgent;
2728
import co.elastic.apm.agent.bci.methodmatching.MethodMatcher;
2829
import co.elastic.apm.agent.bci.methodmatching.configuration.MethodMatcherValueConverter;
2930
import co.elastic.apm.agent.configuration.converter.TimeDuration;
@@ -36,6 +37,7 @@
3637
import org.stagemonitor.configuration.converter.ListValueConverter;
3738
import org.stagemonitor.configuration.converter.MapValueConverter;
3839
import org.stagemonitor.configuration.converter.StringValueConverter;
40+
import org.stagemonitor.configuration.source.ConfigurationSource;
3941

4042
import javax.annotation.Nullable;
4143
import java.util.Arrays;
@@ -45,6 +47,7 @@
4547
import java.util.Map;
4648

4749
import static co.elastic.apm.agent.configuration.validation.RangeValidator.isInRange;
50+
import static co.elastic.apm.agent.logging.LoggingConfiguration.AGENT_HOME_PLACEHOLDER;
4851

4952
public class CoreConfiguration extends ConfigurationOptionProvider {
5053

@@ -53,6 +56,8 @@ public class CoreConfiguration extends ConfigurationOptionProvider {
5356
public static final String SERVICE_NAME = "service_name";
5457
public static final String SAMPLE_RATE = "transaction_sample_rate";
5558
private static final String CORE_CATEGORY = "Core";
59+
public static final String DEFAULT_CONFIG_FILE = AGENT_HOME_PLACEHOLDER + "/elasticapm.properties";
60+
public static final String CONFIG_FILE = "config_file";
5661
private final ConfigurationOption<Boolean> active = ConfigurationOption.booleanOption()
5762
.key(ACTIVE)
5863
.configurationCategory(CORE_CATEGORY)
@@ -367,6 +372,14 @@ public class CoreConfiguration extends ConfigurationOptionProvider {
367372
.description("Disables the collection of breakdown metrics (`span.self_time`)")
368373
.buildWithDefault(true);
369374

375+
private final ConfigurationOption<String> configFileLocation = ConfigurationOption.stringOption()
376+
.key(CONFIG_FILE)
377+
.configurationCategory(CORE_CATEGORY)
378+
.description("Sets the path of the agent config file.\n" +
379+
"The special value `_AGENT_HOME_` is a placeholder for the folder the elastic-apm-agent.jar is in.\n" +
380+
"The location can either be in the classpath of the application or on the file system.")
381+
.buildWithDefault(DEFAULT_CONFIG_FILE);
382+
370383
public boolean isActive() {
371384
return active.get();
372385
}
@@ -461,4 +474,29 @@ boolean isCentralConfigEnabled() {
461474
public boolean isBreakdownMetricsEnabled() {
462475
return breakdownMetrics.get();
463476
}
477+
478+
/*
479+
* Makes sure to not initialize ConfigurationOption, which would initialize the logger
480+
*/
481+
@Nullable
482+
public static String getConfigFileLocation(List<ConfigurationSource> configurationSources) {
483+
String configFileLocation = DEFAULT_CONFIG_FILE;
484+
for (ConfigurationSource configurationSource : configurationSources) {
485+
String valueFromSource = configurationSource.getValue(CONFIG_FILE);
486+
if (valueFromSource != null) {
487+
configFileLocation = valueFromSource;
488+
break;
489+
}
490+
}
491+
if (configFileLocation.contains(AGENT_HOME_PLACEHOLDER)) {
492+
String agentHome = ElasticApmAgent.getAgentHome();
493+
if (agentHome != null) {
494+
return configFileLocation.replace(AGENT_HOME_PLACEHOLDER, agentHome);
495+
} else {
496+
return null;
497+
}
498+
} else {
499+
return configFileLocation;
500+
}
501+
}
464502
}

apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracerBuilder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package co.elastic.apm.agent.impl;
2626

27-
import co.elastic.apm.agent.bci.ElasticApmAgent;
2827
import co.elastic.apm.agent.configuration.AgentArgumentsConfigurationSource;
2928
import co.elastic.apm.agent.configuration.ApmServerConfigurationSource;
3029
import co.elastic.apm.agent.configuration.CoreConfiguration;
@@ -35,7 +34,6 @@
3534
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
3635
import co.elastic.apm.agent.logging.LoggingConfiguration;
3736
import co.elastic.apm.agent.report.ApmServerClient;
38-
import co.elastic.apm.agent.report.ApmServerHealthChecker;
3937
import co.elastic.apm.agent.report.Reporter;
4038
import co.elastic.apm.agent.report.ReporterConfiguration;
4139
import co.elastic.apm.agent.report.ReporterFactory;
@@ -149,6 +147,9 @@ private ConfigurationRegistry getDefaultConfigurationRegistry(List<Configuration
149147
}
150148
}
151149

150+
/*
151+
* Must not initialize any loggers with this as the logger is configured based on configuration.
152+
*/
152153
private List<ConfigurationSource> getConfigSources(@Nullable String agentArguments) {
153154
List<ConfigurationSource> result = new ArrayList<>();
154155
if (agentArguments != null && !agentArguments.isEmpty()) {
@@ -167,9 +168,9 @@ public String getName() {
167168
return "Inline configuration";
168169
}
169170
});
170-
String agentHome = ElasticApmAgent.getAgentHome();
171-
if (agentHome != null && PropertyFileConfigurationSource.isPresent(agentHome + "/elasticapm.properties")) {
172-
result.add(new PropertyFileConfigurationSource(agentHome + "/elasticapm.properties"));
171+
String configFileLocation = CoreConfiguration.getConfigFileLocation(result);
172+
if (configFileLocation != null && PropertyFileConfigurationSource.isPresent(configFileLocation)) {
173+
result.add(new PropertyFileConfigurationSource(configFileLocation));
173174
}
174175
// looks if we can find a elasticapm.properties on the classpath
175176
// mainly useful for unit tests

apm-agent-core/src/main/java/co/elastic/apm/agent/logging/LoggingConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class LoggingConfiguration extends ConfigurationOptionProvider {
5959
private static final String DEFAULT_LOG_FILE = SYSTEM_OUT;
6060

6161
private static final String LOGGING_CATEGORY = "Logging";
62-
private static final String AGENT_HOME_PLACEHOLDER = "_AGENT_HOME_";
62+
public static final String AGENT_HOME_PLACEHOLDER = "_AGENT_HOME_";
6363
private static final String DEPRECATED_LOG_LEVEL_KEY = "logging.log_level";
6464
private static final String DEPRECATED_LOG_FILE_KEY = "logging.log_file";
6565

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*-
2+
* #%L
3+
* Elastic APM Java agent
4+
* %%
5+
* Copyright (C) 2018 - 2019 Elastic and contributors
6+
* %%
7+
* Licensed to Elasticsearch B.V. under one or more contributor
8+
* license agreements. See the NOTICE file distributed with
9+
* this work for additional information regarding copyright
10+
* ownership. Elasticsearch B.V. licenses this file to you under
11+
* the Apache License, Version 2.0 (the "License"); you may
12+
* not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing,
18+
* software distributed under the License is distributed on an
19+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
* KIND, either express or implied. See the License for the
21+
* specific language governing permissions and limitations
22+
* under the License.
23+
* #L%
24+
*/
25+
package co.elastic.apm.agent.impl;
26+
27+
import co.elastic.apm.agent.configuration.CoreConfiguration;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
31+
import org.stagemonitor.configuration.ConfigurationRegistry;
32+
33+
import java.io.IOException;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.util.List;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
40+
class ElasticApmTracerBuilderTest {
41+
42+
43+
@AfterEach
44+
void tearDown() {
45+
System.clearProperty("elastic.apm." + CoreConfiguration.CONFIG_FILE);
46+
}
47+
48+
@Test
49+
void testConfigFileLocation(@TempDir Path tempDir) throws IOException {
50+
Path file = Files.createFile(tempDir.resolve("elastic-apm-test.properties"));
51+
Files.write(file, List.of("instrument=false"));
52+
System.setProperty("elastic.apm." + CoreConfiguration.CONFIG_FILE, file.toString());
53+
54+
ConfigurationRegistry configurationRegistry = new ElasticApmTracerBuilder().build().getConfigurationRegistry();
55+
CoreConfiguration config = configurationRegistry.getConfig(CoreConfiguration.class);
56+
57+
// tests that changing non-dynamic properties also works
58+
assertThat(config.isInstrument()).isFalse();
59+
configurationRegistry.getString(CoreConfiguration.CONFIG_FILE);
60+
assertThat(configurationRegistry.getString(CoreConfiguration.CONFIG_FILE)).isEqualTo(file.toString());
61+
}
62+
}

docs/configuration.asciidoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Click on a key to get more information.
7474
** <<config-trace-methods-duration-threshold>>
7575
** <<config-boot-delegation-packages>>
7676
** <<config-breakdown-metrics>>
77+
** <<config-config-file-location>>
7778
* <<config-http>>
7879
** <<config-capture-body>>
7980
** <<config-capture-body-content-types>>
@@ -535,6 +536,28 @@ Disables the collection of breakdown metrics (`span.self_time`)
535536
| `elastic.apm.breakdown_metrics` | `breakdown_metrics` | `ELASTIC_APM_BREAKDOWN_METRICS`
536537
|============
537538

539+
[float]
540+
[[config-config-file-location]]
541+
==== `config_file_location`
542+
543+
Sets the path of the agent config file.
544+
The special value `_AGENT_HOME_` is a placeholder for the folder the elastic-apm-agent.jar is in.
545+
The location can either be in the classpath of the application or on the file system.
546+
547+
548+
[options="header"]
549+
|============
550+
| Default | Type | Dynamic
551+
| `_AGENT_HOME_/elasticapm.properties` | String | false
552+
|============
553+
554+
555+
[options="header"]
556+
|============
557+
| Java System Properties | Property file | Environment
558+
| `elastic.apm.config_file_location` | `config_file_location` | `ELASTIC_APM_CONFIG_FILE_LOCATION`
559+
|============
560+
538561
[[config-http]]
539562
=== HTTP configuration options
540563
[float]
@@ -1444,6 +1467,16 @@ The default unit for this option is `ms`
14441467
#
14451468
# breakdown_metrics=true
14461469
1470+
# Sets the path of the agent config file.
1471+
# The special value `_AGENT_HOME_` is a placeholder for the folder the elastic-apm-agent.jar is in.
1472+
# The location can either be in the classpath of the application or on the file system.
1473+
#
1474+
# This setting can not be changed at runtime. Changes require a restart of the application.
1475+
# Type: String
1476+
# Default value: _AGENT_HOME_/elasticapm.properties
1477+
#
1478+
# config_file_location=_AGENT_HOME_/elasticapm.properties
1479+
14471480
############################################
14481481
# HTTP #
14491482
############################################

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<version.error_prone>2.2.0</version.error_prone>
5959
<version.h2>1.4.196</version.h2>
6060
<version.jackson>2.9.9</version.jackson>
61-
<version.junit-jupiter>5.3.2</version.junit-jupiter>
61+
<version.junit-jupiter>5.5.1</version.junit-jupiter>
6262
<version.junit.vintage>4.12</version.junit.vintage>
6363
<version.logback>1.2.3</version.logback>
6464
<version.okhttp>3.9.1</version.okhttp>

0 commit comments

Comments
 (0)