Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
package org.patinanetwork.codebloom.config;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
import io.micrometer.observation.ObservationRegistry;
import java.time.Duration;
import lombok.AllArgsConstructor;
import org.patinanetwork.codebloom.grpc.leetcode.auth.v1.LeetcodeAuthStealerServiceGrpc;
import org.patinanetwork.codebloom.grpc.leetcode.auth.v1.LeetcodeAuthStealerServiceGrpc.LeetcodeAuthStealerServiceBlockingStub;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(LeetcodeAuthStealerGrpcClientConfiguration.class)
public class GrpcClientConfiguration {

private final ObservationRegistry observationRegistry;

@Bean
GrpcRequestIdInterceptor grpcRequestIdInterceptor() {
return new GrpcRequestIdInterceptor();
}

@Bean
ManagedChannel lassServiceChannel(final LeetcodeAuthStealerGrpcClientConfiguration authStealerProps) {
var builder = ManagedChannelBuilder.forTarget(authStealerProps.host()).intercept(new ClientInterceptor() {
@Override
public <Q, R> ClientCall<Q, R> interceptCall(
final MethodDescriptor<Q, R> method, final CallOptions options, final Channel next) {
return next.newCall(method, options.withDeadlineAfter(authStealerProps.timeout()));
}
});
ObservationGrpcClientInterceptor grpcObservationInterceptor() {
return new ObservationGrpcClientInterceptor(observationRegistry);
}

private ClientInterceptor[] interceptors(
Duration timeout,
GrpcRequestIdInterceptor requestIdInterceptor,
ObservationGrpcClientInterceptor observationInterceptor) {

return new ClientInterceptor[] {
new GrpcClientTimeoutInterceptor(timeout), requestIdInterceptor, observationInterceptor
};
}

@Bean
ManagedChannel lassServiceChannel(
LeetcodeAuthStealerGrpcClientConfiguration authStealerProps,
GrpcRequestIdInterceptor requestIdInterceptor,
ObservationGrpcClientInterceptor observationInterceptor) {

var builder = ManagedChannelBuilder.forTarget(authStealerProps.host())
.intercept(interceptors(authStealerProps.timeout(), requestIdInterceptor, observationInterceptor));

return (authStealerProps.tls() ? builder.useTransportSecurity() : builder.usePlaintext()).build();
}

@Bean
LeetcodeAuthStealerServiceBlockingStub lassServiceBlockingStub(final ManagedChannel lassServiceChannel) {
LeetcodeAuthStealerServiceBlockingStub lassServiceBlockingStub(ManagedChannel lassServiceChannel) {

return LeetcodeAuthStealerServiceGrpc.newBlockingStub(lassServiceChannel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.patinanetwork.codebloom.config;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.MethodDescriptor;
import java.time.Duration;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class GrpcClientTimeoutInterceptor implements ClientInterceptor {

private final Duration timeout;

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(

Check warning on line 17 in src/main/java/org/patinanetwork/codebloom/config/GrpcClientTimeoutInterceptor.java

View check run for this annotation

SonarQubeCloud / [codebloom_backend] SonarCloud Code Analysis

Rename this generic name to match the regular expression '^[A-Z][0-9]?$'.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_codebloom_backend&issues=AaE45PxySGvnmV0Op73p&open=AaE45PxySGvnmV0Op73p&pullRequest=1006

Check warning on line 17 in src/main/java/org/patinanetwork/codebloom/config/GrpcClientTimeoutInterceptor.java

View check run for this annotation

SonarQubeCloud / [codebloom_backend] SonarCloud Code Analysis

Rename this generic name to match the regular expression '^[A-Z][0-9]?$'.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_codebloom_backend&issues=AaE45PxySGvnmV0Op73o&open=AaE45PxySGvnmV0Op73o&pullRequest=1006
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return next.newCall(method, callOptions.withDeadlineAfter(timeout));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.patinanetwork.codebloom.config;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import org.slf4j.MDC;

public class GrpcRequestIdInterceptor implements ClientInterceptor {

private static final Metadata.Key<String> REQUEST_ID_KEY =
Metadata.Key.of("x-request-id", Metadata.ASCII_STRING_MARSHALLER);

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(

Check warning on line 18 in src/main/java/org/patinanetwork/codebloom/config/GrpcRequestIdInterceptor.java

View check run for this annotation

SonarQubeCloud / [codebloom_backend] SonarCloud Code Analysis

Rename this generic name to match the regular expression '^[A-Z][0-9]?$'.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_codebloom_backend&issues=AaE45PwsSGvnmV0Op73m&open=AaE45PwsSGvnmV0Op73m&pullRequest=1006

Check warning on line 18 in src/main/java/org/patinanetwork/codebloom/config/GrpcRequestIdInterceptor.java

View check run for this annotation

SonarQubeCloud / [codebloom_backend] SonarCloud Code Analysis

Rename this generic name to match the regular expression '^[A-Z][0-9]?$'.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_codebloom_backend&issues=AaE45PwsSGvnmV0Op73n&open=AaE45PwsSGvnmV0Op73n&pullRequest=1006
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {

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

@Override
public void start(Listener<RespT> listener, Metadata headers) {
String requestId = MDC.get("requestId");

if (requestId != null && !headers.containsKey(REQUEST_ID_KEY)) {
headers.put(REQUEST_ID_KEY, requestId);
}

super.start(listener, headers);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.patinanetwork.codebloom.utilities;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

@Component
public class RequestIdFilter extends OncePerRequestFilter {

private static final String HEADER = "X-Request-Id";
private static final String MDC_KEY = "requestId";

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

String requestId = UUID.randomUUID().toString();

try {
MDC.put(MDC_KEY, requestId);
response.setHeader(HEADER, requestId);

filterChain.doFilter(request, response);
} finally {
MDC.remove(MDC_KEY);
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{requestId}] %logger{36} - %msg%n"

spring:
application:
name: codebloom
Expand Down
Loading