-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
log_interceptor.js
141 lines (122 loc) · 4.27 KB
/
log_interceptor.js
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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/
import Stream from 'stream';
import { get, isEqual } from 'lodash';
const GET_CLIENT_HELLO = /GET_CLIENT_HELLO:http/;
function doTagsMatch(event, tags) {
return isEqual(get(event, 'tags'), tags);
}
function doesMessageMatch(errorMessage, match) {
if (!errorMessage) return false;
const isRegExp = match instanceof RegExp;
if (isRegExp) return match.test(errorMessage);
return errorMessage === match;
}
// converts the given event into a debug log if it's an error of the given type
function downgradeIfErrorType(errorType, event) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
if (!isClientError) return null;
const matchesErrorType =
get(event, 'error.code') === errorType || get(event, 'error.errno') === errorType;
if (!matchesErrorType) return null;
const errorTypeTag = errorType.toLowerCase();
return {
event: 'log',
pid: event.pid,
timestamp: event.timestamp,
tags: ['debug', 'connection', errorTypeTag],
data: `${errorType}: Socket was closed by the client (probably the browser) before it could be read completely`,
};
}
function downgradeIfErrorMessage(match, event) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
const errorMessage = get(event, 'error.message');
const matchesErrorMessage = isClientError && doesMessageMatch(errorMessage, match);
if (!matchesErrorMessage) return null;
return {
event: 'log',
pid: event.pid,
timestamp: event.timestamp,
tags: ['debug', 'connection'],
data: errorMessage,
};
}
export class LogInterceptor extends Stream.Transform {
constructor() {
super({
readableObjectMode: true,
writableObjectMode: true,
});
}
/**
* Since the upgrade to hapi 14, any socket read
* error is surfaced as a generic "client error"
* but "ECONNRESET" specifically is not useful for the
* logs unless you are trying to debug edge-case behaviors.
*
* For that reason, we downgrade this from error to debug level
*
* @param {object} - log event
*/
downgradeIfEconnreset(event) {
return downgradeIfErrorType('ECONNRESET', event);
}
/**
* Since the upgrade to hapi 14, any socket write
* error is surfaced as a generic "client error"
* but "EPIPE" specifically is not useful for the
* logs unless you are trying to debug edge-case behaviors.
*
* For that reason, we downgrade this from error to debug level
*
* @param {object} - log event
*/
downgradeIfEpipe(event) {
return downgradeIfErrorType('EPIPE', event);
}
/**
* Since the upgrade to hapi 14, any socket write
* error is surfaced as a generic "client error"
* but "ECANCELED" specifically is not useful for the
* logs unless you are trying to debug edge-case behaviors.
*
* For that reason, we downgrade this from error to debug level
*
* @param {object} - log event
*/
downgradeIfEcanceled(event) {
return downgradeIfErrorType('ECANCELED', event);
}
downgradeIfHTTPSWhenHTTP(event) {
return downgradeIfErrorType('HPE_INVALID_METHOD', event);
}
downgradeIfHTTPWhenHTTPS(event) {
return downgradeIfErrorMessage(GET_CLIENT_HELLO, event);
}
_transform(event, enc, next) {
const downgraded =
this.downgradeIfEconnreset(event) ||
this.downgradeIfEpipe(event) ||
this.downgradeIfEcanceled(event) ||
this.downgradeIfHTTPSWhenHTTP(event) ||
this.downgradeIfHTTPWhenHTTPS(event);
this.push(downgraded || event);
next();
}
}