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

Revamp Filters, Support for HTTP Filters, AccessLog Filters #71

Merged
merged 66 commits into from
Jul 26, 2024

Conversation

ajaypj
Copy link
Collaborator

@ajaypj ajaypj commented Jul 19, 2024

No description provided.

@ajaypj ajaypj changed the title added http access log filter and support for custom http filters added access log filter for grpc and http and custom http filter support Jul 19, 2024
@ajaypj ajaypj changed the title added access log filter for grpc and http and custom http filter support added access log filter for grpc and http, and custom http filter support Jul 19, 2024
.append(serverRequestParams.getMethodName()).append(" ")
.append(size).append(" ")
.append(System.currentTimeMillis()-startTime);
error(sb.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why error? here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added access-log separately and made it info

public void init(FilterConfig filterConfig) throws ServletException {}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change the signature to match how the grpc filter has

public void doProcessRequest(R request)

and

public void doProcessResponse(S response)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -88,7 +89,7 @@ task runHelloWorldServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'com.flipkart.gjex.examples.helloworld.HelloWorldApplication'
args "server", "./src/main/resources/hello_world_config.yml"
jvmArgs '--add-opens=java.base/java.lang=ALL-UNNAMED'
// jvmArgs '--add-opens=java.base/java.lang=ALL-UNNAMED'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncheck this line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 89 to 91
if (enableAccessLogs){
filtersMap.get(methodSignature).add(new GrpcAccessLogFilter());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do this before, and get it added in filtersForMethod as required

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 70 to 73
if (enableAccessLogs) {
context.addFilter(new FilterHolder(accessLogFilter), "/*" ,
EnumSet.of(DispatcherType.REQUEST));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make extract this out to a private separate function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -150,7 +150,7 @@ public void run(T configuration, U configMap, Environment environment) {

// Add all Grpc Filters to the Grpc Server
filters = getInstances(baseInjector, Filter.class);
grpcServer.registerFilters(filters, bindableServices);
grpcServer.registerFilters(filters, bindableServices, configuration.getGrpc().isEnableAccessLogs());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not pass boolean in arguments. Arguments should be proper class or objects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*
* @author ajay.jalgaonkar
*/
public abstract class GjexFilter<Req, Res, M> implements Logging {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the prefix Gjex. Classes have aboslute names, so we don't need to prefix Gjex since the class path itself has gjex

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @param <Req> Proto V3 message
* @param <Res> Proto V3 message
*/
public abstract class GjexGrpcFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the prefix Gjex. Classes have aboslute names, so we don't need to prefix Gjex since the class path itself has gjex

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @author ajay.jalgaonkar
*
*/
public class GrpcAccessLogGjexGrpcFilter<R extends GeneratedMessageV3,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much-repeated names?GrpcAccessLogFilter should have been enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @author ajay.jalgaonkar
*/
@Data
public abstract class GjexHttpFilter extends GjexFilter<ServletRequest, ServletResponse,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename to HttpFilter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 64 to 76
public void doProcessRequest(RequestParams<ServletRequest, Set<String>> requestParams) {
super.doProcessRequest(requestParams);
}

@Override
public void doProcessResponseHeaders(Set<String> responseHeaders) {
super.doProcessResponseHeaders(responseHeaders);
}

@Override
public void doProcessResponse(ResponseParams<ServletResponse> responseParams) {
super.doProcessResponse(responseParams);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you defining this methods? These needs to be implemented by the implementors of this abstract class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

* information contained in the Request headers
*
* @author regu.b
*
*/
@Named("AuthFilter")
public class AuthFilter implements Filter<HelloRequest, HelloReply>, Logging {
public class AuthGjexGrpcFilter extends GjexGrpcFilter<HelloRequest, HelloReply> implements Logging {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name correction please.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @author regu.b
*/
@Named("LoggingFilter")
public class LoggingFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3> implements Filter<Req, Res>, Logging {
public class LoggingGjexGrpcFilter<Req extends GeneratedMessageV3,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name correction please.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -1,6 +1,8 @@
Grpc:
server.port: 50051
server.executorThreads : 4
grpcFilterConfig:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access log can be a top level config.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are other config params other than accesslog, then this filterconfig can be used.
did not want to keep accesslog related config separate

@@ -14,6 +16,8 @@ Api:
service.selectors: 10
service.workers: 30
scheduledexecutor.threadpool.size: 1
httpFilterConfig:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access log can be a top level config.


import java.util.Map;

public class SampleApplication extends Application<SampleConfiguration, Map> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have we deleted these files?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added back

Context previous = attachContext(contextWithHeaders); // attaching headers to gRPC context
try {
filters.forEach(filter -> filter.doProcessResponse((GeneratedMessageV3) response));
grpcFilters.forEach(filter -> filter.doProcessResponse(ResponseParams.builder().response(response).build()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why wrap? what is the usecase?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResponseParams is a common class used for both http and grpc responses. so wrapped the grpc "response"

Comment on lines 154 to 160
RequestParams requestParams =
RequestParams.builder()
.clientIp(call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString())
.resourcePath(call.getMethodDescriptor().getFullMethodName().toLowerCase())
.metadata(headers)
.request(request)
.build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create this in loop? Can it not be outside?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put it inside.

public void setScheduledExecutorThreadPoolSize(int scheduledExecutorThreadPoolSize) {
this.scheduledExecutorThreadPoolSize = scheduledExecutorThreadPoolSize;
}
@JsonProperty("httpFilterConfig")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be named filterConfig since API Section itself is for HTTP

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, given that it contains a single boolean, may be we flatten it, and make accessLog as top level attribute.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should keep filterconfig as separate config for grpc and http. so it can't be top level.
in future other updates to filterconfig can come

this.executorThreads = executorThreads;
}
@JsonProperty("grpcFilterConfig")
private GrpcFilterConfig grpcFilterConfig;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should be filterConfig simply

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment on lines 21 to 24
* A Filter interface for processing Request, Request-Headers, Response and Response-Headers
* around gRPC and HTTP method invocation
*
* @param <Req> Proto V3 message
* @param <Res> Proto V3 message
* @author ajay.jalgaonkar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please dont change the original author information. Since there has been no new code here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset


@Getter
@Builder
public class ResponseParams <S>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required?

Comment on lines 52 to 74
public long getStartTime() {
return startTime;
}

public void setStartTime(long startTime) {
this.startTime = startTime;
}

public RequestParams<R, Metadata> getRequestParams() {
return requestParams;
}

public void setRequestParams(RequestParams<R, Metadata> requestParams) {
this.requestParams = requestParams;
}

public StringBuilder getSb() {
return sb;
}

public void setSb(StringBuilder sb) {
this.sb = sb;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these required, given that @Data has been already used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment on lines 58 to 72
public long getStartTime() {
return startTime;
}

public void setStartTime(long startTime) {
this.startTime = startTime;
}

public StringBuilder getSb() {
return sb;
}

public void setSb(StringBuilder sb) {
this.sb = sb;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really required?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

* @author ajay.jalgaonkar
*/

@Data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to have Data annotation? This is not a pojo class.

Comment on lines 14 to 17
public class HttpFilterParams {
private final Filter filter;
private final String pathSpec;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have RequestParams which has simlar, can we merge?

@Named("AuthFilter")
public class AuthFilter implements Filter<HelloRequest, HelloReply>, Logging {
@Named("AuthGrpcFilter")
public class AuthGrpcFilter extends GrpcFilter<HelloRequest, HelloReply> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can remain AuthFilter

Lets not change the grpc filter names. Gjex is major for grpc and http is provided for migration period. In long term i don't see the need for having this explicit name in filter.s

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Named("LoggingFilter")
public class LoggingFilter<Req extends GeneratedMessageV3, Res extends GeneratedMessageV3> implements Filter<Req, Res>, Logging {
@Named("LoggingGrpcFilter")
public class LoggingGrpcFilter<Req extends GeneratedMessageV3,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can remain LoggingFilter

Lets not change the grpc filter names. Gjex is major for grpc and http is provided for migration period. In long term i don't see the need for having this explicit name in filter.s

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@kingster kingster changed the title added access log filter for grpc and http, and custom http filter support Revamp Filters, Support for HTTP Filters, AccessLog Filters Jul 24, 2024
@kingster kingster merged commit 404e415 into master Jul 26, 2024
3 checks passed
@kingster kingster deleted the http-accesslog branch July 26, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants