Class: GraphQL::Hive::Sampling::DynamicSampler
- Inherits:
-
Object
- Object
- GraphQL::Hive::Sampling::DynamicSampler
- Includes:
- SamplingContext
- Defined in:
- lib/graphql-hive/sampling/dynamic_sampler.rb
Overview
Dynamic sampling for operations reporting
Instance Method Summary collapse
-
#initialize(client_sampler, at_least_once, key_generator) ⇒ DynamicSampler
constructor
A new instance of DynamicSampler.
- #sample?(operation) ⇒ Boolean
Constructor Details
#initialize(client_sampler, at_least_once, key_generator) ⇒ DynamicSampler
Returns a new instance of DynamicSampler.
12 13 14 15 16 |
# File 'lib/graphql-hive/sampling/dynamic_sampler.rb', line 12 def initialize(client_sampler, at_least_once, key_generator) @sampler = client_sampler @tracked_operations = {} @key_generator = key_generator || DEFAULT_SAMPLE_KEY if at_least_once end |
Instance Method Details
#sample?(operation) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/graphql-hive/sampling/dynamic_sampler.rb', line 18 def sample?(operation) sample_context = get_sample_context(operation) if @key_generator operation_key = @key_generator.call(sample_context) unless @tracked_operations.key?(operation_key) @tracked_operations[operation_key] = true return true end end SecureRandom.random_number <= @sampler.call(sample_context) end |