-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for logging MDC in the Kafka buffer (#4131)
Uses logging MDC within the KafkaBuffer entry points. Create the Kafka Buffer consumer threads with MDC. Name the consumer threads to help when tracking down thread dumps. First part of #4126 Signed-off-by: David Venable <[email protected]>
- Loading branch information
Showing
5 changed files
with
321 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...kafka-plugins/src/main/java/org/opensearch/dataprepper/plugins/kafka/common/KafkaMdc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.kafka.common;public class KafkaMdc { | ||
public static final String MDC_KAFKA_PLUGIN_KEY = "kafkaPluginType"; | ||
} |
57 changes: 57 additions & 0 deletions
57
...java/org/opensearch/dataprepper/plugins/kafka/common/thread/KafkaPluginThreadFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.kafka.common.thread; | ||
|
||
import org.opensearch.dataprepper.plugins.kafka.common.KafkaMdc; | ||
import org.slf4j.MDC; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* An implementation of {@link ThreadFactory} for Kafka plugin threads. | ||
*/ | ||
public class KafkaPluginThreadFactory implements ThreadFactory { | ||
private final ThreadFactory delegateThreadFactory; | ||
private final String threadPrefix; | ||
private final String kafkaPluginType; | ||
private final AtomicInteger threadNumber = new AtomicInteger(1); | ||
|
||
KafkaPluginThreadFactory( | ||
final ThreadFactory delegateThreadFactory, | ||
final String kafkaPluginType) { | ||
this.delegateThreadFactory = delegateThreadFactory; | ||
this.threadPrefix = "kafka-" + kafkaPluginType + "-"; | ||
this.kafkaPluginType = kafkaPluginType; | ||
} | ||
|
||
/** | ||
* Creates an instance specifically for use with {@link Executors}. | ||
* | ||
* @param kafkaPluginType The name of the plugin type. e.g. sink, source, buffer | ||
* @return An instance of the {@link KafkaPluginThreadFactory}. | ||
*/ | ||
public static KafkaPluginThreadFactory defaultExecutorThreadFactory(final String kafkaPluginType) { | ||
return new KafkaPluginThreadFactory(Executors.defaultThreadFactory(), kafkaPluginType); | ||
} | ||
|
||
@Override | ||
public Thread newThread(final Runnable runnable) { | ||
final Thread thread = delegateThreadFactory.newThread(() -> { | ||
MDC.put(KafkaMdc.MDC_KAFKA_PLUGIN_KEY, kafkaPluginType); | ||
try { | ||
runnable.run(); | ||
} finally { | ||
MDC.clear(); | ||
} | ||
}); | ||
|
||
thread.setName(threadPrefix + threadNumber.getAndIncrement()); | ||
|
||
return thread; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.