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
# File 'lib/gremlin/instruments.rb', line 6

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

  @r = Redis.new(**Gremlin.configuration.redis)
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

#cast(v) ⇒ Object



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

def cast(v)
  case type
  when :counter
    v.to_i
  when :gauge
    v.to_f
  else
    v
  end
end

#deleteObject



14
15
16
# File 'lib/gremlin/instruments.rb', line 14

def delete
  @r.del retention_key
end

#helpObject



61
62
63
# File 'lib/gremlin/instruments.rb', line 61

def help
  @docstring
end

#help_stringObject



65
66
67
# File 'lib/gremlin/instruments.rb', line 65

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

#nodeObject



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

def node
  `hostname`.strip
end

#parse(value) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/gremlin/instruments.rb', line 22

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

#reprObject



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

def repr
  parse(retention_get)
end

#repr_and_deleteObject



35
36
37
38
39
40
41
42
43
# File 'lib/gremlin/instruments.rb', line 35

def repr_and_delete
  val = nil
  @r.pipelined do
    val = retention_get
    @r.del retention_key
  end
  v = parse(val.value)
  return v
end

#retention_getObject



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

def retention_get
  @r.hgetall retention_key
end

#retention_keyObject



49
50
51
# File 'lib/gremlin/instruments.rb', line 49

def retention_key
  nil
end

#typeObject



53
54
55
# File 'lib/gremlin/instruments.rb', line 53

def type
  nil
end

#type_stringObject



57
58
59
# File 'lib/gremlin/instruments.rb', line 57

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

#valuesObject



80
81
82
83
84
# File 'lib/gremlin/instruments.rb', line 80

def values
  retention_get.each_with_object({}) do |(labels, value), memo|
    memo[JSON.parse(labels)] = cast(value)
  end
end