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

Do not log errors locally & Add ability to not log bodies #26

Merged
merged 1 commit into from
Jan 11, 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
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const defaultConfig = {
remoteConfigFetchEndpoint: '/config',
telemetryEndpoint: '/telemetry',
allowLocalUrls: false,
logRequestHeaders: true,
logRequestBody: true,
logResponseHeaders: true,
logResponseBody: true,
ignoredDomains: [],

// After the close command is sent, wait for this many milliseconds before
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ const Supergood = () => {
const body = await request.clone().text();
const requestData = {
id: requestId,
headers: Object.fromEntries(request.headers.entries()),
headers: supergoodConfig.logRequestHeaders ? Object.fromEntries(request.headers.entries()) : {},
method: request.method,
url: url.href,
path: url.pathname,
search: url.search,
body: safeParseJson(body),
body: supergoodConfig.logRequestBody ? safeParseJson(body) : {},
requestedAt: new Date()
} as RequestType;

Expand Down Expand Up @@ -197,10 +197,10 @@ const Supergood = () => {

const responseData = {
response: {
headers: Object.fromEntries(response.headers.entries()),
headers: supergoodConfig.logResponseHeaders ? Object.fromEntries(response.headers.entries()) : {},
status: response.status,
statusText: response.statusText,
body: response.body && safeParseJson(response.body),
body: supergoodConfig.logResponseBody ? response.body && safeParseJson(response.body) : {},
respondedAt: new Date()
},
...requestData
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ interface ConfigType {
telemetryEndpoint: string; // Defaults to {baseUrl}/telemetry if not provided
waitAfterClose: number;
remoteConfig: RemoteConfigType;
logRequestHeaders: boolean;
logRequestBody: boolean;
logResponseHeaders: boolean;
logResponseBody: boolean;
}

interface TelemetryType {
Expand Down
14 changes: 8 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const logger = ({
error: Error,
{ reportOut }: { reportOut: boolean } = { reportOut: true }
) => {
console.error(
new Date().toISOString(),
`${packageName}@${packageVersion}: ${message}`,
JSON.stringify(payload, null, 2),
error
);
if (process.env.SUPERGOOD_LOG_LEVEL === 'debug') {
console.error(
new Date().toISOString(),
`${packageName}@${packageVersion}: ${message}`,
JSON.stringify(payload, null, 2),
error
);
}
if (reportOut && errorSinkUrl) {
postError(
errorSinkUrl,
Expand Down
74 changes: 74 additions & 0 deletions test/e2e/core.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,80 @@ describe('core functionality', () => {
});
});

describe('log bodies', () => {
it('should not log the requestHeaders if specified in config', async () => {
await Supergood.init(
{
config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logRequestHeaders: false },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET,
},
SUPERGOOD_SERVER
);
await axios.get(`${MOCK_DATA_SERVER}/posts`);
await Supergood.close();
checkPostedEvents(postEventsMock, 1, {
request: expect.objectContaining({
headers: {}
})
});
});

it('should not log the requestBody if specified in config', async () => {
await Supergood.init(
{
config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logRequestBody: false },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET,
},
SUPERGOOD_SERVER
);
await axios.get(`${MOCK_DATA_SERVER}/posts`);
await Supergood.close();
checkPostedEvents(postEventsMock, 1, {
request: expect.objectContaining({
body: {}
})
});
});

it('should not log the responseHeaders if specified in config', async () => {
await Supergood.init(
{
config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logResponseHeaders: false },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET,
},
SUPERGOOD_SERVER
);
await axios.get(`${MOCK_DATA_SERVER}/posts`);
await Supergood.close();
checkPostedEvents(postEventsMock, 1, {
response: expect.objectContaining({
headers: {}
})
});
});

it('should not log the responseBody if specified in config', async () => {
await Supergood.init(
{
config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logResponseBody: false },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET,
},
SUPERGOOD_SERVER
);
await axios.get(`${MOCK_DATA_SERVER}/posts`);
await Supergood.close();
checkPostedEvents(postEventsMock, 1, {
response: expect.objectContaining({
body: {}
})
});
});
})

describe('headers', () => {
it('should capture custom request headers', async () => {
await Supergood.init(
Expand Down
Loading