Class: Gremlin::Instruments::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gremlin/instruments.rb

Direct Known Subclasses

Counter, Gauge, Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, docstring = "placeholder help string", base_labels = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
# File 'lib/gremlin/instruments.rb', line 6

def initialize(name, docstring="placeholder help string", base_labels={})
  @name = name
  @docstring = docstring
  @base_labels = base_labels
  @mutex = Mutex.new

  @values = Redis::NativeHash.find(retention_key) || Redis::NativeHash.new
  @values.key = retention_key
  @values.save
end

Instance Attribute Details

#base_labelsObject

Returns the value of attribute base_labels.



4
5
6
# File 'lib/gremlin/instruments.rb', line 4

def base_labels
  @base_labels
end

#docstringObject

Returns the value of attribute docstring.



4
5
6
# File 'lib/gremlin/instruments.rb', line 4

def docstring
  @docstring
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/gremlin/instruments.rb', line 4

def name
  @name
end

Instance Method Details

#defaultObject



33
34
35
# File 'lib/gremlin/instruments.rb', line 33

def default
  nil
end

#get(labels = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gremlin/instruments.rb', line 69

def get(labels={})
  v = @values[labels.to_json]
  case type
  when :counter
    v.to_i
  when :gauge
    v.to_f
  else
    v
  end
end

#helpObject



37
38
39
# File 'lib/gremlin/instruments.rb', line 37

def help
  @docstring
end

#help_stringObject



41
42
43
# File 'lib/gremlin/instruments.rb', line 41

def help_string
  "# HELP #{@name} #{help}"
end

#nodeObject



17
18
19
# File 'lib/gremlin/instruments.rb', line 17

def node
  `hostname`.strip
end

#reprObject



45
46
47
48
49
50
51
52
# File 'lib/gremlin/instruments.rb', line 45

def repr
  {
    name: @name,
    type: type_string,
    help: help_string,
    values: values.each_with_object({}) { |(l,v), m| m[l.merge(@base_labels)] = v }
  }
end

#retention_keyObject



21
22
23
# File 'lib/gremlin/instruments.rb', line 21

def retention_key
  nil
end

#typeObject



25
26
27
# File 'lib/gremlin/instruments.rb', line 25

def type
  nil
end

#type_stringObject



29
30
31
# File 'lib/gremlin/instruments.rb', line 29

def type_string
  "# TYPE #{@name} #{type}"
end

#valuesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gremlin/instruments.rb', line 54

def values
  @mutex.synchronize do
    @values.each_with_object({}) do |(labels, value), memo|
      case type
      when :counter
        memo[JSON.parse(labels)] = value.to_i
      when :gauge
        memo[JSON.parse(labels)] = value.to_f
      else
        memo[JSON.parse(labels)] = value
      end
    end
  end
end