Add winston formatting support for error causes (#6576)

* Add winston formatting support for error causes

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix failing to run built version

- Finally make logger fully injectable
- Simplify startMainApplication to only have runMany(Sync) invocations
  to fix time of use bugs related to logger

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove legacy type enforced ipc to fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type error

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-11-17 14:56:16 +02:00
1 parent a4d0451dec
commit 8dd1d1a8fa
28 files changed
+544 -506

No files matched your search

@@ -12,8 +12,8 @@ import { pathNames } from "../../common/app-paths/app-path-names";
import { fromPairs, map } from "lodash/fp";
import { pipeline } from "@ogre-tools/fp";
import joinPathsInjectable from "../../common/path/join-paths.injectable";
import { beforeElectronIsReadyInjectionToken } from "../start-main-application/runnable-tokens/before-electron-is-ready-injection-token";
import appNameInjectable from "../../common/vars/app-name.injectable";
import { appPathsRunnablePhaseInjectionToken } from "../start-main-application/runnable-tokens/phases";
const setupAppPathsInjectable = getInjectable({
id: "setup-app-paths",
@@ -51,7 +51,7 @@ const setupAppPathsInjectable = getInjectable({
};
},
injectionToken: beforeElectronIsReadyInjectionToken,
injectionToken: appPathsRunnablePhaseInjectionToken,
});
export default setupAppPathsInjectable;
+3 -5
View File
@@ -111,14 +111,12 @@ export class ContextHandler implements ClusterContextHandler {
const potentialServices = await Promise.allSettled(
providers.map(provider => provider.getPrometheusService(apiClient)),
);
const errors: any[] = [];
const errors = [];
for (const res of potentialServices) {
switch (res.status) {
case "rejected":
if (res.reason) {
errors.push(String(res.reason));
}
errors.push(res.reason);
break;
case "fulfilled":
@@ -128,7 +126,7 @@ export class ContextHandler implements ClusterContextHandler {
}
}
throw Object.assign(new Error("No Prometheus service found"), { cause: errors });
throw new Error("No Prometheus service found", { cause: errors });
}
async resolveAuthProxyUrl(): Promise<string> {
+1 -6
View File
@@ -11,15 +11,10 @@ import * as LensExtensionsCommonApi from "../extensions/common-api";
import * as LensExtensionsMainApi from "../extensions/main-api";
import { getDi } from "./getDi";
import startMainApplicationInjectable from "./start-main-application/start-main-application.injectable";
import shouldStartHiddenInjectable from "./electron-app/features/should-start-hidden.injectable";
const di = getDi();
const shouldStartHidden = di.inject(shouldStartHiddenInjectable);
const startApplication = di.inject(startMainApplicationInjectable);
void startApplication(!shouldStartHidden);
void di.inject(startMainApplicationInjectable);
/**
* Exports for virtual package "@k8slens/extensions" for main-process.
+184
View File
@@ -0,0 +1,184 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { LEVEL, MESSAGE, SPLAT } from "triple-beam";
import chalk from "chalk";
import type { InspectOptions } from "util";
import { inspect } from "util";
// The following license was copied from https://github.com/duccio/winston-console-format/blob/master/LICENSE
// This was modified to support formatting causes
/*
The MIT License (MIT)
Copyright (c) 2014-2015 Eugeny Dementev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
export interface ConsoleFormatOptions {
showMeta?: boolean;
metaStrip?: string[];
inspectOptions?: InspectOptions;
}
interface TransformableInfo {
level: string;
message: string;
[key: string | symbol]: any;
}
export class ConsoleFormat {
private static readonly reSpaces = /^\s+/;
private static readonly reSpacesOrEmpty = /^(\s*)/;
// eslint-disable-next-line no-control-regex
private static readonly reColor = /\x1B\[\d+m/;
private static readonly defaultStrip = [LEVEL, MESSAGE, SPLAT, "level", "message", "ms", "stack"];
private static readonly chars = {
singleLine: "▪",
startLine: "┏",
line: "┃",
endLine: "┗",
};
private readonly showMeta: boolean;
private readonly metaStrip: string[];
private readonly inspectOptions: InspectOptions;
public constructor(opts?: ConsoleFormatOptions) {
this.showMeta = opts?.showMeta ?? true;
this.metaStrip = opts?.metaStrip ?? [];
this.inspectOptions = opts?.inspectOptions ?? {};
}
private getLines(value: unknown): string[] {
return inspect(value, this.inspectOptions).split("\n");
}
private message(info: TransformableInfo, chr: string, color: string): string {
const message = info.message.replace(
ConsoleFormat.reSpacesOrEmpty,
`$1${color}${chalk.dim(chr)}${chalk.reset(" ")}`,
);
return `${info.level}:${message}`;
}
private pad(message?: string): string {
return message?.match(ConsoleFormat.reSpaces)?.[0] ?? "";
}
private ms(info: TransformableInfo): string {
if (info.ms) {
return chalk.italic(chalk.dim(` ${info.ms}`));
}
return "";
}
private stack(info: TransformableInfo): string[] {
const messages: string[] = [];
if (info.stack) {
const error = new Error();
error.stack = info.stack;
messages.push(...this.getLines(error));
}
return messages;
}
private _cause(source: unknown): string[] {
const messages: string[] = [];
if (source instanceof Error && source.cause) {
messages.push(`Cause: ${source.cause}`);
messages.push(...this.getLines(source.cause).map(l => ` ${l}`));
messages.push(...this._cause(source.cause));
}
return messages;
}
private cause(info: TransformableInfo): string[] {
const splats = info[SPLAT];
if (Array.isArray(splats)) {
return splats.flatMap(splat => this._cause(splat));
}
return [];
}
private meta(info: TransformableInfo): string[] {
const messages: string[] = [];
const stripped = { ...info };
ConsoleFormat.defaultStrip.forEach((e) => delete stripped[e]);
this.metaStrip?.forEach((e) => delete stripped[e]);
if (Object.keys(stripped).length > 0) {
messages.push(...this.getLines(stripped));
}
return messages;
}
private getColor(info: TransformableInfo): string {
return info.level.match(ConsoleFormat.reColor)?.[0] ?? "";
}
private write(info: TransformableInfo, messages: string[], color: string): void {
const pad = this.pad(info.message);
messages.forEach((line, index, arr) => {
const lineNumber = chalk.dim(`[${(index + 1).toString().padStart(arr.length.toString().length, " ")}]`);
let chr = ConsoleFormat.chars.line;
if (index === arr.length - 1) {
chr = ConsoleFormat.chars.endLine;
}
info[MESSAGE] += `\n${chalk.dim(info.level)}:${pad}${color}${chalk.dim(chr)}${chalk.reset(" ")}`;
info[MESSAGE] += `${lineNumber} ${line}`;
});
}
public transform(info: TransformableInfo): TransformableInfo {
const messages: string[] = [];
if (this.showMeta) {
messages.push(...this.stack(info));
messages.push(...this.meta(info));
messages.push(...this.cause(info));
}
const color = this.getColor(info);
info[MESSAGE] = this.message(info, ConsoleFormat.chars[messages.length > 0 ? "startLine" : "singleLine"], color);
info[MESSAGE] += this.ms(info);
this.write(info, messages, color);
return info;
}
}
@@ -0,0 +1,35 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { format, transports } from "winston";
import { ConsoleFormat } from "./console-format";
import { loggerTransportInjectionToken } from "../../common/logger/transports";
import logLevelInjectable from "./level.injectable";
const consoleLoggerTransportInjectable = getInjectable({
id: "console-logger-transport",
instantiate: (di) => new transports.Console({
handleExceptions: false,
level: di.inject(logLevelInjectable),
format: format.combine(
format.colorize({ level: true, message: false }),
format.padLevels(),
format.ms(),
new ConsoleFormat({
showMeta: true,
inspectOptions: {
depth: 4,
colors: true,
maxArrayLength: 10,
breakLength: 120,
compact: Infinity,
},
}),
),
}),
injectionToken: loggerTransportInjectionToken,
});
export default consoleLoggerTransportInjectable;
@@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { transports } from "winston";
import directoryForLogsInjectable from "../../common/app-paths/directory-for-logs.injectable";
import { loggerTransportInjectionToken } from "../../common/logger/transports";
const fileLoggerTranportInjectable = getInjectable({
id: "file-logger-tranport",
instantiate: (di) => new transports.File({
handleExceptions: false,
level: "debug",
filename: "lens.log",
/**
* SAFTEY: the `ipcMain` check above should mean that this is only
* called in the main process
*/
dirname: di.inject(directoryForLogsInjectable),
maxsize: 1024 * 1024,
maxFiles: 16,
tailable: true,
}),
injectionToken: loggerTransportInjectionToken,
});
export default fileLoggerTranportInjectable;
@@ -0,0 +1,9 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../../common/test-utils/get-global-override";
import logLevelInjectable from "./level.injectable";
export default getGlobalOverride(logLevelInjectable, () => "error");
+30
View File
@@ -0,0 +1,30 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import isDebuggingInjectable from "../../common/vars/is-debugging.injectable";
export type LogLevel = "silly" | "debug" | "info" | "warn" | "error";
const logLevelInjectable = getInjectable({
id: "log-level",
instantiate: (di): LogLevel => {
const isDebugging = di.inject(isDebuggingInjectable);
const baseLevel = process.env.LOG_LEVEL?.toLowerCase();
switch (baseLevel) {
case "silly":
case "debug":
case "info":
case "warn":
case "error":
return baseLevel;
default:
return isDebugging ? "debug" : "info";
}
},
causesSideEffects: true,
});
export default logLevelInjectable;
@@ -28,14 +28,17 @@ const loadMetricsFor = (getMetrics: GetMetrics) => async (promQueries: string[],
try {
return await getMetrics(cluster, prometheusPath, { query, ...queryParams });
} catch (error) {
if (isRequestError(error)) {
if (lastAttempt || (error.statusCode && error.statusCode >= 400 && error.statusCode < 500)) {
throw new Error("Metrics not available", { cause: error });
}
} else if (error instanceof Error) {
if (
!isRequestError(error)
|| lastAttempt
|| (
!lastAttempt && (
typeof error.statusCode === "number" &&
400 <= error.statusCode && error.statusCode < 500
)
)
) {
throw new Error("Metrics not available", { cause: error });
} else {
throw new Error("Metrics not available");
}
await new Promise(resolve => setTimeout(resolve, (attempt + 1) * 1000)); // add delay before repeating request
@@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Runnable } from "../../../common/runnable/run-many-for";
import type { RunnableSync } from "../../../common/runnable/run-many-sync-for";
/**
* These tokens are here so that the importing of their respective dependencies
* can be delayed until all of them are ready
*/
/**
* This runnable token should only have the app paths init so that it can be run by itself
*/
export const appPathsRunnablePhaseInjectionToken = getInjectionToken<RunnableSync>({
id: "app-paths-runnable-phase",
});
export const showLoadingRunnablePhaseInjectionToken = getInjectionToken<Runnable>({
id: "show-loading-runnable-phase",
});
export const showInitialWindowRunnablePhaseInjectionToken = getInjectionToken<Runnable>({
id: "show-initial-window-runnable-phase",
});
@@ -0,0 +1,51 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import shouldStartHiddenInjectable from "../../electron-app/features/should-start-hidden.injectable";
import openDeepLinkInjectable from "../../protocol-handler/lens-protocol-router-main/open-deep-link-for-url/open-deep-link.injectable";
import commandLineArgumentsInjectable from "../../utils/command-line-arguments.injectable";
import createFirstApplicationWindowInjectable from "../lens-window/application-window/create-first-application-window.injectable";
import splashWindowInjectable from "../lens-window/splash-window/splash-window.injectable";
import { showInitialWindowRunnablePhaseInjectionToken } from "../runnable-tokens/phases";
const getDeepLinkUrl = (commandLineArguments: string[]) => (
commandLineArguments
.map(arg => arg.toLowerCase())
.find(arg => arg.startsWith("lens://"))
);
const showInitialWindowInjectable = getInjectable({
id: "show-initial-window",
instantiate: (di) => {
const shouldStartHidden = di.inject(shouldStartHiddenInjectable);
const shouldStartWindow = !shouldStartHidden;
const createFirstApplicationWindow = di.inject(createFirstApplicationWindowInjectable);
const splashWindow = di.inject(splashWindowInjectable);
const openDeepLink = di.inject(openDeepLinkInjectable);
const commandLineArguments = di.inject(commandLineArgumentsInjectable);
return {
id: "show-initial-window",
run: async () => {
if (shouldStartWindow) {
const deepLinkUrl = getDeepLinkUrl(commandLineArguments);
if (deepLinkUrl) {
await openDeepLink(deepLinkUrl);
} else {
const applicationWindow = createFirstApplicationWindow();
await applicationWindow.start();
}
splashWindow.close();
}
},
};
},
injectionToken: showInitialWindowRunnablePhaseInjectionToken,
});
export default showInitialWindowInjectable;
@@ -0,0 +1,29 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import shouldStartHiddenInjectable from "../../electron-app/features/should-start-hidden.injectable";
import splashWindowInjectable from "../lens-window/splash-window/splash-window.injectable";
import { showLoadingRunnablePhaseInjectionToken } from "../runnable-tokens/phases";
const showLoadingInjectable = getInjectable({
id: "show-loading",
instantiate: (di) => {
const shouldStartHidden = di.inject(shouldStartHiddenInjectable);
const shouldShowLoadingWindow = !shouldStartHidden;
const splashWindow = di.inject(splashWindowInjectable);
return {
id: "show-loading",
run: async () => {
if (shouldShowLoadingWindow) {
await splashWindow.start();
}
},
};
},
injectionToken: showLoadingRunnablePhaseInjectionToken,
});
export default showLoadingInjectable;
@@ -10,13 +10,8 @@ import { beforeElectronIsReadyInjectionToken } from "./runnable-tokens/before-el
import { beforeApplicationIsLoadingInjectionToken } from "./runnable-tokens/before-application-is-loading-injection-token";
import { onLoadOfApplicationInjectionToken } from "./runnable-tokens/on-load-of-application-injection-token";
import { afterApplicationIsLoadedInjectionToken } from "./runnable-tokens/after-application-is-loaded-injection-token";
import splashWindowInjectable from "./lens-window/splash-window/splash-window.injectable";
import openDeepLinkInjectable from "../protocol-handler/lens-protocol-router-main/open-deep-link-for-url/open-deep-link.injectable";
import { pipeline } from "@ogre-tools/fp";
import { find, map, startsWith, toLower } from "lodash/fp";
import commandLineArgumentsInjectable from "../utils/command-line-arguments.injectable";
import waitForElectronToBeReadyInjectable from "../electron-app/features/wait-for-electron-to-be-ready.injectable";
import createFirstApplicationWindowInjectable from "./lens-window/application-window/create-first-application-window.injectable";
import { appPathsRunnablePhaseInjectionToken, showInitialWindowRunnablePhaseInjectionToken, showLoadingRunnablePhaseInjectionToken } from "./runnable-tokens/phases";
const startMainApplicationInjectable = getInjectable({
id: "start-main-application",
@@ -25,53 +20,29 @@ const startMainApplicationInjectable = getInjectable({
const runMany = runManyFor(di);
const runManySync = runManySyncFor(di);
const waitForElectronToBeReady = di.inject(waitForElectronToBeReadyInjectable);
const createFirstApplicationWindow = di.inject(createFirstApplicationWindowInjectable);
const splashWindow = di.inject(splashWindowInjectable);
const openDeepLink = di.inject(openDeepLinkInjectable);
const commandLineArguments = di.inject(commandLineArgumentsInjectable);
const appPathsRunnablePhase = runManySync(appPathsRunnablePhaseInjectionToken);
const beforeElectronIsReady = runManySync(beforeElectronIsReadyInjectionToken);
const beforeApplicationIsLoading = runMany(beforeApplicationIsLoadingInjectionToken);
const showLoadingRunnablePhase = runMany(showLoadingRunnablePhaseInjectionToken);
const onLoadOfApplication = runMany(onLoadOfApplicationInjectionToken);
const showInitialWindowRunnablePhase = runMany(showInitialWindowRunnablePhaseInjectionToken);
const afterApplicationIsLoaded = runMany(afterApplicationIsLoadedInjectionToken);
return (shouldStartWindow: boolean) => {
// Stuff happening before application is ready needs to be synchronous because of
// https://github.com/electron/electron/issues/21370
beforeElectronIsReady();
// Stuff happening before application is ready needs to be synchronous because of
// https://github.com/electron/electron/issues/21370
appPathsRunnablePhase();
beforeElectronIsReady();
return (async () => {
await waitForElectronToBeReady();
await beforeApplicationIsLoading();
if (shouldStartWindow) {
await splashWindow.start();
}
await onLoadOfApplication();
if (shouldStartWindow) {
const deepLinkUrl = getDeepLinkUrl(commandLineArguments);
if (deepLinkUrl) {
await openDeepLink(deepLinkUrl);
} else {
const applicationWindow = createFirstApplicationWindow();
await applicationWindow.start();
}
splashWindow.close();
}
await afterApplicationIsLoaded();
})();
};
return (async () => {
await waitForElectronToBeReady();
await beforeApplicationIsLoading();
await showLoadingRunnablePhase();
await onLoadOfApplication();
await showInitialWindowRunnablePhase();
await afterApplicationIsLoaded();
})();
},
});
const getDeepLinkUrl = (commandLineArguments: string[]) =>
pipeline(commandLineArguments, map(toLower), find(startsWith("lens://")));
export default startMainApplicationInjectable;