reduce k8 events

This commit is contained in:
Maidul Islam
2023-08-24 15:41:29 -04:00
parent 8ee6710e9b
commit 50977cf788
3 changed files with 82 additions and 54 deletions
@@ -9,6 +9,7 @@ import {
ACTION_UPDATE_SECRETS, ACTION_UPDATE_SECRETS,
ALGORITHM_AES_256_GCM, ALGORITHM_AES_256_GCM,
ENCODING_SCHEME_UTF8, ENCODING_SCHEME_UTF8,
K8_USER_AGENT_NAME,
SECRET_PERSONAL SECRET_PERSONAL
} from "../../variables"; } from "../../variables";
import { BadRequestError, UnauthorizedRequestError } from "../../utils/errors"; import { BadRequestError, UnauthorizedRequestError } from "../../utils/errors";
@@ -970,22 +971,36 @@ export const getSecrets = async (req: Request, res: Response) => {
const postHogClient = await TelemetryService.getPostHogClient(); const postHogClient = await TelemetryService.getPostHogClient();
// reduce the number of events captured
let shouldRecordK8Event = false
if (req.authData instanceof ServiceTokenData && req.authData.userAgent == K8_USER_AGENT_NAME) {
const randomNumber = Math.random();
if (randomNumber > 0.9) {
shouldRecordK8Event = true
}
}
if (postHogClient) { if (postHogClient) {
const shouldCapture = req.authData.userAgent !== K8_USER_AGENT_NAME || shouldRecordK8Event;
const approximateForNoneCapturedEvents = secrets.length * 10
if (shouldCapture) {
postHogClient.capture({ postHogClient.capture({
event: "secrets pulled", event: "secrets pulled",
distinctId: await TelemetryService.getDistinctId({ distinctId: await TelemetryService.getDistinctId({
authData: req.authData authData: req.authData
}), }),
properties: { properties: {
numberOfSecrets: secrets.length, numberOfSecrets: shouldRecordK8Event ? approximateForNoneCapturedEvents : secrets.length,
environment, environment,
workspaceId, workspaceId,
channel,
folderId, folderId,
userAgent: req.headers?.["user-agent"] channel: req.authData.userAgentType,
userAgent: req.authData.userAgent
} }
}); });
} }
}
return res.status(200).send({ return res.status(200).send({
secrets, secrets,
+17 -6
View File
@@ -29,6 +29,7 @@ import {
ALGORITHM_AES_256_GCM, ALGORITHM_AES_256_GCM,
ENCODING_SCHEME_BASE64, ENCODING_SCHEME_BASE64,
ENCODING_SCHEME_UTF8, ENCODING_SCHEME_UTF8,
K8_USER_AGENT_NAME,
SECRET_PERSONAL, SECRET_PERSONAL,
SECRET_SHARED SECRET_SHARED
} from "../variables"; } from "../variables";
@@ -568,16 +569,25 @@ export const getSecretsHelper = async ({
const postHogClient = await TelemetryService.getPostHogClient(); const postHogClient = await TelemetryService.getPostHogClient();
const numberOfSignupSecrets = (secrets.filter((secret) => secret?.metadata?.source === "signup")).length; // reduce the number of events captured
let shouldRecordK8Event = false
if (authData instanceof ServiceTokenData && authData.userAgent == K8_USER_AGENT_NAME) {
const randomNumber = Math.random();
if (randomNumber > 0.9) {
shouldRecordK8Event = true
}
}
if (postHogClient && (secrets.length - numberOfSignupSecrets > 0)) { if (postHogClient) {
const shouldCapture = authData.userAgent !== K8_USER_AGENT_NAME || shouldRecordK8Event;
const approximateForNoneCapturedEvents = secrets.length * 10
if (shouldCapture) {
postHogClient.capture({ postHogClient.capture({
event: "secrets pulled", event: "secrets pulled",
distinctId: await TelemetryService.getDistinctId({ distinctId: await TelemetryService.getDistinctId({ authData }),
authData
}),
properties: { properties: {
numberOfSecrets: secrets.length - numberOfSignupSecrets, numberOfSecrets: shouldRecordK8Event ? approximateForNoneCapturedEvents : secrets.length,
environment, environment,
workspaceId, workspaceId,
folderId, folderId,
@@ -586,6 +596,7 @@ export const getSecretsHelper = async ({
} }
}); });
} }
}
return secrets; return secrets;
}; };
+2
View File
@@ -3,3 +3,5 @@ export enum AuthMode {
SERVICE_TOKEN = "serviceToken", SERVICE_TOKEN = "serviceToken",
API_KEY = "apiKey" API_KEY = "apiKey"
} }
export const K8_USER_AGENT_NAME = "k8-operator"