Class: GraphQL::Hive::Sampling::BasicSampler

Inherits:
Object
  • Object
show all
Includes:
SamplingContext
Defined in:
lib/graphql-hive/sampling/basic_sampler.rb

Overview

Basic sampling for operations reporting

Instance Method Summary collapse

Constructor Details

#initialize(client_sample_rate, at_least_once, key_generator) ⇒ BasicSampler

Returns a new instance of BasicSampler.



12
13
14
15
16
# File 'lib/graphql-hive/sampling/basic_sampler.rb', line 12

def initialize(client_sample_rate, at_least_once, key_generator)
  @sample_rate = client_sample_rate || 1
  @tracked_operations = {}
  @key_generator = key_generator || DEFAULT_SAMPLE_KEY if at_least_once
end

Instance Method Details

#sample?(operation) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql-hive/sampling/basic_sampler.rb', line 18

def sample?(operation)
  if @key_generator
    sample_context = get_sample_context(operation)
    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 <= @sample_rate
end