Skip to content
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

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

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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,10 @@ 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.merge!("openfeature" => true, "provider" => "ruby")
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: {})
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
thomaspoignant marked this conversation as resolved.
Show resolved Hide resolved
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)
thomaspoignant marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Loading