Skip to content

Commit

Permalink
More logging around Apache client creation & close (#655)
Browse files Browse the repository at this point in the history
`ApacheHttpClientChannels` prints some debug log lines around creation & close
  • Loading branch information
iamdanfox committed Apr 15, 2020
1 parent 46096e4 commit af5b827
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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

0 comments on commit af5b827

Please sign in to comment.