From 388b267017fe66e36f1c6eff44a4d174b0ae2d81 Mon Sep 17 00:00:00 2001 From: avelicka Date: Fri, 31 Oct 2025 18:36:23 +0200 Subject: [PATCH] feat: add Faraday instrumentation Signed-off-by: avelicka --- .../openfeature-go-feature-flag-provider/README.md | 10 +++++----- .../go-feature-flag/go_feature_flag_provider.rb | 2 +- .../lib/openfeature/go-feature-flag/goff_api.rb | 3 ++- .../lib/openfeature/go-feature-flag/options.rb | 13 +++++++++++-- .../spec/openfeature/gofeatureflag/options_spec.rb | 9 +++++++++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/providers/openfeature-go-feature-flag-provider/README.md b/providers/openfeature-go-feature-flag-provider/README.md index 274a9fb..cbe33c5 100644 --- a/providers/openfeature-go-feature-flag-provider/README.md +++ b/providers/openfeature-go-feature-flag-provider/README.md @@ -45,11 +45,11 @@ gem install openfeature-go-feature-flag-provider The `OpenFeature::GoFeatureFlag::Provider` needs some options to be created and then set in the OpenFeature SDK. -| **Option** | **Description** | -|------------|---------------------------------------------------------------------------------------------------------------------------------------------| -| `endpoint` | **(mandatory)** The URL to access to the relay-proxy.
*(example: `https://relay.proxy.gofeatureflag.org/`)* | -| `headers` | A `Hash` object containing the headers to send to the relay-proxy.
*(example to send APIKey: `{"Authorization" => "Bearer my-api-key"}` | - +| **Option** | **Description** | +|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------| +| `endpoint` | **(mandatory)** The URL to access to the relay-proxy.
*(example: `https://relay.proxy.gofeatureflag.org/`)* | +| `headers` | A `Hash` object containing the headers to send to the relay-proxy.
*(example to send APIKey: `{"Authorization" => "Bearer my-api-key"}` | +| `instrumentation` | [Faraday instrumentation](https://github.com/lostisland/faraday/blob/main/docs/middleware/included/instrumentation.md) hash | The only required option to create a `GoFeatureFlagProvider` is the URL _(`endpoint`)_ to your GO Feature Flag relay-proxy instance. ```ruby diff --git a/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/go_feature_flag_provider.rb b/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/go_feature_flag_provider.rb index cb92266..7fbbc72 100644 --- a/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/go_feature_flag_provider.rb +++ b/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/go_feature_flag_provider.rb @@ -10,7 +10,7 @@ class Provider def initialize(options: Options.new) @metadata = SDK::Provider::ProviderMetadata.new(name: PROVIDER_NAME) @options = options - @goff_api = GoFeatureFlagApi.new(endpoint: options.endpoint, custom_headers: options.custom_headers) + @goff_api = GoFeatureFlagApi.new(endpoint: options.endpoint, custom_headers: options.custom_headers, instrumentation: options.instrumentation) end def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil) diff --git a/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/goff_api.rb b/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/goff_api.rb index 40f7586..67dc0dc 100644 --- a/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/goff_api.rb +++ b/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/goff_api.rb @@ -11,8 +11,9 @@ module OpenFeature module GoFeatureFlag # This class is the entry point for the GoFeatureFlagProvider class GoFeatureFlagApi - def initialize(endpoint: nil, custom_headers: nil) + def initialize(endpoint: nil, custom_headers: nil, instrumentation: nil) @faraday_connection = Faraday.new(url: endpoint, headers: headers(custom_headers)) do |f| + f.request :instrumentation, instrumentation if instrumentation f.adapter :net_http_persistent do |http| http.idle_timeout = 30 end diff --git a/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/options.rb b/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/options.rb index 823fe7b..309ee2a 100644 --- a/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/options.rb +++ b/providers/openfeature-go-feature-flag-provider/lib/openfeature/go-feature-flag/options.rb @@ -6,13 +6,15 @@ module OpenFeature module GoFeatureFlag # This class is the configuration class for the GoFeatureFlagProvider class Options - attr_accessor :endpoint, :custom_headers, :exporter_metadata + attr_accessor :endpoint, :custom_headers, :exporter_metadata, :instrumentation - def initialize(endpoint: nil, headers: {}, exporter_metadata: {}) + def initialize(endpoint: nil, headers: {}, exporter_metadata: {}, instrumentation: nil) validate_endpoint(endpoint: endpoint) + validate_instrumentation(instrumentation: instrumentation) @endpoint = endpoint @custom_headers = headers @exporter_metadata = exporter_metadata + @instrumentation = instrumentation end private @@ -25,6 +27,13 @@ def validate_endpoint(endpoint: nil) rescue URI::InvalidURIError raise ArgumentError, "Invalid URL for endpoint: #{endpoint}" end + + def validate_instrumentation(instrumentation: nil) + return if instrumentation.nil? + return if instrumentation.is_a?(Hash) + + raise ArgumentError, "Invalid type for instrumentation: #{instrumentation.class}" + end end end end diff --git a/providers/openfeature-go-feature-flag-provider/spec/openfeature/gofeatureflag/options_spec.rb b/providers/openfeature-go-feature-flag-provider/spec/openfeature/gofeatureflag/options_spec.rb index 46e55e9..3fac4ef 100644 --- a/providers/openfeature-go-feature-flag-provider/spec/openfeature/gofeatureflag/options_spec.rb +++ b/providers/openfeature-go-feature-flag-provider/spec/openfeature/gofeatureflag/options_spec.rb @@ -14,5 +14,14 @@ it "should raise if endpoint is not http" do expect { OpenFeature::GoFeatureFlag::Options.new(endpoint: "ftp://gofeatureflag.org") }.to raise_error(ArgumentError, "Invalid URL for endpoint: ftp://gofeatureflag.org") end + + it "should return instrumentation if configured" do + options = OpenFeature::GoFeatureFlag::Options.new(endpoint: "http://localhost:1031", instrumentation: {name: "custom_name"}) + expect(options.instrumentation).to eql(name: "custom_name") + end + + it "should raise if instrumentation is not hash" do + expect { OpenFeature::GoFeatureFlag::Options.new(instrumentation: "custom_name") }.to raise_error(ArgumentError, "Invalid type for instrumentation: String") + end end end