Skip to content

Commit

Permalink
Merge pull request #689 from i-Cell-Mobilsoft-Open-Source/feature/659…
Browse files Browse the repository at this point in the history
…-enhanced-grpc-client-logging-trimming

Feature/659 enhanced grpc client logging trimming
  • Loading branch information
tamkarcsi authored Oct 9, 2024
2 parents 69d9b46 + bee6382 commit 64d565f
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* host: localhost # default localhost
* port: 8199 # default 8199
* maxInboundMetadataSize: 1_048_576 # byte, default 8192
* requestLogSize: 1000 # Characters, default 1000
* responseLogSize: 1000 # Characters, default 1000
* </pre>
*
* @author Imre Scheffer
Expand Down Expand Up @@ -66,6 +68,16 @@ public class GrpcClientConfig implements IGrpcClientConfig {
*/
public static final String MAX_INBOUND_METADATA_SIZE = "maxInboundMetadataSize";

/**
* gRPC client request log maximum size in characters, default is 1000
*/
public static final String REQUEST_LOG_SIZE = "requestLogSize";

/**
* gRPC client response log maximum size in characters, default is 1000
*/
public static final String RESPONSE_LOG_SIZE = "responseLogSize";

@Inject
private Config config;

Expand Down Expand Up @@ -93,6 +105,16 @@ public Integer getMaxInboundMetadataSize() {
return config.getOptionalValue(joinKey(MAX_INBOUND_METADATA_SIZE), Integer.class).orElse(8192);
}

@Override
public Integer getRequestLogSize() {
return config.getOptionalValue(joinKey(REQUEST_LOG_SIZE), Integer.class).orElse(1000);
}

@Override
public Integer getResponseLogSize() {
return config.getOptionalValue(joinKey(RESPONSE_LOG_SIZE), Integer.class).orElse(1000);
}

/**
* Getter for the field {@code configKey}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ public interface IGrpcClientConfig {
* @return Inbound metadata max size
*/
Integer getMaxInboundMetadataSize();

/**
* Gets the request log maximum size
*
* @return the request log maximum size
*/
Integer getRequestLogSize();

/**
* Gets the response log maximum size
*
* @return the response log maximum size
*/
Integer getResponseLogSize();

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import hu.icellmobilsoft.coffee.grpc.client.GrpcClient;
import hu.icellmobilsoft.coffee.tool.utils.annotation.AnnotationUtil;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.stub.AbstractBlockingStub;
Expand All @@ -47,6 +48,11 @@
@ApplicationScoped
public class GrpcClientProducerFactory {

/**
* {@link CallOptions.Key} for configValue parameter
*/
public static final CallOptions.Key<String> CONFIG_VALUE_KEY = CallOptions.Key.createWithDefault("configValue", StringUtils.EMPTY);

/**
* Default constructor, constructs a new object.
*/
Expand Down Expand Up @@ -96,7 +102,7 @@ public AbstractBlockingStub grpcClientTemplateProducer(final InjectionPoint inje

instance.destroy(channel);

return blockingStub;
return (AbstractBlockingStub) blockingStub.withOption(CONFIG_VALUE_KEY, configKey);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
package hu.icellmobilsoft.coffee.grpc.client.interceptor;

import jakarta.enterprise.inject.spi.CDI;

import org.apache.commons.lang3.StringUtils;

import hu.icellmobilsoft.coffee.grpc.client.GrpcClient;
import hu.icellmobilsoft.coffee.grpc.client.config.GrpcClientConfig;
import hu.icellmobilsoft.coffee.grpc.client.extension.GrpcClientProducerFactory;
import hu.icellmobilsoft.coffee.se.logging.DefaultLogger;
import hu.icellmobilsoft.coffee.se.logging.Logger;
import io.grpc.CallOptions;
Expand Down Expand Up @@ -49,14 +56,44 @@ public ClientRequestInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {

ClientCall<ReqT, RespT> call = new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
final String configKey = callOptions.getOption(GrpcClientProducerFactory.CONFIG_VALUE_KEY);

return new SimpleForwardingClientCall<>(next.newCall(method, callOptions)) {

int logSize = 0;
int count = 0;

@Override
public void sendMessage(ReqT message) {
LOGGER.info("Sending request message: [{0}]", message);

int requestLogSize = CDI.current().select(GrpcClientConfig.class, new GrpcClient.Literal(configKey)).get().getRequestLogSize();

if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Sending request message part [{0}]: [{1}]", count++, String.valueOf(message));
} else {
StringBuilder messageToPrint = new StringBuilder();

String messageString = String.valueOf(message);
if (messageString.length() > requestLogSize - logSize) {
if (requestLogSize - logSize > 0) {
messageToPrint.append(StringUtils.truncate(messageString, requestLogSize - logSize));
logSize += messageToPrint.length();
messageToPrint.append("...<truncated>");
}
} else {
messageToPrint.append(messageString);
logSize += messageToPrint.length();
}

if (messageToPrint.length() > 0) {
LOGGER.info("Sending request message part [{0}]: [{1}]", count++, messageToPrint.toString());
}
}

super.sendMessage(message);
}

};
return call;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
package hu.icellmobilsoft.coffee.grpc.client.interceptor;

import jakarta.enterprise.inject.spi.CDI;

import org.apache.commons.lang3.StringUtils;

import hu.icellmobilsoft.coffee.grpc.client.GrpcClient;
import hu.icellmobilsoft.coffee.grpc.client.config.GrpcClientConfig;
import hu.icellmobilsoft.coffee.grpc.client.extension.GrpcClientProducerFactory;
import hu.icellmobilsoft.coffee.se.logging.DefaultLogger;
import hu.icellmobilsoft.coffee.se.logging.Logger;
import io.grpc.CallOptions;
Expand Down Expand Up @@ -52,13 +59,47 @@ public ClientResponseInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {

ClientCall<ReqT, RespT> call = new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
final String configKey = callOptions.getOption(GrpcClientProducerFactory.CONFIG_VALUE_KEY);

return new SimpleForwardingClientCall<>(next.newCall(method, callOptions)) {

@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
Listener<RespT> listener = new SimpleForwardingClientCallListener<RespT>(responseListener) {

Listener<RespT> listener = new SimpleForwardingClientCallListener<>(responseListener) {

int logSize = 0;
int count = 0;

@Override
public void onMessage(RespT message) {
LOGGER.info("Received response message: [{0}]", message);
int responseLogSize = CDI.current()
.select(GrpcClientConfig.class, new GrpcClient.Literal(configKey))
.get()
.getResponseLogSize();

if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Received response message part [{0}]: [{1}]", count++,String.valueOf(message));
} else {
StringBuilder messageToPrint = new StringBuilder();

String messageString = String.valueOf(message);
if (messageString.length() > responseLogSize - logSize) {
if (responseLogSize - logSize > 0) {
messageToPrint.append(StringUtils.truncate(messageString, responseLogSize - logSize));
logSize += messageToPrint.length();
messageToPrint.append("...<truncated>");
}
} else {
messageToPrint.append(messageString);
logSize += messageToPrint.length();
}

if (messageToPrint.length() > 0) {
LOGGER.info("Received response message part [{0}]: [{1}]", count++, messageToPrint.toString());
}
}

super.onMessage(message);
}

Expand All @@ -72,7 +113,5 @@ public void onClose(Status status, Metadata trailers) {
super.start(listener, headers);
}
};

return call;
}
}
2 changes: 2 additions & 0 deletions docs/en/common/core/coffee-grpc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ coffee:
host: localhost # default localhost
port: 8199 # default 8199
maxInboundMetadataSize: 8192 # Bytes, default 8192 (8KiB)
requestLogSize: 1000 # Characters, default 1000
responseLogSize: 1000 # Characters, default 1000
----

.CDI inject DummyServiceGrpc usage
Expand Down
6 changes: 6 additions & 0 deletions docs/en/migration/migration280to290.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ Some english translation issue has been fixed.

=== coffee-module-notification
The commons-email was updated to a Jakarta-compatible release, and the module has been re-enabled.

=== coffee-grpc

** `GrpcClientConfig` extended with the `requestLogSize` and the `responseLogSize` parameter,
which serves to set the client request and response maximum log size in characters,
the characters exceeding the limit will be truncated.
2 changes: 2 additions & 0 deletions docs/hu/common/core/coffee-grpc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ coffee:
host: localhost # default localhost
port: 8199 # default 8199
maxInboundMetadataSize: 8192 # Bytes, default 8192 (8KiB)
requestLogSize: 1000 # Characters, default 1000
responseLogSize: 1000 # Characters, default 1000
----

.CDI inject DummyServiceGrpc haszálata
Expand Down
1 change: 1 addition & 0 deletions docs/hu/migration/migration270to280.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ A változtatások nem eredményeznek átállási munkálatokat, visszafelé komp
=== coffee-grpc

** microprofile-health támogatás

** Külön managed-executor-service használata (java /ee/concurrency/executor/grpc) a gRPC hívásokhoz.
A `managed-executor-service` konfigurációja a `coffee-grpc-server-extension` dokumentációban van bemutatva.

Expand Down
6 changes: 6 additions & 0 deletions docs/hu/migration/migration280to290.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ Javításra került néhány hiba az angol nyelvű dokumentációban.

=== coffee-module-notification
Frissítésre került a commons-email jakarta kompatibilis release, így ez a modul visszakapcsolásra került.

=== coffee-grpc

** GrpcClientConfig kibővítve a requestLogSize és a responseLogSize paraméterekkel,
ezek a kliens kérés és válasz maximális log méretének korlátozására szolgálnak karakterekben,
a limit feletti karakterek levágásra kerülnek.

0 comments on commit 64d565f

Please sign in to comment.