Skip to content

Commit

Permalink
feat(go-feature-flag): Support exporter metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
  • Loading branch information
thomaspoignant committed Jan 24, 2025
1 parent 4a747bb commit c58f3ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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"})
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

0 comments on commit c58f3ab

Please sign in to comment.