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

ca.bitcoco.jsk.http.HttpUtils Maven / Gradle / Ivy

The newest version!
package ca.bitcoco.jsk.http;

import ca.bitcoco.jsk.operation.GlobalMetric;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongHistogram;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpUtils {

    private static final String UNKNOWN = "unknown";

    public final static LongHistogram HTTP_REQUEST_DURATION_METRIC = GlobalMetric.GetLongHistogram("http_request_duration", "the duration of service's http request", "ms");

    public final static LongHistogram HTTP_CLIENT_REQUEST_DURATION_METRIC = GlobalMetric.GetLongHistogram("http_client_request_duration", "the duration of http client request", "ms");

    /**
     * General service's API request duration metric
     */
    public static void RecordHttpRequestDurationMetric(String endpoint, String method, String path, int statusCode, Long latency) {
        Attributes attributes = Attributes.builder()
                .put("endpoint", endpoint)
                .put("method", method)
                .put("status_code", statusCode)
                .put("path", path)
                .putAll(GlobalMetric.GlobalAttributes()).build();
        HTTP_REQUEST_DURATION_METRIC.record(latency, attributes);
    }

    /**
     * General http client request duration metric
     */
    public static void RecordHttpClientRequestDurationMetric(String httpClientName, String method, String path, int statusCode, Long latency) {
        Attributes attributes = Attributes.builder()
                .put("name", httpClientName)
                .put("method", method)
                .put("status_code", statusCode)
                .put("path", path)
                .putAll(GlobalMetric.GlobalAttributes()).build();
        HTTP_CLIENT_REQUEST_DURATION_METRIC.record(latency, attributes);
    }

    public static String getQuery(String url) {
        URL home;
        try {
            home = new URL(url);
        } catch (MalformedURLException e) {
            return null;
        }
        return home.getQuery();
    }

    public static Map getQueryMap(String query) {
        String[] params = query.split("&");
        Map map = new HashMap();

        for (String param : params) {
            String name = param.split("=")[0];
            String value = param.split("=")[1];
            map.put(name, value);
        }
        return map;
    }

//    public static String getRemoteIpFrom(HttpServletRequest request) {
//        String ip = null;
//        int tryCount = 1;
//
//        while (!isIpFound(ip) && tryCount <= 6) {
//            switch (tryCount) {
//                case 1:
//                    ip = request.getHeader(HttpHeader.X_FORWARDED_FOR.key());
//                    break;
//                case 2:
//                    ip = request.getHeader(HttpHeader.PROXY_CLIENT_IP.key());
//                    break;
//                case 3:
//                    ip = request.getHeader(HttpHeader.WL_PROXY_CLIENT_IP.key());
//                    break;
//                case 4:
//                    ip = request.getHeader(HttpHeader.HTTP_CLIENT_IP.key());
//                    break;
//                case 5:
//                    ip = request.getHeader(HttpHeader.HTTP_X_FORWARDED_FOR.key());
//                    break;
//                default:
//                    ip = request.getRemoteAddr();
//            }
//
//            tryCount++;
//        }
//
//        return ip;
//    }

    private static boolean isIpFound(String ip) {
        return ip != null && ip.length() > 0 && !UNKNOWN.equalsIgnoreCase(ip);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy