Class: ProtorClient

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/pb/version.rb,
lib/protor_client.rb

Constant Summary collapse

VERSION =
'0.1.0'
DEFAULT_BUCKET =
[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10].freeze
DEFAULT_CONFIG =
{
  host: 'localhost',
  port: 8080,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ ProtorClient

Returns a new instance of ProtorClient.

Yields:



18
19
20
21
22
23
24
25
# File 'lib/protor_client.rb', line 18

def initialize(&block)
  super(&block)

  @families = []
  @config = DEFAULT_CONFIG.dup
  @conn = nil
  yield(config) if block_given?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/protor_client.rb', line 9

def config
  @config
end

Instance Method Details

#closeconnObject



83
84
85
86
# File 'lib/protor_client.rb', line 83

def closeconn
  @conn.close
  @conn = nil
end

#counter(metric_name, value, labels = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/protor_client.rb', line 27

def counter(metric_name, value, labels = {})
  safely do 
    @families << Protomodel::Sample.new(:service => config[:service], :name => metric_name, :kind => "c", :value => value, :label => labels)
  end
  logger.debug("add counter") if logger
end

#gauge(metric_name, value, labels = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/protor_client.rb', line 34

def gauge(metric_name, value, labels = {})
  safely do
    @families << Protomodel::Sample.new(:service => config[:service], :name => metric_name, :kind => "g", :value => value, :label => labels)
  end
  logger.debug("add gauge") if logger
end

#histogram(metric_name, value, buckets = DEFAULT_BUCKET, labels = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/protor_client.rb', line 41

def histogram(metric_name, value, buckets = DEFAULT_BUCKET, labels = {})
  safely do
    @families << Protomodel::Sample.new(:service => config[:service], :name => metric_name, :kind => "h", :value => value, :label => labels, :histogramDef => buckets)
  end
  logger.debug("add histogram") if logger
end

#histogram_linear(metric_name, value, buckets, labels = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/protor_client.rb', line 48

def histogram_linear(metric_name, value, buckets, labels = {})
  safely do
    @families << Protomodel::Sample.new(:service => config[:service], :name => metric_name, :kind => "hl", :value => value, :label => labels, :histogramDef => buckets)
  end
  logger.debug("add histogram linear") if logger
end

#publishObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/protor_client.rb', line 55

def publish
  safely do 
    return unless @families.length
    openconn unless @conn
    msg = Protomodel::Samples.new(:array => @families)
    encoded = Protomodel::Samples.encode(msg)
    begin 
      timeout(5) do
        @conn.write(encoded)
        reset if @conn.gets
      end
    rescue Exception => err # second attempt
      openconn
      begin
        timeout(5) do
          @conn.write(encoded)
          reset if @conn.gets
        end
      rescue Exception => err #raise error and put on logger
        logger.error(err) if logger
        raise err unless silent
        return
      end
    end
  end
  logger.debug("publish") if logger
end