Skip to content

Commit

Permalink
Tagging API calls withContext
Browse files Browse the repository at this point in the history
  • Loading branch information
aklarfeld committed Apr 9, 2024
1 parent a1212d5 commit 397ee46
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ const Supergood = () => {
try {
const requestData = requestCache.get(requestId) as {
request: RequestType;
tags: Record<string, string | number | string[]>;
};

if (requestData) {
Expand Down Expand Up @@ -263,9 +264,10 @@ const Supergood = () => {
};

const cacheRequest = async (request: RequestType, baseUrl: string) => {
requestCache.set(request.id, { request });
requestCache.set(request.id, { request, tags: getTags() });
log.debug('Setting Request Cache', {
request
request,
tags: getTags()
});
};

Expand All @@ -279,6 +281,10 @@ const Supergood = () => {
log.debug('Deleting Request Cache', { id: event.request.id });
};

const getTags = () => {
return { ...supergoodTags, ...(supergoodAsyncLocalStorage.getStore()?.tags || {}) };
}

// Force flush cache means don't wait for responses
const flushCache = async ({ force } = { force: false }) => {
const responseCacheKeys = responseCache.keys();
Expand All @@ -296,7 +302,6 @@ const Supergood = () => {
const responseArray = prepareData(
responseCacheValues as EventRequestType[],
supergoodConfig.remoteConfig,
{ ...supergoodTags, ...(supergoodAsyncLocalStorage.getStore()?.tags || {}) }
) as Array<EventRequestType>;

let data = [...responseArray];
Expand All @@ -305,8 +310,7 @@ const Supergood = () => {
if (force) {
const requestArray = prepareData(
requestCacheValues as EventRequestType[],
supergoodConfig.remoteConfig,
{ ...supergoodTags, ...(supergoodAsyncLocalStorage.getStore()?.tags || {}) }
supergoodConfig.remoteConfig
) as Array<EventRequestType>;
data = [...requestArray, ...responseArray];
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ interface MetadataType {
interface EventRequestType {
request: RequestType;
response: ResponseType;
tags?: Record<string, string | number | string[]>;
metadata?: {
sensitiveKeys: Array<SensitiveKeyMetadata>;
tags: Record<string, string | number | string[]>;
tags?: Record<string, string | number | string[]>;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ it('will prepare the data appropriately for posting to the server', () => {
}
};

const events = prepareData([obj], remoteConfig, {});
const events = prepareData([obj], remoteConfig);
expect(_get(events[0], 'response.body.user.email')).toBeFalsy();
expect(_get(events[0], 'request.body.blogType.name')).toBeFalsy();
expect(events[0].metadata.sensitiveKeys.length).toEqual(2);
Expand Down
17 changes: 12 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,27 @@ const expandSensitiveKeySetForArrays = (
};

const redactValuesFromKeys = (
event: { request?: RequestType; response?: ResponseType },
event: { request?: RequestType; response?: ResponseType, tags?: Record<string, string | number | string[]> },
remoteConfig: RemoteConfigType
): {
event: { request?: RequestType; response?: ResponseType };
sensitiveKeyMetadata: Array<SensitiveKeyMetadata>;
tags: Record<string, string | number | string[]>;
} => {
// Move the tags off the event object and into the metadata object
let tags = {};
if(event.tags) {
tags = event.tags;
delete event.tags;
}

let sensitiveKeyMetadata: Array<SensitiveKeyMetadata> = [];
const endpointConfig = getEndpointConfigForRequest(
event.request as RequestType,
remoteConfig
);
if (!endpointConfig || !endpointConfig?.sensitiveKeys?.length)
return { event, sensitiveKeyMetadata };
return { event, sensitiveKeyMetadata, tags };
else {
const sensitiveKeys = expandSensitiveKeySetForArrays(
event,
Expand All @@ -200,7 +208,7 @@ const redactValuesFromKeys = (
});
}
}
return { event, sensitiveKeyMetadata };
return { event, sensitiveKeyMetadata, tags };
}
};

Expand Down Expand Up @@ -243,10 +251,9 @@ const redactValue = (
const prepareData = (
events: Array<EventRequestType>,
remoteConfig: RemoteConfigType,
tags: Record<string, string | number | string[]>
) => {
return events.map((e) => {
const { event, sensitiveKeyMetadata } = redactValuesFromKeys(
const { event, sensitiveKeyMetadata, tags } = redactValuesFromKeys(
e,
remoteConfig
);
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/tags.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fetch from 'node-fetch';
import { get } from 'lodash';

import Supergood from '../../src';
import { sleep } from '../../src/utils';
import {
MOCK_DATA_SERVER,
SUPERGOOD_CLIENT_ID,
Expand Down Expand Up @@ -46,13 +47,14 @@ describe('Custom tags', () => {
SUPERGOOD_SERVER
);

await Supergood.withContext({ customTag: 'customValue' }, async () => {
await Supergood.withContext({ call: 'A' }, async () => {
await fetch(`${MOCK_DATA_SERVER}/profile`);
await Supergood.waitAndFlushCache();
});

await Supergood.close();

const eventsPosted = getEvents(postEventsMock);
expect(eventsPosted.length).toEqual(1);
expect(get(eventsPosted[0], 'metadata.tags.customTag')).toEqual('customValue');
expect(get(eventsPosted[0], 'metadata.tags.call')).toEqual('A');
});
})
2 changes: 1 addition & 1 deletion test/e2e/telemetry.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('telemetry posting', () => {
getTelemetry(postTelemetryMock);

expect(cacheKeys).toEqual(1);
expect(cacheSize).toEqual(160);
expect(cacheSize).toEqual(240);
expect(serviceName).toEqual('test-service-name');
});
});

0 comments on commit 397ee46

Please sign in to comment.