summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java
blob: e4c6c9a091853ca2b0568d93a8cc649fc5363393 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;

import com.google.inject.Inject;
import com.yahoo.container.core.LogHandlerConfig;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.Executor;

public class LogHandler extends ThreadedHttpRequestHandler {

    private final LogReader logReader;

    @Inject
    public LogHandler(Executor executor, LogHandlerConfig config) {
        this(executor, new LogReader(config.logDirectory(), config.logPattern()));
    }

    LogHandler(Executor executor, LogReader logReader) {
        super(executor);
        this.logReader = logReader;
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        JSONObject responseJSON = new JSONObject();

        Instant earliestLogThreshold = Optional.ofNullable(request.getProperty("from"))
                .map(Long::valueOf).map(Instant::ofEpochMilli).orElse(Instant.MIN);
        Instant latestLogThreshold = Optional.ofNullable(request.getProperty("to"))
                .map(Long::valueOf).map(Instant::ofEpochMilli).orElseGet(Instant::now);

        try {
            if (request.hasProperty("streaming")) {
                return new HttpResponse(200) {
                    @Override
                    public void render(OutputStream outputStream) {
                        logReader.writeLogs(outputStream, earliestLogThreshold, latestLogThreshold);
                    }
                };
            }

            JSONObject logJson = logReader.readLogs(earliestLogThreshold, latestLogThreshold);
            responseJSON.put("logs", logJson);
        } catch (IOException | JSONException e) {
            return new HttpResponse(404) {
                @Override
                public void render(OutputStream outputStream) {}
            };
        }

        return new HttpResponse(200) {
            @Override
            public void render(OutputStream outputStream) throws IOException {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
                outputStreamWriter.write(responseJSON.toString());
                outputStreamWriter.close();
            }
        };
    }
}