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

More logging around Apache client creation & close #655

Merged
merged 5 commits into from
Apr 15, 2020
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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-655.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: '`ApacheHttpClientChannels` prints some debug log lines around creation
& close'
links:
- https://github.com/palantir/dialogue/pull/655
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.Safe;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import com.palantir.logsafe.exceptions.SafeRuntimeException;
import com.palantir.tritium.metrics.MetricRegistries;
import com.palantir.tritium.metrics.registry.MetricName;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
Expand Down Expand Up @@ -73,6 +75,7 @@
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.apache.http.pool.PoolStats;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -145,21 +148,40 @@ private static MetricName clientPoolSizeMetricName(String clientName, String sta

/** Intentionally opaque wrapper type - we don't want people using the inner Apache client directly. */
public static final class CloseableClient implements Closeable {
private final String name;
private final CloseableHttpClient client;
private final PoolingHttpClientConnectionManager pool;
private final ResponseLeakDetector leakDetector;

@Nullable
private final ExecutorService executor;

CloseableClient(
CloseableHttpClient client, ResponseLeakDetector leakDetector, @Nullable ExecutorService executor) {
String name,
CloseableHttpClient client,
PoolingHttpClientConnectionManager pool,
ResponseLeakDetector leakDetector,
@Nullable ExecutorService executor) {
this.name = name;
this.client = client;
this.pool = pool;
this.leakDetector = leakDetector;
this.executor = executor;
}

@Override
public void close() throws IOException {
PoolStats poolStats = pool.getTotalStats();
SafeRuntimeException stacktrace =
log.isDebugEnabled() ? new SafeRuntimeException("Exception for stacktrace") : null;
log.info(
"Closing Apache client",
SafeArg.of("name", name),
SafeArg.of("client", System.identityHashCode(client)),
SafeArg.of("idle", poolStats.getAvailable()),
SafeArg.of("leased", poolStats.getLeased()),
SafeArg.of("pending", poolStats.getPending()),
stacktrace);
client.close();
}

Expand Down Expand Up @@ -300,8 +322,19 @@ public CloseableClient build() {
.build());
});

CloseableHttpClient client = builder.build();
log.info(
"Created Apache client",
SafeArg.of("name", name),
SafeArg.of("client", System.identityHashCode(client)),
UnsafeArg.of("clientConfiguration", clientConfiguration),
UnsafeArg.of("executor", executor));
return new CloseableClient(
builder.build(), ResponseLeakDetector.of(name, conf.taggedMetricRegistry()), executor);
name,
client,
connectionManager,
ResponseLeakDetector.of(name, conf.taggedMetricRegistry()),
executor);
}
}

Expand Down