Class: Fluent::SamplingFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_sampling.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fluent/plugin/filter_sampling.rb', line 8

def configure(conf)
  super

  @sample_unit = case @sample_unit
                 when 'tag'
                   :tag
                 when 'all'
                   :all
                 else
                   raise Fluent::ConfigError, "sample_unit allows only 'tag' or 'all'"
                 end
  @counts = {}
  @resets = {} if @minimum_rate_per_min
end

#filter_stream(tag, es) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/fluent/plugin/filter_sampling.rb', line 23

def filter_stream(tag, es)
  t = if @sample_unit == :all
        'all'
      else
        tag
      end

  new_es = Fluent::MultiEventStream.new

  # Access to @counts SHOULD be protected by mutex, with a heavy penalty.
  # Code below is not thread safe, but @counts (counter for sampling rate) is not
  # so serious value (and probably will not be broken...),
  # then i let here as it is now.
  if @minimum_rate_per_min
    unless @resets[t]
      @resets[t] = Fluent::Engine.now + (60 - rand(30))
    end
    if Fluent::Engine.now > @resets[t]
      @resets[t] = Fluent::Engine.now + 60
      @counts[t] = 0
    end
    es.each do |time,record|
      c = (@counts[t] = @counts.fetch(t, 0) + 1)
      if c < @minimum_rate_per_min or c % @interval == 0
        new_es.add(time, record.dup)
      end
    end
  else
    es.each do |time,record|
      c = (@counts[t] = @counts.fetch(t, 0) + 1)
      if c % @interval == 0
        new_es.add(time, record.dup)
        # reset only just before @counts[t] is to be bignum from fixnum
        @counts[t] = 0 if c > 0x6fffffff
      end
    end
  end
  new_es
end