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

io.airlift.http.server.DelimitedRequestLog Maven / Gradle / Ivy

There is a newer version: 285
Show newest version
/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.airlift.http.server;

import io.airlift.event.client.EventClient;
import io.airlift.tracetoken.TraceTokenManager;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.RolloverFileOutputStream;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import static io.airlift.http.server.HttpRequestEvent.createHttpRequestEvent;

class DelimitedRequestLog
        implements RequestLog
{
    // Tab-separated
    // Time, ip, method, url, user, agent, response code, request length, response length, response time

    private final RolloverFileOutputStream out;
    private final Writer writer;

    private final DateTimeFormatter isoFormatter;
    private final TraceTokenManager traceTokenManager;
    private final EventClient eventClient;
    private final CurrentTimeMillisProvider currentTimeMillisProvider;

    public DelimitedRequestLog(String filename, int retainDays, TraceTokenManager traceTokenManager, EventClient eventClient)
            throws IOException
    {
        this(filename, retainDays, traceTokenManager, eventClient, new SystemCurrentTimeMillisProvider());
    }

    public DelimitedRequestLog(String filename,
            int retainDays,
            TraceTokenManager traceTokenManager,
            EventClient eventClient,
            CurrentTimeMillisProvider currentTimeMillisProvider)
            throws IOException
    {
        this.traceTokenManager = traceTokenManager;
        this.eventClient = eventClient;
        this.currentTimeMillisProvider = currentTimeMillisProvider;
        out = new RolloverFileOutputStream(filename, true, retainDays);
        writer = new OutputStreamWriter(out);

        isoFormatter = new DateTimeFormatterBuilder()
                .append(ISODateTimeFormat.dateHourMinuteSecondFraction())
                .appendTimeZoneOffset("Z", true, 2, 2)
                .toFormatter();
    }

    public void log(Request request, Response response)
    {
        long currentTime = currentTimeMillisProvider.getCurrentTimeMillis();
        HttpRequestEvent event = createHttpRequestEvent(request, response, traceTokenManager, currentTime);

        StringBuilder builder = new StringBuilder();
        builder.append(isoFormatter.print(event.getTimeStamp()))
                .append('\t')
                .append(event.getClientAddress())
                .append('\t')
                .append(event.getMethod())
                .append('\t')
                .append(request.getUri()) // TODO: escape
                .append('\t')
                .append(event.getUser())
                .append('\t')
                .append(event.getAgent()) // TODO: escape
                .append('\t')
                .append(event.getResponseCode())
                .append('\t')
                .append(event.getRequestSize())
                .append('\t')
                .append(event.getResponseSize())
                .append('\t')
                .append(event.getTimeToLastByte())
                .append('\t')
                .append(event.getTraceToken())
                .append('\n');

        String line = builder.toString();
        synchronized (writer) {
            try {
                writer.write(line);
                writer.flush();
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        eventClient.post(event);
    }

    public void start()
            throws Exception
    {
    }

    public void stop()
            throws Exception
    {
        out.close();
    }

    public boolean isRunning()
    {
        return true;
    }

    public boolean isStarted()
    {
        return true;
    }

    public boolean isStarting()
    {
        return false;
    }

    public boolean isStopping()
    {
        return false;
    }

    public boolean isStopped()
    {
        return false;
    }

    public boolean isFailed()
    {
        return false;
    }

    public void addLifeCycleListener(Listener listener)
    {
    }

    public void removeLifeCycleListener(Listener listener)
    {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy