1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Simplify code for telemetry decorator

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

Co-authored-by: Gabriel <gaccettola@mirantis.com>
This commit is contained in:
Iku-turso 2023-03-23 15:56:06 +02:00
parent aeac31be99
commit 39a5c71aef

View File

@ -12,11 +12,18 @@ import {
import assert from "assert";
import { isFunction } from "lodash/fp";
import emitTelemetryInjectable from "./emit-telemetry.injectable";
import emitTelemetryInjectable, {
EmitTelemetry,
} from "./emit-telemetry.injectable";
import type { WhiteListItem } from "./telemetry-white-list-for-functions.injectable";
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
import logErrorInjectable from "../../../common/log-error.injectable";
import logErrorInjectable, {
LogError,
} from "../../../common/log-error.injectable";
import type { AppEvent } from "../../../common/app-event-bus/event-bus";
const telemetryDecoratorInjectable = getInjectable({
id: "telemetry-decorator",
@ -36,11 +43,9 @@ const telemetryDecoratorInjectable = getInjectable({
assert(currentContext);
const emitTelemetry = diForDecorator.inject(
emitTelemetryInjectable
);
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
const logError = diForDecorator.inject(logErrorInjectable);
const doTelemetry = doTelemetryFor({ logError, emitTelemetry });
const whiteList = diForDecorator.inject(
telemetryWhiteListForFunctionsInjectable
@ -50,32 +55,13 @@ const telemetryDecoratorInjectable = getInjectable({
const whiteListed = whiteListMap.get(currentContext.injectable.id);
if (!whiteListed) {
return instance(...args);
const result = instance(...args);
if (whiteListed) {
doTelemetry(whiteListed, args, currentContext);
}
let params;
try {
params = whiteListed.getParams(...args);
} catch (e) {
params = {
error:
"Tried to produce params for telemetry, but getParams() threw an error",
};
logError(
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
e
);
}
emitTelemetry({
action: currentContext.injectable.id,
params,
});
return instance(...args);
return result;
};
},
}),
@ -86,6 +72,43 @@ const telemetryDecoratorInjectable = getInjectable({
injectionToken: instantiationDecoratorToken,
});
const doTelemetryFor =
({
logError,
emitTelemetry,
}: {
logError: LogError;
emitTelemetry: EmitTelemetry;
}) =>
(
whiteListed: { getParams: (...args: unknown[]) => AppEvent["params"] },
args: any[],
currentContext: any
) => {
try {
const params = whiteListed.getParams(...args);
emitTelemetry({
action: currentContext.injectable.id,
params,
});
} catch (e: any) {
logError(
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
e
);
emitTelemetry({
action: currentContext.injectable.id,
params: {
error:
"Tried to produce params for telemetry, but getParams() threw an error",
},
});
}
};
const getWhiteListMap = (whiteList: WhiteListItem[]) =>
new Map(
whiteList.map((item) =>
@ -101,8 +124,8 @@ const getWhiteListMap = (whiteList: WhiteListItem[]) =>
{
getParams: item.getParams,
},
],
),
]
)
);
export default telemetryDecoratorInjectable;