Skip to content

Add synchronization to SimpleLogRecordProcessor and SimpleSpanProcessor to ensure thread-safe export of logs and spans respectively #6885

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

Merged
merged 1 commit into from
Nov 18, 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 @@ -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.
Expand All @@ -64,7 +66,12 @@ private SimpleLogRecordProcessor(LogRecordExporter logRecordExporter) {
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(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -86,7 +88,12 @@ public void onEnd(ReadableSpan span) {
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(
() -> {
Expand Down
Loading