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

com.gs.api.accelrx.web.client.request.WebClientRequestContextLogger Maven / Gradle / Ivy

The newest version!
package com.gs.api.accelrx.web.client.request;

import com.gs.api.accelrx.AccelRxWebClientResponse;
import com.gs.api.accelrx.PerformanceLog;
import com.gs.api.accelrx.WebClientRequestLogger;
import io.reactivex.rxjava3.core.Completable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class WebClientRequestContextLogger implements WebClientRequestLogger {
    private static final Logger log = LoggerFactory.getLogger(WebClientRequestContextLogger.class);

    private final String serviceName;
    private final String operationId;
    private final PerformanceLog perfLog;

    private long startTime;

    public WebClientRequestContextLogger(String serviceName, String operationId, PerformanceLog perfLog) {
        this.serviceName = serviceName;
        this.operationId = operationId;
        this.perfLog = perfLog;
    }

    @Override
    public Completable start() {
        return Completable.fromAction(() -> {
            startTime = System.nanoTime();
            perfLog.markStartTime("client:" + serviceName);
        });
    }

    @Override
    public Completable startCreateRequest() {
        return Completable.fromAction(() -> {
            perfLog.markStartTime("client:" + serviceName + ":create-request");
        });
    }

    @Override
    public Completable endCreateRequest() {
        return Completable.fromAction(() -> {
            perfLog.markEndTime("client:" + serviceName + ":create-request");
        });
    }

    @Override
    public Completable startRequest() {
        return Completable.fromAction(() -> {
            perfLog.markStartTime("client:" + serviceName + ":request");
        });
    }

    @Override
    public Completable endRequest() {
        return Completable.fromAction(() -> {
            perfLog.markEndTime("client:" + serviceName + ":request");
        });
    }

    @Override
    public Completable startProcessResponse() {
        return Completable.fromAction(() -> {
            perfLog.markStartTime("client:" + serviceName + ":process-response");
        });
    }

    @Override
    public Completable endProcessResponse() {
        return Completable.fromAction(() -> {
            perfLog.markEndTime("client:" + serviceName + ":process-response");
        });
    }

    @Override
    public void log(AccelRxWebClientResponse response) {
        if (response.successful()) {
            successLog(response);
        } else {
            failureLog(response);
        }
    }

    private void failureLog(AccelRxWebClientResponse response) {
        log.warn("Web service call to '{}:{}' -> failed with statusCode={} and message={} in duration={} ms",
                response.serviceName(),
                response.operationId(),
                response.statusCode(),
                response.message(),
                TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
    }

    private void successLog(AccelRxWebClientResponse response) {
        perfLog.markEndTime("client:" + serviceName);
        perfLog.logClientPerformance(serviceName);

        log.info("Web service call to '{}:{}' -> succeeded with statusCode={} in duration={}ms",
                response.serviceName(),
                response.operationId(),
                response.statusCode(),
                TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy