Skip to content

Commit

Permalink
renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-jalgaonkar committed Jul 24, 2024
1 parent f5a71dc commit c6b69df
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public class ApiService {
@JsonProperty("scheduledexecutor.threadpool.size")
private int scheduledExecutorThreadPoolSize;

@JsonProperty("httpFilterConfig")
@JsonProperty("filterConfig")
private HttpFilterConfig httpFilterConfig;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public class GrpcConfig {
@JsonProperty("server.executorThreads")
private int executorThreads = 0;

@JsonProperty("grpcFilterConfig")
@JsonProperty("filterConfig")
private GrpcFilterConfig grpcFilterConfig;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* A Filter interface for processing Request, Request-Headers, Response and Response-Headers
* around gRPC and HTTP method invocation
*
* @author ajay.jalgaonkar
* @author regu.b
*/
public abstract class Filter<Req, Res, M> implements Logging {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@
import com.flipkart.gjex.core.filter.ResponseParams;
import com.google.protobuf.GeneratedMessageV3;
import io.grpc.Metadata;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;

/**
* Filter for logging grpc access log requests
* @author ajay.jalgaonkar
*
*/

@Data
@EqualsAndHashCode(callSuper=false)
public class AccessLogGrpcFilter<R extends GeneratedMessageV3,
S extends GeneratedMessageV3> extends GrpcFilter<R,S> {
private long startTime = 0;
private RequestParams<R, Metadata> requestParams;
private StringBuilder sb;
public class AccessLogGrpcFilter<R extends GeneratedMessageV3, S extends GeneratedMessageV3> extends GrpcFilter<R,S> {
protected long startTime;
protected RequestParams<R, Metadata> requestParams;
protected Logger logger = getLoggerWithName("ACCESS-LOG");

@Override
public void doProcessRequest(RequestParams<R, Metadata> requestParamsInput) {
startTime = System.currentTimeMillis();
requestParams = requestParamsInput;
sb = new StringBuilder();
}

@Override
Expand All @@ -37,11 +32,10 @@ public void doProcessResponse(ResponseParams<S> responseParams) {
if (responseParams.getResponse() != null){
size = String.valueOf(responseParams.getResponse().getSerializedSize());
}
sb.append(requestParams.getClientIp()).append(" ")
.append(requestParams.getResourcePath()).append(" ")
.append(size).append(" ")
.append(System.currentTimeMillis()-startTime);
info("access-log", sb.toString());
if (logger.isInfoEnabled()){
logger.info("{} {} {} {}", requestParams.getClientIp(), requestParams.getResourcePath(),
size, System.currentTimeMillis()-startTime);
}
}

@Override
Expand All @@ -64,12 +58,4 @@ public RequestParams<R, Metadata> getRequestParams() {
public void setRequestParams(RequestParams<R, Metadata> requestParams) {
this.requestParams = requestParams;
}

public StringBuilder getSb() {
return sb;
}

public void setSb(StringBuilder sb) {
this.sb = sb;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.flipkart.gjex.core.filter.RequestParams;
import com.flipkart.gjex.core.filter.ResponseParams;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
Expand All @@ -19,37 +18,31 @@
*
*/

@Data
@EqualsAndHashCode(callSuper=false)
public class AccessLogHttpFilter extends HttpFilter {
private long startTime;
private StringBuilder sb;
protected long startTime;
protected Logger logger = getLoggerWithName("ACCESS-LOG");
protected static final String CONTENT_LENGTH_HEADER = "Content-Length";
protected static final String X_FORWARDED_FOR_HEADER = "x-forwarded-for";

@Override
public void doProcessRequest(RequestParams<ServletRequest, Set<String>> requestParams) {
public void doProcessRequest(RequestParams<ServletRequest, Set<String>> requestParamsInput) {
startTime = System.currentTimeMillis();
sb = new StringBuilder();
}

@Override
public void doProcessResponseHeaders(Set<String> responseHeaders) {
if (getRequest() instanceof HttpServletRequest && getResponse() instanceof HttpServletResponse){
HttpServletRequest httpServletRequest= (HttpServletRequest) getRequest();
HttpServletResponse httpServletResponse = (HttpServletResponse) getResponse();
sb.append(httpServletRequest.getHeader("x-forwarded-for")).append(" ")
.append(httpServletRequest.getRequestURI()).append(" ")
.append(httpServletResponse.getStatus()).append(" ")
.append(httpServletResponse.getHeader("Content-Length")).append(" ");
} else {
sb.append("Did not get HTTP request").append(" ");
}
}

@Override
public void doProcessResponse(ResponseParams<ServletResponse> response) {
sb.append(getRequest().getRemoteAddr()).append(" ");
sb.append(System.currentTimeMillis()-startTime);
info("access-log", sb.toString());
if (logger.isInfoEnabled()){
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response.getResponse();
logger.info("{} {} {} {} {} {}",
httpServletRequest.getHeader(X_FORWARDED_FOR_HEADER),
httpServletRequest.getRequestURI(),
httpServletResponse.getStatus(),
httpServletResponse.getHeader(CONTENT_LENGTH_HEADER),
request.getRemoteAddr(),
System.currentTimeMillis()-startTime
);
}
}

@Override
Expand All @@ -62,12 +55,4 @@ public long getStartTime() {
public void setStartTime(long startTime) {
this.startTime = startTime;
}

public StringBuilder getSb() {
return sb;
}

public void setSb(StringBuilder sb) {
this.sb = sb;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
* @author ajay.jalgaonkar
*/

@Data
@EqualsAndHashCode(callSuper=false)
public abstract class HttpFilter extends Filter<ServletRequest, ServletResponse,
Set<String>> implements javax.servlet.Filter {
private ServletRequest request;
private ServletResponse response;
protected ServletRequest request;
protected ServletResponse response;

@Override
public final void doFilter(ServletRequest requestInput, ServletResponse responseOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ default void debugLog(String msg, Object... args){
logger().debug(msgWithLogIdent(msg), args);
}

default void info(String loggerName, String msg){
Logger logger = getLoggerWithName(loggerName);
if(logger.isInfoEnabled()) logger.info(msg);
}

default void info(String msg) {
if (logger().isInfoEnabled()) logger().info(msgWithLogIdent(msg));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
* @author regu.b
*
*/
@Named("AuthGrpcFilter")
public class AuthGrpcFilter extends GrpcFilter<HelloRequest, HelloReply> {
@Named("AuthFilter")
public class AuthFilter extends GrpcFilter<HelloRequest, HelloReply> {

/** Fictitious authentication key*/
@SuppressWarnings("rawtypes")
Expand All @@ -44,7 +44,7 @@ public class AuthGrpcFilter extends GrpcFilter<HelloRequest, HelloReply> {

@Override
public GrpcFilter<HelloRequest, HelloReply> getInstance(){
return new AuthGrpcFilter();
return new AuthFilter();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@
* An implementation of the {@link GrpcFilter} interface as example that simply logs Request information
* @author regu.b
*/
@Named("LoggingGrpcFilter")
public class LoggingGrpcFilter<Req extends GeneratedMessageV3,
Res extends GeneratedMessageV3> extends GrpcFilter<Req, Res> {
@Named("LoggingFilter")
public class LoggingFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3> extends GrpcFilter<Req, Res> {

/** Custom response key to indicate request was logged on the server*/
static final Metadata.Key<String> CUSTOM_HEADER_KEY = Metadata.Key.of("request_response_logged_header_key", Metadata.ASCII_STRING_MARSHALLER);

@Override
public GrpcFilter<Req, Res> getInstance(){
return new LoggingGrpcFilter<>();
return new LoggingFilter<>();
}

@Override
Expand All @@ -53,5 +52,4 @@ public void doProcessResponseHeaders(Metadata responseHeaders) {
public void doProcessResponse(ResponseParams<Res> responseParams) {
info("Logging from filter. Response payload is : " + responseParams.getResponse().toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.flipkart.gjex.core.filter.grpc.GrpcFilter;
import com.flipkart.gjex.core.tracing.TracingSampler;
import com.flipkart.gjex.core.filter.http.HttpFilterParams;
import com.flipkart.gjex.examples.helloworld.filter.AuthGrpcFilter;
import com.flipkart.gjex.examples.helloworld.filter.LoggingGrpcFilter;
import com.flipkart.gjex.examples.helloworld.filter.AuthFilter;
import com.flipkart.gjex.examples.helloworld.filter.LoggingFilter;
import com.flipkart.gjex.examples.helloworld.service.GreeterService;
import com.flipkart.gjex.examples.helloworld.tracing.AllWhitelistTracingSampler;
import com.flipkart.gjex.examples.helloworld.web.HelloWorldResourceConfig;
Expand Down Expand Up @@ -49,8 +49,8 @@ protected void configure() {
// install(new ClientModule<GreeterGrpc.GreeterBlockingStub>(GreeterGrpc.GreeterBlockingStub.class,new ChannelConfig("localhost",9999)));
bind(GreeterGrpc.GreeterBlockingStub.class).toInstance(GreeterGrpc.newBlockingStub(channel));
bind(BindableService.class).annotatedWith(Names.named("GreeterService")).to(GreeterService.class);
bind(GrpcFilter.class).annotatedWith(Names.named("LoggingGrpcFilter")).to(LoggingGrpcFilter.class);
bind(GrpcFilter.class).annotatedWith(Names.named("AuthGrpcFilter")).to(AuthGrpcFilter.class);
bind(GrpcFilter.class).annotatedWith(Names.named("LoggingFilter")).to(LoggingFilter.class);
bind(GrpcFilter.class).annotatedWith(Names.named("AuthFilter")).to(AuthFilter.class);
bind(TracingSampler.class).to(AllWhitelistTracingSampler.class);
bind(ResourceConfig.class).annotatedWith(Names.named("HelloWorldResourceConfig")).to(HelloWorldResourceConfig.class);
bind(HttpFilterParams.class).annotatedWith(Names.named("ExampleHttpFilterParams"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import javax.inject.Inject;
import javax.inject.Named;

import com.flipkart.gjex.examples.helloworld.filter.AuthGrpcFilter;
import com.flipkart.gjex.examples.helloworld.filter.AuthFilter;
import io.dropwizard.metrics5.annotation.Timed;
import com.flipkart.gjex.core.filter.grpc.ApplicationHeaders;
import com.flipkart.gjex.core.filter.grpc.MethodFilters;
Expand All @@ -27,7 +27,7 @@
import com.flipkart.gjex.core.task.TaskException;
import com.flipkart.gjex.core.tracing.Traced;
import com.flipkart.gjex.examples.helloworld.bean.HelloBean;
import com.flipkart.gjex.examples.helloworld.filter.LoggingGrpcFilter;
import com.flipkart.gjex.examples.helloworld.filter.LoggingFilter;

import io.grpc.Metadata;
import io.grpc.Status;
Expand Down Expand Up @@ -68,7 +68,7 @@ public GreeterService(@Named("hw.greeting") String greeting, HelloBeanService he
@Override
@Api(deadlineConfig = "apiProperties.sayhello.deadline") // specify an API level Deadline that will cascade to all @ConcurrentTask invoked in serving this API
@Timed // the Timed annotation for publishing JMX metrics via MBean
@MethodFilters({LoggingGrpcFilter.class, AuthGrpcFilter.class}) // Method level filters
@MethodFilters({LoggingFilter.class, AuthFilter.class}) // Method level filters
@Traced(withSamplingRate=0.0f) // Start a new Trace or participate in a Client-initiated distributed trace
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {

Expand Down Expand Up @@ -126,7 +126,7 @@ private void invokeGrpcCall(HelloRequest req, HelloReply reply) {

@Override
@Timed // the Timed annotation for publishing JMX metrics via MBean
@MethodFilters({LoggingGrpcFilter.class, AuthGrpcFilter.class}) // Method level filters
@MethodFilters({LoggingFilter.class, AuthFilter.class}) // Method level filters
@Traced(withSamplingRate=0.0f) // Start a new Trace or participate in a Client-initiated distributed trace
public StreamObserver<Ping> pingPong(StreamObserver<Pong> responseObserver) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,23 @@ public class ExampleHttpFilter extends AccessLogHttpFilter {
@Override
public void doProcessRequest(RequestParams<ServletRequest, Set<String>> requestParams) {
setStartTime(System.currentTimeMillis());
setSb(new StringBuilder());
getSb().append("example-filter: ");
}

@Override
public void doProcessResponseHeaders(Set<String> responseHeaders) {
if (getRequest() instanceof HttpServletRequest && getResponse() instanceof HttpServletResponse){
HttpServletRequest httpServletRequest= (HttpServletRequest) getRequest();
HttpServletResponse httpServletResponse = (HttpServletResponse) getResponse();
getSb().append(httpServletRequest.getHeader("x-forwarded-for")).append(" ")
.append(httpServletRequest.getRequestURI()).append(" ")
.append(httpServletResponse.getStatus()).append(" ")
.append(httpServletResponse.getHeader("Content-Length")).append(" ");
} else {
getSb().append("Did not get HTTP request").append(" ");
}
}

@Override
public void doProcessResponse(ResponseParams<ServletResponse> response) {
getSb().append(getRequest().getRemoteAddr()).append(" ");
getSb().append(System.currentTimeMillis()-getStartTime());
info("access-log", getSb().toString());
if (logger.isInfoEnabled()){
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response.getResponse();
logger.info("{} {} {} {} {} {} {}",
this.getClass().getSimpleName(),
httpServletRequest.getHeader(X_FORWARDED_FOR_HEADER),
httpServletRequest.getRequestURI(),
httpServletResponse.getStatus(),
httpServletResponse.getHeader(CONTENT_LENGTH_HEADER),
request.getRemoteAddr(),
System.currentTimeMillis()-startTime
);
}
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/resources/hello_world_config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Grpc:
server.port: 50051
server.executorThreads : 4
grpcFilterConfig:
filterConfig:
enableAccessLogs: true

Dashboard:
Expand All @@ -16,7 +16,7 @@ Api:
service.selectors: 10
service.workers: 30
scheduledexecutor.threadpool.size: 1
httpFilterConfig:
filterConfig:
enableAccessLogs: true

Tracing:
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{5} %X - %msg%n"/>
</Console>
<Console name="access-log" target="SYSTEM_OUT">
<Console name="ACCESS-LOG" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{5} %X - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="io.netty.channel" level="info" additivity="false">
<AppenderRef ref="access-log"/>
<AppenderRef ref="ACCESS-LOG"/>
</Logger>
<Logger name="io.netty.channel" level="error" additivity="false">
<AppenderRef ref="console-log"/>
Expand Down

0 comments on commit c6b69df

Please sign in to comment.