Skip to content

Commit 84e38c3

Browse files
committed
Prevent trace context propagation for self-rescheduling operations to not create everlasting APM transactions
1 parent 01b6de3 commit 84e38c3

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

server/src/main/java/org/elasticsearch/health/node/LocalHealthMonitor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private Monitoring(
262262
* Schedule the first run of the monitor.
263263
*/
264264
public void start() {
265-
scheduledRun = threadPool.schedule(this, TimeValue.ZERO, executor);
265+
scheduleNextRun(TimeValue.ZERO);
266266
}
267267

268268
/**
@@ -383,8 +383,14 @@ private void scheduleNextRunIfNecessary() {
383383
if (cancelled) {
384384
return;
385385
}
386-
try {
387-
scheduledRun = threadPool.schedule(this, interval, executor);
386+
scheduleNextRun(interval);
387+
}
388+
389+
private void scheduleNextRun(TimeValue delay) {
390+
// Prevent the propagation of the trace context here to not create everlasting APM transactions.
391+
// Such a trace context is created when executing a transport action.
392+
try (var ignored = threadPool.getThreadContext().newEmptySystemContext()) {
393+
scheduledRun = threadPool.schedule(this, delay, executor);
388394
} catch (final EsRejectedExecutionException e) {
389395
logger.debug(() -> format("Scheduled health monitoring was rejected on thread pool [%s]", executor), e);
390396
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunner.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,11 @@ protected void doRun() {
338338
}
339339
holder.problemTracker.finishReport();
340340
if (nextDelayInMsSinceEpoch >= 0) {
341-
doDatafeedRealtime(nextDelayInMsSinceEpoch, jobId, holder);
341+
// Prevent the propagation of the trace context here to not create everlasting APM transactions.
342+
// Such a trace context is created when executing any transport action.
343+
try (var ignored = threadPool.getThreadContext().clearTraceContext()) {
344+
doDatafeedRealtime(nextDelayInMsSinceEpoch, jobId, holder);
345+
}
342346
}
343347
}
344348
}, delay, threadPool.executor(MachineLearning.DATAFEED_THREAD_POOL_NAME));

0 commit comments

Comments
 (0)