Class: Fluent::Plugin::PerfTools::Base

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

Overview

base class for all perf-tools command

provides common behavior for all command

Direct Known Subclasses

Cachestat

Constant Summary collapse

DEFAULT_INTERVAL =
30
DEFAULT_COMMAND_ARGS =
""
DEFAULT_TIME_KEY =
"TIME"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_args:, interval:) ⇒ Base

Returns a new instance of Base.



43
44
45
46
47
# File 'lib/fluent/plugin/perf_tools/base.rb', line 43

def initialize(command_args:, interval:)
  @command_args = command_args || DEFAULT_COMMAND_ARGS
  @interval = interval || DEFAULT_INTERVAL
  @time_key = DEFAULT_TIME_KEY
end

Class Attribute Details

.commandObject (readonly)

Returns the value of attribute command.



14
15
16
# File 'lib/fluent/plugin/perf_tools/base.rb', line 14

def command
  @command
end

.discard_regexpObject (readonly)

Returns the value of attribute discard_regexp.



14
15
16
# File 'lib/fluent/plugin/perf_tools/base.rb', line 14

def discard_regexp
  @discard_regexp
end

.locationObject (readonly)

Returns the value of attribute location.



14
15
16
# File 'lib/fluent/plugin/perf_tools/base.rb', line 14

def location
  @location
end

Instance Attribute Details

#command_argsObject (readonly)

Returns the value of attribute command_args.



41
42
43
# File 'lib/fluent/plugin/perf_tools/base.rb', line 41

def command_args
  @command_args
end

#intervalObject (readonly)

Returns the value of attribute interval.



41
42
43
# File 'lib/fluent/plugin/perf_tools/base.rb', line 41

def interval
  @interval
end

#time_keyObject (readonly)

Returns the value of attribute time_key.



41
42
43
# File 'lib/fluent/plugin/perf_tools/base.rb', line 41

def time_key
  @time_key
end

Class Method Details

.command_pathObject



36
37
38
# File 'lib/fluent/plugin/perf_tools/base.rb', line 36

def command_path
  File.expand_path(File.join(File.expand_path(__dir__), "../../../../perf-tools/#{location}/#{command}"))
end

.set_command(command) ⇒ Object



16
17
18
# File 'lib/fluent/plugin/perf_tools/base.rb', line 16

def set_command(command)
  @command = command
end

.set_discard_regexp(regexp) ⇒ Object



20
21
22
# File 'lib/fluent/plugin/perf_tools/base.rb', line 20

def set_discard_regexp(regexp)
  @discard_regexp = regexp
end

.set_location(location) ⇒ Object



24
25
26
# File 'lib/fluent/plugin/perf_tools/base.rb', line 24

def set_location(location)
  @location = location
end

.set_time_key(key) ⇒ Object



32
33
34
# File 'lib/fluent/plugin/perf_tools/base.rb', line 32

def set_time_key(key)
  @time_key = key
end

.time_keyObject



28
29
30
# File 'lib/fluent/plugin/perf_tools/base.rb', line 28

def time_key
  @time_key ||= DEFAULT_TIME_KEY
end

Instance Method Details

#command_pathObject



74
75
76
# File 'lib/fluent/plugin/perf_tools/base.rb', line 74

def command_path
  self.class.command_path
end

#discard_regexpObject



78
79
80
# File 'lib/fluent/plugin/perf_tools/base.rb', line 78

def discard_regexp
  self.class.discard_regexp
end

#parse_time(record) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/fluent/plugin/perf_tools/base.rb', line 66

def parse_time(record)
  if time_key && record.respond_to?(:has_key?) && record.key?(time_key)
    Fluent::EventTime.from_time(Time.parse(record[time_key]))
  else
    Fluent::EventTime.now
  end
end

#streamObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/perf_tools/base.rb', line 49

def stream
  headers = nil
  IO.popen("sudo #{command_path} #{command_args} #{interval}").each do |line|
    next if discard_regexp && line.match(discard_regexp)

    unless headers
      headers = line.split
      next
    end

    record = headers.zip(line.split).to_h
    time = parse_time(record)

    yield time, record
  end
end