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
16
17
18
# 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

  Redis::Client.default = Redis.new(**Gremlin.configuration.redis)
  @r = Redis.new(**Gremlin.configuration.redis)
  @values = Redis::NativeHash.find(retention_key) || Redis::NativeHash.new
  @values.redis = Redis.new(**Gremlin.configuration.redis)
  @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



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

def default
  nil
end

#helpObject



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

def help
  @docstring
end

#help_stringObject



44
45
46
# File 'lib/gremlin/instruments.rb', line 44

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

#nodeObject



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

def node
  `hostname`.strip
end

#reprObject



48
49
50
51
52
53
54
55
# File 'lib/gremlin/instruments.rb', line 48

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



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

def retention_key
  nil
end

#typeObject



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

def type
  nil
end

#type_stringObject



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

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

#valuesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gremlin/instruments.rb', line 57

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