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",
|
||||||
@ -36,11 +43,9 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
|
|
||||||
assert(currentContext);
|
assert(currentContext);
|
||||||
|
|
||||||
const emitTelemetry = diForDecorator.inject(
|
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
||||||
emitTelemetryInjectable
|
|
||||||
);
|
|
||||||
|
|
||||||
const logError = diForDecorator.inject(logErrorInjectable);
|
const logError = diForDecorator.inject(logErrorInjectable);
|
||||||
|
const doTelemetry = doTelemetryFor({ logError, emitTelemetry });
|
||||||
|
|
||||||
const whiteList = diForDecorator.inject(
|
const whiteList = diForDecorator.inject(
|
||||||
telemetryWhiteListForFunctionsInjectable
|
telemetryWhiteListForFunctionsInjectable
|
||||||
@ -50,32 +55,13 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
|
|
||||||
const whiteListed = whiteListMap.get(currentContext.injectable.id);
|
const whiteListed = whiteListMap.get(currentContext.injectable.id);
|
||||||
|
|
||||||
if (!whiteListed) {
|
const result = instance(...args);
|
||||||
return instance(...args);
|
|
||||||
|
if (whiteListed) {
|
||||||
|
doTelemetry(whiteListed, args, currentContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
let params;
|
return result;
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -86,6 +72,43 @@ 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) =>
|
||||||
@ -101,8 +124,8 @@ const getWhiteListMap = (whiteList: WhiteListItem[]) =>
|
|||||||
{
|
{
|
||||||
getParams: item.getParams,
|
getParams: item.getParams,
|
||||||
},
|
},
|
||||||
],
|
]
|
||||||
),
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
export default telemetryDecoratorInjectable;
|
export default telemetryDecoratorInjectable;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user