Skip to content

Commit

Permalink
FF-2525 Add Assignment Details to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
greghuels committed Jun 27, 2024
1 parent 4454928 commit 274704a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 39 deletions.
17 changes: 17 additions & 0 deletions src/assignment-logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IAssignmentEvent } from './assignment-logger';
import { AllocationEvaluationCode } from './flag-evaluation-details';

describe('IAssignmentEvent', () => {
it('should allow adding arbitrary fields', () => {
Expand All @@ -11,6 +12,22 @@ describe('IAssignmentEvent', () => {
timestamp: new Date().toISOString(),
subjectAttributes: { age: 25, country: 'USA' },
holdoutKey: 'holdout_key_123',
details: {
variationKey: 'variationKey',
variationValue: 'variation_123',
flagEvaluationCode: 'MATCH',
flagEvaluationDescription: '',
configFetchedAt: new Date().toISOString(),
configPublishedAt: new Date().toISOString(),
matchedRule: null,
matchedAllocation: {
key: 'allocation_123',
allocationEvaluationCode: AllocationEvaluationCode.MATCH,
orderPosition: 1,
},
unmatchedAllocations: [],
unevaluatedAllocations: [],
},
};

expect(event.holdoutKey).toBe('holdout_key_123');
Expand Down
7 changes: 7 additions & 0 deletions src/assignment-logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IFlagEvaluationDetails } from './flag-evaluation-details';

export enum HoldoutVariationEnum {
STATUS_QUO = 'status_quo',
ALL_SHIPPED = 'all_shipped_variants',
Expand Down Expand Up @@ -46,6 +48,11 @@ export interface IAssignmentEvent {
[propName: string]: unknown;

metaData?: Record<string, unknown>;

/**
* The flag evaluation details
*/
details: IFlagEvaluationDetails;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/client/eppo-client-assignment-details.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import FetchHttpClient from '../http-client';
import { Flag, ObfuscatedFlag, VariationType } from '../interfaces';
import { OperatorType } from '../rules';

import EppoClient, { AssignmentDetails } from './eppo-client';
import EppoClient, { IAssignmentDetails } from './eppo-client';

async function init(configurationStore: IConfigurationStore<Flag | ObfuscatedFlag>) {
const apiEndpoints = new ApiEndpoints({
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('EppoClient get*AssignmentDetails', () => {
subjectAttributes,
0,
);
const expected: AssignmentDetails<number> = {
const expected: IAssignmentDetails<number> = {
value: 3,
variationKey: 'three',
variationValue: 3,
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('EppoClient get*AssignmentDetails', () => {
subjectAttributes,
0,
);
const expected: AssignmentDetails<number> = {
const expected: IAssignmentDetails<number> = {
value: 2,
variationKey: 'two',
variationValue: 2,
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('EppoClient get*AssignmentDetails', () => {
subjectAttributes,
'',
);
const expected: AssignmentDetails<string> = {
const expected: IAssignmentDetails<string> = {
value: 'control',
flagEvaluationCode: 'MATCH',
flagEvaluationDescription:
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('EppoClient get*AssignmentDetails', () => {
matchedAllocation: null,
unmatchedAllocations: [],
unevaluatedAllocations: [],
} as AssignmentDetails<number>);
} as IAssignmentDetails<number>);
});

describe('UFC General Test Cases', () => {
Expand Down
10 changes: 1 addition & 9 deletions src/client/eppo-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import {
MOCK_UFC_RESPONSE_FILE,
OBFUSCATED_MOCK_UFC_RESPONSE_FILE,
SubjectTestCase,
getTestAssignmentDetails,
getTestAssignments,
readAssignmentTestData,
readMockUFCResponse,
validateTestAssignmentDetails,
validateTestAssignments,
} from '../../test/testHelpers';
import ApiEndpoints from '../api-endpoints';
Expand All @@ -19,16 +17,10 @@ import { IConfigurationStore } from '../configuration-store/configuration-store'
import { MemoryOnlyConfigurationStore } from '../configuration-store/memory.store';
import { MAX_EVENT_QUEUE_SIZE, POLL_INTERVAL_MS, POLL_JITTER_PCT } from '../constants';
import FlagConfigurationRequestor from '../flag-configuration-requestor';
import { AllocationEvaluationCode } from '../flag-evaluation-details';
import FetchHttpClient from '../http-client';
import { Flag, ObfuscatedFlag, VariationType } from '../interfaces';
import { OperatorType } from '../rules';

import EppoClient, {
AssignmentDetails,
FlagConfigurationRequestParameters,
checkTypeMatch,
} from './eppo-client';
import EppoClient, { FlagConfigurationRequestParameters, checkTypeMatch } from './eppo-client';

export async function init(configurationStore: IConfigurationStore<Flag | ObfuscatedFlag>) {
const apiEndpoints = new ApiEndpoints({
Expand Down
19 changes: 10 additions & 9 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { decodeFlag } from '../decoding';
import { EppoValue } from '../eppo_value';
import { Evaluator, FlagEvaluation, noneResult } from '../evaluator';
import FlagConfigurationRequestor from '../flag-configuration-requestor';
import { FlagEvaluationDetails, FlagEvaluationDetailsBuilder } from '../flag-evaluation-details';
import { IFlagEvaluationDetails, FlagEvaluationDetailsBuilder } from '../flag-evaluation-details';
import FetchHttpClient from '../http-client';
import { Flag, ObfuscatedFlag, Variation, VariationType } from '../interfaces';
import { getMD5Hash } from '../obfuscation';
Expand All @@ -27,8 +27,8 @@ import { AttributeType, ValueType } from '../types';
import { validateNotBlank } from '../validation';
import { LIB_VERSION } from '../version';

export interface AssignmentDetails<T extends Variation['value'] | object>
extends FlagEvaluationDetails {
export interface IAssignmentDetails<T extends Variation['value'] | object>
extends IFlagEvaluationDetails {
value: T;
}

Expand Down Expand Up @@ -177,7 +177,7 @@ export default class EppoClient {
subjectKey: string,
subjectAttributes: Record<string, AttributeType>,
defaultValue: string,
): AssignmentDetails<string> {
): IAssignmentDetails<string> {
const { eppoValue, flagEvaluationDetails } = this.getAssignmentVariation(
flagKey,
subjectKey,
Expand Down Expand Up @@ -239,7 +239,7 @@ export default class EppoClient {
subjectKey: string,
subjectAttributes: Record<string, AttributeType>,
defaultValue: boolean,
): AssignmentDetails<boolean> {
): IAssignmentDetails<boolean> {
const { eppoValue, flagEvaluationDetails } = this.getAssignmentVariation(
flagKey,
subjectKey,
Expand Down Expand Up @@ -289,7 +289,7 @@ export default class EppoClient {
subjectKey: string,
subjectAttributes: Record<string, AttributeType>,
defaultValue: number,
): AssignmentDetails<number> {
): IAssignmentDetails<number> {
const { eppoValue, flagEvaluationDetails } = this.getAssignmentVariation(
flagKey,
subjectKey,
Expand Down Expand Up @@ -339,7 +339,7 @@ export default class EppoClient {
subjectKey: string,
subjectAttributes: Record<string, AttributeType>,
defaultValue: number,
): AssignmentDetails<number> {
): IAssignmentDetails<number> {
const { eppoValue, flagEvaluationDetails } = this.getAssignmentVariation(
flagKey,
subjectKey,
Expand Down Expand Up @@ -377,7 +377,7 @@ export default class EppoClient {
subjectKey: string,
subjectAttributes: Record<string, AttributeType>,
defaultValue: object,
): AssignmentDetails<object> {
): IAssignmentDetails<object> {
const { eppoValue, flagEvaluationDetails } = this.getAssignmentVariation(
flagKey,
subjectKey,
Expand All @@ -397,7 +397,7 @@ export default class EppoClient {
subjectAttributes: Record<string, AttributeType>,
defaultValue: EppoValue,
expectedVariationType: VariationType,
): { eppoValue: EppoValue; flagEvaluationDetails: FlagEvaluationDetails } {
): { eppoValue: EppoValue; flagEvaluationDetails: IFlagEvaluationDetails } {
try {
const result = this.getAssignmentDetail(
flagKey,
Expand Down Expand Up @@ -619,6 +619,7 @@ export default class EppoClient {
sdkLanguage: 'javascript',
sdkLibVersion: LIB_VERSION,
},
details: result.flagEvaluationDetails,
};

if (variation && allocationKey) {
Expand Down
6 changes: 3 additions & 3 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AllocationEvaluation,
AllocationEvaluationCode,
FlagEvaluationDetails,
IFlagEvaluationDetails,
FlagEvaluationDetailsBuilder,
} from './flag-evaluation-details';
import { Flag, Shard, Range, Variation, Allocation, Split, VariationType } from './interfaces';
Expand All @@ -17,7 +17,7 @@ export interface FlagEvaluation {
variation: Variation | null;
extraLogging: Record<string, string>;
doLog: boolean;
flagEvaluationDetails: FlagEvaluationDetails;
flagEvaluationDetails: IFlagEvaluationDetails;
}

export class Evaluator {
Expand Down Expand Up @@ -166,7 +166,7 @@ export function noneResult(
flagKey: string,
subjectKey: string,
subjectAttributes: SubjectAttributes,
flagEvaluationDetails: FlagEvaluationDetails,
flagEvaluationDetails: IFlagEvaluationDetails,
): FlagEvaluation {
return {
flagKey,
Expand Down
18 changes: 9 additions & 9 deletions src/flag-evaluation-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface AllocationEvaluation {
orderPosition: number;
}

export interface FlagEvaluationDetails {
export interface IFlagEvaluationDetails {
variationKey: string | null;
variationValue: Variation['value'] | null;
flagEvaluationCode: FlagEvaluationCode;
Expand All @@ -40,12 +40,12 @@ export interface FlagEvaluationDetails {
}

export class FlagEvaluationDetailsBuilder {
private variationKey: FlagEvaluationDetails['variationKey'];
private variationValue: FlagEvaluationDetails['variationValue'];
private matchedRule: FlagEvaluationDetails['matchedRule'];
private matchedAllocation: FlagEvaluationDetails['matchedAllocation'];
private unmatchedAllocations: FlagEvaluationDetails['unmatchedAllocations'];
private unevaluatedAllocations: FlagEvaluationDetails['unevaluatedAllocations'];
private variationKey: IFlagEvaluationDetails['variationKey'];
private variationValue: IFlagEvaluationDetails['variationValue'];
private matchedRule: IFlagEvaluationDetails['matchedRule'];
private matchedAllocation: IFlagEvaluationDetails['matchedAllocation'];
private unmatchedAllocations: IFlagEvaluationDetails['unmatchedAllocations'];
private unevaluatedAllocations: IFlagEvaluationDetails['unevaluatedAllocations'];

constructor(
private readonly allocations: Allocation[],
Expand Down Expand Up @@ -121,12 +121,12 @@ export class FlagEvaluationDetailsBuilder {
buildForNoneResult = (
flagEvaluationCode: FlagEvaluationCode,
flagEvaluationDescription: string,
): FlagEvaluationDetails => this.setNone().build(flagEvaluationCode, flagEvaluationDescription);
): IFlagEvaluationDetails => this.setNone().build(flagEvaluationCode, flagEvaluationDescription);

build = (
flagEvaluationCode: FlagEvaluationCode,
flagEvaluationDescription: string,
): FlagEvaluationDetails => ({
): IFlagEvaluationDetails => ({
flagEvaluationCode,
flagEvaluationDescription,
variationKey: this.variationKey,
Expand Down
8 changes: 4 additions & 4 deletions test/testHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';

import { AssignmentDetails } from '../src/client/eppo-client';
import { IAssignmentDetails } from '../src/client/eppo-client';
import { Flag, VariationType } from '../src/interfaces';
import { AttributeType } from '../src/types';

Expand All @@ -13,7 +13,7 @@ export interface SubjectTestCase {
subjectKey: string;
subjectAttributes: Record<string, AttributeType>;
assignment: string | number | boolean | object;
assignmentDetails: AssignmentDetails<string | number | boolean | object>;
assignmentDetails: IAssignmentDetails<string | number | boolean | object>;
}

export interface IAssignmentTestCase {
Expand Down Expand Up @@ -70,7 +70,7 @@ export function getTestAssignmentDetails(
) => never,
): {
subject: SubjectTestCase;
assignmentDetails: AssignmentDetails<string | boolean | number | object>;
assignmentDetails: IAssignmentDetails<string | boolean | number | object>;
}[] {
return testCase.subjects.map((subject) => ({
subject,
Expand Down Expand Up @@ -108,7 +108,7 @@ export function validateTestAssignments(
export function validateTestAssignmentDetails(
assignments: {
subject: SubjectTestCase;
assignmentDetails: AssignmentDetails<string | boolean | number | object>;
assignmentDetails: IAssignmentDetails<string | boolean | number | object>;
}[],
flag: string,
) {
Expand Down

0 comments on commit 274704a

Please sign in to comment.