Class: Fluent::RedisCounterOutput::Pattern

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf_element) ⇒ Pattern

Returns a new instance of Pattern.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fluent/plugin/out_redis_counter.rb', line 98

def initialize(conf_element)
  if !conf_element.has_key?('count_key') && !conf_element.has_key?('count_key_format')
    raise RedisCounterException, '"count_key" or "count_key_format" is required.'
  end
  if conf_element.has_key?('count_key') && conf_element.has_key?('count_key_format')
    raise RedisCounterException, 'both "count_key" and "count_key_format" are specified.'
  end

  if conf_element.has_key?('count_key')
    @count_key = conf_element['count_key']
  else
    if conf_element.has_key?('localtime') && conf_element.has_key?('utc')
      raise RedisCounterException, 'both "localtime" and "utc" are specified.'
    end
    is_localtime = true
    if conf_element.has_key?('utc')
      is_localtime = false
    end
    @count_key_format = [conf_element['count_key_format'], is_localtime]
    @record_value_formatter = RecordValueFormatter.new(@count_key_format[0])
  end

  if conf_element.has_key?('count_value_key')
    @count_value_key = conf_element['count_value_key']
  else
    @count_value = 1
    if conf_element.has_key?('count_value')
      begin
        @count_value = Integer(conf_element['count_value'])
      rescue
        raise RedisCounterException, 'invalid "count_value", integer required.'
      end
    end
  end

  @matches = {}
  conf_element.each_pair.select { |key, value|
    key =~ /^match_/
  }.each { |key, value|
    name = key['match_'.size .. key.size]
    @matches[name] = Regexp.new(value)
  }
end

Instance Attribute Details

#count_valueObject (readonly)

Returns the value of attribute count_value.



96
97
98
# File 'lib/fluent/plugin/out_redis_counter.rb', line 96

def count_value
  @count_value
end

#count_value_keyObject (readonly)

Returns the value of attribute count_value_key.



96
97
98
# File 'lib/fluent/plugin/out_redis_counter.rb', line 96

def count_value_key
  @count_value_key
end

#matchesObject (readonly)

Returns the value of attribute matches.



96
97
98
# File 'lib/fluent/plugin/out_redis_counter.rb', line 96

def matches
  @matches
end

Instance Method Details

#get_count_key(time, record) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/fluent/plugin/out_redis_counter.rb', line 151

def get_count_key(time, record)
  if @count_key_format == nil
    @count_key
  else
    count_key = @record_value_formatter.key(record)
    formatter = TimeFormatter.new(count_key, @count_key_format[1])
    formatter.format(time)
  end
end

#get_count_value(record) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/fluent/plugin/out_redis_counter.rb', line 161

def get_count_value(record)
  if @count_value_key
    ret = record[@count_value_key] || 0
    return ret.kind_of?(Integer) ? ret : 0
  else
    if @count_value
      return @count_value
    end
  end
end

#is_match?(record) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
# File 'lib/fluent/plugin/out_redis_counter.rb', line 142

def is_match?(record)
  @matches.each_pair{ |key, value|
    if !record.has_key?(key) || !(record[key] =~ value)
      return false
    end
  }
  return true
end