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:
parent
aeac31be99
commit
39a5c71aef
@ -12,11 +12,18 @@ import {
|
|||||||
|
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { isFunction } from "lodash/fp";
|
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 type { WhiteListItem } from "./telemetry-white-list-for-functions.injectable";
|
||||||
import telemetryWhiteListForFunctionsInjectable 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({
|
const telemetryDecoratorInjectable = getInjectable({
|
||||||
id: "telemetry-decorator",
|
id: "telemetry-decorator",
|
||||||
@ -24,60 +31,39 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
instantiate: (diForDecorator) => ({
|
instantiate: (diForDecorator) => ({
|
||||||
decorate:
|
decorate:
|
||||||
(instantiateToBeDecorated: any) =>
|
(instantiateToBeDecorated: any) =>
|
||||||
(di: DiContainerForInjection, instantiationParameter: any) => {
|
(di: DiContainerForInjection, instantiationParameter: any) => {
|
||||||
const instance = instantiateToBeDecorated(di, instantiationParameter);
|
const instance = instantiateToBeDecorated(di, instantiationParameter);
|
||||||
|
|
||||||
if (!isFunction(instance)) {
|
if (!isFunction(instance)) {
|
||||||
return instance;
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (...args: any[]) => {
|
||||||
|
const currentContext = di.context.at(-1);
|
||||||
|
|
||||||
|
assert(currentContext);
|
||||||
|
|
||||||
|
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
||||||
|
const logError = diForDecorator.inject(logErrorInjectable);
|
||||||
|
const doTelemetry = doTelemetryFor({ logError, emitTelemetry });
|
||||||
|
|
||||||
|
const whiteList = diForDecorator.inject(
|
||||||
|
telemetryWhiteListForFunctionsInjectable
|
||||||
|
);
|
||||||
|
|
||||||
|
const whiteListMap = getWhiteListMap(whiteList);
|
||||||
|
|
||||||
|
const whiteListed = whiteListMap.get(currentContext.injectable.id);
|
||||||
|
|
||||||
|
const result = instance(...args);
|
||||||
|
|
||||||
|
if (whiteListed) {
|
||||||
|
doTelemetry(whiteListed, args, currentContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (...args: any[]) => {
|
return result;
|
||||||
const currentContext = di.context.at(-1);
|
};
|
||||||
|
},
|
||||||
assert(currentContext);
|
|
||||||
|
|
||||||
const emitTelemetry = diForDecorator.inject(
|
|
||||||
emitTelemetryInjectable
|
|
||||||
);
|
|
||||||
|
|
||||||
const logError = diForDecorator.inject(logErrorInjectable);
|
|
||||||
|
|
||||||
const whiteList = diForDecorator.inject(
|
|
||||||
telemetryWhiteListForFunctionsInjectable
|
|
||||||
);
|
|
||||||
|
|
||||||
const whiteListMap = getWhiteListMap(whiteList);
|
|
||||||
|
|
||||||
const whiteListed = whiteListMap.get(currentContext.injectable.id);
|
|
||||||
|
|
||||||
if (!whiteListed) {
|
|
||||||
return instance(...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
decorable: false,
|
decorable: false,
|
||||||
@ -86,23 +72,60 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
injectionToken: instantiationDecoratorToken,
|
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[]) =>
|
const getWhiteListMap = (whiteList: WhiteListItem[]) =>
|
||||||
new Map(
|
new Map(
|
||||||
whiteList.map((item) =>
|
whiteList.map((item) =>
|
||||||
typeof item === "string"
|
typeof item === "string"
|
||||||
? [
|
? [
|
||||||
item,
|
item,
|
||||||
{
|
{
|
||||||
getParams: () => undefined,
|
getParams: () => undefined,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
: [
|
: [
|
||||||
item.id,
|
item.id,
|
||||||
{
|
{
|
||||||
getParams: item.getParams,
|
getParams: item.getParams,
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
),
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
export default telemetryDecoratorInjectable;
|
export default telemetryDecoratorInjectable;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user