Class: Mixpanel::Flags::FlagsProvider
- Inherits:
-
Object
- Object
- Mixpanel::Flags::FlagsProvider
- Defined in:
- lib/mixpanel-ruby/flags/flags_provider.rb
Overview
Base class for feature flags providers Provides common HTTP handling and exposure event tracking
Direct Known Subclasses
Instance Method Summary collapse
-
#call_flags_endpoint(additional_params = nil) ⇒ Hash
Make HTTP request to flags API endpoint.
-
#initialize(provider_config, endpoint, tracker_callback, evaluation_mode, error_handler) ⇒ FlagsProvider
constructor
A new instance of FlagsProvider.
- #shutdown ⇒ Object
-
#track_exposure_event(flag_key, selected_variant, context, latency_ms = nil) ⇒ Object
Track exposure event to Mixpanel.
Constructor Details
#initialize(provider_config, endpoint, tracker_callback, evaluation_mode, error_handler) ⇒ FlagsProvider
Returns a new instance of FlagsProvider.
20 21 22 23 24 25 26 27 28 |
# File 'lib/mixpanel-ruby/flags/flags_provider.rb', line 20 def initialize(provider_config, endpoint, tracker_callback, evaluation_mode, error_handler) @provider_config = provider_config @endpoint = endpoint @tracker_callback = tracker_callback @evaluation_mode = evaluation_mode @error_handler = error_handler @exposure_executor = provider_config[:exposure_executor] @credentials = provider_config[:credentials] end |
Instance Method Details
#call_flags_endpoint(additional_params = nil) ⇒ Hash
Make HTTP request to flags API endpoint
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mixpanel-ruby/flags/flags_provider.rb', line 35 def call_flags_endpoint(additional_params = nil) # Always use token in query params common_params = Utils.prepare_common_query_params( @provider_config[:token], Mixpanel::VERSION ) params = common_params.merge(additional_params || {}) # Add project_id as query parameter when using service account credentials # Note: project_id is required for service account auth but not used for token auth if @credentials params['project_id'] = @credentials.project_id end query_string = URI.encode_www_form(params) uri = URI::HTTPS.build( host: @provider_config[:api_host], path: @endpoint, query: query_string ) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.open_timeout = @provider_config[:request_timeout_in_seconds] http.read_timeout = @provider_config[:request_timeout_in_seconds] request = Net::HTTP::Get.new(uri.request_uri) # Use service account credentials for basic auth if provided, otherwise use token if @credentials request.basic_auth(@credentials.username, @credentials.secret) else request.basic_auth(@provider_config[:token], '') end request['Content-Type'] = 'application/json' request['traceparent'] = Utils.generate_traceparent begin response = http.request(request) rescue Net::OpenTimeout, Net::ReadTimeout => e raise ConnectionError.new("Request timeout: #{e.}") rescue StandardError => e raise ConnectionError.new("Network error: #{e.}") end unless response.code == '200' raise ServerError.new("HTTP #{response.code}: #{response.body}") end begin JSON.parse(response.body) rescue JSON::ParserError => e raise ServerError.new("Invalid JSON response: #{e.}") end end |
#shutdown ⇒ Object
95 |
# File 'lib/mixpanel-ruby/flags/flags_provider.rb', line 95 def shutdown; end |
#track_exposure_event(flag_key, selected_variant, context, latency_ms = nil) ⇒ Object
Track exposure event to Mixpanel
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mixpanel-ruby/flags/flags_provider.rb', line 102 def track_exposure_event(flag_key, selected_variant, context, latency_ms = nil) distinct_id = context['distinct_id'] || context[:distinct_id] unless distinct_id # Local eval succeeds when the flag's Variant Assignment Key is # something other than distinct_id (e.g., device_id), but the # exposure event still needs distinct_id to attribute the user. # Surface the drop instead of silently returning so callers can # see they need to include distinct_id in the context. @error_handler&.handle(MixpanelError.new( "Cannot track exposure event for flag '#{flag_key}' without a distinct_id in the context" )) return end properties = { 'distinct_id' => distinct_id, 'Experiment name' => flag_key, 'Variant name' => selected_variant.variant_key, '$experiment_type' => 'feature_flag', 'Flag evaluation mode' => @evaluation_mode } properties['Variant fetch latency (ms)'] = latency_ms if latency_ms properties['$experiment_id'] = selected_variant.experiment_id if selected_variant.experiment_id properties['$is_experiment_active'] = selected_variant.is_experiment_active unless selected_variant.is_experiment_active.nil? properties['$is_qa_tester'] = selected_variant.is_qa_tester unless selected_variant.is_qa_tester.nil? dispatch_exposure(distinct_id, properties) end |