Skip to content

Commit 96b7d60

Browse files
authored
Adding pause/resume functionality to reduce agent overhead on stress (#1040)
1 parent bbba17a commit 96b7d60

File tree

80 files changed

+2550
-434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2550
-434
lines changed

CHANGELOG.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ endif::[]
3333
unless <<config-use-elastic-traceparent-header, `use_elastic_traceparent_header`>> is set to false.
3434
* Creating spans for slow methods with the help of the sampling profiler https://github.com/jvm-profiling-tools/async-profiler[async-profiler].
3535
This is a low-overhead way of seeing which methods make your transactions slow and a replacement for the `trace_methods` configuration option.
36-
See <<supported-java-methods>> for more details.
36+
See <<supported-java-methods>> for more details
37+
* Adding a Circuit Breaker to pause the agent when stress is detected on the system and resume when the stress is relieved.
38+
See <<circuit-breaker>> and {pull}1040[#1040] for more info.
3739
3840
[float]
3941
===== Bug fixes

apm-agent-benchmarks/src/main/java/co/elastic/apm/agent/benchmark/AbstractMockApmServerBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void setUp(Blackhole blackhole) throws IOException {
8282
.addConfigSource(new SimpleSource()
8383
.add(CoreConfiguration.SERVICE_NAME, "benchmark")
8484
.add(CoreConfiguration.INSTRUMENT, Boolean.toString(apmEnabled))
85-
.add(CoreConfiguration.ACTIVE, Boolean.toString(apmEnabled))
85+
.add("active", Boolean.toString(apmEnabled))
8686
.add("api_request_size", "10mb")
8787
.add("capture_headers", "false")
8888
// .add("profiling_inferred_spans", "true")

apm-agent-core/src/main/java/co/elastic/apm/agent/bci/MatcherTimerLifecycleListener.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -25,21 +25,17 @@
2525
package co.elastic.apm.agent.bci;
2626

2727
import co.elastic.apm.agent.bci.bytebuddy.MatcherTimer;
28-
import co.elastic.apm.agent.context.LifecycleListener;
28+
import co.elastic.apm.agent.context.AbstractLifecycleListener;
2929
import co.elastic.apm.agent.impl.ElasticApmTracer;
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

3333
import java.util.ArrayList;
3434
import java.util.Collections;
3535

36-
public class MatcherTimerLifecycleListener implements LifecycleListener {
36+
public class MatcherTimerLifecycleListener extends AbstractLifecycleListener {
3737
private static final Logger logger = LoggerFactory.getLogger(MatcherTimerLifecycleListener.class);
3838

39-
@Override
40-
public void start(ElasticApmTracer tracer) {
41-
}
42-
4339
@Override
4440
public void stop() {
4541
if (logger.isDebugEnabled()) {

apm-agent-core/src/main/java/co/elastic/apm/agent/bci/OsgiBootDelegationEnabler.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -25,7 +25,7 @@
2525
package co.elastic.apm.agent.bci;
2626

2727
import co.elastic.apm.agent.configuration.CoreConfiguration;
28-
import co.elastic.apm.agent.context.LifecycleListener;
28+
import co.elastic.apm.agent.context.AbstractLifecycleListener;
2929
import co.elastic.apm.agent.impl.ElasticApmTracer;
3030

3131
import javax.annotation.Nullable;
@@ -41,7 +41,7 @@
4141
* See {@link AgentMain#init(String, java.lang.instrument.Instrumentation)}
4242
* </p>
4343
*/
44-
public class OsgiBootDelegationEnabler implements LifecycleListener {
44+
public class OsgiBootDelegationEnabler extends AbstractLifecycleListener {
4545
private static final String APM_BASE_PACKAGE = "co.elastic.apm.agent.*";
4646
// see https://confluence.atlassian.com/jirakb/using-javaagent-with-jira-790793295.html#UsingjavaagentwithJIRA-Resolution
4747
private static final String ATLASSIAN_BOOTDELEGATION_DEFAULTS = "META-INF.services,com.yourkit,com.singularity.*,com.jprofiler," +
@@ -70,9 +70,4 @@ private static void appendToSystemProperty(String propertyName, @Nullable String
7070
System.setProperty(propertyName, append);
7171
}
7272
}
73-
74-
@Override
75-
public void stop() {
76-
// noop
77-
}
7873
}

apm-agent-core/src/main/java/co/elastic/apm/agent/bci/methodmatching/TraceMethodInstrumentation.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ public static void onMethodEnter(@Advice.Origin Class<?> clazz,
7474
if (tracer != null) {
7575
final TraceContextHolder<?> parent = tracer.getActive();
7676
if (parent == null) {
77-
span = tracer.startRootTransaction(clazz.getClassLoader())
78-
.withName(signature)
79-
.activate();
77+
span = tracer.startRootTransaction(clazz.getClassLoader());
78+
if (span != null) {
79+
span.withName(signature).activate();
80+
}
8081
} else if (parent.isSampled()) {
8182
span = parent.createSpan()
8283
.withName(signature)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -227,6 +227,16 @@ public String getName() {
227227
return "APM Server";
228228
}
229229

230+
@Override
231+
public void pause() {
232+
// Keep polling for remote config changes, in case the user wants to resume a paused agent or change the stress
233+
// monitoring configurations.
234+
}
235+
236+
@Override
237+
public void resume() {
238+
}
239+
230240
@Override
231241
public void stop() {
232242
if (this.threadPool != null) {

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,14 @@
5353

5454
public class CoreConfiguration extends ConfigurationOptionProvider {
5555

56-
public static final String ACTIVE = "active";
5756
public static final String INSTRUMENT = "instrument";
5857
public static final String SERVICE_NAME = "service_name";
5958
public static final String SERVICE_NODE_NAME = "service_node_name";
6059
public static final String SAMPLE_RATE = "transaction_sample_rate";
61-
private static final String CORE_CATEGORY = "Core";
60+
public static final String CORE_CATEGORY = "Core";
6261
private static final String DEFAULT_CONFIG_FILE = AGENT_HOME_PLACEHOLDER + "/elasticapm.properties";
6362
public static final String CONFIG_FILE = "config_file";
6463

65-
private final ConfigurationOption<Boolean> active = ConfigurationOption.booleanOption()
66-
.key(ACTIVE)
67-
.configurationCategory(CORE_CATEGORY)
68-
.description("A boolean specifying if the agent should be active or not.\n" +
69-
"When active, the agent instruments incoming HTTP requests, tracks errors and collects and sends metrics.\n" +
70-
"When inactive, the agent works as a noop, not collecting data and not communicating with the APM sever.\n" +
71-
"As this is a reversible switch, agent threads are not being killed when inactivated, but they will be \n" +
72-
"mostly idle in this state, so the overhead should be negligible.\n" +
73-
"\n" +
74-
"You can use this setting to dynamically disable Elastic APM at runtime.")
75-
.dynamic(true)
76-
.buildWithDefault(true);
77-
7864
private final ConfigurationOption<Boolean> instrument = ConfigurationOption.booleanOption()
7965
.key(INSTRUMENT)
8066
.configurationCategory(CORE_CATEGORY)
@@ -526,10 +512,6 @@ public class CoreConfiguration extends ConfigurationOptionProvider {
526512
.tags("internal")
527513
.buildWithDefault(4096);
528514

529-
public boolean isActive() {
530-
return active.get();
531-
}
532-
533515
public boolean isInstrument() {
534516
return instrument.get();
535517
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -25,7 +25,7 @@
2525
package co.elastic.apm.agent.configuration;
2626

2727
import co.elastic.apm.agent.configuration.converter.TimeDuration;
28-
import co.elastic.apm.agent.context.LifecycleListener;
28+
import co.elastic.apm.agent.context.AbstractLifecycleListener;
2929
import co.elastic.apm.agent.impl.ElasticApmTracer;
3030
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
3131
import co.elastic.apm.agent.util.VersionUtils;
@@ -43,7 +43,7 @@
4343
* under Apache license 2.0.
4444
* </p>
4545
*/
46-
public class StartupInfo implements LifecycleListener {
46+
public class StartupInfo extends AbstractLifecycleListener {
4747

4848
private static final Logger logger = LoggerFactory.getLogger(StartupInfo.class);
4949
private final String elasticApmVersion;
@@ -105,9 +105,4 @@ private void logConfigWithNonDefaultValue(Logger logger, ConfigurationOption<?>
105105
option.getNameOfCurrentConfigurationSource());
106106
}
107107
}
108-
109-
@Override
110-
public void stop() throws Exception {
111-
// noop
112-
}
113108
}

apm-agent-core/src/main/java/co/elastic/apm/agent/context/AbstractLifecycleListener.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* the Apache License, Version 2.0 (the "License"); you may
1212
* not use this file except in compliance with the License.
1313
* You may obtain a copy of the License at
14-
*
14+
*
1515
* http://www.apache.org/licenses/LICENSE-2.0
16-
*
16+
*
1717
* Unless required by applicable law or agreed to in writing,
1818
* software distributed under the License is distributed on an
1919
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -27,14 +27,17 @@
2727
import co.elastic.apm.agent.impl.ElasticApmTracer;
2828

2929
public abstract class AbstractLifecycleListener implements LifecycleListener {
30-
protected final ElasticApmTracer tracer;
3130

32-
protected AbstractLifecycleListener(ElasticApmTracer tracer) {
33-
this.tracer = tracer;
31+
@Override
32+
public void start(ElasticApmTracer tracer) {
3433
}
3534

3635
@Override
37-
public void start(ElasticApmTracer tracer) {
36+
public void pause() throws Exception {
37+
}
38+
39+
@Override
40+
public void resume() throws Exception {
3841
}
3942

4043
@Override
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*-
2+
* #%L
3+
* Elastic APM Java agent
4+
* %%
5+
* Copyright (C) 2018 - 2020 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.context;
26+
27+
import java.io.Closeable;
28+
29+
public class ClosableLifecycleListenerAdapter extends AbstractLifecycleListener {
30+
31+
private final Closeable closeable;
32+
33+
public static LifecycleListener of(Closeable closeable) {
34+
return new ClosableLifecycleListenerAdapter(closeable);
35+
}
36+
37+
private ClosableLifecycleListenerAdapter(Closeable closeable) {
38+
this.closeable = closeable;
39+
}
40+
41+
@Override
42+
public void stop() throws Exception {
43+
closeable.close();
44+
}
45+
}

0 commit comments

Comments
 (0)