Class: GraphQL::Hive::Sampler

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-hive/sampler.rb

Overview

Sampler instance for usage reporter

Instance Method Summary collapse

Constructor Details

#initialize(sampling_options, logger = nil) ⇒ Sampler

Returns a new instance of Sampler.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql-hive/sampler.rb', line 7

def initialize(sampling_options, logger = nil)
  # backwards compatibility with old `collect_usage_sampling` field
  if sampling_options.is_a?(Numeric)
    logger&.warn(
      "`collect_usage_sampling` is deprecated for fixed sampling rates, " \
      "use `collect_usage_sampling: { sample_rate: XX }` instead"
    )
    passed_sampling_rate = sampling_options
    sampling_options = {sample_rate: passed_sampling_rate}
  end

  sampling_options ||= {}

  @sampler = if sampling_options[:sampler]
    Sampling::DynamicSampler.new(
      sampling_options[:sampler],
      sampling_options[:at_least_once],
      sampling_options[:key_generator]
    )
  else
    Sampling::BasicSampler.new(
      sampling_options[:sample_rate],
      sampling_options[:at_least_once],
      sampling_options[:key_generator]
    )
  end
end

Instance Method Details

#sample?(operation) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/graphql-hive/sampler.rb', line 35

def sample?(operation)
  @sampler.sample?(operation)
end