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 proper shutdown implementations for all exporters #5113

Merged
merged 1 commit into from
Jan 14, 2023
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 @@ -63,6 +63,7 @@ public final class OkHttpGrpcExporter<T extends Marshaler> implements GrpcExport

// We only log unimplemented once since it's a configuration issue that won't be recovered.
private final AtomicBoolean loggedUnimplemented = new AtomicBoolean();
private final AtomicBoolean isShutdown = new AtomicBoolean();

private final String type;
private final ExporterMetrics exporterMetrics;
Expand Down Expand Up @@ -91,6 +92,10 @@ public final class OkHttpGrpcExporter<T extends Marshaler> implements GrpcExport

@Override
public CompletableResultCode export(T exportRequest, int numItems) {
if (isShutdown.get()) {
return CompletableResultCode.ofFailure();
}

exporterMetrics.addSeen(numItems);

Request.Builder requestBuilder = new Request.Builder().url(url).headers(headers);
Expand Down Expand Up @@ -209,6 +214,10 @@ private static String grpcMessage(Response response) {

@Override
public CompletableResultCode shutdown() {
if (!isShutdown.compareAndSet(false, true)) {
logger.log(Level.WARNING, "Calling shutdown() multiple times.");
return CompletableResultCode.ofSuccess();
}
client.dispatcher().cancelAll();
client.dispatcher().executorService().shutdownNow();
client.connectionPool().evictAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public final class UpstreamGrpcExporter<T extends Marshaler> implements GrpcExpo

// We only log unavailable once since it's a configuration issue that won't be recovered.
private final AtomicBoolean loggedUnimplemented = new AtomicBoolean();
private final AtomicBoolean isShutdown = new AtomicBoolean();

private final String type;
private final ExporterMetrics exporterMetrics;
Expand All @@ -57,6 +58,10 @@ public final class UpstreamGrpcExporter<T extends Marshaler> implements GrpcExpo

@Override
public CompletableResultCode export(T exportRequest, int numItems) {
if (isShutdown.get()) {
return CompletableResultCode.ofFailure();
}

exporterMetrics.addSeen(numItems);

CompletableResultCode result = new CompletableResultCode();
Expand Down Expand Up @@ -118,6 +123,9 @@ public void onFailure(Throwable t) {

@Override
public CompletableResultCode shutdown() {
if (!isShutdown.compareAndSet(false, true)) {
logger.log(Level.WARNING, "Calling shutdown() multiple times.");
}
return CompletableResultCode.ofSuccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.internal.ThrottlingLogger;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;
Expand Down Expand Up @@ -44,6 +45,7 @@ public final class OkHttpExporter<T extends Marshaler> {
private static final Logger internalLogger = Logger.getLogger(OkHttpExporter.class.getName());

private final ThrottlingLogger logger = new ThrottlingLogger(internalLogger);
private final AtomicBoolean isShutdown = new AtomicBoolean();

private final String type;
private final OkHttpClient client;
Expand Down Expand Up @@ -76,6 +78,10 @@ public final class OkHttpExporter<T extends Marshaler> {
}

public CompletableResultCode export(T exportRequest, int numItems) {
if (isShutdown.get()) {
return CompletableResultCode.ofFailure();
}

exporterMetrics.addSeen(numItems);

Request.Builder requestBuilder = new Request.Builder().url(url);
Expand Down Expand Up @@ -139,11 +145,14 @@ public void onResponse(Call call, Response response) {
}

public CompletableResultCode shutdown() {
CompletableResultCode result = CompletableResultCode.ofSuccess();
if (!isShutdown.compareAndSet(false, true)) {
logger.log(Level.WARNING, "Calling shutdown() multiple times.");
return CompletableResultCode.ofSuccess();
}
client.dispatcher().cancelAll();
client.dispatcher().executorService().shutdownNow();
client.connectionPool().evictAll();
return result;
return CompletableResultCode.ofSuccess();
}

static boolean isRetryable(Response response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -43,6 +44,7 @@ public final class JaegerThriftSpanExporter implements SpanExporter {

private final ThrottlingLogger logger =
new ThrottlingLogger(Logger.getLogger(JaegerThriftSpanExporter.class.getName()));
private final AtomicBoolean isShutdown = new AtomicBoolean();
private final ThriftSender thriftSender;
private final Process process;

Expand Down Expand Up @@ -82,13 +84,16 @@ public final class JaegerThriftSpanExporter implements SpanExporter {
*/
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
if (isShutdown.get()) {
return CompletableResultCode.ofFailure();
}

Map<Process, List<Span>> batches =
spans.stream().collect(Collectors.groupingBy(SpanData::getResource)).entrySet().stream()
.collect(
Collectors.toMap(
entry -> createProcess(entry.getKey()),
entry -> Adapter.toJaeger(entry.getValue())));

List<CompletableResultCode> batchResults = new ArrayList<>(batches.size());
batches.forEach(
(process, jaegerSpans) -> {
Expand Down Expand Up @@ -148,13 +153,9 @@ public static JaegerThriftSpanExporterBuilder builder() {
*/
@Override
public CompletableResultCode shutdown() {
CompletableResultCode result = new CompletableResultCode();
// todo
return result.succeed();
}

// Visible for testing
Process getProcess() {
return process;
if (!isShutdown.compareAndSet(false, true)) {
logger.log(Level.WARNING, "Calling shutdown() multiple times.");
}
return CompletableResultCode.ofSuccess();
}
}
Loading