Class: Gremlin::Instruments::Summary

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

Defined Under Namespace

Classes: Value

Instance Attribute Summary

Attributes inherited from Base

#base_labels, #docstring, #name

Instance Method Summary collapse

Methods inherited from Base

#help, #help_string, #initialize, #node, #type_string

Constructor Details

This class inherits a constructor from Gremlin::Instruments::Base

Instance Method Details

#defaultObject



83
84
85
# File 'lib/gremlin/instruments/summary.rb', line 83

def default
  Gremlin::Quantile::Estimator.new
end

#get(labels = {}) ⇒ Object



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

def get(labels={})
  @mutex.synchronize do
    val = @values[labels.to_json]
    return val if val.nil?

    deserialized = JSON.parse(val)
    Value.new(unpack(deserialized))
  end
end

#observe(labels = {}, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gremlin/instruments/summary.rb', line 17

def observe(labels={}, value)
  @mutex.synchronize do
    e = @values[labels.to_json]

    if e.nil?
      estimator = default
    else
      deserialized = JSON.parse(e)
      estimator = unpack(deserialized)
    end
    sum = estimator.observe(value)

    @values[labels.to_json] = estimator.serialize
    @values.save

    sum
  end
end

#reprObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gremlin/instruments/summary.rb', line 64

def repr
  vals = values.each_with_object({}) { |(l,v), m| m[l.merge(@base_labels)] = v }

  values = vals.each_with_object({}) do |(labels, value), memo|
    if value.is_a? Hash
      value.each do |b, i|
        memo[labels.merge({quantile: b})] = i
      end
    end
  end

  {
    name: @name,
    type: type_string,
    help: help_string,
    values: values
  }
end

#retention_keyObject



91
92
93
# File 'lib/gremlin/instruments/summary.rb', line 91

def retention_key
  "gremlin_prometheus_#{node}_summary_#{name}"
end

#typeObject



87
88
89
# File 'lib/gremlin/instruments/summary.rb', line 87

def type
  :summary
end

#unpack(metric_hash) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/gremlin/instruments/summary.rb', line 56

def unpack(metric_hash)
  Gremlin::Quantile::Estimator.new(metric_hash["buffer"],
                                 metric_hash["head"],
                                 metric_hash["observations"].to_i,
                                 metric_hash["sum"].to_f,
                                 *metric_hash["invariants"].map { |i| Gremlin::Quantile::Quantile.new i["quantile"], i["inaccuracy"] })
end

#valuesObject



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

def values
  @mutex.synchronize do
    @values.each_with_object({}) do |(labels, value), memo|
      deserialized = JSON.parse(value)
      e = unpack(deserialized)
      memo[JSON.parse(labels)] = Value.new(e)
    end
  end
end