Skip to content

Commit

Permalink
feat: Remove index and custom event sampling. (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Oct 2, 2023
1 parent 837c4e1 commit c3f2d88
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 462 deletions.
12 changes: 0 additions & 12 deletions packages/shared/akamai-edgeworker-sdk/src/featureStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ export class EdgeFeatureStore implements LDFeatureStore {
case 'segments':
callback(item.segments[dataKey]);
break;
case 'configurationOverrides':
callback(item.configurationOverrides?.[dataKey] ?? null);
break;
case 'metrics':
callback(item.metrics?.[dataKey] ?? null);
break;
default:
callback(null);
}
Expand Down Expand Up @@ -96,12 +90,6 @@ export class EdgeFeatureStore implements LDFeatureStore {
case 'segments':
callback(item.segments);
break;
case 'configurationOverrides':
callback(item.configurationOverrides || {});
break;
case 'metrics':
callback(item.metrics || {});
break;
default:
throw new Error(`Unsupported DataKind: ${namespace}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ describe('given an event processor', () => {
trackEvents: true,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -326,7 +325,6 @@ describe('given an event processor', () => {
trackEvents: true,
default: 'default',
samplingRatio: 2,
indexSamplingRatio: 1, // Disable the index events.
withReasons: true,
});

Expand Down Expand Up @@ -360,7 +358,6 @@ describe('given an event processor', () => {
trackEvents: true,
default: 'default',
samplingRatio: 2,
indexSamplingRatio: 1, // Disable the index events.
withReasons: true,
});

Expand All @@ -379,36 +376,6 @@ describe('given an event processor', () => {
]);
});

it('excludes index events that are not sampled', async () => {
// @ts-ignore
shouldSample.mockImplementation((ratio) => ratio === 2);
Date.now = jest.fn(() => 1000);
eventProcessor.sendEvent({
kind: 'feature',
creationDate: 1000,
context: Context.fromLDContext(user),
key: 'flagkey',
version: 11,
variation: 1,
value: 'value',
trackEvents: true,
default: 'default',
samplingRatio: 2,
indexSamplingRatio: 1, // Disable the index events.
withReasons: true,
});

await eventProcessor.flush();
const request = await eventSender.queue.take();
expect(shouldSample).toHaveBeenCalledWith(2);
expect(shouldSample).toHaveBeenCalledWith(1);

expect(request.data).toEqual([
{ ...makeFeatureEvent(1000, 11), samplingRatio: 2 },
makeSummary(1000, 1000, 1, 11),
]);
});

it('handles the version being 0', async () => {
Date.now = jest.fn(() => 1000);
eventProcessor.sendEvent({
Expand All @@ -422,7 +389,6 @@ describe('given an event processor', () => {
trackEvents: true,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -455,7 +421,6 @@ describe('given an event processor', () => {
debugEventsUntilDate: 2000,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -487,7 +452,6 @@ describe('given an event processor', () => {
debugEventsUntilDate: 2000,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -526,7 +490,6 @@ describe('given an event processor', () => {
debugEventsUntilDate: 1500,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -558,7 +521,6 @@ describe('given an event processor', () => {
trackEvents: true,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});
eventProcessor.sendEvent({
Expand All @@ -572,7 +534,6 @@ describe('given an event processor', () => {
trackEvents: true,
default: 'potato',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -637,7 +598,6 @@ describe('given an event processor', () => {
trackEvents: false,
default: 'default',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});
eventProcessor.sendEvent({
Expand All @@ -651,7 +611,6 @@ describe('given an event processor', () => {
trackEvents: false,
default: 'potato',
samplingRatio: 1,
indexSamplingRatio: 1,
withReasons: true,
});

Expand Down Expand Up @@ -709,42 +668,6 @@ describe('given an event processor', () => {
key: 'eventkey',
data: { thing: 'stuff' },
samplingRatio: 1,
indexSamplingRatio: 1,
});

await eventProcessor.flush();
const request = await eventSender.queue.take();

expect(request.data).toEqual([
{
kind: 'index',
creationDate: 1000,
context: { ...user, kind: 'user' },
},
{
kind: 'custom',
key: 'eventkey',
data: { thing: 'stuff' },
creationDate: 1000,
contextKeys: {
user: 'userKey',
},
},
]);
});

it('does not queue a custom event that is not sampled', async () => {
// @ts-ignore
shouldSample.mockImplementation((ratio) => ratio !== 2);
Date.now = jest.fn(() => 1000);
eventProcessor.sendEvent({
kind: 'custom',
creationDate: 1000,
context: Context.fromLDContext(user),
key: 'eventkey',
data: { thing: 'stuff' },
samplingRatio: 2,
indexSamplingRatio: 1,
});

await eventProcessor.flush();
Expand All @@ -756,27 +679,6 @@ describe('given an event processor', () => {
creationDate: 1000,
context: { ...user, kind: 'user' },
},
]);
});

it('does not queue a index event that is not sampled with a custom event', async () => {
// @ts-ignore
shouldSample.mockImplementation((ratio) => ratio === 2);
Date.now = jest.fn(() => 1000);
eventProcessor.sendEvent({
kind: 'custom',
creationDate: 1000,
context: Context.fromLDContext(user),
key: 'eventkey',
data: { thing: 'stuff' },
samplingRatio: 2,
indexSamplingRatio: 1,
});

await eventProcessor.flush();
const request = await eventSender.queue.take();

expect(request.data).toEqual([
{
kind: 'custom',
key: 'eventkey',
Expand All @@ -785,7 +687,6 @@ describe('given an event processor', () => {
contextKeys: {
user: 'userKey',
},
samplingRatio: 2,
},
]);
});
Expand All @@ -798,7 +699,6 @@ describe('given an event processor', () => {
key: 'eventkey',
data: { thing: 'stuff' },
samplingRatio: 1,
indexSamplingRatio: 1,
});

await eventProcessor.flush();
Expand Down Expand Up @@ -832,7 +732,6 @@ describe('given an event processor', () => {
data: { thing: 'stuff' },
metricValue: 1.5,
samplingRatio: 1,
indexSamplingRatio: 1,
});

await eventProcessor.flush();
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/common/src/internal/events/EventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ export default class EventProcessor implements LDEventProcessor {

const addIndexEvent = shouldNotDeduplicate && !isIdentifyEvent;

if (addIndexEvent && shouldSample(inputEvent.indexSamplingRatio)) {
if (addIndexEvent) {
this.enqueue(
this.makeOutputEvent(
{
kind: 'index',
creationDate: inputEvent.creationDate,
context: inputEvent.context,
samplingRatio: inputEvent.indexSamplingRatio,
samplingRatio: 1,
},
false,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export default class InputCustomEvent {
public readonly key: string,
public readonly data?: any,
public readonly metricValue?: number,
// Currently custom events are not sampled, but this is here to make the handling
// code more uniform.
public readonly samplingRatio: number = 1,
public readonly indexSamplingRatio: number = 1,
) {
this.creationDate = Date.now();
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default class InputEvalEvent {
debugEventsUntilDate?: number,
excludeFromSummaries?: boolean,
public readonly samplingRatio: number = 1,
public readonly indexSamplingRatio: number = 1,
) {
this.creationDate = Date.now();
this.default = defValue;
Expand Down
29 changes: 0 additions & 29 deletions packages/shared/common/src/internal/events/LDEventOverrides.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/shared/common/src/internal/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import InputEvalEvent from './InputEvalEvent';
import InputEvent from './InputEvent';
import InputIdentifyEvent from './InputIdentifyEvent';
import InputMigrationEvent from './InputMigrationEvent';
import { LDEventOverrides } from './LDEventOverrides';
import shouldSample from './sampling';

export {
Expand All @@ -14,6 +13,5 @@ export {
InputIdentifyEvent,
InputMigrationEvent,
EventProcessor,
LDEventOverrides,
shouldSample,
};
12 changes: 0 additions & 12 deletions packages/shared/sdk-server-edge/src/api/EdgeFeatureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ export class EdgeFeatureStore implements LDFeatureStore {
case 'segments':
callback(item.segments[dataKey]);
break;
case 'configurationOverrides':
callback(item.configurationOverrides?.[dataKey] ?? null);
break;
case 'metrics':
callback(item.metrics?.[dataKey] ?? null);
break;
default:
callback(null);
}
Expand Down Expand Up @@ -89,12 +83,6 @@ export class EdgeFeatureStore implements LDFeatureStore {
case 'segments':
callback(item.segments);
break;
case 'configurationOverrides':
callback(item.configurationOverrides || {});
break;
case 'metrics':
callback(item.metrics || {});
break;
default:
callback({});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ describe('given an event processor', () => {
const allData = {
flags: { flag: { version: 1 } },
segments: { segment: { version: 1 } },
configurationOverrides: { override: { version: 1 } },
metrics: { metric: { version: 1 } },
};
const jsonData = JSON.stringify(allData);

Expand Down Expand Up @@ -74,10 +72,6 @@ describe('given an event processor', () => {
expect(flags).toEqual(allData.flags);
const segments = await storeFacade.all(VersionedDataKinds.Segments);
expect(segments).toEqual(allData.segments);
const configurationOverrides = await storeFacade.all(VersionedDataKinds.ConfigurationOverrides);
expect(configurationOverrides).toEqual(allData.configurationOverrides);
const metrics = await storeFacade.all(VersionedDataKinds.Metrics);
expect(metrics).toEqual(allData.metrics);
});
});

Expand Down
Loading

0 comments on commit c3f2d88

Please sign in to comment.