Skip to content

Commit 2d81ada

Browse files
committed
fix: Ensure proper handling of missing reason in EvaluationEventBuilder and add corresponding unit tests
Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
1 parent 5369348 commit 2d81ada

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/OpenFeature/Telemetry/EvaluationEventBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public static EvaluationEvent Build(HookContext<Value> hookContext, FlagEvaluati
2525
{ TelemetryConstants.Provider, hookContext.ProviderMetadata.Name }
2626
};
2727

28-
attributes[TelemetryConstants.Reason] = !string.IsNullOrWhiteSpace(details.Reason) ? details.Reason?.ToLowerInvariant() : Reason.Unknown;
2928

3029
var body = new Dictionary<string, object>();
3130

31+
attributes[TelemetryConstants.Reason] = !string.IsNullOrWhiteSpace(details.Reason) ? details.Reason?.ToLowerInvariant() : Reason.Unknown;
3232
attributes[TelemetryConstants.Variant] = details.Variant;
3333
attributes[TelemetryFlagMetadata.ContextId] = details.FlagMetadata?.GetString(TelemetryFlagMetadata.ContextId);
3434
attributes[TelemetryFlagMetadata.FlagSetId] = details.FlagMetadata?.GetString(TelemetryFlagMetadata.FlagSetId);

test/OpenFeature.Tests/Telemetry/EvaluationEventBuilderTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,27 @@ public void Build_ShouldHandleMissingFlagMetadata()
105105
Assert.Null(evaluationEvent.Attributes[TelemetryFlagMetadata.FlagSetId]);
106106
Assert.Null(evaluationEvent.Attributes[TelemetryFlagMetadata.Version]);
107107
}
108+
109+
[Theory]
110+
[InlineData(null)]
111+
[InlineData("")]
112+
[InlineData(" ")]
113+
public void Build_ShouldHandleMissingReason(string? reason)
114+
{
115+
// Arrange
116+
var clientMetadata = new ClientMetadata("client", "1.0.0");
117+
var providerMetadata = new Metadata("provider");
118+
var hookContext = new HookContext<Value>("flagKey", new Value("value"), FlagValueType.Object, clientMetadata,
119+
providerMetadata, EvaluationContext.Empty);
120+
var flagMetadata = new ImmutableMetadata();
121+
var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.None,
122+
reason: reason, variant: "", flagMetadata: flagMetadata);
123+
124+
// Act
125+
var evaluationEvent = EvaluationEventBuilder.Build(hookContext, details);
126+
127+
// Assert
128+
Assert.Equal(Reason.Unknown, evaluationEvent.Attributes[TelemetryConstants.Reason]);
129+
}
108130
}
109131
}

0 commit comments

Comments
 (0)