Class: Fluent::NorikraPlugin::Target

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, config) ⇒ Target

Returns a new instance of Target.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fluent/plugin/norikra/target.rb', line 23

def initialize(target, config)
  @name = target
  @escaped_name = self.class.escape(@name)
  @auto_field = config.auto_field.nil? ? true : config.auto_field
  @time_key = config.time_key
  @escape_fieldname = config.escape_fieldname

  @filter = RecordFilter.new(*([:include, :include_regexp, :exclude, :exclude_regexp].map{|s| config.filter_params[s]}))
  @fields = config.field_definitions
  if @time_key
    @fields[:integer].push @time_key
  end
  @queries = config.query_generators.map{|g| g.generate(@name, @escaped_name)}
end

Instance Attribute Details

#auto_fieldObject

Returns the value of attribute auto_field.



3
4
5
# File 'lib/fluent/plugin/norikra/target.rb', line 3

def auto_field
  @auto_field
end

#escaped_nameObject (readonly)

Returns the value of attribute escaped_name.



4
5
6
# File 'lib/fluent/plugin/norikra/target.rb', line 4

def escaped_name
  @escaped_name
end

#fieldsObject

Returns the value of attribute fields.



3
4
5
# File 'lib/fluent/plugin/norikra/target.rb', line 3

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/fluent/plugin/norikra/target.rb', line 3

def name
  @name
end

#queriesObject

Returns the value of attribute queries.



3
4
5
# File 'lib/fluent/plugin/norikra/target.rb', line 3

def queries
  @queries
end

#time_keyObject

Returns the value of attribute time_key.



3
4
5
# File 'lib/fluent/plugin/norikra/target.rb', line 3

def time_key
  @time_key
end

Class Method Details

.escape(src) ⇒ Object



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

def self.escape(src)
  if src.nil? || src.empty?
    return 'FluentdGenerated'
  end

  dst = src.gsub(/[^_a-zA-Z0-9]/, '_')
  unless dst =~ /^[a-zA-Z]([_a-zA-Z0-9]*[a-zA-Z0-9])?$/
    unless dst =~ /^[a-zA-Z]/
      dst = 'Fluentd' + dst
    end
    unless dst =~ /[a-zA-Z0-9]$/
      dst = dst + 'Generated'
    end
  end
  dst
end

Instance Method Details

#escape_recursive(record) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/norikra/target.rb', line 51

def escape_recursive(record)
  return record unless record.is_a?(Hash) || record.is_a?(Array)
  return record.map{|v| escape_recursive(v) } if record.is_a?(Array)

  # Hash
  r = {}
  record.keys.each do |key|
    k = if key =~ /[^$_a-zA-Z0-9]/
          key.gsub(/[^$_a-zA-Z0-9]/, '_')
        else
          key
        end
    v = escape_recursive(record[key])
    r[k] = v
  end
  r
end

#filter(time, record) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fluent/plugin/norikra/target.rb', line 38

def filter(time, record)
  r = @filter.filter(record)
  if @time_key
    # Fluentd time (sec) -> Norikra timestamp (milliseconds)
    r = r.merge({ @time_key => time * 1000 })
  end
  if @escape_fieldname
    escape_recursive(r)
  else
    r
  end
end

#reserve_fieldsObject



69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin/norikra/target.rb', line 69

def reserve_fields
  f = {}
  @fields.keys.each do |type_sym|
    @fields[type_sym].each do |fieldname|
      f[fieldname] = type_sym.to_s
    end
  end
  f
end