Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread dump debug option #3589

Merged
merged 2 commits into from
May 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ public String toString(Collection<String> value) {
"exist and use it to dump bytecode of instrumented classes.")
.buildWithDefault("");

private final ConfigurationOption<TimeDuration> threadDumpInterval = TimeDurationValueConverter.durationOption("ms")
.key("thread_dump_interval")
.configurationCategory(CORE_CATEGORY)
.tags("internal")
.description("Triggers a thread dump at regular frequency, should be used to help configure trace_methods without knowledge of application code")
.buildWithDefault(TimeDuration.of("0ms"));

private final ConfigurationOption<Boolean> typeMatchingWithNamePreFilter = ConfigurationOption.booleanOption()
.key("enable_type_matching_name_pre_filtering")
.configurationCategory(CORE_CATEGORY)
Expand Down Expand Up @@ -1017,6 +1024,10 @@ public String getBytecodeDumpPath() {
return bytecodeDumpPath.get();
}

public long getThreadDumpInterval() {
return threadDumpInterval.get().getMillis();
}

public boolean isTypeMatchingWithNamePreFilter() {
return typeMatchingWithNamePreFilter.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.
*/
package co.elastic.apm.agent.util;

import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.Tracer;

import javax.annotation.Nullable;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadDump extends AbstractLifecycleListener {

private Logger log = LoggerFactory.getLogger(ThreadDump.class);

@Nullable
private ScheduledThreadPoolExecutor executor;

@Override
public void start(Tracer tracer) throws Exception {

long threadDumpInterval = tracer.getConfig(CoreConfiguration.class).getThreadDumpInterval();
if (threadDumpInterval <= 0) {
return;
}

if (!log.isDebugEnabled()) {
log.error("thread dump option requires debug log level");
return;
}

if (threadDumpInterval < 100) {
log.error("thread dump frequency too high, adjusted to every 100ms");
threadDumpInterval = 100;
}

log.warn("thread dump will be generated every %s ms", threadDumpInterval);

executor = ExecutorUtils.createSingleThreadSchedulingDaemonPool("thread-dump");

executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
StringBuilder sb = new StringBuilder();
for (ThreadInfo threadInfo : threadMXBean.dumpAllThreads(true, true)) {
sb.append(threadInfo.toString());
}

log.debug("thread dump: \n\n {}", sb);

}
}, threadDumpInterval, threadDumpInterval, TimeUnit.MILLISECONDS);
}

@Override
public void stop() throws Exception {
if (executor == null) {
return;
}
ExecutorUtils.shutdownAndWaitTermination(executor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ co.elastic.apm.agent.metrics.builtin.AgentOverheadMetrics
co.elastic.apm.agent.impl.circuitbreaker.CircuitBreaker
co.elastic.apm.agent.collections.WeakMapCleaner
co.elastic.apm.agent.report.serialize.MetricRegistryReporter
co.elastic.apm.agent.util.ThreadDump
Loading