Skip to content

Commit 04754fd

Browse files
feat(go-feature-flag): Support exporter metadata (#45)
## This PR In this PR we support `exporterMetadata` that will allow to add static information that will be added to your feature event sent to the GO Feature Flag exporter. In this PR we add those exporter metadata to the evaluation context in order to pass them to GO Feature Flag relay-proxy. ### Related Issues thomaspoignant/go-feature-flag#2983 --------- Signed-off-by: Thomas Poignant <[email protected]>
1 parent 4a747bb commit 04754fd

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/go_feature_flag_provider.rb

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def evaluate(flag_key:, default_value:, allowed_classes:, evaluation_context: ni
3535
evaluation_context = OpenFeature::SDK::EvaluationContext.new if evaluation_context.nil?
3636
validate_parameters(flag_key, evaluation_context)
3737

38+
# enrich the evaluation context with the exporter_metadata
39+
@options.exporter_metadata.merge!("openfeature" => true, "provider" => "ruby")
40+
evaluation_context.fields["gofeatureflag"] = {"exporterMetadata" => @options.exporter_metadata}
41+
3842
# do a http call to the go feature flag server
3943
parsed_response = @goff_api.evaluate_ofrep_api(flag_key: flag_key, evaluation_context: evaluation_context)
4044
parsed_response = OfrepApiResponse unless parsed_response.is_a?(OfrepApiResponse)

providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/options.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ module OpenFeature
66
module GoFeatureFlag
77
# This class is the configuration class for the GoFeatureFlagProvider
88
class Options
9-
attr_accessor :endpoint, :custom_headers
9+
attr_accessor :endpoint, :custom_headers, :exporter_metadata
1010

11-
def initialize(endpoint: nil, headers: {})
11+
def initialize(endpoint: nil, headers: {}, exporter_metadata: {})
1212
validate_endpoint(endpoint: endpoint)
1313
@endpoint = endpoint
1414
@custom_headers = headers
15+
@exporter_metadata = exporter_metadata
1516
end
1617

1718
private

providers/openfeature-go-feature-flag-provider/spec/openfeature/gofeatureflag/provider_spec.rb

+37
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,41 @@
684684
expect(eval).to eql(want)
685685
end
686686
end
687+
688+
context "#exporter_metadata" do
689+
it "should send exporter_metadata in the API if set in provider" do
690+
test_name = RSpec.current_example.description
691+
request_body = nil
692+
stub_request(:post, "http://localhost:1031/ofrep/v1/evaluate/flags/boolean_flag")
693+
.with { |req| request_body = req.body }
694+
.to_return(status: 200, body:
695+
{
696+
key: "double_key",
697+
metadata: {"website" => "https://gofeatureflag.org"},
698+
value: true,
699+
reason: "TARGETING_MATCH",
700+
variant: "variantA"
701+
}.to_json)
702+
703+
options = OpenFeature::GoFeatureFlag::Options.new(endpoint: "http://localhost:1031", exporter_metadata: {
704+
"key1" => "value1",
705+
"key2" => 123,
706+
"key3" => 123.45
707+
})
708+
goff_test_provider = OpenFeature::GoFeatureFlag::Provider.new(options: options)
709+
710+
OpenFeature::SDK.configure do |config|
711+
config.set_provider(goff_test_provider, domain: test_name)
712+
end
713+
client = OpenFeature::SDK.build_client(domain: test_name)
714+
client.fetch_boolean_details(
715+
flag_key: "boolean_flag",
716+
default_value: false,
717+
evaluation_context: OpenFeature::SDK::EvaluationContext.new(targeting_key: "9b9450f8-ab5c-4dcf-872f-feda3f6ccb16")
718+
)
719+
got = JSON.parse(request_body)
720+
want = JSON.parse('{"context":{"gofeatureflag":{"exporterMetadata":{"key1":"value1","openfeature":true,"provider":"ruby", "key2": 123, "key3":123.45}},"targetingKey":"9b9450f8-ab5c-4dcf-872f-feda3f6ccb16"}}')
721+
expect(got).to eql(want)
722+
end
723+
end
687724
end

0 commit comments

Comments
 (0)