Add synchronization to SimpleLogRecordProcessor and SimpleSpanProcessor to ensure thread-safe export of logs and spans respectively (#6885)
This commit is contained in:
parent
ec3c55ffeb
commit
2a97eaedc5
|
@ -41,6 +41,8 @@ public final class SimpleLogRecordProcessor implements LogRecordProcessor {
|
|||
Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||
private final AtomicBoolean isShutdown = new AtomicBoolean(false);
|
||||
|
||||
private final Object exporterLock = new Object();
|
||||
|
||||
/**
|
||||
* Returns a new {@link SimpleLogRecordProcessor} which exports logs to the {@link
|
||||
* LogRecordExporter} synchronously.
|
||||
|
@ -64,7 +66,12 @@ public final class SimpleLogRecordProcessor implements LogRecordProcessor {
|
|||
public void onEmit(Context context, ReadWriteLogRecord logRecord) {
|
||||
try {
|
||||
List<LogRecordData> logs = Collections.singletonList(logRecord.toLogRecordData());
|
||||
CompletableResultCode result = logRecordExporter.export(logs);
|
||||
CompletableResultCode result;
|
||||
|
||||
synchronized (exporterLock) {
|
||||
result = logRecordExporter.export(logs);
|
||||
}
|
||||
|
||||
pendingExports.add(result);
|
||||
result.whenComplete(
|
||||
() -> {
|
||||
|
|
|
@ -41,6 +41,8 @@ public final class SimpleSpanProcessor implements SpanProcessor {
|
|||
Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||
private final AtomicBoolean isShutdown = new AtomicBoolean(false);
|
||||
|
||||
private final Object exporterLock = new Object();
|
||||
|
||||
/**
|
||||
* Returns a new {@link SimpleSpanProcessor} which exports spans to the {@link SpanExporter}
|
||||
* synchronously.
|
||||
|
@ -86,7 +88,12 @@ public final class SimpleSpanProcessor implements SpanProcessor {
|
|||
if (span != null && (exportUnsampledSpans || span.getSpanContext().isSampled())) {
|
||||
try {
|
||||
List<SpanData> spans = Collections.singletonList(span.toSpanData());
|
||||
CompletableResultCode result = spanExporter.export(spans);
|
||||
CompletableResultCode result;
|
||||
|
||||
synchronized (exporterLock) {
|
||||
result = spanExporter.export(spans);
|
||||
}
|
||||
|
||||
pendingExports.add(result);
|
||||
result.whenComplete(
|
||||
() -> {
|
||||
|
|
Loading…
Reference in New Issue