Skip to content

Commit

Permalink
feat: Event sampling. (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Aug 23, 2023
1 parent 02e92a4 commit f9b4e6e
Show file tree
Hide file tree
Showing 26 changed files with 794 additions and 184 deletions.
1 change: 1 addition & 0 deletions contract-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ app.get('/', (req, res) => {
'tags',
'big-segments',
'user-type',
'migrations',
],
});
});
Expand Down
89 changes: 88 additions & 1 deletion contract-tests/sdkClientEntity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import ld from 'node-server-sdk';
import got from 'got';
import ld, {
LDExecution,
LDExecutionOrdering,
LDMigrationError,
LDMigrationSuccess,
LDSerialExecution,
Migration,
} from 'node-server-sdk';

import BigSegmentTestStore from './BigSegmentTestStore.js';
import { Log, sdkLogger } from './log.js';
Expand Down Expand Up @@ -126,6 +134,85 @@ export async function newSdkClientEntity(options) {
case 'getBigSegmentStoreStatus':
return await client.bigSegmentStoreStatusProvider.requireStatus();

case 'migrationVariation':
const migrationVariation = params.migrationVariation;
const res = await client.variationMigration(
migrationVariation.key,
migrationVariation.context,
migrationVariation.defaultStage,
);
return { result: res.value };

case 'migrationOperation':
const migrationOperation = params.migrationOperation;
const migration = new Migration(client, {
execution: new LDSerialExecution(LDExecutionOrdering.Fixed),
latencyTracking: migrationOperation.trackLatency,
errorTracking: migrationOperation.trackErrors,
check: migrationOperation.trackConsistency ? (a, b) => a === b : undefined,
readNew: async () => {
try {
const res = await got.post(migrationOperation.newEndpoint, {});
return LDMigrationSuccess(res.body);
} catch (err) {
return LDMigrationError(err.message);
}
},
writeNew: async () => {
try {
const res = await got.post(migrationOperation.newEndpoint, {});
return LDMigrationSuccess(res.body);
} catch (err) {
return LDMigrationError(err.message);
}
},
readOld: async () => {
try {
const res = await got.post(migrationOperation.oldEndpoint, {});
return LDMigrationSuccess(res.body);
} catch (err) {
return LDMigrationError(err.message);
}
},
writeOld: async () => {
try {
const res = await got.post(migrationOperation.oldEndpoint, {});
return LDMigrationSuccess(res.body);
} catch (err) {
return LDMigrationError(err.message);
}
},
});

switch (migrationOperation.operation) {
case 'read': {
const res = await migration.read(
migrationOperation.key,
migrationOperation.context,
migrationOperation.defaultStage,
);
if (res.success) {
return { result: res.result };
} else {
return { result: res.error };
}
}
case 'write': {
const res = await migration.write(
migrationOperation.key,
migrationOperation.context,
migrationOperation.defaultStage,
);

if (res.authoritative.success) {
return { result: res.authoritative.result };
} else {
return { result: res.authoritative.error };
}
}
}
return undefined;

default:
throw badCommandError;
}
Expand Down
Loading

0 comments on commit f9b4e6e

Please sign in to comment.