Class: Fluent::StatsitePlugin::Metric

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

Constant Summary collapse

TYPE =
%w(kv g ms h c s)
HASH_FIELD =
%w(
  type
  key
  value
)
STRING_PATTERN =
/^(#{MetricFormat::PATTERN}):(#{MetricFormat::PATTERN})\|(#{TYPE.join('|')})$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, type) ⇒ Metric

Returns a new instance of Metric.



16
17
18
19
20
# File 'lib/fluent/plugin/statsite/metric.rb', line 16

def initialize(key, value, type)
  @key = key
  @value = value
  @type = type
end

Class Method Details

.validate(m) ⇒ Object



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
62
63
# File 'lib/fluent/plugin/statsite/metric.rb', line 32

def self.validate(m)
  if not (m.class == Hash or m.class == String)
    raise ConfigError, "a type of metrics element must be Hash or String, but specified as #{m.class}"
  end

  case m
  when Hash
    m.keys.each do |k|
      if not HASH_FIELD.member?(k)
        raise ConfigError, "invalid metrics element hash key: #{k}"
      end
    end

    HASH_FIELD.each do |f|
      if not m.has_key?(f)
          raise ConfigError, "metrics element must contain '#{f}'"
      end
    end

    if not TYPE.member?(m['type'])
      raise ConfigError, "metrics type must be one of the following: #{TYPE.join(' ')}, but specified as #{m['type']}"
    end

    new(MetricFormat.validate(m['key']), MetricFormat.validate(m['value']), m['type'])
  when String
    if (STRING_PATTERN =~ m).nil?
      raise ConfigError, "metrics string must be #{STRING_PATTERN}, but specified as #{m}"
    end

    new(MetricFormat.validate($1), MetricFormat.validate($2), $3)
  end
end

Instance Method Details

#convert(record) ⇒ Object



22
23
24
25
26
# File 'lib/fluent/plugin/statsite/metric.rb', line 22

def convert(record)
  k = @key.convert(record)
  v = @value.convert(record)
  (k.nil? or v.nil?) ? nil : "#{k}:#{v}|#{@type}\n"
end

#to_sObject



28
29
30
# File 'lib/fluent/plugin/statsite/metric.rb', line 28

def to_s
  "Metric(#{@key}, #{@value}, type=#{@type})"
end