Skip to content

Commit

Permalink
fix / improve event types usages
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Nov 20, 2020
1 parent 5a37913 commit a60d262
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
5 changes: 2 additions & 3 deletions packages/kbn-legacy-logging/src/log_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { EventData, isEventData } from './metadata';

export interface BaseEvent {
event: string;
timestamp: string;
pid: string;
timestamp: number;
pid: number;
tags?: string[];
}

Expand All @@ -39,7 +39,6 @@ export interface ResponseEvent extends BaseEvent {

export interface OpsEvent extends BaseEvent {
event: 'ops';
pid: string;
os: {
load: string[];
};
Expand Down
10 changes: 6 additions & 4 deletions packages/kbn-legacy-logging/src/log_interceptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
* under the License.
*/

import { ErrorEvent } from './log_events';
import { LogInterceptor } from './log_interceptor';

function stubClientErrorEvent(errorMeta: Record<string, any>) {
function stubClientErrorEvent(errorMeta: Record<string, any>): ErrorEvent {
const error = new Error();
Object.assign(error, errorMeta);
return {
event: 'error',
url: '',
pid: 1234,
timestamp: Date.now(),
tags: ['connection', 'client', 'error'],
Expand Down Expand Up @@ -53,7 +55,7 @@ describe('server logging LogInterceptor', () => {
it('does not match if the tags are not in order', () => {
const interceptor = new LogInterceptor();
const event = stubEconnresetEvent();
event.tags = [...event.tags.slice(1), event.tags[0]];
event.tags = [...event.tags!.slice(1), event.tags![0]];
expect(interceptor.downgradeIfEconnreset(event)).toBe(null);
});

Expand Down Expand Up @@ -81,7 +83,7 @@ describe('server logging LogInterceptor', () => {
it('does not match if the tags are not in order', () => {
const interceptor = new LogInterceptor();
const event = stubEpipeEvent();
event.tags = [...event.tags.slice(1), event.tags[0]];
event.tags = [...event.tags!.slice(1), event.tags![0]];
expect(interceptor.downgradeIfEpipe(event)).toBe(null);
});

Expand Down Expand Up @@ -109,7 +111,7 @@ describe('server logging LogInterceptor', () => {
it('does not match if the tags are not in order', () => {
const interceptor = new LogInterceptor();
const event = stubEcanceledEvent();
event.tags = [...event.tags.slice(1), event.tags[0]];
event.tags = [...event.tags!.slice(1), event.tags![0]];
expect(interceptor.downgradeIfEcanceled(event)).toBe(null);
});

Expand Down
25 changes: 14 additions & 11 deletions packages/kbn-legacy-logging/src/log_interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@

import Stream from 'stream';
import { get, isEqual } from 'lodash';
import { AnyEvent } from './log_events';

/**
* Matches error messages when clients connect via HTTP instead of HTTPS; see unit test for full message. Warning: this can change when Node
* and its bundled OpenSSL binary are upgraded.
*/
const OPENSSL_GET_RECORD_REGEX = /ssl3_get_record:http/;

function doTagsMatch(event: Record<string, any>, tags: string[]) {
return isEqual(get(event, 'tags'), tags);
function doTagsMatch(event: AnyEvent, tags: string[]) {
return isEqual(event.tags, tags);
}

function doesMessageMatch(errorMessage: string, match: RegExp | string) {
Expand All @@ -41,7 +42,7 @@ function doesMessageMatch(errorMessage: string, match: RegExp | string) {
}

// converts the given event into a debug log if it's an error of the given type
function downgradeIfErrorType(errorType: string, event: Record<string, any>) {
function downgradeIfErrorType(errorType: string, event: AnyEvent) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
if (!isClientError) {
return null;
Expand All @@ -64,12 +65,14 @@ function downgradeIfErrorType(errorType: string, event: Record<string, any>) {
};
}

function downgradeIfErrorMessage(match: RegExp | string, event: Record<string, any>) {
function downgradeIfErrorMessage(match: RegExp | string, event: AnyEvent) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
const errorMessage = get(event, 'error.message');
const matchesErrorMessage = isClientError && doesMessageMatch(errorMessage, match);

if (!matchesErrorMessage) return null;
if (!matchesErrorMessage) {
return null;
}

return {
event: 'log',
Expand Down Expand Up @@ -98,7 +101,7 @@ export class LogInterceptor extends Stream.Transform {
*
* @param {object} - log event
*/
downgradeIfEconnreset(event: any) {
downgradeIfEconnreset(event: AnyEvent) {
return downgradeIfErrorType('ECONNRESET', event);
}

Expand All @@ -112,7 +115,7 @@ export class LogInterceptor extends Stream.Transform {
*
* @param {object} - log event
*/
downgradeIfEpipe(event: any) {
downgradeIfEpipe(event: AnyEvent) {
return downgradeIfErrorType('EPIPE', event);
}

Expand All @@ -126,19 +129,19 @@ export class LogInterceptor extends Stream.Transform {
*
* @param {object} - log event
*/
downgradeIfEcanceled(event: any) {
downgradeIfEcanceled(event: AnyEvent) {
return downgradeIfErrorType('ECANCELED', event);
}

downgradeIfHTTPSWhenHTTP(event: any) {
downgradeIfHTTPSWhenHTTP(event: AnyEvent) {
return downgradeIfErrorType('HPE_INVALID_METHOD', event);
}

downgradeIfHTTPWhenHTTPS(event: any) {
downgradeIfHTTPWhenHTTPS(event: AnyEvent) {
return downgradeIfErrorMessage(OPENSSL_GET_RECORD_REGEX, event);
}

_transform(event: any, enc: string, next: Stream.TransformCallback) {
_transform(event: AnyEvent, enc: string, next: Stream.TransformCallback) {
const downgraded =
this.downgradeIfEconnreset(event) ||
this.downgradeIfEpipe(event) ||
Expand Down

0 comments on commit a60d262

Please sign in to comment.