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

com.dnastack.oauth.client.TraceableHttpClientFactory Maven / Gradle / Ivy

package com.dnastack.oauth.client;

import brave.Tracing;
import brave.okhttp3.TracingInterceptor;
import feign.Client;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

import java.util.concurrent.TimeUnit;

@Slf4j
public class TraceableHttpClientFactory {
    public static long DEFAULT_CONNECTION_TIMEOUT = 10L;
    public static OkHttpClient.Builder getOkHttpClientBuilder(Tracing tracing) {
        return new OkHttpClient.Builder()
            .dispatcher(
                new Dispatcher(
                    tracing.currentTraceContext()
                        .executorService(new Dispatcher().executorService())
                )
            )
            .addNetworkInterceptor(TracingInterceptor.create(tracing));
    }

    public static OkHttpClient buildOkHttpClient(Tracing tracing) {
        return buildOkHttpClient(tracing, DEFAULT_CONNECTION_TIMEOUT);
    }

    public static OkHttpClient buildOkHttpClient(Tracing tracing, long timeout) {
        return getOkHttpClientBuilder(tracing)
            .addNetworkInterceptor(
                new HttpLoggingInterceptor(log::info)
                    .setLevel(HttpLoggingInterceptor.Level.BASIC)
            )
            .connectTimeout(timeout, TimeUnit.SECONDS)
            .readTimeout(timeout, TimeUnit.SECONDS)
            .writeTimeout(timeout, TimeUnit.SECONDS)
            .build();
    }

    public static Client buildFeignClient(Tracing tracing) {
        return buildFeignClient(tracing, DEFAULT_CONNECTION_TIMEOUT);
    }

    public static Client buildFeignClient(Tracing tracing, long timeout) {
        return new feign.okhttp.OkHttpClient(buildOkHttpClient(tracing, timeout));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy