Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Scrub logs #1427

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions @shared/api/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,16 @@ export const getAccountIndexerBalances = async (
const data = (await response.json()) as AccountBalancesInterface;
if (!response.ok) {
const _err = JSON.stringify(data);
captureException(`Failed to fetch account balances - ${_err}`);
captureException(
`Failed to fetch account balances - ${response.status}: ${response.statusText}`,
);
throw new Error(_err);
}

if ("error" in data && (data?.error?.horizon || data?.error?.soroban)) {
const _err = JSON.stringify(data.error);
captureException(`Failed to fetch account balances - ${_err}`);
captureException(
`Failed to fetch account balances - ${response.status}: ${response.statusText}`,
);
}

const formattedBalances = {} as NonNullable<
Expand Down
12 changes: 2 additions & 10 deletions extension/src/background/helpers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ export const subscribeAccount = async (publicKey: string) => {
}
} catch (e) {
console.error(e);
// Turn on when Mercury is enabled
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need these, we can get these logs from our server logs and probably don't want this triggering when Mercury is turned off.

// captureException(
// `Failed to subscribe account with Mercury - ${JSON.stringify(e)}`,
// );
}

return { publicKey };
Expand Down Expand Up @@ -296,9 +292,7 @@ export const subscribeTokenBalance = async (
}
} catch (e) {
console.error(e);
captureException(
`Failed to subscribe token balance - ${JSON.stringify(e)}`,
);
captureException(`Failed to subscribe token balance - ${contractId}`);
}
};

Expand All @@ -324,9 +318,7 @@ export const subscribeTokenHistory = async (
}
} catch (e) {
console.error(e);
captureException(
`Failed to subscribe token history - ${JSON.stringify(e)}`,
);
captureException(`Failed to subscribe token history - ${contractId}`);
}
};

Expand Down
24 changes: 24 additions & 0 deletions extension/src/popup/components/ErrorTracking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Integrations } from "@sentry/tracing";

import { SENTRY_KEY } from "constants/env";
import { settingsDataSharingSelector } from "popup/ducks/settings";
import { scrubPathGkey } from "popup/helpers/formatters";
import { INDEXER_URL } from "@shared/constants/mercury";
import packageJson from "../../../../package.json";

export const ErrorTracking = () => {
Expand All @@ -19,6 +21,28 @@ export const ErrorTracking = () => {
// Amplitude 4xx's on too many Posts, which is expected behavior
/api\.amplitude\.com\/2\/httpapi/i,
],
beforeSend(event) {
if (!event.request) {
return event;
}

const url = event.request?.url;
if (url?.includes(`${INDEXER_URL}/account-history`)) {
const route = "account-history/";
const scrubbedUrl = scrubPathGkey(route, url);
// eslint-disable-next-line no-param-reassign
event.request.url = scrubbedUrl;
}

if (url?.includes(`${INDEXER_URL}/account-balances`)) {
const route = "account-balances/";
const scrubbedUrl = scrubPathGkey(route, url);
// eslint-disable-next-line no-param-reassign
event.request.url = scrubbedUrl;
}

return event;
},
});
}

Expand Down
26 changes: 25 additions & 1 deletion extension/src/popup/helpers/__tests__/formatters.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { formatAmount } from "../formatters";
import { formatAmount, scrubPathGkey } from "../formatters";
import { formatTokenAmount } from "../soroban";

describe("formatAmount", () => {
Expand All @@ -15,3 +15,27 @@ describe("formatAmount", () => {
expect(formatTokenAmount(value, 7)).toBe(formatted);
});
});

describe("scrubPathGkey", () => {
const ADDRESS = "GCBDC5AVPZEOSO3IAASQZSVRJMHX3UCCZH5O7S53FPZ636LQ5RHEW65H";
it("should redact a G address from a URL", () => {
const route = "account-history/";
const url =
"http://0.0.0.0:3002/api/v1/account-history/GCBDC5AVPZEOSO3IAASQZSVRJMHX3UCCZH5O7S53FPZ636LQ5RHEW65H?network=PUBLIC";
const expected =
"http://0.0.0.0:3002/api/v1/account-history/REDACTED?network=PUBLIC";
expect(scrubPathGkey(route, url)).toBe(expected);
});
it("should redact a G address from a URL with no query params", () => {
const route = "account-history/";
const url =
"http://0.0.0.0:3002/api/v1/account-history/GCBDC5AVPZEOSO3IAASQZSVRJMHX3UCCZH5O7S53FPZ636LQ5RHEW65H";
const expected = "http://0.0.0.0:3002/api/v1/account-history/REDACTED";
expect(scrubPathGkey(route, url)).toBe(expected);
});
it("should return the URL in cases where it cannot redact", () => {
const route = "account-history/";
const url = "not-even-a-url";
expect(scrubPathGkey(route, url)).toBe(url);
});
});
10 changes: 10 additions & 0 deletions extension/src/popup/helpers/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,13 @@ export const formatAmount = (val: string) => {

export const formattedBuffer = (data: Buffer) =>
truncatedPublicKey(Buffer.from(data).toString("hex").toUpperCase());

export const scrubPathGkey = (route: string, url: string) => {
try {
const [base, slug] = url.split(route);
const end = slug.indexOf("?") === -1 ? slug.length : slug.indexOf("?");
return `${base}${route}${"REDACTED"}${slug.substring(end)}`;
} catch (error) {
return url;
}
};