|
| 1 | +from openfeature.evaluation_context import EvaluationContext |
| 2 | +from openfeature.exception import ErrorCode |
| 3 | +from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType, Reason |
| 4 | +from openfeature.hook import HookContext |
| 5 | +from openfeature.provider import Metadata |
| 6 | +from openfeature.telemetry import ( |
| 7 | + TelemetryAttribute, |
| 8 | + TelemetryBodyField, |
| 9 | + TelemetryFlagMetadata, |
| 10 | + create_evaluation_event, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def test_create_evaluation_event(): |
| 15 | + # given |
| 16 | + hook_context = HookContext( |
| 17 | + flag_key="flag_key", |
| 18 | + flag_type=FlagType.BOOLEAN, |
| 19 | + default_value=True, |
| 20 | + evaluation_context=EvaluationContext(), |
| 21 | + provider_metadata=Metadata(name="test_provider"), |
| 22 | + ) |
| 23 | + details = FlagEvaluationDetails( |
| 24 | + flag_key=hook_context.flag_key, |
| 25 | + value=False, |
| 26 | + reason=Reason.CACHED, |
| 27 | + ) |
| 28 | + |
| 29 | + # when |
| 30 | + event = create_evaluation_event(hook_context=hook_context, details=details) |
| 31 | + |
| 32 | + # then |
| 33 | + assert event.name == "feature_flag.evaluation" |
| 34 | + assert event.attributes[TelemetryAttribute.KEY] == "flag_key" |
| 35 | + assert event.attributes[TelemetryAttribute.EVALUATION_REASON] == "cached" |
| 36 | + assert event.attributes[TelemetryAttribute.PROVIDER_NAME] == "test_provider" |
| 37 | + assert event.body[TelemetryBodyField.VALUE] is False |
| 38 | + |
| 39 | + |
| 40 | +def test_create_evaluation_event_with_variant(): |
| 41 | + # given |
| 42 | + hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, EvaluationContext()) |
| 43 | + details = FlagEvaluationDetails( |
| 44 | + flag_key=hook_context.flag_key, |
| 45 | + value=True, |
| 46 | + variant="true", |
| 47 | + ) |
| 48 | + |
| 49 | + # when |
| 50 | + event = create_evaluation_event(hook_context=hook_context, details=details) |
| 51 | + |
| 52 | + # then |
| 53 | + assert event.name == "feature_flag.evaluation" |
| 54 | + assert event.attributes[TelemetryAttribute.KEY] == "flag_key" |
| 55 | + assert event.attributes[TelemetryAttribute.VARIANT] == "true" |
| 56 | + assert event.attributes[TelemetryAttribute.EVALUATION_REASON] == "unknown" |
| 57 | + |
| 58 | + |
| 59 | +def test_create_evaluation_event_with_metadata(): |
| 60 | + # given |
| 61 | + hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, EvaluationContext()) |
| 62 | + details = FlagEvaluationDetails( |
| 63 | + flag_key=hook_context.flag_key, |
| 64 | + value=False, |
| 65 | + flag_metadata={ |
| 66 | + TelemetryFlagMetadata.CONTEXT_ID: "5157782b-2203-4c80-a857-dbbd5e7761db", |
| 67 | + TelemetryFlagMetadata.FLAG_SET_ID: "proj-1", |
| 68 | + TelemetryFlagMetadata.VERSION: "v1", |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + # when |
| 73 | + event = create_evaluation_event(hook_context=hook_context, details=details) |
| 74 | + |
| 75 | + # then |
| 76 | + assert ( |
| 77 | + event.attributes[TelemetryAttribute.CONTEXT_ID] |
| 78 | + == "5157782b-2203-4c80-a857-dbbd5e7761db" |
| 79 | + ) |
| 80 | + assert event.attributes[TelemetryAttribute.SET_ID] == "proj-1" |
| 81 | + assert event.attributes[TelemetryAttribute.VERSION] == "v1" |
| 82 | + |
| 83 | + |
| 84 | +def test_create_evaluation_event_with_error(): |
| 85 | + # given |
| 86 | + hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, EvaluationContext()) |
| 87 | + details = FlagEvaluationDetails( |
| 88 | + flag_key=hook_context.flag_key, |
| 89 | + value=False, |
| 90 | + reason=Reason.ERROR, |
| 91 | + error_code=ErrorCode.FLAG_NOT_FOUND, |
| 92 | + error_message="flag error", |
| 93 | + ) |
| 94 | + |
| 95 | + # when |
| 96 | + event = create_evaluation_event(hook_context=hook_context, details=details) |
| 97 | + |
| 98 | + # then |
| 99 | + assert event.attributes[TelemetryAttribute.EVALUATION_REASON] == "error" |
| 100 | + assert event.attributes[TelemetryAttribute.ERROR_TYPE] == "flag_not_found" |
| 101 | + assert event.attributes[TelemetryAttribute.EVALUATION_ERROR_MESSAGE] == "flag error" |
0 commit comments