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
Expand Up @@ -3,13 +3,13 @@
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

class FastUUIDGenerator {
public class FastUUIDGenerator {
/**
* This function generates UUIDs using ThreadLocalRandom, which is faster and doesn't block like
* the default randomUUID method that relies on /dev/random. It's suitable for most random UUID
* needs.
*/
static UUID randomUUID() {
public static UUID randomUUID() {

Choose a reason for hiding this comment

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

Did we change this just for the purpose of the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we needed this implementation of random uuid generation in client side and in our interceptor to generate random uuid as it it faster than traditional UUID generator which requires synchronization between threads.

long mostSigBits = ThreadLocalRandom.current().nextLong();
long leastSigBits = ThreadLocalRandom.current().nextLong();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public static RequestContext forTenantId(String tenantId) {
FastUUIDGenerator.randomUUID().toString());
}

public static RequestContext withRequestId(String tenantId, String requestId) {
return new RequestContext()
.put(RequestContextConstants.TENANT_ID_HEADER_KEY, tenantId)
.put(RequestContextConstants.REQUEST_ID_HEADER_KEY, requestId);
}

public static RequestContext fromMetadata(Metadata metadata) {
RequestContext requestContext = new RequestContext();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.hypertrace.core.grpcutils.server;

import static org.hypertrace.core.grpcutils.context.RequestContextConstants.REQUEST_ID_HEADER_KEY;

import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.ForwardingServerCallListener;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.hypertrace.core.grpcutils.context.FastUUIDGenerator;
import org.hypertrace.core.grpcutils.context.RequestContext;
import org.slf4j.MDC;

@Slf4j
public final class RequestContextLoggingServerInterceptor implements ServerInterceptor {

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> serverCall,
Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {
RequestContext currentContext =
Optional.ofNullable(RequestContext.CURRENT.get())
.orElseGet(() -> RequestContext.fromMetadata(metadata));
Optional<String> opRequestId = currentContext.getHeaderValue(REQUEST_ID_HEADER_KEY);
if (opRequestId.isEmpty()) {
opRequestId = Optional.of(FastUUIDGenerator.randomUUID().toString());
}
final String requestId = opRequestId.get();
ServerCall.Listener<ReqT> listener =
Contexts.interceptCall(
Context.current().withValue(RequestContext.CURRENT, currentContext),
serverCall,
metadata,
serverCallHandler);
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<>(listener) {

@Override
public void onCancel() {
try {
MDC.clear();
} catch (Exception e) {
log.error("Error while clearing request context details from MDC params", e);
}
super.onCancel();
}

@Override
public void onComplete() {
try {
MDC.clear();
} catch (Exception e) {
log.error("Error while clearing request context details from MDC params", e);
}
super.onComplete();
}

@Override
public void onMessage(ReqT message) {
try {
MDC.put(REQUEST_ID_HEADER_KEY, requestId);
} catch (Exception e) {
log.error("Error while setting request context details in MDC params", e);
}
super.onMessage(message);
}
};
}
}
Loading