Skip to content

feat(go-feature-flag): Support exporter metadata #45

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
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def evaluate(flag_key:, default_value:, allowed_classes:, evaluation_context: ni
evaluation_context = OpenFeature::SDK::EvaluationContext.new if evaluation_context.nil?
validate_parameters(flag_key, evaluation_context)

# enrich the evaluation context with the exporter_metadata
@options.exporter_metadata ||= {}
@options.exporter_metadata.merge!({"openfeature" => true, "provider" => "ruby"})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are default exporter_metadata values that are added for every evaluation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I need them in the GO Feature Flag server, and since I am using the OFREP API and I have no other fields than the context to add those extra informations.

Copy link
Member Author

@thomaspoignant thomaspoignant Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have put it as a header, but the problem is the limit in header size that we may reach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. That's a clever workaround 😄

evaluation_context.fields["gofeatureflag"] = {"exporterMetadata" => @options.exporter_metadata}

# do a http call to the go feature flag server
parsed_response = @goff_api.evaluate_ofrep_api(flag_key: flag_key, evaluation_context: evaluation_context)
parsed_response = OfrepApiResponse unless parsed_response.is_a?(OfrepApiResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ module OpenFeature
module GoFeatureFlag
# This class is the configuration class for the GoFeatureFlagProvider
class Options
attr_accessor :endpoint, :custom_headers
attr_accessor :endpoint, :custom_headers, :exporter_metadata

def initialize(endpoint: nil, headers: {})
def initialize(endpoint: nil, headers: {}, exporter_metadata: nil)
validate_endpoint(endpoint: endpoint)
@endpoint = endpoint
@custom_headers = headers
@exporter_metadata = exporter_metadata
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,41 @@
expect(eval).to eql(want)
end
end

context "#exporter_metadata" do
it "should send exporter_metadata in the API if set in provider" do
test_name = RSpec.current_example.description
request_body = nil
stub_request(:post, "http://localhost:1031/ofrep/v1/evaluate/flags/boolean_flag")
.with { |req| request_body = req.body }
.to_return(status: 200, body:
{
key: "double_key",
metadata: {"website" => "https://gofeatureflag.org"},
value: true,
reason: "TARGETING_MATCH",
variant: "variantA"
}.to_json)

options = OpenFeature::GoFeatureFlag::Options.new(endpoint: "http://localhost:1031", exporter_metadata: {
"key1" => "value1",
"key2" => 123,
"key3" => 123.45
})
goff_test_provider = OpenFeature::GoFeatureFlag::Provider.new(options: options)

OpenFeature::SDK.configure do |config|
config.set_provider(goff_test_provider, domain: test_name)
end
client = OpenFeature::SDK.build_client(domain: test_name)
client.fetch_boolean_details(
flag_key: "boolean_flag",
default_value: false,
evaluation_context: OpenFeature::SDK::EvaluationContext.new(targeting_key: "9b9450f8-ab5c-4dcf-872f-feda3f6ccb16")
)
got = JSON.parse(request_body)
want = JSON.parse('{"context":{"gofeatureflag":{"exporterMetadata":{"key1":"value1","openfeature":true,"provider":"ruby", "key2": 123, "key3":123.45}},"targetingKey":"9b9450f8-ab5c-4dcf-872f-feda3f6ccb16"}}')
expect(got).to eql(want)
end
end
end
Loading