Skip to content

Commit

Permalink
[ci] filter out proc-runner logs from stdout on CI (#114568)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <[email protected]>
  • Loading branch information
Spencer and spalger authored Oct 13, 2021
1 parent 330fd83 commit eeed2ca
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 48 deletions.
10 changes: 10 additions & 0 deletions packages/kbn-dev-utils/src/ci_stats_reporter/ci_stats_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@

import type { ToolingLog } from '../tooling_log';

/**
* Information about how CiStatsReporter should talk to the ci-stats service. Normally
* it is read from a JSON environment variable using the `parseConfig()` function
* exported by this module.
*/
export interface Config {
/** ApiToken necessary for writing build data to ci-stats service */
apiToken: string;
/**
* uuid which should be obtained by first creating a build with the
* ci-stats service and then passing it to all subsequent steps
*/
buildId: string;
}

Expand Down
1 change: 1 addition & 0 deletions packages/kbn-dev-utils/src/ci_stats_reporter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
*/

export * from './ci_stats_reporter';
export type { Config } from './ci_stats_config';
export * from './ship_ci_stats_cli';
export { getTimeReporter } from './report_time';
2 changes: 2 additions & 0 deletions packages/kbn-dev-utils/src/proc_runner/proc_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class ProcRunner {
private signalUnsubscribe: () => void;

constructor(private log: ToolingLog) {
this.log = log.withType('ProcRunner');

this.signalUnsubscribe = exitHook(() => {
this.teardown().catch((error) => {
log.error(`ProcRunner teardown error: ${error.stack}`);
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-dev-utils/src/run/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import exitHook from 'exit-hook';
import { ToolingLog } from '../tooling_log';
import { isFailError } from './fail';

/**
* A function which will be called when the CLI is torn-down which should
* quickly cleanup whatever it needs.
*/
export type CleanupTask = () => void;

export class Cleanup {
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-dev-utils/src/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './run';
export * from './run_with_commands';
export * from './flags';
export * from './fail';
export type { CleanupTask } from './cleanup';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/kbn-dev-utils/src/tooling_log/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/

export { ToolingLog } from './tooling_log';
export type { ToolingLogOptions } from './tooling_log';
export { ToolingLogTextWriter, ToolingLogTextWriterConfig } from './tooling_log_text_writer';
export { pickLevelFromFlags, parseLogLevel, LogLevel, ParsedLogLevel } from './log_levels';
export { ToolingLogCollectingWriter } from './tooling_log_collecting_writer';
export type { Writer } from './writer';
export type { Message } from './message';
8 changes: 8 additions & 0 deletions packages/kbn-dev-utils/src/tooling_log/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@

export type MessageTypes = 'verbose' | 'debug' | 'info' | 'success' | 'warning' | 'error' | 'write';

/**
* The object shape passed to ToolingLog writers each time the log is used.
*/
export interface Message {
/** level/type of message */
type: MessageTypes;
/** indentation intended when message written to a text log */
indent: number;
/** type of logger this message came from */
source?: string;
/** args passed to the logging method */
args: any[];
}
37 changes: 37 additions & 0 deletions packages/kbn-dev-utils/src/tooling_log/tooling_log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,40 @@ describe('#getWritten$()', () => {
await testWrittenMsgs([{ write: jest.fn(() => false) }, { write: jest.fn(() => false) }]);
});
});

describe('#withType()', () => {
it('creates a child logger with a unique type that respects all other settings', () => {
const writerA = new ToolingLogCollectingWriter();
const writerB = new ToolingLogCollectingWriter();
const log = new ToolingLog();
log.setWriters([writerA]);

const fork = log.withType('someType');
log.info('hello');
fork.info('world');
fork.indent(2);
log.debug('indented');
fork.indent(-2);
log.debug('not-indented');

log.setWriters([writerB]);
fork.info('to new writer');
fork.indent(5);
log.info('also to new writer');

expect(writerA.messages).toMatchInlineSnapshot(`
Array [
" info hello",
" info source[someType] world",
" │ debg indented",
" debg not-indented",
]
`);
expect(writerB.messages).toMatchInlineSnapshot(`
Array [
" info source[someType] to new writer",
" │ info also to new writer",
]
`);
});
});
66 changes: 52 additions & 14 deletions packages/kbn-dev-utils/src/tooling_log/tooling_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,45 @@ import { ToolingLogTextWriter, ToolingLogTextWriterConfig } from './tooling_log_
import { Writer } from './writer';
import { Message, MessageTypes } from './message';

export interface ToolingLogOptions {
/**
* type name for this logger, will be assigned to the "source"
* properties of messages produced by this logger
*/
type?: string;
/**
* parent ToolingLog. When a ToolingLog has a parent they will both
* share indent and writers state. Changing the indent width or
* writers on either log will update the other too.
*/
parent?: ToolingLog;
}

export class ToolingLog {
private indentWidth = 0;
private writers: Writer[];
private indentWidth$: Rx.BehaviorSubject<number>;
private writers$: Rx.BehaviorSubject<Writer[]>;
private readonly written$: Rx.Subject<Message>;
private readonly type: string | undefined;

constructor(writerConfig?: ToolingLogTextWriterConfig, options?: ToolingLogOptions) {
this.indentWidth$ = options?.parent ? options.parent.indentWidth$ : new Rx.BehaviorSubject(0);

constructor(writerConfig?: ToolingLogTextWriterConfig) {
this.writers = writerConfig ? [new ToolingLogTextWriter(writerConfig)] : [];
this.written$ = new Rx.Subject();
this.writers$ = options?.parent
? options.parent.writers$
: new Rx.BehaviorSubject<Writer[]>([]);
if (!options?.parent && writerConfig) {
this.writers$.next([new ToolingLogTextWriter(writerConfig)]);
}

this.written$ = options?.parent ? options.parent.written$ : new Rx.Subject();
this.type = options?.type;
}

/**
* Get the current indentation level of the ToolingLog
*/
public getIndent() {
return this.indentWidth;
return this.indentWidth$.getValue();
}

/**
Expand All @@ -39,8 +63,8 @@ export class ToolingLog {
* @param block a function to run and reset any indentation changes after
*/
public indent<T>(delta = 0, block?: () => Promise<T>) {
const originalWidth = this.indentWidth;
this.indentWidth = Math.max(this.indentWidth + delta, 0);
const originalWidth = this.indentWidth$.getValue();
this.indentWidth$.next(Math.max(originalWidth + delta, 0));
if (!block) {
return;
}
Expand All @@ -49,7 +73,7 @@ export class ToolingLog {
try {
return await block();
} finally {
this.indentWidth = originalWidth;
this.indentWidth$.next(originalWidth);
}
})();
}
Expand Down Expand Up @@ -83,26 +107,40 @@ export class ToolingLog {
}

public getWriters() {
return this.writers.slice(0);
return [...this.writers$.getValue()];
}

public setWriters(writers: Writer[]) {
this.writers = [...writers];
this.writers$.next([...writers]);
}

public getWritten$() {
return this.written$.asObservable();
}

/**
* Create a new ToolingLog which sets a different "type", allowing messages to be filtered out by "source"
* @param type A string that will be passed along with messages from this logger which can be used to filter messages with `ignoreSources`
*/
public withType(type: string) {
return new ToolingLog(undefined, {
type,
parent: this,
});
}

private sendToWriters(type: MessageTypes, args: any[]) {
const msg = {
const indent = this.indentWidth$.getValue();
const writers = this.writers$.getValue();
const msg: Message = {
type,
indent: this.indentWidth,
indent,
source: this.type,
args,
};

let written = false;
for (const writer of this.writers) {
for (const writer of writers) {
if (writer.write(msg)) {
written = true;
}
Expand Down
Loading

0 comments on commit eeed2ca

Please sign in to comment.