Class: SystemMetrics::Instrument::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/system_metrics/instrument/base.rb

Overview

Base class for System Metric instruments. The default implementations for the methods in this class are all based on a regular expression that is matched against a pattern. Custom intruments that simply need to match against a notfication name can easily extend this class like:

class SearchInstrument < SystemMetrics::Instrument::Base
  def initialize
    super /search$/
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Base

Create an instrument that will match notification names on the given pattern.



20
21
22
# File 'lib/system_metrics/instrument/base.rb', line 20

def initialize(pattern)
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



16
17
18
# File 'lib/system_metrics/instrument/base.rb', line 16

def pattern
  @pattern
end

Instance Method Details

#handles?(event) ⇒ Boolean

Declares whether this instrument handles the given event type.

Please Note: Even if the instrument would ultimately like to ignore the event, it should still return true if it generally handles events like the one passed.

Returns:

  • (Boolean)


43
44
45
# File 'lib/system_metrics/instrument/base.rb', line 43

def handles?(event)
  (event.name =~ pattern) != nil
end

#ignore?(event) ⇒ Boolean

Indicates whether the given event should be completely ingored and not collected. This is called only if #handles?(event) returns ‘true`

Returns:

  • (Boolean)


50
51
52
# File 'lib/system_metrics/instrument/base.rb', line 50

def ignore?(event)
  false
end

#mapped_pathsObject

Holds the mapped paths used in prunning.



25
26
27
# File 'lib/system_metrics/instrument/base.rb', line 25

def mapped_paths
  @mapped_paths ||= default_mapped_paths
end

#prepare(event) ⇒ Object

Provides an opportunity to modify the event before it’s collected and stored. This is where you would normally modify the payload to add, remove, or format its elements.



57
58
59
# File 'lib/system_metrics/instrument/base.rb', line 57

def prepare(event)
  # Modify the payload if you care to
end

#prune_path(raw_path) ⇒ Object

Prune paths based on the mapped paths set.



30
31
32
33
34
35
36
# File 'lib/system_metrics/instrument/base.rb', line 30

def prune_path(raw_path)
  mapped_paths.each do |path, replacement|
    next unless path.present?
    raw_path = raw_path.gsub(path, replacement)
  end
  raw_path
end