Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve allocation evaluation details for ASSIGNMENT_ERROR #118

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
FlagEvaluationDetailsBuilder,
IFlagEvaluationDetails,
} from '../flag-evaluation-details-builder';
import { FlagEvaluationError } from '../flag-evaluation-error';
import FetchHttpClient from '../http-client';
import {
BanditParameters,
Expand Down Expand Up @@ -679,29 +680,50 @@ export default class EppoClient {
subjectAttributes,
expectedVariationType,
);
return this.parseVariationWithDetails(result, defaultValue, expectedVariationType);
} catch (error) {
const eppoValue = this.rethrowIfNotGraceful(error, defaultValue);
if (error instanceof FlagEvaluationError && error.flagEvaluationDetails) {
greghuels marked this conversation as resolved.
Show resolved Hide resolved
return {
eppoValue,
flagEvaluationDetails: error.flagEvaluationDetails,
};
} else {
const flagEvaluationDetails = new FlagEvaluationDetailsBuilder(
'',
[],
'',
'',
).buildForNoneResult('ASSIGNMENT_ERROR', `Assignment Error: ${error.message}`);
return {
eppoValue,
flagEvaluationDetails,
};
}
}
}

if (!result.variation) {
private parseVariationWithDetails(
result: FlagEvaluation,
defaultValue: EppoValue,
expectedVariationType: VariationType,
): { eppoValue: EppoValue; flagEvaluationDetails: IFlagEvaluationDetails } {
try {
if (!result.variation || result.flagEvaluationDetails.flagEvaluationCode !== 'MATCH') {
greghuels marked this conversation as resolved.
Show resolved Hide resolved
return {
eppoValue: defaultValue,
flagEvaluationDetails: result.flagEvaluationDetails,
};
}

return {
eppoValue: EppoValue.valueOf(result.variation.value, expectedVariationType),
flagEvaluationDetails: result.flagEvaluationDetails,
};
} catch (error) {
const eppoValue = this.rethrowIfNotGraceful(error, defaultValue);
const flagEvaluationDetails = new FlagEvaluationDetailsBuilder(
'',
[],
'',
'',
).buildForNoneResult('ASSIGNMENT_ERROR', `Assignment Error: ${error.message}`);
return {
eppoValue,
flagEvaluationDetails,
flagEvaluationDetails: result.flagEvaluationDetails,
};
}
}
Expand Down Expand Up @@ -784,16 +806,6 @@ export default class EppoClient {
result.flagKey = flagKey;
}

if (result?.variation && !checkValueTypeMatch(expectedVariationType, result.variation.value)) {
const { key: vKey, value: vValue } = result.variation;
const reason = `Variation (${vKey}) is configured for type ${expectedVariationType}, but is set to incompatible value (${vValue})`;
const flagEvaluationDetails = flagEvaluationDetailsBuilder.buildForNoneResult(
'ASSIGNMENT_ERROR',
reason,
);
return noneResult(flagKey, subjectKey, subjectAttributes, flagEvaluationDetails);
}

try {
if (result?.doLog) {
this.logAssignment(result);
Expand Down
189 changes: 108 additions & 81 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { checkValueTypeMatch } from './client/eppo-client';
import {
AllocationEvaluation,
AllocationEvaluationCode,
IFlagEvaluationDetails,
FlagEvaluationDetailsBuilder,
FlagEvaluationCode,
} from './flag-evaluation-details-builder';
import { FlagEvaluationError } from './flag-evaluation-error';
import {
Flag,
Shard,
Expand Down Expand Up @@ -50,116 +52,141 @@ export class Evaluator {
configDetails.configFetchedAt,
configDetails.configPublishedAt,
);
try {
if (!flag.enabled) {
return noneResult(
flag.key,
subjectKey,
subjectAttributes,
flagEvaluationDetailsBuilder.buildForNoneResult(
'FLAG_UNRECOGNIZED_OR_DISABLED',
`Unrecognized or disabled flag: ${flag.key}`,
),
);
}

const now = new Date();
for (let i = 0; i < flag.allocations.length; i++) {
const allocation = flag.allocations[i];
const addUnmatchedAllocation = (code: AllocationEvaluationCode) => {
flagEvaluationDetailsBuilder.addUnmatchedAllocation({
key: allocation.key,
allocationEvaluationCode: code,
orderPosition: i + 1,
});
};

if (!flag.enabled) {
if (allocation.startAt && now < new Date(allocation.startAt)) {
addUnmatchedAllocation(AllocationEvaluationCode.BEFORE_START_TIME);
continue;
}
if (allocation.endAt && now > new Date(allocation.endAt)) {
addUnmatchedAllocation(AllocationEvaluationCode.AFTER_END_TIME);
continue;
}
const { matched, matchedRule } = matchesRules(
allocation?.rules ?? [],
{ id: subjectKey, ...subjectAttributes },
obfuscated,
);
if (matched) {
for (const split of allocation.splits) {
if (
split.shards.every((shard) => this.matchesShard(shard, subjectKey, flag.totalShards))
) {
const variation = flag.variations[split.variationKey];
const { flagEvaluationCode, flagEvaluationDescription } =
this.getMatchedEvaluationCodeAndDescription(
variation,
allocation,
split,
subjectKey,
expectedVariationType,
);
const flagEvaluationDetails = flagEvaluationDetailsBuilder
.setMatch(i, variation, allocation, matchedRule, expectedVariationType)
.build(flagEvaluationCode, flagEvaluationDescription);
return {
flagKey: flag.key,
subjectKey,
subjectAttributes,
allocationKey: allocation.key,
variation,
extraLogging: split.extraLogging ?? {},
doLog: allocation.doLog,
flagEvaluationDetails,
};
}
}
// matched, but does not fall within split range
addUnmatchedAllocation(AllocationEvaluationCode.TRAFFIC_EXPOSURE_MISS);
} else {
addUnmatchedAllocation(AllocationEvaluationCode.FAILING_RULE);
}
}
return noneResult(
flag.key,
subjectKey,
subjectAttributes,
flagEvaluationDetailsBuilder.buildForNoneResult(
'FLAG_UNRECOGNIZED_OR_DISABLED',
`Unrecognized or disabled flag: ${flag.key}`,
'DEFAULT_ALLOCATION_NULL',
'No allocations matched. Falling back to "Default Allocation", serving NULL',
),
);
}

const now = new Date();
const unmatchedAllocations: Array<AllocationEvaluation> = [];
for (let i = 0; i < flag.allocations.length; i++) {
const allocation = flag.allocations[i];
const addUnmatchedAllocation = (code: AllocationEvaluationCode) => {
unmatchedAllocations.push({
key: allocation.key,
allocationEvaluationCode: code,
orderPosition: i + 1,
});
};

if (allocation.startAt && now < new Date(allocation.startAt)) {
addUnmatchedAllocation(AllocationEvaluationCode.BEFORE_START_TIME);
continue;
}
if (allocation.endAt && now > new Date(allocation.endAt)) {
addUnmatchedAllocation(AllocationEvaluationCode.AFTER_END_TIME);
continue;
}
const { matched, matchedRule } = matchesRules(
allocation?.rules ?? [],
{ id: subjectKey, ...subjectAttributes },
obfuscated,
} catch (err) {
const flagEvaluationDetails = flagEvaluationDetailsBuilder.gracefulBuild(
'ASSIGNMENT_ERROR',
`Assignment Error: ${err.message}`,
);
if (matched) {
for (const split of allocation.splits) {
if (
split.shards.every((shard) => this.matchesShard(shard, subjectKey, flag.totalShards))
) {
const variation = flag.variations[split.variationKey];
const flagEvaluationDetails = flagEvaluationDetailsBuilder
.setMatch(
i,
variation,
allocation,
matchedRule,
unmatchedAllocations,
expectedVariationType,
)
.build(
'MATCH',
this.getMatchedEvaluationDetailsMessage(allocation, split, subjectKey),
);
return {
flagKey: flag.key,
subjectKey,
subjectAttributes,
allocationKey: allocation.key,
variation,
extraLogging: split.extraLogging ?? {},
doLog: allocation.doLog,
flagEvaluationDetails,
};
}
}
// matched, but does not fall within split range
addUnmatchedAllocation(AllocationEvaluationCode.TRAFFIC_EXPOSURE_MISS);
} else {
addUnmatchedAllocation(AllocationEvaluationCode.FAILING_RULE);
if (flagEvaluationDetails) {
const flagEvaluationError = new FlagEvaluationError(err.message);
flagEvaluationError.flagEvaluationDetails = flagEvaluationDetails;
throw flagEvaluationError;
}
throw err;
}
return noneResult(
flag.key,
subjectKey,
subjectAttributes,
flagEvaluationDetailsBuilder
.setNoMatchFound(unmatchedAllocations)
.build(
'DEFAULT_ALLOCATION_NULL',
'No allocations matched. Falling back to "Default Allocation", serving NULL',
),
);
}

matchesShard(shard: Shard, subjectKey: string, totalShards: number): boolean {
const assignedShard = this.sharder.getShard(hashKey(shard.salt, subjectKey), totalShards);
return shard.ranges.some((range) => isInShardRange(assignedShard, range));
}

private getMatchedEvaluationDetailsMessage = (
private getMatchedEvaluationCodeAndDescription = (
variation: Variation,
allocation: Allocation,
split: Split,
subjectKey: string,
): string => {
expectedVariationType: VariationType | undefined,
): { flagEvaluationCode: FlagEvaluationCode; flagEvaluationDescription: string } => {
if (!checkValueTypeMatch(expectedVariationType, variation.value)) {
const { key: vKey, value: vValue } = variation;
return {
flagEvaluationCode: 'ASSIGNMENT_ERROR',
flagEvaluationDescription: `Variation (${vKey}) is configured for type ${expectedVariationType}, but is set to incompatible value (${vValue})`,
};
}
const hasDefinedRules = !!allocation.rules?.length;
const isExperiment = allocation.splits.length > 1;
const isPartialRollout = split.shards.length > 1;
const isExperimentOrPartialRollout = isExperiment || isPartialRollout;

if (hasDefinedRules && isExperimentOrPartialRollout) {
return `Supplied attributes match rules defined in allocation "${allocation.key}" and ${subjectKey} belongs to the range of traffic assigned to "${split.variationKey}".`;
return {
flagEvaluationCode: 'MATCH',
flagEvaluationDescription: `Supplied attributes match rules defined in allocation "${allocation.key}" and ${subjectKey} belongs to the range of traffic assigned to "${split.variationKey}".`,
};
}
if (hasDefinedRules && !isExperimentOrPartialRollout) {
return `Supplied attributes match rules defined in allocation "${allocation.key}".`;
return {
flagEvaluationCode: 'MATCH',
flagEvaluationDescription: `Supplied attributes match rules defined in allocation "${allocation.key}".`,
};
}
return `${subjectKey} belongs to the range of traffic assigned to "${split.variationKey}" defined in allocation "${allocation.key}".`;
return {
flagEvaluationCode: 'MATCH',
flagEvaluationDescription: `${subjectKey} belongs to the range of traffic assigned to "${split.variationKey}" defined in allocation "${allocation.key}".`,
};
};
}

Expand Down
Loading
Loading