summaryrefslogtreecommitdiffstats
path: root/container-accesslogging/src/main/java/com/yahoo/container/logging/AccessLogSampler.java
blob: f722511dd0ca13e29f6390ee94e8c3793d694ad1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Samples entries from access log. It first samples every query until it have some data, and then sub-samples
 * much less frequently to reduce CPU usage and latency impact. It only samples successful requests and requests
 * that starts with /search.
 *
 * @author dybis
 */
public class AccessLogSampler implements AccessLogInterface {

    private final AtomicLong accessLineCounter = new AtomicLong(0);
    private final CircularArrayAccessLogKeeper circularArrayAccessLogKeeper;

    public AccessLogSampler(CircularArrayAccessLogKeeper circularArrayAccessLogKeeper) {
        this.circularArrayAccessLogKeeper = circularArrayAccessLogKeeper;
    }

    @Override
    public void log(AccessLogEntry accessLogEntry) {
        if (accessLogEntry.getStatusCode() != 200) {
            return;
        }
	String uriString = accessLogEntry.getURI().toString();
	if (! uriString.startsWith("/search")) {
            return;
	}
        final long count = accessLineCounter.incrementAndGet();
        if (count >= CircularArrayAccessLogKeeper.SIZE && count % CircularArrayAccessLogKeeper.SIZE  != 0) {
            return;
        }
        circularArrayAccessLogKeeper.addUri(uriString);
    }
}