Class: Fluent::NorikraPlugin::FetchRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/norikra/fetch_request.rb

Constant Summary collapse

METHODS =
[:event, :sweep]
TAG_TYPES =
['query_name', 'field', 'string']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, target, interval, tag_type, tag_arg, tag_prefix) ⇒ FetchRequest

Returns a new instance of FetchRequest.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 9

def initialize(method, target, interval, tag_type, tag_arg, tag_prefix)
  raise ArgumentError, "unknown method '#{method}'" unless METHODS.include?(method.to_sym)

  @method = method.to_sym
  @target = target
  @interval = interval.to_i

  raise ArgumentError, "unknown tag type specifier '#{tag_type}'" unless TAG_TYPES.include?(tag_type.to_s)
  raw_tag_prefix = tag_prefix.to_s
  if (! raw_tag_prefix.empty?) && (! raw_tag_prefix.end_with?('.')) # tag_prefix specified, and ends without dot
    raw_tag_prefix += '.'
  end

  @tag_generator = case tag_type.to_s
                   when 'query_name' then lambda{|query_name,record| raw_tag_prefix + query_name}
                   when 'field'      then lambda{|query_name,record| raw_tag_prefix + (record[tag_arg] || 'NULL')}
                   when 'string'     then lambda{|query_name,record| raw_tag_prefix + tag_arg}
                   else
                     raise "bug"
                   end
  @time = Time.now + 1 # should be fetched soon ( 1sec later )
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



6
7
8
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 6

def interval
  @interval
end

#methodObject

Returns the value of attribute method.



6
7
8
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 6

def method
  @method
end

#tag_generatorObject

Returns the value of attribute tag_generator.



6
7
8
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 6

def tag_generator
  @tag_generator
end

#tag_prefixObject

Returns the value of attribute tag_prefix.



6
7
8
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 6

def tag_prefix
  @tag_prefix
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 6

def target
  @target
end

#timeObject

Returns the value of attribute time.



7
8
9
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 7

def time
  @time
end

Instance Method Details

#<=>(other) ⇒ Object



32
33
34
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 32

def <=>(other)
  self.time <=> other.time
end

#event(client) ⇒ Object



63
64
65
66
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 63

def event(client)
  events = client.event(@target) # [[time(int from epoch), event], ...]
  {@target => events}
end

#fetch(client) ⇒ Object

returns hash: { tag => [[time, record], …], … }



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/norikra/fetch_request.rb', line 41

def fetch(client)
  # events { query_name => [[time, record], ...], ... }
  events = case @method
           when :event then event(client)
           when :sweep then sweep(client)
           else
             raise "BUG: unknown method: #{@method}"
           end

  output = {}

  events.keys.each do |query_name|
    events[query_name].each do |time, record|
      tag = @tag_generator.call(query_name, record)
      output[tag] ||= []
      output[tag] << [time, record]
    end
  end

  output
end

#next!Object



36
37
38
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 36

def next!
  @time = Time.now + @interval
end

#sweep(client) ⇒ Object



68
69
70
# File 'lib/fluent/plugin/norikra/fetch_request.rb', line 68

def sweep(client)
  client.sweep(@target) # {query_name => event_array, ...}
end