Class: Fluent::SamplingFilterOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_sampling_filter.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fluent/plugin/out_sampling_filter.rb', line 15

def configure(conf)
  super

  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  elsif @add_prefix.empty?
    raise Fluent::ConfigError, "either of 'add_prefix' or 'remove_prefix' must be specified"
  end
  @added_prefix_string = @add_prefix + '.' unless @add_prefix.empty?

  @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

#emit(tag, es, chain) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/out_sampling_filter.rb', line 54

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

  pairs = []

  # 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
        pairs.push [time, record]
      end
    end
  else
    es.each do |time,record|
      c = (@counts[t] = @counts.fetch(t, 0) + 1)
      if c % @interval == 0
        pairs.push [time, record]
        # reset only just before @counts[t] is to be bignum from fixnum
        @counts[t] = 0 if c > 0x6fffffff
      end
    end
  end

  emit_sampled(tag, pairs)

  chain.next
end

#emit_sampled(tag, time_record_pairs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin/out_sampling_filter.rb', line 38

def emit_sampled(tag, time_record_pairs)
  if @remove_prefix and
      ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix)
    tag = tag[@removed_length..-1]
  end
  if tag.length > 0
    tag = @added_prefix_string + tag if @added_prefix_string
  else
    tag = @add_prefix
  end

  time_record_pairs.each {|t,r|
    router.emit(tag, t, r)
  }
end