-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add OTEL compatible telemetry object builder #397
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
Merged
askpt
merged 32 commits into
main
from
askpt/381-add-opentelemetry-compatible-telemetry
May 19, 2025
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
00f75fc
feat: Add TelemetryAttributes for OpenTelemetry compliant event logging
askpt c6287eb
feat: Add TelemetryEvaluationData for feature flag event logging
askpt 38a110a
feat: Add TelemetryFlagMetadata for flag metadata attributes
askpt 75330fe
feat: Rename TelemetryAttributes to TelemetryConstants and add Evalua…
askpt 783df4f
feat: Introduce EvaluationEvent class and refactor EvaluationEventFac…
askpt f94d83a
feat: Add EvaluationEventBuilder for creating evaluation events for f…
askpt f6f3d0f
test: Add unit tests for EvaluationEventBuilder to validate event cre…
askpt 30949be
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 5369348
fix: Simplify variant assignment in EvaluationEventBuilder and enhanc…
askpt 2d81ada
fix: Ensure proper handling of missing reason in EvaluationEventBuild…
askpt 8a38d09
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 0d1a869
fix: Normalize error code to lowercase in EvaluationEventBuilder
askpt 2a69a2a
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 81edc80
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt dd7793c
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 26d7dd5
fix: Correct telemetry constant values for feature flag attributes
askpt 6a51440
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 5c11da2
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt b373d16
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 62f3b59
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 6b48487
refactor(tests): streamline EvaluationEventBuilderTests for clarity a…
askpt 48eb669
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 1b984c6
Removed Body.
askpt 5c97687
docs: update XML documentation for TelemetryFlagMetadata class
askpt ac2d968
feat: update telemetry attributes to include evaluated value and refa…
askpt 789312e
feat: enhance EvaluationEventBuilder to handle empty error messages a…
askpt 62d8b5e
feat: refactor EvaluationEventBuilder to improve handling of flag met…
askpt 0cf35e2
feat: normalize Reason attribute to lowercase in EvaluationEventBuilder
askpt 334b8e4
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt 11ca4ce
Merge branch 'main' into askpt/381-add-opentelemetry-compatible-telem…
askpt f91851d
refactor: change attributes type to IDictionary and make properties r…
askpt b7d34fb
refactor: change EvaluationEventBuilder to sealed and update Build me…
askpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace OpenFeature.Telemetry; | ||
|
|
||
| /// <summary> | ||
| /// Represents an evaluation event for feature flags. | ||
| /// </summary> | ||
| public class EvaluationEvent | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="EvaluationEvent"/> class. | ||
| /// </summary> | ||
| /// <param name="name">The name of the event.</param> | ||
| /// <param name="attributes">The attributes of the event.</param> | ||
| public EvaluationEvent(string name, IDictionary<string, object?> attributes) | ||
| { | ||
| Name = name; | ||
| Attributes = new Dictionary<string, object?>(attributes); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the event. | ||
| /// </summary> | ||
| public string Name { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the attributes of the event. | ||
| /// </summary> | ||
| public IReadOnlyDictionary<string, object?> Attributes { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using System.Collections.Generic; | ||
| using OpenFeature.Constant; | ||
| using OpenFeature.Model; | ||
|
|
||
| namespace OpenFeature.Telemetry; | ||
|
|
||
| /// <summary> | ||
| /// Class for creating evaluation events for feature flags. | ||
| /// </summary> | ||
| public sealed class EvaluationEventBuilder | ||
| { | ||
| private const string EventName = "feature_flag.evaluation"; | ||
|
|
||
| /// <summary> | ||
| /// Gets the default instance of the <see cref="EvaluationEventBuilder"/>. | ||
| /// </summary> | ||
| public static EvaluationEventBuilder Default { get; } = new(); | ||
|
|
||
| /// <summary> | ||
| /// Creates an evaluation event based on the provided hook context and flag evaluation details. | ||
| /// </summary> | ||
| /// <param name="hookContext">The context of the hook containing flag key and provider metadata.</param> | ||
| /// <param name="details">The details of the flag evaluation including reason, variant, and metadata.</param> | ||
| /// <returns>An instance of <see cref="EvaluationEvent"/> containing the event name, attributes, and body.</returns> | ||
| public EvaluationEvent Build(HookContext<Value> hookContext, FlagEvaluationDetails<Value> details) | ||
| { | ||
| var attributes = new Dictionary<string, object?> | ||
| { | ||
| { TelemetryConstants.Key, hookContext.FlagKey }, | ||
| { TelemetryConstants.Provider, hookContext.ProviderMetadata.Name } | ||
| }; | ||
|
|
||
| attributes[TelemetryConstants.Reason] = !string.IsNullOrWhiteSpace(details.Reason) | ||
| ? details.Reason?.ToLowerInvariant() | ||
| : Reason.Unknown.ToLowerInvariant(); | ||
| attributes[TelemetryConstants.Variant] = details.Variant; | ||
| attributes[TelemetryConstants.Value] = details.Value; | ||
|
|
||
| if (details.FlagMetadata != null) | ||
| { | ||
| attributes[TelemetryConstants.ContextId] = details.FlagMetadata.GetString(TelemetryFlagMetadata.ContextId); | ||
| attributes[TelemetryConstants.FlagSetId] = details.FlagMetadata.GetString(TelemetryFlagMetadata.FlagSetId); | ||
| attributes[TelemetryConstants.Version] = details.FlagMetadata.GetString(TelemetryFlagMetadata.Version); | ||
| } | ||
|
|
||
| if (details.ErrorType != ErrorType.None) | ||
| { | ||
| attributes[TelemetryConstants.ErrorCode] = details.ErrorType.ToString().ToLowerInvariant(); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(details.ErrorMessage)) | ||
| { | ||
| attributes[TelemetryConstants.ErrorMessage] = details.ErrorMessage; | ||
| } | ||
| } | ||
|
|
||
| return new EvaluationEvent(EventName, attributes); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| namespace OpenFeature.Telemetry; | ||
|
|
||
| /// <summary> | ||
| /// The attributes of an OpenTelemetry compliant event for flag evaluation. | ||
| /// <see href="https://opentelemetry.io/docs/specs/semconv/feature-flags/feature-flags-logs/"/> | ||
| /// </summary> | ||
| public static class TelemetryConstants | ||
| { | ||
| /// <summary> | ||
| /// The lookup key of the feature flag. | ||
| /// </summary> | ||
| public const string Key = "feature_flag.key"; | ||
|
|
||
| /// <summary> | ||
| /// Describes a class of error the operation ended with. | ||
| /// </summary> | ||
| public const string ErrorCode = "error.type"; | ||
|
|
||
| /// <summary> | ||
| /// A message explaining the nature of an error occurring during flag evaluation. | ||
| /// </summary> | ||
| public const string ErrorMessage = "error.message"; | ||
|
|
||
| /// <summary> | ||
| /// A semantic identifier for an evaluated flag value. | ||
| /// </summary> | ||
| public const string Variant = "feature_flag.result.variant"; | ||
|
|
||
| /// <summary> | ||
| /// The evaluated value of the feature flag. | ||
| /// </summary> | ||
| public const string Value = "feature_flag.result.value"; | ||
|
|
||
| /// <summary> | ||
| /// The unique identifier for the flag evaluation context. For example, the targeting key. | ||
| /// </summary> | ||
| public const string ContextId = "feature_flag.context.id"; | ||
|
|
||
| /// <summary> | ||
| /// The reason code which shows how a feature flag value was determined. | ||
| /// </summary> | ||
| public const string Reason = "feature_flag.result.reason"; | ||
|
|
||
| /// <summary> | ||
| /// Describes a class of error the operation ended with. | ||
| /// </summary> | ||
| public const string Provider = "feature_flag.provider.name"; | ||
|
|
||
| /// <summary> | ||
| /// The identifier of the flag set to which the feature flag belongs. | ||
| /// </summary> | ||
| public const string FlagSetId = "feature_flag.set.id"; | ||
|
|
||
| /// <summary> | ||
| /// The version of the ruleset used during the evaluation. This may be any stable value which uniquely identifies the ruleset. | ||
| /// </summary> | ||
| public const string Version = "feature_flag.version"; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| namespace OpenFeature.Telemetry; | ||
|
|
||
| /// <summary> | ||
| /// Well-known flag metadata attributes for telemetry events. | ||
| /// <remarks>See also: https://openfeature.dev/specification/appendix-d#flag-metadata</remarks> | ||
| /// </summary> | ||
| public static class TelemetryFlagMetadata | ||
| { | ||
| /// <summary> | ||
| /// The context identifier returned in the flag metadata uniquely identifies | ||
| /// the subject of the flag evaluation. If not available, the targeting key | ||
| /// should be used. | ||
| /// </summary> | ||
| public const string ContextId = "contextId"; | ||
|
|
||
| /// <summary> | ||
| /// ///A logical identifier for the flag set. | ||
| /// </summary> | ||
| public const string FlagSetId = "flagSetId"; | ||
|
|
||
| /// <summary> | ||
| /// A version string (format unspecified) for the flag or flag set. | ||
| /// </summary> | ||
| public const string Version = "version"; | ||
| } |
174 changes: 174 additions & 0 deletions
174
test/OpenFeature.Tests/Telemetry/EvaluationEventBuilderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| using System.Collections.Generic; | ||
| using OpenFeature.Constant; | ||
| using OpenFeature.Model; | ||
| using OpenFeature.Telemetry; | ||
| using Xunit; | ||
|
|
||
| namespace OpenFeature.Tests.Telemetry; | ||
|
|
||
| public class EvaluationEventBuilderTests | ||
| { | ||
| private readonly EvaluationEventBuilder _builder = EvaluationEventBuilder.Default; | ||
|
|
||
| [Fact] | ||
| public void Build_ShouldReturnEventWithCorrectAttributes() | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value(), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var metadata = new Dictionary<string, object> | ||
| { | ||
| { "flagSetId", "flagSetId" }, { "contextId", "contextId" }, { "version", "version" } | ||
| }; | ||
| var flagMetadata = new ImmutableMetadata(metadata); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.None, | ||
| reason: "reason", variant: "variant", flagMetadata: flagMetadata); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Equal("feature_flag.evaluation", evaluationEvent.Name); | ||
| Assert.Equal("flagKey", evaluationEvent.Attributes[TelemetryConstants.Key]); | ||
| Assert.Equal("provider", evaluationEvent.Attributes[TelemetryConstants.Provider]); | ||
| Assert.Equal("reason", evaluationEvent.Attributes[TelemetryConstants.Reason]); | ||
| Assert.Equal("variant", evaluationEvent.Attributes[TelemetryConstants.Variant]); | ||
| Assert.Equal("contextId", evaluationEvent.Attributes[TelemetryConstants.ContextId]); | ||
| Assert.Equal("flagSetId", evaluationEvent.Attributes[TelemetryConstants.FlagSetId]); | ||
| Assert.Equal("version", evaluationEvent.Attributes[TelemetryConstants.Version]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Build_ShouldHandleErrorDetails() | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value(), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var metadata = new Dictionary<string, object> | ||
| { | ||
| { "flagSetId", "flagSetId" }, { "contextId", "contextId" }, { "version", "version" } | ||
| }; | ||
| var flagMetadata = new ImmutableMetadata(metadata); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.General, | ||
| errorMessage: "errorMessage", reason: "reason", variant: "variant", flagMetadata: flagMetadata); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Equal("general", evaluationEvent.Attributes[TelemetryConstants.ErrorCode]); | ||
| Assert.Equal("errorMessage", evaluationEvent.Attributes[TelemetryConstants.ErrorMessage]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Build_ShouldHandleMissingVariant() | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value("value"), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var metadata = new Dictionary<string, object> | ||
| { | ||
| { "flagSetId", "flagSetId" }, { "contextId", "contextId" }, { "version", "version" } | ||
| }; | ||
| var flagMetadata = new ImmutableMetadata(metadata); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.None, | ||
| reason: "reason", variant: null, flagMetadata: flagMetadata); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Null(evaluationEvent.Attributes[TelemetryConstants.Variant]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Build_ShouldHandleMissingFlagMetadata() | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value("value"), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var flagMetadata = new ImmutableMetadata(); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.None, | ||
| reason: "reason", variant: "", flagMetadata: flagMetadata); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Null(evaluationEvent.Attributes[TelemetryConstants.ContextId]); | ||
| Assert.Null(evaluationEvent.Attributes[TelemetryConstants.FlagSetId]); | ||
| Assert.Null(evaluationEvent.Attributes[TelemetryConstants.Version]); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| public void Build_ShouldHandleMissingReason(string? reason) | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value("value"), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var flagMetadata = new ImmutableMetadata(); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.None, | ||
| reason: reason, variant: "", flagMetadata: flagMetadata); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Equal(Reason.Unknown.ToLowerInvariant(), evaluationEvent.Attributes[TelemetryConstants.Reason]); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| public void Build_ShouldHandleErrorWithEmptyErrorMessage(string? errorMessage) | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value("value"), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var flagMetadata = new ImmutableMetadata(); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", new Value("value"), ErrorType.General, | ||
| errorMessage: errorMessage, reason: "reason", variant: "", flagMetadata: flagMetadata); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Equal("general", evaluationEvent.Attributes[TelemetryConstants.ErrorCode]); | ||
| Assert.False(evaluationEvent.Attributes.ContainsKey(TelemetryConstants.ErrorMessage)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Build_ShouldIncludeValueAttributeInEvent() | ||
| { | ||
| // Arrange | ||
| var clientMetadata = new ClientMetadata("client", "1.0.0"); | ||
| var providerMetadata = new Metadata("provider"); | ||
| var hookContext = new HookContext<Value>("flagKey", new Value(), FlagValueType.Object, clientMetadata, | ||
| providerMetadata, EvaluationContext.Empty); | ||
| var testValue = new Value("test-value"); | ||
| var details = new FlagEvaluationDetails<Value>("flagKey", testValue, ErrorType.None, | ||
| reason: "reason", variant: "variant", flagMetadata: new ImmutableMetadata()); | ||
|
|
||
| // Act | ||
| var evaluationEvent = _builder.Build(hookContext, details); | ||
|
|
||
| // Assert | ||
| Assert.Equal(testValue, evaluationEvent.Attributes[TelemetryConstants.Value]); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.