All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.turbospaces.grpc.GrpcClientInterceptor Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package com.turbospaces.grpc;

import java.util.Objects;
import java.util.UUID;

import com.turbospaces.boot.AbstractBootstrapAware;
import com.turbospaces.boot.Bootstrap;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.Context;
import io.grpc.MethodDescriptor;
import io.micrometer.core.instrument.binder.grpc.MetricCollectingClientInterceptor;
import io.netty.util.AsciiString;

public class GrpcClientInterceptor extends AbstractBootstrapAware implements ClientInterceptor {
    private MetricCollectingClientInterceptor interceptor;

    @Override
    public void setBootstrap(Bootstrap bootstrap) throws Throwable {
        super.setBootstrap(bootstrap);
        interceptor = new MetricCollectingClientInterceptor(bootstrap.meterRegistry());
    }
    @Override
    public  ClientCall interceptCall(
            MethodDescriptor method,
            CallOptions callOptions,
            Channel next) {
        ClientCall interceptCall = interceptor.interceptCall(method, callOptions, next);
        ContextPreservingClientCall preservingClientCall = new ContextPreservingClientCall(interceptCall, method);
        preservingClientCall.setBootstrap(bootstrap);
        return preservingClientCall;
    }
    public static Context setContext(UUID messageId, AsciiString routingKey) {
        Context current = Context.current();
        current = current.withValue(GrpcHeaders.CONTEXT_MESSAGE_ID, messageId.toString());

        if (Objects.nonNull(routingKey)) {
            current = current.withValue(GrpcHeaders.CONTEXT_ROUTING_KEY, routingKey.toString());
        }
        return current.attach();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy