aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/handlers/lasterrorsholder/LastErrorsHolder.java
blob: 927486bdd72709ec53881fd761a109413297241a (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver.handlers.lasterrorsholder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.io.Connection;
import com.yahoo.io.ConnectionFactory;
import com.yahoo.io.Listener;
import com.yahoo.log.LogLevel;
import com.yahoo.log.LogMessage;
import com.yahoo.logserver.handlers.AbstractLogHandler;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * The LastErrorsHolder handler is used for holding the last n
 * messages at level error or higher. Connecting to this handler
 * will return a Json object with the last errors (default is last 100 errors)
 *
 * @author hmusum
 */
public class LastErrorsHolder extends AbstractLogHandler implements ConnectionFactory {

    private static final Logger log = Logger.getLogger(LastErrorsHolder.class.getName());
    private static final int maxNumErrors = 100;
    private final Object lock = new Object();

    private int port;
    private Listener listener;

    private final ArrayList<LogMessage> errors = new ArrayList<>();
    private int numberOfErrors = 0;

    /**
     * @param port The port to which this handler listens to.
     */
    public LastErrorsHolder(int port) throws IOException {
        this.port = port;
        listen(port);
    }

    public void listen(int port) throws IOException {
        if (listener != null) {
            throw new IllegalStateException("already listening to port " + this.port);
        }
        listener = new Listener("last-errors-holder");
        listener.listen(this, port);
        listener.start();
        log.log(LogLevel.CONFIG, "port=" + port);
    }

    public boolean doHandle(LogMessage msg) {
        if (msg.getLevel().equals(LogLevel.ERROR) || msg.getLevel().equals(LogLevel.FATAL)) {
            synchronized (lock) {
                numberOfErrors++;
                if (errors.size() < maxNumErrors) {
                    errors.add(msg);
                } else if (numberOfErrors == maxNumErrors) {
                    log.log(LogLevel.DEBUG, String.format("Not storing errors, have reached maximum number of errors: %d, total number of errors received: %d",
                            maxNumErrors, numberOfErrors));
                }
            }
        }
        return true;
    }

    public void close() {
        try {
            listener.interrupt();
            listener.join();
            log.log(LogLevel.DEBUG, "listener stopped");
        } catch (InterruptedException e) {
            log.log(LogLevel.WARNING, "listener was interrupted", e);
        }
    }

    public void flush() {
    }

    /**
     * Factory method for creating new connections. Since we just return a result
     * when client connection happens, we also write the result here
     *
     * @param socket   The new SocketChannel
     * @param listener The Listener instance we want to use
     */
    public Connection newConnection(SocketChannel socket, Listener listener) {
        if (log.isLoggable(LogLevel.DEBUG)) {
            log.log(LogLevel.DEBUG, "New last-errors-holder connection: " + socket);
        }
        LastErrorsHolderConnection connection = new LastErrorsHolderConnection(socket);
        synchronized (lock) {
            Messages messages = new Messages();
            for (LogMessage error : errors) {
                messages.addMessage(
                        new Message(error.getTime()/1000,
                                error.getHost(),
                                error.getService(),
                                error.getLevel().getName(),
                                error.getPayload()));
            }
            messages.setNumberOfErrors(numberOfErrors);

            try {
                ObjectMapper mapper = new ObjectMapper();
                StringWriter stringWriter = new StringWriter();
                mapper.writeValue(stringWriter, messages);
                connection.enqueue(StandardCharsets.UTF_8.encode(stringWriter.toString()));
            } catch (IOException e) {
                log.log(LogLevel.WARNING, "Could not enqueue log message", e);
            }

            errors.clear();
            numberOfErrors = 0;
        }

        return connection;
    }

    public String toString() {
        return LastErrorsHolder.class.getName();
    }


    static class Messages {
        private final List<Message> messages = new ArrayList<>();
        private long errorCount = 0; // There might be more errors than number of messages

        void addMessage(Message message) {
            messages.add(message);
        }

        void setNumberOfErrors(long errorCount) {
            this.errorCount = errorCount;
        }

        public List<Message> getMessages() {
            return messages;
        }

        public long getErrorCount() {
            return errorCount;
        }
    }

    static class Message {
        private final long time;
        private final String hostname;
        private final String service;
        private final String logLevel;
        private final String message;

        Message(long time, String hostname, String service, String logLevel, String message) {
            this.time = time;
            this.hostname = hostname;
            this.service = service;
            this.logLevel = logLevel;
            this.message = message;
        }

        public long getTime() {
            return time;
        }

        public String getMessage() {
            return message;
        }

        public String getLogLevel() {
            return logLevel;
        }

        public String getHostname() {
            return hostname;
        }

        public String getService() {
            return service;
        }
    }

}