Class: Cloudscopes::Sample

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudscopes/sample.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, metric) ⇒ Sample



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloudscopes/sample.rb', line 21

def initialize(namespace, metric)
  @name = metric['name']
  @unit = metric['unit']
  @value = nil
  
  begin
    return if metric['requires'] and ! Cloudscopes.get_binding.eval(metric['requires']) 
    @value = Cloudscopes.get_binding.eval(metric['value'])
  rescue => e
    STDERR.puts("Error evaluating #{@name}: #{e}")
    puts e.backtrace
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/cloudscopes/sample.rb', line 5

def name
  @name
end

#unitObject (readonly)

Returns the value of attribute unit.



5
6
7
# File 'lib/cloudscopes/sample.rb', line 5

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/cloudscopes/sample.rb', line 5

def value
  @value
end

Instance Method Details

#dimensionsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cloudscopes/sample.rb', line 7

def dimensions
  Cloudscopes.data_dimensions.collect do |key,value|
    begin
      if ! value.start_with?('"') and value.include?('#') # user wants to expand a string expression, but can't be bothered with escaping double quotes
        { name: key, value: Cloudscopes.get_binding.eval('"' + value + '"') }
      else
        { name: key, value: Cloudscopes.get_binding.eval(value) }
      end
    rescue NameError # assume the user meant to send the static text
      { name: key, value: value }
    end
  end
end

#to_cloudwatch_metric_dataObject



39
40
41
42
43
44
45
# File 'lib/cloudscopes/sample.rb', line 39

def to_cloudwatch_metric_data
  return nil if @value.nil?
  data = { metric_name: @name, value: @value }
  data[:unit] = @unit if @unit
  data[:dimensions] = dimensions
  data
end

#validObject



35
36
37
# File 'lib/cloudscopes/sample.rb', line 35

def valid
  ! @value.nil?
end