launchdarkly-server-sdk 8.8.3-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +13 -0
  3. data/README.md +61 -0
  4. data/lib/launchdarkly-server-sdk.rb +1 -0
  5. data/lib/ldclient-rb/cache_store.rb +45 -0
  6. data/lib/ldclient-rb/config.rb +658 -0
  7. data/lib/ldclient-rb/context.rb +565 -0
  8. data/lib/ldclient-rb/evaluation_detail.rb +387 -0
  9. data/lib/ldclient-rb/events.rb +642 -0
  10. data/lib/ldclient-rb/expiring_cache.rb +77 -0
  11. data/lib/ldclient-rb/flags_state.rb +88 -0
  12. data/lib/ldclient-rb/impl/big_segments.rb +117 -0
  13. data/lib/ldclient-rb/impl/broadcaster.rb +78 -0
  14. data/lib/ldclient-rb/impl/context.rb +96 -0
  15. data/lib/ldclient-rb/impl/context_filter.rb +166 -0
  16. data/lib/ldclient-rb/impl/data_source.rb +188 -0
  17. data/lib/ldclient-rb/impl/data_store.rb +109 -0
  18. data/lib/ldclient-rb/impl/dependency_tracker.rb +102 -0
  19. data/lib/ldclient-rb/impl/diagnostic_events.rb +129 -0
  20. data/lib/ldclient-rb/impl/evaluation_with_hook_result.rb +34 -0
  21. data/lib/ldclient-rb/impl/evaluator.rb +539 -0
  22. data/lib/ldclient-rb/impl/evaluator_bucketing.rb +86 -0
  23. data/lib/ldclient-rb/impl/evaluator_helpers.rb +50 -0
  24. data/lib/ldclient-rb/impl/evaluator_operators.rb +131 -0
  25. data/lib/ldclient-rb/impl/event_sender.rb +100 -0
  26. data/lib/ldclient-rb/impl/event_summarizer.rb +68 -0
  27. data/lib/ldclient-rb/impl/event_types.rb +136 -0
  28. data/lib/ldclient-rb/impl/flag_tracker.rb +58 -0
  29. data/lib/ldclient-rb/impl/integrations/consul_impl.rb +170 -0
  30. data/lib/ldclient-rb/impl/integrations/dynamodb_impl.rb +300 -0
  31. data/lib/ldclient-rb/impl/integrations/file_data_source.rb +229 -0
  32. data/lib/ldclient-rb/impl/integrations/redis_impl.rb +306 -0
  33. data/lib/ldclient-rb/impl/integrations/test_data/test_data_source.rb +40 -0
  34. data/lib/ldclient-rb/impl/migrations/migrator.rb +287 -0
  35. data/lib/ldclient-rb/impl/migrations/tracker.rb +136 -0
  36. data/lib/ldclient-rb/impl/model/clause.rb +45 -0
  37. data/lib/ldclient-rb/impl/model/feature_flag.rb +254 -0
  38. data/lib/ldclient-rb/impl/model/preprocessed_data.rb +64 -0
  39. data/lib/ldclient-rb/impl/model/segment.rb +132 -0
  40. data/lib/ldclient-rb/impl/model/serialization.rb +72 -0
  41. data/lib/ldclient-rb/impl/repeating_task.rb +46 -0
  42. data/lib/ldclient-rb/impl/sampler.rb +25 -0
  43. data/lib/ldclient-rb/impl/store_client_wrapper.rb +141 -0
  44. data/lib/ldclient-rb/impl/store_data_set_sorter.rb +55 -0
  45. data/lib/ldclient-rb/impl/unbounded_pool.rb +34 -0
  46. data/lib/ldclient-rb/impl/util.rb +95 -0
  47. data/lib/ldclient-rb/impl.rb +13 -0
  48. data/lib/ldclient-rb/in_memory_store.rb +100 -0
  49. data/lib/ldclient-rb/integrations/consul.rb +45 -0
  50. data/lib/ldclient-rb/integrations/dynamodb.rb +92 -0
  51. data/lib/ldclient-rb/integrations/file_data.rb +108 -0
  52. data/lib/ldclient-rb/integrations/redis.rb +98 -0
  53. data/lib/ldclient-rb/integrations/test_data/flag_builder.rb +663 -0
  54. data/lib/ldclient-rb/integrations/test_data.rb +213 -0
  55. data/lib/ldclient-rb/integrations/util/store_wrapper.rb +246 -0
  56. data/lib/ldclient-rb/integrations.rb +6 -0
  57. data/lib/ldclient-rb/interfaces.rb +974 -0
  58. data/lib/ldclient-rb/ldclient.rb +822 -0
  59. data/lib/ldclient-rb/memoized_value.rb +32 -0
  60. data/lib/ldclient-rb/migrations.rb +230 -0
  61. data/lib/ldclient-rb/non_blocking_thread_pool.rb +46 -0
  62. data/lib/ldclient-rb/polling.rb +102 -0
  63. data/lib/ldclient-rb/reference.rb +295 -0
  64. data/lib/ldclient-rb/requestor.rb +102 -0
  65. data/lib/ldclient-rb/simple_lru_cache.rb +25 -0
  66. data/lib/ldclient-rb/stream.rb +196 -0
  67. data/lib/ldclient-rb/util.rb +132 -0
  68. data/lib/ldclient-rb/version.rb +3 -0
  69. data/lib/ldclient-rb.rb +27 -0
  70. metadata +400 -0
@@ -0,0 +1,822 @@
1
+ require "ldclient-rb/impl/big_segments"
2
+ require "ldclient-rb/impl/broadcaster"
3
+ require "ldclient-rb/impl/data_source"
4
+ require "ldclient-rb/impl/data_store"
5
+ require "ldclient-rb/impl/diagnostic_events"
6
+ require "ldclient-rb/impl/evaluator"
7
+ require "ldclient-rb/impl/evaluation_with_hook_result"
8
+ require "ldclient-rb/impl/flag_tracker"
9
+ require "ldclient-rb/impl/store_client_wrapper"
10
+ require "ldclient-rb/impl/migrations/tracker"
11
+ require "concurrent"
12
+ require "concurrent/atomics"
13
+ require "digest/sha1"
14
+ require "forwardable"
15
+ require "logger"
16
+ require "benchmark"
17
+ require "json"
18
+ require "openssl"
19
+
20
+ module LaunchDarkly
21
+ #
22
+ # A client for LaunchDarkly. Client instances are thread-safe. Users
23
+ # should create a single client instance for the lifetime of the application.
24
+ #
25
+ class LDClient
26
+ include Impl
27
+ extend Forwardable
28
+
29
+ def_delegators :@config, :logger
30
+
31
+ #
32
+ # Creates a new client instance that connects to LaunchDarkly. A custom
33
+ # configuration parameter can also supplied to specify advanced options,
34
+ # but for most use cases, the default configuration is appropriate.
35
+ #
36
+ # The client will immediately attempt to connect to LaunchDarkly and retrieve
37
+ # your feature flag data. If it cannot successfully do so within the time limit
38
+ # specified by `wait_for_sec`, the constructor will return a client that is in
39
+ # an uninitialized state. See {#initialized?} for more details.
40
+ #
41
+ # @param sdk_key [String] the SDK key for your LaunchDarkly account
42
+ # @param config [Config] an optional client configuration object
43
+ # @param wait_for_sec [Float] maximum time (in seconds) to wait for initialization
44
+ #
45
+ # @return [LDClient] The LaunchDarkly client instance
46
+ #
47
+ def initialize(sdk_key, config = Config.default, wait_for_sec = 5)
48
+ # Note that sdk_key is normally a required parameter, and a nil value would cause the SDK to
49
+ # fail in most configurations. However, there are some configurations where it would be OK
50
+ # (offline = true, *or* we are using LDD mode or the file data source and events are disabled
51
+ # so we're not connecting to any LD services) so rather than try to check for all of those
52
+ # up front, we will let the constructors for the data source implementations implement this
53
+ # fail-fast as appropriate, and just check here for the part regarding events.
54
+ if !config.offline? && config.send_events
55
+ raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil?
56
+ end
57
+
58
+ @sdk_key = sdk_key
59
+ @hooks = Concurrent::Array.new(config.hooks)
60
+
61
+ @shared_executor = Concurrent::SingleThreadExecutor.new
62
+
63
+ data_store_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@shared_executor, config.logger)
64
+ store_sink = LaunchDarkly::Impl::DataStore::UpdateSink.new(data_store_broadcaster)
65
+
66
+ # We need to wrap the feature store object with a FeatureStoreClientWrapper in order to add
67
+ # some necessary logic around updates. Unfortunately, we have code elsewhere that accesses
68
+ # the feature store through the Config object, so we need to make a new Config that uses
69
+ # the wrapped store.
70
+ @store = Impl::FeatureStoreClientWrapper.new(config.feature_store, store_sink, config.logger)
71
+ updated_config = config.clone
72
+ updated_config.instance_variable_set(:@feature_store, @store)
73
+ @config = updated_config
74
+
75
+ @data_store_status_provider = LaunchDarkly::Impl::DataStore::StatusProvider.new(@store, store_sink)
76
+
77
+ @big_segment_store_manager = Impl::BigSegmentStoreManager.new(config.big_segments, @config.logger)
78
+ @big_segment_store_status_provider = @big_segment_store_manager.status_provider
79
+
80
+ get_flag = lambda { |key| @store.get(FEATURES, key) }
81
+ get_segment = lambda { |key| @store.get(SEGMENTS, key) }
82
+ get_big_segments_membership = lambda { |key| @big_segment_store_manager.get_context_membership(key) }
83
+ @evaluator = LaunchDarkly::Impl::Evaluator.new(get_flag, get_segment, get_big_segments_membership, @config.logger)
84
+
85
+ if !@config.offline? && @config.send_events && !@config.diagnostic_opt_out?
86
+ diagnostic_accumulator = Impl::DiagnosticAccumulator.new(Impl::DiagnosticAccumulator.create_diagnostic_id(sdk_key))
87
+ else
88
+ diagnostic_accumulator = nil
89
+ end
90
+
91
+ if @config.offline? || !@config.send_events
92
+ @event_processor = NullEventProcessor.new
93
+ else
94
+ @event_processor = EventProcessor.new(sdk_key, config, nil, diagnostic_accumulator)
95
+ end
96
+
97
+ if @config.use_ldd?
98
+ @config.logger.info { "[LDClient] Started LaunchDarkly Client in LDD mode" }
99
+ @data_source = NullUpdateProcessor.new
100
+ return # requestor and update processor are not used in this mode
101
+ end
102
+
103
+ flag_tracker_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@shared_executor, @config.logger)
104
+ @flag_tracker = LaunchDarkly::Impl::FlagTracker.new(flag_tracker_broadcaster, lambda { |key, context| variation(key, context, nil) })
105
+
106
+ data_source_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@shared_executor, @config.logger)
107
+
108
+ # Make the update sink available on the config so that our data source factory can access the sink with a shared executor.
109
+ @config.data_source_update_sink = LaunchDarkly::Impl::DataSource::UpdateSink.new(@store, data_source_broadcaster, flag_tracker_broadcaster)
110
+
111
+ @data_source_status_provider = LaunchDarkly::Impl::DataSource::StatusProvider.new(data_source_broadcaster, @config.data_source_update_sink)
112
+
113
+ data_source_or_factory = @config.data_source || self.method(:create_default_data_source)
114
+ if data_source_or_factory.respond_to? :call
115
+ # Currently, data source factories take two parameters unless they need to be aware of diagnostic_accumulator, in
116
+ # which case they take three parameters. This will be changed in the future to use a less awkware mechanism.
117
+ if data_source_or_factory.arity == 3
118
+ @data_source = data_source_or_factory.call(sdk_key, @config, diagnostic_accumulator)
119
+ else
120
+ @data_source = data_source_or_factory.call(sdk_key, @config)
121
+ end
122
+ else
123
+ @data_source = data_source_or_factory
124
+ end
125
+
126
+ ready = @data_source.start
127
+
128
+ return unless wait_for_sec > 0
129
+
130
+ if wait_for_sec > 60
131
+ @config.logger.warn { "[LDClient] Client was configured to block for up to #{wait_for_sec} seconds when initializing. We recommend blocking no longer than 60." }
132
+ end
133
+
134
+ ok = ready.wait(wait_for_sec)
135
+ if !ok
136
+ @config.logger.error { "[LDClient] Timeout encountered waiting for LaunchDarkly client initialization" }
137
+ elsif !@data_source.initialized?
138
+ @config.logger.error { "[LDClient] LaunchDarkly client initialization failed" }
139
+ end
140
+ end
141
+
142
+ #
143
+ # Add a hook to the client. In order to register a hook before the client starts, please use the `hooks` property of
144
+ # {#LDConfig}.
145
+ #
146
+ # Hooks provide entrypoints which allow for observation of SDK functions.
147
+ #
148
+ # @param hook [Interfaces::Hooks::Hook]
149
+ #
150
+ def add_hook(hook)
151
+ unless hook.is_a?(Interfaces::Hooks::Hook)
152
+ @config.logger.error { "[LDClient] Attempted to add a hook that does not include the LaunchDarkly::Intefaces::Hooks::Hook mixin. Ignoring." }
153
+ return
154
+ end
155
+
156
+ @hooks.push(hook)
157
+ end
158
+
159
+ #
160
+ # Tells the client that all pending analytics events should be delivered as soon as possible.
161
+ #
162
+ # When the LaunchDarkly client generates analytics events (from {#variation}, {#variation_detail},
163
+ # {#identify}, or {#track}), they are queued on a worker thread. The event thread normally
164
+ # sends all queued events to LaunchDarkly at regular intervals, controlled by the
165
+ # {Config#flush_interval} option. Calling `flush` triggers a send without waiting for the
166
+ # next interval.
167
+ #
168
+ # Flushing is asynchronous, so this method will return before it is complete. However, if you
169
+ # call {#close}, events are guaranteed to be sent before that method returns.
170
+ #
171
+ def flush
172
+ @event_processor.flush
173
+ end
174
+
175
+ #
176
+ # Creates a hash string that can be used by the JavaScript SDK to identify a context.
177
+ # For more information, see [Secure mode](https://docs.launchdarkly.com/sdk/features/secure-mode#ruby).
178
+ #
179
+ # @param context [Hash, LDContext]
180
+ # @return [String, nil] a hash string or nil if the provided context was invalid
181
+ #
182
+ def secure_mode_hash(context)
183
+ context = Impl::Context.make_context(context)
184
+ unless context.valid?
185
+ @config.logger.warn("secure_mode_hash called with invalid context: #{context.error}")
186
+ return nil
187
+ end
188
+
189
+ OpenSSL::HMAC.hexdigest("sha256", @sdk_key, context.fully_qualified_key)
190
+ end
191
+
192
+ #
193
+ # Returns whether the client has been initialized and is ready to serve feature flag requests.
194
+ #
195
+ # If this returns false, it means that the client did not succeed in connecting to
196
+ # LaunchDarkly within the time limit that you specified in the constructor. It could
197
+ # still succeed in connecting at a later time (on another thread), or it could have
198
+ # given up permanently (for instance, if your SDK key is invalid). In the meantime,
199
+ # any call to {#variation} or {#variation_detail} will behave as follows:
200
+ #
201
+ # 1. It will check whether the feature store already contains data (that is, you
202
+ # are using a database-backed store and it was populated by a previous run of this
203
+ # application). If so, it will use the last known feature flag data.
204
+ #
205
+ # 2. Failing that, it will return the value that you specified for the `default`
206
+ # parameter of {#variation} or {#variation_detail}.
207
+ #
208
+ # @return [Boolean] true if the client has been initialized
209
+ #
210
+ def initialized?
211
+ @config.offline? || @config.use_ldd? || @data_source.initialized?
212
+ end
213
+
214
+ #
215
+ # Determines the variation of a feature flag to present for a context.
216
+ #
217
+ # @param key [String] the unique feature key for the feature flag, as shown
218
+ # on the LaunchDarkly dashboard
219
+ # @param context [Hash, LDContext] a hash or LDContext instance describing the context requesting the flag
220
+ # @param default the default value of the flag; this is used if there is an error
221
+ # condition making it impossible to find or evaluate the flag
222
+ #
223
+ # @return the variation for the provided context, or the default value if there's an error
224
+ #
225
+ def variation(key, context, default)
226
+ context = Impl::Context::make_context(context)
227
+ result = evaluate_with_hooks(key, context, default, :variation) do
228
+ detail, _, _ = variation_with_flag(key, context, default)
229
+ LaunchDarkly::Impl::EvaluationWithHookResult.new(detail)
230
+ end
231
+
232
+ result.evaluation_detail.value
233
+ end
234
+
235
+ #
236
+ # Determines the variation of a feature flag for a context, like {#variation}, but also
237
+ # provides additional information about how this value was calculated.
238
+ #
239
+ # The return value of `variation_detail` is an {EvaluationDetail} object, which has
240
+ # three properties: the result value, the positional index of this value in the flag's
241
+ # list of variations, and an object describing the main reason why this value was
242
+ # selected. See {EvaluationDetail} for more on these properties.
243
+ #
244
+ # Calling `variation_detail` instead of `variation` also causes the "reason" data to
245
+ # be included in analytics events, if you are capturing detailed event data for this flag.
246
+ #
247
+ # For more information, see the reference guide on
248
+ # [Evaluation reasons](https://docs.launchdarkly.com/sdk/concepts/evaluation-reasons).
249
+ #
250
+ # @param key [String] the unique feature key for the feature flag, as shown
251
+ # on the LaunchDarkly dashboard
252
+ # @param context [Hash, LDContext] a hash or object describing the context requesting the flag,
253
+ # @param default the default value of the flag; this is used if there is an error
254
+ # condition making it impossible to find or evaluate the flag
255
+ #
256
+ # @return [EvaluationDetail] an object describing the result
257
+ #
258
+ def variation_detail(key, context, default)
259
+ context = Impl::Context::make_context(context)
260
+ result = evaluate_with_hooks(key, context, default, :variation_detail) do
261
+ detail, _, _ = evaluate_internal(key, context, default, true)
262
+ LaunchDarkly::Impl::EvaluationWithHookResult.new(detail)
263
+ end
264
+
265
+ result.evaluation_detail
266
+ end
267
+
268
+ #
269
+ # evaluate_with_hook will run the provided block, wrapping it with evaluation hook support.
270
+ #
271
+ # Example:
272
+ #
273
+ # ```ruby
274
+ # evaluate_with_hooks(key, context, default, method) do
275
+ # puts 'This is being wrapped with evaluation hooks'
276
+ # end
277
+ # ```
278
+ #
279
+ # @param key [String]
280
+ # @param context [LDContext]
281
+ # @param default [any]
282
+ # @param method [Symbol]
283
+ # @param &block [#call] Implicit passed block
284
+ #
285
+ # @return [LaunchDarkly::Impl::EvaluationWithHookResult]
286
+ #
287
+ private def evaluate_with_hooks(key, context, default, method)
288
+ return yield if @hooks.empty?
289
+
290
+ hooks, evaluation_series_context = prepare_hooks(key, context, default, method)
291
+ hook_data = execute_before_evaluation(hooks, evaluation_series_context)
292
+ evaluation_result = yield
293
+ execute_after_evaluation(hooks, evaluation_series_context, hook_data, evaluation_result.evaluation_detail)
294
+
295
+ evaluation_result
296
+ end
297
+
298
+ #
299
+ # Execute the :before_evaluation stage of the evaluation series.
300
+ #
301
+ # This method will return the results of each hook, indexed into an array in the same order as the hooks. If a hook
302
+ # raised an uncaught exception, the value will be nil.
303
+ #
304
+ # @param hooks [Array<Interfaces::Hooks::Hook>]
305
+ # @param evaluation_series_context [EvaluationSeriesContext]
306
+ #
307
+ # @return [Array<any>]
308
+ #
309
+ private def execute_before_evaluation(hooks, evaluation_series_context)
310
+ hooks.map do |hook|
311
+ try_execute_stage(:before_evaluation, hook.metadata.name) do
312
+ hook.before_evaluation(evaluation_series_context, {})
313
+ end
314
+ end
315
+ end
316
+
317
+ #
318
+ # Execute the :after_evaluation stage of the evaluation series.
319
+ #
320
+ # This method will return the results of each hook, indexed into an array in the same order as the hooks. If a hook
321
+ # raised an uncaught exception, the value will be nil.
322
+ #
323
+ # @param hooks [Array<Interfaces::Hooks::Hook>]
324
+ # @param evaluation_series_context [EvaluationSeriesContext]
325
+ # @param hook_data [Array<any>]
326
+ # @param evaluation_detail [EvaluationDetail]
327
+ #
328
+ # @return [Array<any>]
329
+ #
330
+ private def execute_after_evaluation(hooks, evaluation_series_context, hook_data, evaluation_detail)
331
+ hooks.zip(hook_data).reverse.map do |(hook, data)|
332
+ try_execute_stage(:after_evaluation, hook.metadata.name) do
333
+ hook.after_evaluation(evaluation_series_context, data, evaluation_detail)
334
+ end
335
+ end
336
+ end
337
+
338
+ #
339
+ # Try to execute the provided block. If execution raises an exception, catch and log it, then move on with
340
+ # execution.
341
+ #
342
+ # @return [any]
343
+ #
344
+ private def try_execute_stage(method, hook_name)
345
+ begin
346
+ yield
347
+ rescue => e
348
+ @config.logger.error { "[LDClient] An error occurred in #{method} of the hook #{hook_name}: #{e}" }
349
+ nil
350
+ end
351
+ end
352
+
353
+ #
354
+ # Return a copy of the existing hooks and a few instance of the EvaluationSeriesContext used for the evaluation series.
355
+ #
356
+ # @param key [String]
357
+ # @param context [LDContext]
358
+ # @param default [any]
359
+ # @param method [Symbol]
360
+ # @return [Array[Array<Interfaces::Hooks::Hook>, Interfaces::Hooks::EvaluationSeriesContext]]
361
+ #
362
+ private def prepare_hooks(key, context, default, method)
363
+ # Copy the hooks to use a consistent set during the evaluation series.
364
+ #
365
+ # Hooks can be added and we want to ensure all correct stages for a given hook execute. For example, we do not
366
+ # want to trigger the after_evaluation method without also triggering the before_evaluation method.
367
+ hooks = @hooks.dup
368
+ evaluation_series_context = Interfaces::Hooks::EvaluationSeriesContext.new(key, context, default, method)
369
+
370
+ [hooks, evaluation_series_context]
371
+ end
372
+
373
+ #
374
+ # This method returns the migration stage of the migration feature flag for the given evaluation context.
375
+ #
376
+ # This method returns the default stage if there is an error or the flag does not exist. If the default stage is not
377
+ # a valid stage, then a default stage of 'off' will be used instead.
378
+ #
379
+ # @param key [String]
380
+ # @param context [LDContext]
381
+ # @param default_stage [Symbol]
382
+ #
383
+ # @return [Array<Symbol, Interfaces::Migrations::OpTracker>]
384
+ #
385
+ def migration_variation(key, context, default_stage)
386
+ unless Migrations::VALID_STAGES.include? default_stage
387
+ @config.logger.error { "[LDClient] default_stage #{default_stage} is not a valid stage; continuing with 'off' as default" }
388
+ default_stage = Migrations::STAGE_OFF
389
+ end
390
+
391
+ context = Impl::Context::make_context(context)
392
+ result = evaluate_with_hooks(key, context, default_stage, :migration_variation) do
393
+ detail, flag, _ = variation_with_flag(key, context, default_stage.to_s)
394
+
395
+ stage = detail.value
396
+ stage = stage.to_sym if stage.respond_to? :to_sym
397
+
398
+ if Migrations::VALID_STAGES.include?(stage)
399
+ tracker = Impl::Migrations::OpTracker.new(@config.logger, key, flag, context, detail, default_stage)
400
+ next LaunchDarkly::Impl::EvaluationWithHookResult.new(detail, {stage: stage, tracker: tracker})
401
+ end
402
+
403
+ detail = LaunchDarkly::Impl::Evaluator.error_result(LaunchDarkly::EvaluationReason::ERROR_WRONG_TYPE, default_stage.to_s)
404
+ tracker = Impl::Migrations::OpTracker.new(@config.logger, key, flag, context, detail, default_stage)
405
+
406
+ LaunchDarkly::Impl::EvaluationWithHookResult.new(detail, {stage: default_stage, tracker: tracker})
407
+ end
408
+
409
+ [result.results[:stage], result.results[:tracker]]
410
+ end
411
+
412
+ #
413
+ # Registers the context. This method simply creates an analytics event containing the context
414
+ # properties, so that LaunchDarkly will know about that context if it does not already.
415
+ #
416
+ # Calling {#variation} or {#variation_detail} also sends the context information to
417
+ # LaunchDarkly (if events are enabled), so you only need to use {#identify} if you
418
+ # want to identify the context without evaluating a flag.
419
+ #
420
+ # Note that event delivery is asynchronous, so the event may not actually be sent
421
+ # until later; see {#flush}.
422
+ #
423
+ # @param context [Hash, LDContext] a hash or object describing the context to register
424
+ # @return [void]
425
+ #
426
+ def identify(context)
427
+ context = LaunchDarkly::Impl::Context.make_context(context)
428
+ unless context.valid?
429
+ @config.logger.warn("Identify called with invalid context: #{context.error}")
430
+ return
431
+ end
432
+
433
+ if context.key == ""
434
+ @config.logger.warn("Identify called with empty key")
435
+ return
436
+ end
437
+
438
+ @event_processor.record_identify_event(context)
439
+ end
440
+
441
+ #
442
+ # Tracks that a context performed an event. This method creates a "custom" analytics event
443
+ # containing the specified event name (key), context properties, and optional data.
444
+ #
445
+ # Note that event delivery is asynchronous, so the event may not actually be sent
446
+ # until later; see {#flush}.
447
+ #
448
+ # As of this version’s release date, the LaunchDarkly service does not support the `metricValue`
449
+ # parameter. As a result, specifying `metricValue` will not yet produce any different behavior
450
+ # from omitting it. Refer to the [SDK reference guide](https://docs.launchdarkly.com/sdk/features/events#ruby)
451
+ # for the latest status.
452
+ #
453
+ # @param event_name [String] The name of the event
454
+ # @param context [Hash, LDContext] a hash or object describing the context to track
455
+ # @param data [Hash] An optional hash containing any additional data associated with the event
456
+ # @param metric_value [Number] A numeric value used by the LaunchDarkly experimentation
457
+ # feature in numeric custom metrics. Can be omitted if this event is used by only
458
+ # non-numeric metrics. This field will also be returned as part of the custom event
459
+ # for Data Export.
460
+ # @return [void]
461
+ #
462
+ def track(event_name, context, data = nil, metric_value = nil)
463
+ context = LaunchDarkly::Impl::Context.make_context(context)
464
+ unless context.valid?
465
+ @config.logger.warn("Track called with invalid context: #{context.error}")
466
+ return
467
+ end
468
+
469
+ @event_processor.record_custom_event(context, event_name, data, metric_value)
470
+ end
471
+
472
+ #
473
+ # Tracks the results of a migrations operation. This event includes measurements which can be used to enhance the
474
+ # observability of a migration within the LaunchDarkly UI.
475
+ #
476
+ # This event should be generated through {Interfaces::Migrations::OpTracker}. If you are using the
477
+ # {Interfaces::Migrations::Migrator} to handle migrations, this event will be created and emitted
478
+ # automatically.
479
+ #
480
+ # @param tracker [LaunchDarkly::Interfaces::Migrations::OpTracker]
481
+ #
482
+ def track_migration_op(tracker)
483
+ unless tracker.is_a? LaunchDarkly::Interfaces::Migrations::OpTracker
484
+ @config.logger.error { "invalid op tracker received in track_migration_op" }
485
+ return
486
+ end
487
+
488
+ event = tracker.build
489
+ if event.is_a? String
490
+ @config.logger.error { "[LDClient] Error occurred generating migration op event; #{event}" }
491
+ return
492
+ end
493
+
494
+
495
+ @event_processor.record_migration_op_event(event)
496
+ end
497
+
498
+ #
499
+ # Returns a {FeatureFlagsState} object that encapsulates the state of all feature flags for a given context,
500
+ # including the flag values and also metadata that can be used on the front end. This method does not
501
+ # send analytics events back to LaunchDarkly.
502
+ #
503
+ # @param context [Hash, LDContext] a hash or object describing the context requesting the flags,
504
+ # @param options [Hash] Optional parameters to control how the state is generated
505
+ # @option options [Boolean] :client_side_only (false) True if only flags marked for use with the
506
+ # client-side SDK should be included in the state. By default, all flags are included.
507
+ # @option options [Boolean] :with_reasons (false) True if evaluation reasons should be included
508
+ # in the state (see {#variation_detail}). By default, they are not included.
509
+ # @option options [Boolean] :details_only_for_tracked_flags (false) True if any flag metadata that is
510
+ # normally only used for event generation - such as flag versions and evaluation reasons - should be
511
+ # omitted for any flag that does not have event tracking or debugging turned on. This reduces the size
512
+ # of the JSON data if you are passing the flag state to the front end.
513
+ # @return [FeatureFlagsState] a {FeatureFlagsState} object which can be serialized to JSON
514
+ #
515
+ def all_flags_state(context, options={})
516
+ return FeatureFlagsState.new(false) if @config.offline?
517
+
518
+ unless initialized?
519
+ if @store.initialized?
520
+ @config.logger.warn { "Called all_flags_state before client initialization; using last known values from data store" }
521
+ else
522
+ @config.logger.warn { "Called all_flags_state before client initialization. Data store not available; returning empty state" }
523
+ return FeatureFlagsState.new(false)
524
+ end
525
+ end
526
+
527
+ context = Impl::Context::make_context(context)
528
+ unless context.valid?
529
+ @config.logger.error { "[LDClient] Context was invalid for all_flags_state (#{context.error})" }
530
+ return FeatureFlagsState.new(false)
531
+ end
532
+
533
+ begin
534
+ features = @store.all(FEATURES)
535
+ rescue => exn
536
+ Util.log_exception(@config.logger, "Unable to read flags for all_flags_state", exn)
537
+ return FeatureFlagsState.new(false)
538
+ end
539
+
540
+ state = FeatureFlagsState.new(true)
541
+ client_only = options[:client_side_only] || false
542
+ with_reasons = options[:with_reasons] || false
543
+ details_only_if_tracked = options[:details_only_for_tracked_flags] || false
544
+ features.each do |k, f|
545
+ if client_only && !f[:clientSide]
546
+ next
547
+ end
548
+ begin
549
+ (eval_result, eval_state) = @evaluator.evaluate(f, context)
550
+ detail = eval_result.detail
551
+ rescue => exn
552
+ detail = EvaluationDetail.new(nil, nil, EvaluationReason::error(EvaluationReason::ERROR_EXCEPTION))
553
+ Util.log_exception(@config.logger, "Error evaluating flag \"#{k}\" in all_flags_state", exn)
554
+ end
555
+
556
+ requires_experiment_data = experiment?(f, detail.reason)
557
+ flag_state = {
558
+ key: f[:key],
559
+ value: detail.value,
560
+ variation: detail.variation_index,
561
+ reason: detail.reason,
562
+ prerequisites: eval_state.prerequisites,
563
+ version: f[:version],
564
+ trackEvents: f[:trackEvents] || requires_experiment_data,
565
+ trackReason: requires_experiment_data,
566
+ debugEventsUntilDate: f[:debugEventsUntilDate],
567
+ }
568
+
569
+ state.add_flag(flag_state, with_reasons, details_only_if_tracked)
570
+ end
571
+
572
+ state
573
+ end
574
+
575
+ #
576
+ # Releases all network connections and other resources held by the client, making it no longer usable.
577
+ #
578
+ # @return [void]
579
+ def close
580
+ @config.logger.info { "[LDClient] Closing LaunchDarkly client..." }
581
+ @data_source.stop
582
+ @event_processor.stop
583
+ @big_segment_store_manager.stop
584
+ @store.stop
585
+ @shared_executor.shutdown
586
+ end
587
+
588
+ #
589
+ # Returns an interface for tracking the status of a Big Segment store.
590
+ #
591
+ # The {Interfaces::BigSegmentStoreStatusProvider} has methods for checking whether the Big Segment store
592
+ # is (as far as the SDK knows) currently operational and tracking changes in this status.
593
+ #
594
+ attr_reader :big_segment_store_status_provider
595
+
596
+ #
597
+ # Returns an interface for tracking the status of a persistent data store.
598
+ #
599
+ # The {LaunchDarkly::Interfaces::DataStore::StatusProvider} has methods for
600
+ # checking whether the data store is (as far as the SDK knows) currently
601
+ # operational, tracking changes in this status, and getting cache
602
+ # statistics. These are only relevant for a persistent data store; if you
603
+ # are using an in-memory data store, then this method will return a stub
604
+ # object that provides no information.
605
+ #
606
+ # @return [LaunchDarkly::Interfaces::DataStore::StatusProvider]
607
+ #
608
+ attr_reader :data_store_status_provider
609
+
610
+ #
611
+ # Returns an interface for tracking the status of the data source.
612
+ #
613
+ # The data source is the mechanism that the SDK uses to get feature flag
614
+ # configurations, such as a streaming connection (the default) or poll
615
+ # requests. The {LaunchDarkly::Interfaces::DataSource::StatusProvider} has
616
+ # methods for checking whether the data source is (as far as the SDK knows)
617
+ # currently operational and tracking changes in this status.
618
+ #
619
+ # @return [LaunchDarkly::Interfaces::DataSource::StatusProvider]
620
+ #
621
+ attr_reader :data_source_status_provider
622
+
623
+ #
624
+ # Returns an interface for tracking changes in feature flag configurations.
625
+ #
626
+ # The {LaunchDarkly::Interfaces::FlagTracker} contains methods for
627
+ # requesting notifications about feature flag changes using an event
628
+ # listener model.
629
+ #
630
+ attr_reader :flag_tracker
631
+
632
+ private
633
+
634
+ def create_default_data_source(sdk_key, config, diagnostic_accumulator)
635
+ if config.offline?
636
+ return NullUpdateProcessor.new
637
+ end
638
+ raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil? # see LDClient constructor comment on sdk_key
639
+ if config.stream?
640
+ StreamProcessor.new(sdk_key, config, diagnostic_accumulator)
641
+ else
642
+ config.logger.info { "Disabling streaming API" }
643
+ config.logger.warn { "You should only disable the streaming API if instructed to do so by LaunchDarkly support" }
644
+ requestor = Requestor.new(sdk_key, config)
645
+ PollingProcessor.new(config, requestor)
646
+ end
647
+ end
648
+
649
+ #
650
+ # @param key [String]
651
+ # @param context [LDContext]
652
+ # @param default [Object]
653
+ #
654
+ # @return [Array<EvaluationDetail, [LaunchDarkly::Impl::Model::FeatureFlag, nil], [String, nil]>]
655
+ #
656
+ def variation_with_flag(key, context, default)
657
+ evaluate_internal(key, context, default, false)
658
+ end
659
+
660
+ #
661
+ # @param key [String]
662
+ # @param context [LDContext]
663
+ # @param default [Object]
664
+ # @param with_reasons [Boolean]
665
+ #
666
+ # @return [Array<EvaluationDetail, [LaunchDarkly::Impl::Model::FeatureFlag, nil], [String, nil]>]
667
+ #
668
+ def evaluate_internal(key, context, default, with_reasons)
669
+ if @config.offline?
670
+ return Evaluator.error_result(EvaluationReason::ERROR_CLIENT_NOT_READY, default), nil, nil
671
+ end
672
+
673
+ if context.nil?
674
+ @config.logger.error { "[LDClient] Must specify context" }
675
+ detail = Evaluator.error_result(EvaluationReason::ERROR_USER_NOT_SPECIFIED, default)
676
+ return detail, nil, "no context provided"
677
+ end
678
+
679
+ unless context.valid?
680
+ @config.logger.error { "[LDClient] Context was invalid for evaluation of flag '#{key}' (#{context.error}); returning default value" }
681
+ detail = Evaluator.error_result(EvaluationReason::ERROR_USER_NOT_SPECIFIED, default)
682
+ return detail, nil, context.error
683
+ end
684
+
685
+ unless initialized?
686
+ if @store.initialized?
687
+ @config.logger.warn { "[LDClient] Client has not finished initializing; using last known values from feature store" }
688
+ else
689
+ @config.logger.error { "[LDClient] Client has not finished initializing; feature store unavailable, returning default value" }
690
+ detail = Evaluator.error_result(EvaluationReason::ERROR_CLIENT_NOT_READY, default)
691
+ record_unknown_flag_eval(key, context, default, detail.reason, with_reasons)
692
+ return detail, nil, "client not initialized"
693
+ end
694
+ end
695
+
696
+ begin
697
+ feature = @store.get(FEATURES, key)
698
+ rescue
699
+ # Ignored
700
+ end
701
+
702
+ if feature.nil?
703
+ @config.logger.info { "[LDClient] Unknown feature flag \"#{key}\". Returning default value" }
704
+ detail = Evaluator.error_result(EvaluationReason::ERROR_FLAG_NOT_FOUND, default)
705
+ record_unknown_flag_eval(key, context, default, detail.reason, with_reasons)
706
+ return detail, nil, "feature flag not found"
707
+ end
708
+
709
+ begin
710
+ (res, _) = @evaluator.evaluate(feature, context)
711
+ unless res.prereq_evals.nil?
712
+ res.prereq_evals.each do |prereq_eval|
713
+ record_prereq_flag_eval(prereq_eval.prereq_flag, prereq_eval.prereq_of_flag, context, prereq_eval.detail, with_reasons)
714
+ end
715
+ end
716
+ detail = res.detail
717
+ if detail.default_value?
718
+ detail = EvaluationDetail.new(default, nil, detail.reason)
719
+ end
720
+ record_flag_eval(feature, context, detail, default, with_reasons)
721
+ [detail, feature, nil]
722
+ rescue => exn
723
+ Util.log_exception(@config.logger, "Error evaluating feature flag \"#{key}\"", exn)
724
+ detail = Evaluator.error_result(EvaluationReason::ERROR_EXCEPTION, default)
725
+ record_flag_eval_error(feature, context, default, detail.reason, with_reasons)
726
+ [detail, feature, exn.to_s]
727
+ end
728
+ end
729
+
730
+ private def record_flag_eval(flag, context, detail, default, with_reasons)
731
+ add_experiment_data = experiment?(flag, detail.reason)
732
+ @event_processor.record_eval_event(
733
+ context,
734
+ flag[:key],
735
+ flag[:version],
736
+ detail.variation_index,
737
+ detail.value,
738
+ (add_experiment_data || with_reasons) ? detail.reason : nil,
739
+ default,
740
+ add_experiment_data || flag[:trackEvents] || false,
741
+ flag[:debugEventsUntilDate],
742
+ nil,
743
+ flag[:samplingRatio],
744
+ !!flag[:excludeFromSummaries]
745
+ )
746
+ end
747
+
748
+ private def record_prereq_flag_eval(prereq_flag, prereq_of_flag, context, detail, with_reasons)
749
+ add_experiment_data = experiment?(prereq_flag, detail.reason)
750
+ @event_processor.record_eval_event(
751
+ context,
752
+ prereq_flag[:key],
753
+ prereq_flag[:version],
754
+ detail.variation_index,
755
+ detail.value,
756
+ (add_experiment_data || with_reasons) ? detail.reason : nil,
757
+ nil,
758
+ add_experiment_data || prereq_flag[:trackEvents] || false,
759
+ prereq_flag[:debugEventsUntilDate],
760
+ prereq_of_flag[:key],
761
+ prereq_flag[:samplingRatio],
762
+ !!prereq_flag[:excludeFromSummaries]
763
+ )
764
+ end
765
+
766
+ private def record_flag_eval_error(flag, context, default, reason, with_reasons)
767
+ @event_processor.record_eval_event(context, flag[:key], flag[:version], nil, default, with_reasons ? reason : nil, default,
768
+ flag[:trackEvents], flag[:debugEventsUntilDate], nil, flag[:samplingRatio], !!flag[:excludeFromSummaries])
769
+ end
770
+
771
+ #
772
+ # @param flag_key [String]
773
+ # @param context [LaunchDarkly::LDContext]
774
+ # @param default [any]
775
+ # @param reason [LaunchDarkly::EvaluationReason]
776
+ # @param with_reasons [Boolean]
777
+ #
778
+ private def record_unknown_flag_eval(flag_key, context, default, reason, with_reasons)
779
+ @event_processor.record_eval_event(context, flag_key, nil, nil, default, with_reasons ? reason : nil, default,
780
+ false, nil, nil, 1, false)
781
+ end
782
+
783
+ private def experiment?(flag, reason)
784
+ return false unless reason
785
+
786
+ if reason.in_experiment
787
+ return true
788
+ end
789
+
790
+ case reason[:kind]
791
+ when 'RULE_MATCH'
792
+ index = reason[:ruleIndex]
793
+ unless index.nil?
794
+ rules = flag[:rules] || []
795
+ return index >= 0 && index < rules.length && rules[index][:trackEvents]
796
+ end
797
+ when 'FALLTHROUGH'
798
+ return !!flag[:trackEventsFallthrough]
799
+ end
800
+ false
801
+ end
802
+ end
803
+
804
+ #
805
+ # Used internally when the client is offline.
806
+ # @private
807
+ #
808
+ class NullUpdateProcessor
809
+ def start
810
+ e = Concurrent::Event.new
811
+ e.set
812
+ e
813
+ end
814
+
815
+ def initialized?
816
+ true
817
+ end
818
+
819
+ def stop
820
+ end
821
+ end
822
+ end