-
Notifications
You must be signed in to change notification settings - Fork 42
feat: add OTel event creation util func #325
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
toddbaert
merged 13 commits into
open-feature:main
from
bbland1:bbland1/322-add-telemetry-utils
Feb 27, 2025
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4e62d2b
feat:a base idea of telemetry util after looking at js version
bbland1 b567a33
style: cleaning up comments
bbland1 cea1d82
feat: version and reason ser for the eval event, passing first test
bbland1 ca366cb
test: writing unit tests for flag metadata and with variant
bbland1 040e2ab
fix: flag metadata not being properly set causing failing variant test
bbland1 91d8bde
test: writing test for unknown reason check and with errors
bbland1 8b7039d
Merge branch 'main' into bbland1/322-add-telemetry-utils
beeme1mr c3d2da6
test: adding tests for General error code and test specific error cod…
bbland1 1f326b7
refactor: changing variable that is never used caught by the linter t…
bbland1 70ee059
Merge branch 'main' into bbland1/322-add-telemetry-utils
bbland1 78afe2b
refactor: moved telemetry to own package & updated some types for rea…
bbland1 2e8a4be
fix: put the unused const values in the proper attributes
bbland1 887052d
Merge branch 'main' into bbland1/322-add-telemetry-utils
toddbaert 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,95 @@ | ||
package openfeature | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type EvaluationEvent struct { | ||
Name string | ||
Attributes map[string]interface{} | ||
Body map[string]interface{} | ||
bbland1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const ( | ||
// The OpenTelemetry compliant event attributes for flag evaluation. | ||
// Specification: https://opentelemetry.io/docs/specs/semconv/feature-flags/feature-flags-logs/ | ||
|
||
TelemetryKey string = "feature_flag.key" | ||
TelemetryErrorCode string = "error.type" | ||
TelemetryVariant string = "feature_flag.variant" | ||
TelemetryContextID string = "feature_flag.context.id" | ||
TelemetryErrorMsg string = "feature_flag.evaluation.error.message" | ||
TelemetryReason string = "feature_flag.evaluation.reason" | ||
TelemetryProvider string = "feature_flag.provider_name" | ||
TelemetryFlagSetID string = "feature_flag.set.id" | ||
TelemetryVersion string = "feature_flag.version" | ||
|
||
|
||
// Well-known flag metadata attributes for telemetry events. | ||
// Specification: https://openfeature.dev/specification/appendix-d#flag-metadata | ||
TelemetryFlagMetaContextId string = "contextId" | ||
TelemetryFlagMetaFlagSetId string = "flagSetId" | ||
TelemetryFlagMetaVersion string = "version" | ||
|
||
// OpenTelemetry event body. | ||
// Specification: https://opentelemetry.io/docs/specs/semconv/feature-flags/feature-flags-logs/ | ||
TelemetryBody string = "value" | ||
|
||
FlagEvaluationEventName string = "feature_flag.evaluation" | ||
) | ||
|
||
func CreateEvaluationEvent(hookContext HookContext, details InterfaceEvaluationDetails) EvaluationEvent { | ||
bbland1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attributes := map[string]interface{}{ | ||
bbland1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TelemetryKey: hookContext.flagKey, | ||
TelemetryProvider: hookContext.providerMetadata.Name, | ||
} | ||
|
||
if details.EvaluationDetails.ResolutionDetail.Reason != "" { | ||
attributes[TelemetryReason] = strings.ToLower(string(details.ResolutionDetail.Reason)) | ||
} else { | ||
attributes[TelemetryReason] = strings.ToLower(string(UnknownReason)) | ||
} | ||
|
||
body := map[string]interface{}{} | ||
bbland1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if details.Variant != "" { | ||
attributes[TelemetryVariant] = details.EvaluationDetails.ResolutionDetail.Variant | ||
} else { | ||
body[TelemetryBody] = details.Value | ||
} | ||
|
||
contextID, exists := details.EvaluationDetails.ResolutionDetail.FlagMetadata[TelemetryFlagMetaContextId] | ||
if exists && contextID != "" { | ||
attributes[TelemetryFlagMetaContextId] = contextID | ||
} else { | ||
contextID = hookContext.evaluationContext.targetingKey | ||
} | ||
|
||
setID, exists := details.EvaluationDetails.ResolutionDetail.FlagMetadata[TelemetryFlagMetaFlagSetId] | ||
if exists { | ||
attributes[TelemetryFlagMetaFlagSetId] = setID | ||
} | ||
|
||
version, exists := details.EvaluationDetails.ResolutionDetail.FlagMetadata[TelemetryFlagMetaVersion] | ||
if exists { | ||
attributes[TelemetryFlagMetaVersion] = version | ||
} | ||
|
||
if details.EvaluationDetails.ResolutionDetail.Reason == ErrorReason { | ||
if details.ResolutionDetail.ErrorCode != "" { | ||
attributes[TelemetryErrorCode] = details.ResolutionDetail.ErrorCode | ||
} else { | ||
attributes[TelemetryErrorCode] = GeneralCode | ||
} | ||
|
||
if details.ResolutionDetail.ErrorMessage != "" { | ||
attributes[TelemetryErrorMsg] = details.ResolutionDetail.ErrorMessage | ||
} | ||
} | ||
|
||
return EvaluationEvent{ | ||
Name: FlagEvaluationEventName, | ||
Attributes: attributes, | ||
Body: body, | ||
} | ||
} |
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,255 @@ | ||
package openfeature | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestCreateEvaluationEvent_1_3_1_BasicEvent(t *testing.T) { | ||
flagKey := "test-flag" | ||
mockProviderMetadata := Metadata{ | ||
Name: "test-provider", | ||
} | ||
mockClientMetadata := ClientMetadata{ | ||
domain: "test-client", | ||
} | ||
mockHookContext := HookContext{ | ||
flagKey: flagKey, | ||
flagType: Boolean, | ||
defaultValue: true, | ||
clientMetadata: mockClientMetadata, | ||
providerMetadata: mockProviderMetadata, | ||
} | ||
|
||
mockDetails := InterfaceEvaluationDetails{ | ||
Value: true, | ||
EvaluationDetails: EvaluationDetails{ | ||
FlagKey: flagKey, | ||
FlagType: Boolean, | ||
ResolutionDetail: ResolutionDetail{ | ||
Reason: StaticReason, | ||
FlagMetadata: FlagMetadata{}, | ||
}, | ||
}, | ||
} | ||
|
||
event := CreateEvaluationEvent(mockHookContext, mockDetails) | ||
|
||
if event.Name != "feature_flag.evaluation" { | ||
t.Errorf("Expected event name to be 'feature_flag.evaluation', got '%s'", event.Name) | ||
} | ||
|
||
if event.Attributes[TelemetryKey] != flagKey { | ||
t.Errorf("Expected event attribute 'KEY' to be '%s', got '%s'", flagKey, event.Attributes[TelemetryKey]) | ||
} | ||
|
||
if event.Attributes[TelemetryReason] != strings.ToLower(string(StaticReason)) { | ||
t.Errorf("Expected evaluation reason to be '%s', got '%s'", strings.ToLower(string(StaticReason)), event.Attributes[TelemetryReason]) | ||
} | ||
|
||
if event.Attributes[TelemetryProvider] != "test-provider" { | ||
t.Errorf("Expected provider name to be 'test-provider', got '%s'", event.Attributes[TelemetryProvider]) | ||
} | ||
|
||
if event.Body[TelemetryBody] != true { | ||
t.Errorf("Expected event body 'VALUE' to be 'true', got '%v'", event.Body[TelemetryBody]) | ||
} | ||
} | ||
|
||
func TestCreateEvaluationEvent_1_4_6_WithVariant(t *testing.T) { | ||
flagKey := "test-flag" | ||
mockProviderMetadata := Metadata{ | ||
Name: "test-provider", | ||
} | ||
mockClientMetadata := ClientMetadata{ | ||
domain: "test-client", | ||
} | ||
mockHookContext := HookContext{ | ||
flagKey: flagKey, | ||
flagType: Boolean, | ||
defaultValue: true, | ||
clientMetadata: mockClientMetadata, | ||
providerMetadata: mockProviderMetadata, | ||
} | ||
|
||
mockDetails := InterfaceEvaluationDetails{ | ||
Value: true, | ||
EvaluationDetails: EvaluationDetails{ | ||
FlagKey: flagKey, | ||
FlagType: Boolean, | ||
ResolutionDetail: ResolutionDetail{ | ||
Variant: "true", | ||
}, | ||
}, | ||
} | ||
|
||
event := CreateEvaluationEvent(mockHookContext, mockDetails) | ||
|
||
if event.Name != "feature_flag.evaluation" { | ||
t.Errorf("Expected event name to be 'feature_flag.evaluation', got '%s'", event.Name) | ||
} | ||
|
||
if event.Attributes[TelemetryKey] != flagKey { | ||
t.Errorf("Expected event attribute 'KEY' to be '%s', got '%s'", flagKey, event.Attributes[TelemetryKey]) | ||
} | ||
|
||
if event.Attributes[TelemetryVariant] != "true" { | ||
t.Errorf("Expected event attribute 'VARIANT' to be 'true', got '%s'", event.Attributes[TelemetryVariant]) | ||
} | ||
|
||
} | ||
func TestCreateEvaluationEvent_1_4_14_WithFlagMetaData(t *testing.T) { | ||
flagKey := "test-flag" | ||
mockProviderMetadata := Metadata{ | ||
Name: "test-provider", | ||
} | ||
mockClientMetadata := ClientMetadata{ | ||
domain: "test-client", | ||
} | ||
mockHookContext := HookContext{ | ||
flagKey: flagKey, | ||
flagType: Boolean, | ||
defaultValue: true, | ||
clientMetadata: mockClientMetadata, | ||
providerMetadata: mockProviderMetadata, | ||
} | ||
|
||
mockDetails := InterfaceEvaluationDetails{ | ||
Value: false, | ||
EvaluationDetails: EvaluationDetails{ | ||
FlagKey: flagKey, | ||
FlagType: Boolean, | ||
ResolutionDetail: ResolutionDetail{ | ||
FlagMetadata: FlagMetadata{ | ||
TelemetryFlagMetaFlagSetId: "test-set", | ||
TelemetryFlagMetaContextId: "metadata-context", | ||
TelemetryFlagMetaVersion: "v1.0", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
event := CreateEvaluationEvent(mockHookContext, mockDetails) | ||
|
||
if event.Attributes[TelemetryFlagMetaFlagSetId] != "test-set" { | ||
t.Errorf("Expected 'Flag SetID' in Flag Metadata name to be 'test-set', got '%s'", event.Attributes[TelemetryFlagMetaFlagSetId]) | ||
} | ||
|
||
if event.Attributes[TelemetryFlagMetaContextId] != "metadata-context" { | ||
t.Errorf("Expected 'Flag ContextID' in Flag Metadata name to be 'metadata-context', got '%s'", event.Attributes[TelemetryFlagMetaContextId]) | ||
} | ||
|
||
if event.Attributes[TelemetryFlagMetaVersion] != "v1.0" { | ||
t.Errorf("Expected 'Flag Version' in Flag Metadata name to be 'v1.0', got '%s'", event.Attributes[TelemetryFlagMetaVersion]) | ||
} | ||
} | ||
func TestCreateEvaluationEvent_1_4_8_WithErrors(t *testing.T) { | ||
flagKey := "test-flag" | ||
mockProviderMetadata := Metadata{ | ||
Name: "test-provider", | ||
} | ||
mockClientMetadata := ClientMetadata{ | ||
domain: "test-client", | ||
} | ||
mockHookContext := HookContext{ | ||
flagKey: flagKey, | ||
flagType: Boolean, | ||
defaultValue: true, | ||
clientMetadata: mockClientMetadata, | ||
providerMetadata: mockProviderMetadata, | ||
} | ||
|
||
mockDetails := InterfaceEvaluationDetails{ | ||
Value: false, | ||
EvaluationDetails: EvaluationDetails{ | ||
FlagKey: flagKey, | ||
ResolutionDetail: ResolutionDetail{ | ||
Reason: ErrorReason, | ||
ErrorCode: FlagNotFoundCode, | ||
ErrorMessage: "a test error", | ||
FlagMetadata: FlagMetadata{}, | ||
}, | ||
}, | ||
} | ||
|
||
event := CreateEvaluationEvent(mockHookContext, mockDetails) | ||
|
||
if event.Attributes[TelemetryErrorCode] != FlagNotFoundCode { | ||
t.Errorf("Expected 'ERROR_CODE' to be 'GENERAL', got '%s'", event.Attributes[TelemetryErrorCode]) | ||
} | ||
|
||
if event.Attributes[TelemetryErrorMsg] != "a test error" { | ||
t.Errorf("Expected 'ERROR_MESSAGE' to be 'a test error', got '%s'", event.Attributes[TelemetryErrorMsg]) | ||
} | ||
} | ||
|
||
func TestCreateEvaluationEvent_1_4_8_WithGeneralErrors(t *testing.T) { | ||
flagKey := "test-flag" | ||
mockProviderMetadata := Metadata{ | ||
Name: "test-provider", | ||
} | ||
mockClientMetadata := ClientMetadata{ | ||
domain: "test-client", | ||
} | ||
mockHookContext := HookContext{ | ||
flagKey: flagKey, | ||
flagType: Boolean, | ||
defaultValue: true, | ||
clientMetadata: mockClientMetadata, | ||
providerMetadata: mockProviderMetadata, | ||
} | ||
|
||
mockDetails := InterfaceEvaluationDetails{ | ||
Value: false, | ||
EvaluationDetails: EvaluationDetails{ | ||
FlagKey: flagKey, | ||
ResolutionDetail: ResolutionDetail{ | ||
Reason: ErrorReason, | ||
ErrorMessage: "a test error", | ||
FlagMetadata: FlagMetadata{}, | ||
}, | ||
}, | ||
} | ||
|
||
event := CreateEvaluationEvent(mockHookContext, mockDetails) | ||
|
||
if event.Attributes[TelemetryErrorCode] != GeneralCode { | ||
t.Errorf("Expected 'ERROR_CODE' to be 'GENERAL', got '%s'", event.Attributes[TelemetryErrorCode]) | ||
} | ||
|
||
if event.Attributes[TelemetryErrorMsg] != "a test error" { | ||
t.Errorf("Expected 'ERROR_MESSAGE' to be 'a test error', got '%s'", event.Attributes[TelemetryErrorMsg]) | ||
} | ||
} | ||
func TestCreateEvaluationEvent_1_4_7_WithUnknownReason(t *testing.T) { | ||
flagKey := "test-flag" | ||
mockProviderMetadata := Metadata{ | ||
Name: "test-provider", | ||
} | ||
mockClientMetadata := ClientMetadata{ | ||
domain: "test-client", | ||
} | ||
mockHookContext := HookContext{ | ||
flagKey: flagKey, | ||
flagType: Boolean, | ||
defaultValue: true, | ||
clientMetadata: mockClientMetadata, | ||
providerMetadata: mockProviderMetadata, | ||
} | ||
|
||
mockDetails := InterfaceEvaluationDetails{ | ||
Value: false, | ||
EvaluationDetails: EvaluationDetails{ | ||
FlagKey: flagKey, | ||
ResolutionDetail: ResolutionDetail{ | ||
FlagMetadata: FlagMetadata{}, | ||
}, | ||
}, | ||
} | ||
|
||
event := CreateEvaluationEvent(mockHookContext, mockDetails) | ||
|
||
if event.Attributes[TelemetryReason] != strings.ToLower(string(UnknownReason)) { | ||
t.Errorf("Expected evaluation reason to be '%s', got '%s'", strings.ToLower(string(UnknownReason)), event.Attributes[TelemetryReason]) | ||
} | ||
} |
Oops, something went wrong.
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.