Skip to content

Commit 21f161f

Browse files
authored
Add feature flag for remote config (#739)
1 parent 113167e commit 21f161f

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ private void pollConfig(ConfigurationRegistry configurationRegistry) {
151151
*/
152152
@Nullable
153153
String fetchConfig(final ConfigurationRegistry configurationRegistry) {
154+
if (!configurationRegistry.getConfig(CoreConfiguration.class).isCentralConfigEnabled()) {
155+
logger.debug("Remote configuration is disabled");
156+
return null;
157+
}
154158
try {
155159
return apmServerClient.execute("/config/v1/agents", new ApmServerClient.ConnectionHandler<String>() {
156160
@Override
@@ -177,11 +181,11 @@ private String tryFetchConfig(ConfigurationRegistry configurationRegistry, HttpU
177181
payloadSerializer.setOutputStream(connection.getOutputStream());
178182
payloadSerializer.serializeMetadata(metaData);
179183
payloadSerializer.flush();
184+
etag = connection.getHeaderField("ETag");
180185

181186
final int status = connection.getResponseCode();
182187
switch (status) {
183188
case SC_OK:
184-
etag = connection.getHeaderField("ETag");
185189
InputStream is = connection.getInputStream();
186190
final JsonReader<Object> reader = dslJson.newReader(is, buffer);
187191
reader.startObject();
@@ -202,16 +206,7 @@ private String tryFetchConfig(ConfigurationRegistry configurationRegistry, HttpU
202206
logger.debug("Configuration did not change");
203207
break;
204208
case SC_NOT_FOUND:
205-
etag = null;
206-
// means that there either is no configuration for this agent
207-
// or that this is an APM Server < 7.3 which does not have the config endpoint
208-
logger.debug("No remote config found for this agent");
209-
// makes sure to remove the configuration if all configs are deleted for this agent in Kibana
210-
if (!config.isEmpty()) {
211-
logger.info("Received new configuration from APM Server: <empty>");
212-
}
213-
config = Collections.emptyMap();
214-
configurationRegistry.reloadDynamicConfigurationOptions();
209+
logger.debug("This APM Server does not support central configuration. Update to APM Server 7.3+");
215210
break;
216211
case SC_FORBIDDEN:
217212
logger.debug("Central configuration is disabled. Set kibana.enabled: true in your APM Server configuration.");

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ public class CoreConfiguration extends ConfigurationOptionProvider {
354354
"----\n")
355355
.buildWithDefault("co.elastic.apm.agent.*");
356356

357+
private final ConfigurationOption<Boolean> centralConfig = ConfigurationOption.booleanOption()
358+
.key("central_config")
359+
.configurationCategory(CORE_CATEGORY)
360+
.description("When enabled, the agent will make periodic requests to the APM Server to fetch updated configuration.")
361+
.dynamic(true)
362+
.buildWithDefault(true);
363+
357364
private final ConfigurationOption<Boolean> breakdownMetrics = ConfigurationOption.booleanOption()
358365
.key("breakdown_metrics")
359366
.configurationCategory(CORE_CATEGORY)
@@ -447,6 +454,10 @@ public Map<String, String> getGlobalLabels() {
447454
return globalLabels.get();
448455
}
449456

457+
boolean isCentralConfigEnabled() {
458+
return centralConfig.get();
459+
}
460+
450461
public boolean isBreakdownMetricsEnabled() {
451462
return breakdownMetrics.get();
452463
}

apm-agent-core/src/test/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSourceTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
5151
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5252
import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
53+
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
5354
import static com.github.tomakehurst.wiremock.client.WireMock.post;
5455
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
5556
import static com.github.tomakehurst.wiremock.client.WireMock.serverError;
@@ -62,6 +63,7 @@
6263
import static org.mockito.Mockito.mock;
6364
import static org.mockito.Mockito.times;
6465
import static org.mockito.Mockito.verify;
66+
import static org.mockito.Mockito.when;
6567

6668
public class ApmServerConfigurationSourceTest {
6769

@@ -100,18 +102,26 @@ public void testLoadRemoteConfig() throws Exception {
100102
}
101103
}
102104

105+
@Test
106+
public void testRemoteConfigDisabled() {
107+
when(config.getConfig(CoreConfiguration.class).isCentralConfigEnabled()).thenReturn(false);
108+
configurationSource.fetchConfig(config);
109+
assertThat(configurationSource.getValue("foo")).isNull();
110+
mockApmServer.verify(0, postRequestedFor(urlEqualTo("/config/v1/agents")));
111+
}
112+
103113
@Test
104114
public void testNotFound() {
105115
mockApmServer.stubFor(post(urlEqualTo("/config/v1/agents")).willReturn(notFound()));
106116
assertThat(configurationSource.fetchConfig(config)).isNull();
107-
verify(mockLogger, times(1)).debug(contains("No remote config found for this agent"));
117+
verify(mockLogger, times(1)).debug(contains("This APM Server does not support central configuration"));
108118
}
109119

110120
@Test
111121
public void configDeleted() {
112122
configurationSource.fetchConfig(config);
113123
assertThat(configurationSource.getValue("foo")).isEqualTo("bar");
114-
mockApmServer.stubFor(post(urlEqualTo("/config/v1/agents")).willReturn(notFound()));
124+
mockApmServer.stubFor(post(urlEqualTo("/config/v1/agents")).willReturn(okJson("{}")));
115125
configurationSource.fetchConfig(config);
116126
assertThat(configurationSource.getValue("foo")).isNull();
117127
}

0 commit comments

Comments
 (0)