Skip to content

Commit

Permalink
Add ability to not log body (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklarfeld authored Jan 11, 2024
1 parent 06d58e4 commit ba9b15c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 10 deletions.
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

0 comments on commit ba9b15c

Please sign in to comment.