public void collectRequestStats(HttpRequestInfo req) {
        // ipv4/ipv6 tracking
        String clientIp;
        final String xForwardedFor = req.getHeaders().getFirst(X_FORWARDED_FOR_HEADER);
        if (xForwardedFor == null) {
            clientIp = req.getClientIp();
        } else {
            clientIp = extractClientIpFromXForwardedFor(xForwardedFor);
        }

        final boolean isIPv6 = (clientIp != null) ? isIPv6(clientIp) : false;

        final String ipVersionKey = isIPv6 ? "ipv6" : "ipv4";
        incrementNamedCountingMonitor(ipVersionKey, ipVersionCounterMap);

        // host header
        String host = req.getHeaders().getFirst(HOST_HEADER);
        if (host != null) {
            int colonIdx;
            if (isIPv6) {
                // an ipv6 host might be a raw IP with 7+ colons
                colonIdx = host.lastIndexOf(":");
            } else {
                // strips port from host
                colonIdx = host.indexOf(":");
            }
            if (colonIdx > -1) {
                host = host.substring(0, colonIdx);
            }
            incrementNamedCountingMonitor(hostKey(host), this.hostCounterMap);
        }

        // http vs. https
        String protocol = req.getHeaders().getFirst(X_FORWARDED_PROTO_HEADER);
        if (protocol == null) {
            protocol = req.getScheme();
        }
        incrementNamedCountingMonitor(protocolKey(protocol), this.protocolCounterMap);
    }
@Test
    void testCollectRequestStats() {
        final String host = "api.netflix.com";
        final String proto = "https";

        final HttpRequestInfo req = Mockito.mock(HttpRequestInfo.class);
        Headers headers = new Headers();
        when(req.getHeaders()).thenReturn(headers);
        headers.set(StatsManager.HOST_HEADER, host);
        headers.set(StatsManager.X_FORWARDED_PROTO_HEADER, proto);
        when(req.getClientIp()).thenReturn("127.0.0.1");

        final StatsManager sm = StatsManager.getManager();
        sm.collectRequestStats(req);

        final NamedCountingMonitor hostMonitor = sm.getHostMonitor(host);
        assertNotNull(hostMonitor, "hostMonitor should not be null");

        final NamedCountingMonitor protoMonitor = sm.getProtocolMonitor(proto);
        assertNotNull(protoMonitor, "protoMonitor should not be null");

        assertEquals(1, hostMonitor.getCount());
        assertEquals(1, protoMonitor.getCount());
    }