Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Add support for Spring's JMS flavor - instrumenting `org.springframework.jms.listener.SessionAwareMessageListener`
* Add support to legacy ApacheHttpClient APIs (which adds support to Axis2 configured to use ApacheHttpClient)
* Added support for setting `server_urls` dynamically via properties file [#723](https://github.com/elastic/apm-agent-java/issues/723)
* Add [`config_file`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-config-file) option

## Bug Fixes
* Some JMS Consumers and Producers are filtered due to class name filtering in instrumentation matching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package co.elastic.apm.agent.configuration;

import co.elastic.apm.agent.bci.ElasticApmAgent;
import co.elastic.apm.agent.bci.methodmatching.MethodMatcher;
import co.elastic.apm.agent.bci.methodmatching.configuration.MethodMatcherValueConverter;
import co.elastic.apm.agent.configuration.converter.TimeDuration;
Expand All @@ -36,6 +37,7 @@
import org.stagemonitor.configuration.converter.ListValueConverter;
import org.stagemonitor.configuration.converter.MapValueConverter;
import org.stagemonitor.configuration.converter.StringValueConverter;
import org.stagemonitor.configuration.source.ConfigurationSource;

import javax.annotation.Nullable;
import java.util.Arrays;
Expand All @@ -45,6 +47,7 @@
import java.util.Map;

import static co.elastic.apm.agent.configuration.validation.RangeValidator.isInRange;
import static co.elastic.apm.agent.logging.LoggingConfiguration.AGENT_HOME_PLACEHOLDER;

public class CoreConfiguration extends ConfigurationOptionProvider {

Expand All @@ -53,6 +56,8 @@ public class CoreConfiguration extends ConfigurationOptionProvider {
public static final String SERVICE_NAME = "service_name";
public static final String SAMPLE_RATE = "transaction_sample_rate";
private static final String CORE_CATEGORY = "Core";
public static final String DEFAULT_CONFIG_FILE = AGENT_HOME_PLACEHOLDER + "/elasticapm.properties";
public static final String CONFIG_FILE = "config_file";
private final ConfigurationOption<Boolean> active = ConfigurationOption.booleanOption()
.key(ACTIVE)
.configurationCategory(CORE_CATEGORY)
Expand Down Expand Up @@ -360,6 +365,14 @@ public class CoreConfiguration extends ConfigurationOptionProvider {
.description("Disables the collection of breakdown metrics (`span.self_time`)")
.buildWithDefault(true);

private final ConfigurationOption<String> configFileLocation = ConfigurationOption.stringOption()
.key(CONFIG_FILE)
.configurationCategory(CORE_CATEGORY)
.description("Sets the path of the agent config file.\n" +
"The special value `_AGENT_HOME_` is a placeholder for the folder the elastic-apm-agent.jar is in.\n" +
"The location can either be in the classpath of the application or on the file system.")
.buildWithDefault(DEFAULT_CONFIG_FILE);

public boolean isActive() {
return active.get();
}
Expand Down Expand Up @@ -450,4 +463,29 @@ public Map<String, String> getGlobalLabels() {
public boolean isBreakdownMetricsEnabled() {
return breakdownMetrics.get();
}

/*
* Makes sure to not initialize ConfigurationOption, which would initialize the logger
*/
@Nullable
public static String getConfigFileLocation(List<ConfigurationSource> configurationSources) {
String configFileLocation = DEFAULT_CONFIG_FILE;
for (ConfigurationSource configurationSource : configurationSources) {
String valueFromSource = configurationSource.getValue(CONFIG_FILE);
if (valueFromSource != null) {
configFileLocation = valueFromSource;
break;
}
}
if (configFileLocation.contains(AGENT_HOME_PLACEHOLDER)) {
String agentHome = ElasticApmAgent.getAgentHome();
if (agentHome != null) {
return configFileLocation.replace(AGENT_HOME_PLACEHOLDER, agentHome);
} else {
return null;
}
} else {
return configFileLocation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package co.elastic.apm.agent.impl;

import co.elastic.apm.agent.bci.ElasticApmAgent;
import co.elastic.apm.agent.configuration.AgentArgumentsConfigurationSource;
import co.elastic.apm.agent.configuration.ApmServerConfigurationSource;
import co.elastic.apm.agent.configuration.CoreConfiguration;
Expand All @@ -35,7 +34,6 @@
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.logging.LoggingConfiguration;
import co.elastic.apm.agent.report.ApmServerClient;
import co.elastic.apm.agent.report.ApmServerHealthChecker;
import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.report.ReporterConfiguration;
import co.elastic.apm.agent.report.ReporterFactory;
Expand Down Expand Up @@ -149,6 +147,9 @@ private ConfigurationRegistry getDefaultConfigurationRegistry(List<Configuration
}
}

/*
* Must not initialize any loggers with this as the logger is configured based on configuration.
*/
private List<ConfigurationSource> getConfigSources(@Nullable String agentArguments) {
List<ConfigurationSource> result = new ArrayList<>();
if (agentArguments != null && !agentArguments.isEmpty()) {
Expand All @@ -167,9 +168,9 @@ public String getName() {
return "Inline configuration";
}
});
String agentHome = ElasticApmAgent.getAgentHome();
if (agentHome != null && PropertyFileConfigurationSource.isPresent(agentHome + "/elasticapm.properties")) {
result.add(new PropertyFileConfigurationSource(agentHome + "/elasticapm.properties"));
String configFileLocation = CoreConfiguration.getConfigFileLocation(result);
if (configFileLocation != null && PropertyFileConfigurationSource.isPresent(configFileLocation)) {
result.add(new PropertyFileConfigurationSource(configFileLocation));
}
// looks if we can find a elasticapm.properties on the classpath
// mainly useful for unit tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class LoggingConfiguration extends ConfigurationOptionProvider {
private static final String DEFAULT_LOG_FILE = SYSTEM_OUT;

private static final String LOGGING_CATEGORY = "Logging";
private static final String AGENT_HOME_PLACEHOLDER = "_AGENT_HOME_";
public static final String AGENT_HOME_PLACEHOLDER = "_AGENT_HOME_";
private static final String DEPRECATED_LOG_LEVEL_KEY = "logging.log_level";
private static final String DEPRECATED_LOG_FILE_KEY = "logging.log_file";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*-
* #%L
* Elastic APM Java agent
* %%
* Copyright (C) 2018 - 2019 Elastic and contributors
* %%
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* #L%
*/
package co.elastic.apm.agent.impl;

import co.elastic.apm.agent.configuration.CoreConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.stagemonitor.configuration.ConfigurationRegistry;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class ElasticApmTracerBuilderTest {


@AfterEach
void tearDown() {
System.clearProperty("elastic.apm." + CoreConfiguration.CONFIG_FILE);
}

@Test
void testConfigFileLocation(@TempDir Path tempDir) throws IOException {
Path file = Files.createFile(tempDir.resolve("elastic-apm-test.properties"));
Files.write(file, List.of("instrument=false"));
System.setProperty("elastic.apm." + CoreConfiguration.CONFIG_FILE, file.toString());

ConfigurationRegistry configurationRegistry = new ElasticApmTracerBuilder().build().getConfigurationRegistry();
CoreConfiguration config = configurationRegistry.getConfig(CoreConfiguration.class);

// tests that changing non-dynamic properties also works
assertThat(config.isInstrument()).isFalse();
configurationRegistry.getString(CoreConfiguration.CONFIG_FILE);
assertThat(configurationRegistry.getString(CoreConfiguration.CONFIG_FILE)).isEqualTo(file.toString());
}
}
33 changes: 33 additions & 0 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Click on a key to get more information.
** <<config-trace-methods-duration-threshold>>
** <<config-boot-delegation-packages>>
** <<config-breakdown-metrics>>
** <<config-config-file-location>>
* <<config-http>>
** <<config-capture-body>>
** <<config-capture-body-content-types>>
Expand Down Expand Up @@ -535,6 +536,28 @@ Disables the collection of breakdown metrics (`span.self_time`)
| `elastic.apm.breakdown_metrics` | `breakdown_metrics` | `ELASTIC_APM_BREAKDOWN_METRICS`
|============

[float]
[[config-config-file-location]]
==== `config_file_location`

Sets the path of the agent config file.
The special value `_AGENT_HOME_` is a placeholder for the folder the elastic-apm-agent.jar is in.
The location can either be in the classpath of the application or on the file system.


[options="header"]
|============
| Default | Type | Dynamic
| `_AGENT_HOME_/elasticapm.properties` | String | false
|============


[options="header"]
|============
| Java System Properties | Property file | Environment
| `elastic.apm.config_file_location` | `config_file_location` | `ELASTIC_APM_CONFIG_FILE_LOCATION`
|============

[[config-http]]
=== HTTP configuration options
[float]
Expand Down Expand Up @@ -1446,6 +1469,16 @@ The default unit for this option is `ms`
#
# breakdown_metrics=true

# Sets the path of the agent config file.
# The special value `_AGENT_HOME_` is a placeholder for the folder the elastic-apm-agent.jar is in.
# The location can either be in the classpath of the application or on the file system.
#
# This setting can not be changed at runtime. Changes require a restart of the application.
# Type: String
# Default value: _AGENT_HOME_/elasticapm.properties
#
# config_file_location=_AGENT_HOME_/elasticapm.properties

############################################
# HTTP #
############################################
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<version.error_prone>2.2.0</version.error_prone>
<version.h2>1.4.196</version.h2>
<version.jackson>2.9.9</version.jackson>
<version.junit-jupiter>5.3.2</version.junit-jupiter>
<version.junit-jupiter>5.5.1</version.junit-jupiter>
<version.junit.vintage>4.12</version.junit.vintage>
<version.logback>1.2.3</version.logback>
<version.okhttp>3.9.1</version.okhttp>
Expand Down