Skip to content

Commit

Permalink
Hide spamming logs for unsupported channel options
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Venturi - Diennea committed Jul 22, 2022
1 parent e5a63af commit 1b2e09f
Showing 1 changed file with 88 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
Licensed to Diennea S.r.l. under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. Diennea S.r.l. licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Licensed to Diennea S.r.l. under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. Diennea S.r.l. licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.carapaceproxy.core;
Expand All @@ -25,6 +25,7 @@
import com.google.common.annotations.VisibleForTesting;
import io.micrometer.core.instrument.Metrics;
import io.netty.channel.ChannelOption;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollChannelOption;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
Expand Down Expand Up @@ -329,12 +330,15 @@ public Publisher<Void> forward(ProxyRequest request, boolean cache) {
.responseTimeout(Duration.ofMillis(connectionConfig.getStuckRequestTimeout()))
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionConfig.getConnectTimeout())
.option(ChannelOption.SO_KEEPALIVE, true) // Enables TCP keepalive: TCP starts sending keepalive probes when a connection is idle for some time.
.option(NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPIDLE), connectionConfig.getKeepaliveIdle())
.option(NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPINTERVAL), connectionConfig.getKeepaliveInterval())
.option(NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPCOUNT), connectionConfig.getKeepaliveCount())
.option(EpollChannelOption.TCP_KEEPIDLE, connectionConfig.getKeepaliveIdle())
.option(EpollChannelOption.TCP_KEEPINTVL, connectionConfig.getKeepaliveInterval())
.option(EpollChannelOption.TCP_KEEPCNT, connectionConfig.getKeepaliveCount())
.option(Epoll.isAvailable()
? EpollChannelOption.TCP_KEEPIDLE
: NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPIDLE), connectionConfig.getKeepaliveIdle())
.option(Epoll.isAvailable()
? EpollChannelOption.TCP_KEEPINTVL
: NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPINTERVAL), connectionConfig.getKeepaliveInterval())
.option(Epoll.isAvailable()
? EpollChannelOption.TCP_KEEPCNT
: NioChannelOption.of(ExtendedSocketOptions.TCP_KEEPCOUNT), connectionConfig.getKeepaliveCount())
.doOnRequest((req, conn) -> {
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Start sending request for "
Expand All @@ -346,17 +350,17 @@ public Publisher<Void> forward(ProxyRequest request, boolean cache) {
endpointStats.getTotalRequests().incrementAndGet();
endpointStats.getLastActivity().set(System.currentTimeMillis());
}).doAfterRequest((req, conn) -> {
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Finished sending request for "
+ " Using client id " + key.getHostPort() + "_" + connectionConfig.getId()
+ " Uri " + request.getUri()
+ " Timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS"))
+ " Backend " + endpointHost + ":" + endpointPort);
}
}).doAfterResponseSuccess((resp, conn) -> {
PENDING_REQUESTS_GAUGE.dec();
endpointStats.getLastActivity().set(System.currentTimeMillis());
});
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Finished sending request for "
+ " Using client id " + key.getHostPort() + "_" + connectionConfig.getId()
+ " Uri " + request.getUri()
+ " Timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS"))
+ " Backend " + endpointHost + ":" + endpointPort);
}
}).doAfterResponseSuccess((resp, conn) -> {
PENDING_REQUESTS_GAUGE.dec();
endpointStats.getLastActivity().set(System.currentTimeMillis());
});
});

AtomicBoolean cacheable = new AtomicBoolean(cache);
Expand All @@ -376,64 +380,64 @@ public Publisher<Void> forward(ProxyRequest request, boolean cache) {
req.header(HttpHeaderNames.HOST, request.getRequestHeaders().get(HttpHeaderNames.HOST)); // netty overrides the value, we need to force it
return out.send(request.getRequestData()); // client request body
}).response((resp, flux) -> { // endpoint response
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Receive response from backend for " + request.getRemoteAddress()
+ " Using client id " + key.getHostPort() + "_" + connectionConfig.getId()
+ " uri" + request.getUri()
+ " timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS"))
+ " Backend: " + request.getAction().host);
}

request.setResponseStatus(resp.status());
request.setResponseHeaders(resp.responseHeaders().copy()); // headers from endpoint to client
if (cacheable.get() && parent.getCache().isCacheable(resp) && cacheReceiver.receivedFromRemote(resp)) {
addCachedResponseHeaders(request);
} else {
cacheable.set(false);
}
addCustomResponseHeaders(request, request.getAction().customHeaders);
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Receive response from backend for " + request.getRemoteAddress()
+ " Using client id " + key.getHostPort() + "_" + connectionConfig.getId()
+ " uri" + request.getUri()
+ " timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS"))
+ " Backend: " + request.getAction().host);
}

return request.sendResponseData(flux.retain().doOnNext(data -> { // response data
request.setLastActivity(System.currentTimeMillis());
endpointStats.getLastActivity().set(System.currentTimeMillis());
if (cacheable.get()) {
cacheReceiver.receivedFromRemote(data);
}
}).doOnComplete(() -> {
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Send all response to client " + request.getRemoteAddress()
+ " Using client id " + key.getHostPort() + "_" + connectionConfig.getId()
+ " for uri " + request.getUri()
+ " timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS"))
+ " Backend: " + request.getAction().host);
}
if (cacheable.get()) {
parent.getCache().cacheContent(cacheReceiver);
}
}));
}).onErrorResume(err -> { // custom endpoint request/response error handling
PENDING_REQUESTS_GAUGE.dec();
request.setResponseStatus(resp.status());
request.setResponseHeaders(resp.responseHeaders().copy()); // headers from endpoint to client
if (cacheable.get() && parent.getCache().isCacheable(resp) && cacheReceiver.receivedFromRemote(resp)) {
addCachedResponseHeaders(request);
} else {
cacheable.set(false);
}
addCustomResponseHeaders(request, request.getAction().customHeaders);

String endpoint = request.getAction().host + ":" + request.getAction().port;
if (err instanceof io.netty.handler.timeout.ReadTimeoutException) {
STUCK_REQUESTS_COUNTER.inc();
LOGGER.log(Level.SEVERE, "Read timeout error occurred for endpoint {0}; request: {1}", new Object[]{endpoint, request});
if (parent.getCurrentConfiguration().isBackendsUnreachableOnStuckRequests()) {
parent.getBackendHealthManager().reportBackendUnreachable(
endpoint, System.currentTimeMillis(), "Error: " + err
);
}
return serveInternalErrorMessage(request);
return request.sendResponseData(flux.retain().doOnNext(data -> { // response data
request.setLastActivity(System.currentTimeMillis());
endpointStats.getLastActivity().set(System.currentTimeMillis());
if (cacheable.get()) {
cacheReceiver.receivedFromRemote(data);
}

LOGGER.log(Level.SEVERE, "Error proxying request for endpoint {0}; request: {1};\nError: {2}", new Object[]{endpoint, request, err});
if (err instanceof ConnectException) {
}).doOnComplete(() -> {
if (CarapaceLogger.isLoggingDebugEnabled()) {
CarapaceLogger.debug("Send all response to client " + request.getRemoteAddress()
+ " Using client id " + key.getHostPort() + "_" + connectionConfig.getId()
+ " for uri " + request.getUri()
+ " timestamp " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss.SSS"))
+ " Backend: " + request.getAction().host);
}
if (cacheable.get()) {
parent.getCache().cacheContent(cacheReceiver);
}
}));
}).onErrorResume(err -> { // custom endpoint request/response error handling
PENDING_REQUESTS_GAUGE.dec();

String endpoint = request.getAction().host + ":" + request.getAction().port;
if (err instanceof io.netty.handler.timeout.ReadTimeoutException) {
STUCK_REQUESTS_COUNTER.inc();
LOGGER.log(Level.SEVERE, "Read timeout error occurred for endpoint {0}; request: {1}", new Object[]{endpoint, request});
if (parent.getCurrentConfiguration().isBackendsUnreachableOnStuckRequests()) {
parent.getBackendHealthManager().reportBackendUnreachable(
endpoint, System.currentTimeMillis(), "Error: " + err
);
}
return serveServiceNotAvailable(request);
});
return serveInternalErrorMessage(request);
}

LOGGER.log(Level.SEVERE, "Error proxying request for endpoint {0}; request: {1};\nError: {2}", new Object[]{endpoint, request, err});
if (err instanceof ConnectException) {
parent.getBackendHealthManager().reportBackendUnreachable(
endpoint, System.currentTimeMillis(), "Error: " + err
);
}
return serveServiceNotAvailable(request);
});
}

private Publisher<Void> serveServiceNotAvailable(ProxyRequest request) {
Expand Down

0 comments on commit 1b2e09f

Please sign in to comment.