Class: StatfulClient

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

Overview

Statful Client Instance

Defined Under Namespace

Classes: MyHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Object

Initialize the client

Parameters:

  • config (Hash) (defaults to: {})

    Client bootstrap configuration

Options Hash (config):

  • :host (String)

    Destination host required

  • :port (String)

    Destination port required

  • :transport (String)

    Transport protocol, one of (udp or http) required

  • :timeout (Integer)

    Timeout for http transport

  • :token (String)

    Authentication account token for http transport

  • :app (String)

    Global metric app tag

  • :dryrun (TrueClass/FalseClass)

    Enable dry-run mode

  • :logger (Object)

    Logger instance that supports debug (if dryrun is enabled) and error methods

  • :tags (Hash)

    Global list of metric tags

  • :sample_rate (Integer)

    Global sample rate (as a percentage), between: (1-100)

  • :namespace (String)

    Global default namespace

  • :flush_size (Integer)

    Buffer flush upper size limit



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/client.rb', line 31

def initialize(config = {})
  user_config = MyHash[config].symbolize_keys

  if !user_config.has_key?(:transport) || !%w(udp http).include?(user_config[:transport])
    raise ArgumentError.new('Transport is missing or invalid')
  end

  if user_config[:transport] == 'http'
    raise ArgumentError.new('Token is missing') if user_config[:token].nil?
  end

  if user_config.has_key?(:sample_rate) && !user_config[:sample_rate].between?(1, 100)
    raise ArgumentError.new('Sample rate is not within range (1-100)')
  end

  default_config = {
    :host => 'api.statful.com',
    :port => 443,
    :transport => 'http',
    :tags => {},
    :sample_rate => 100,
    :namespace => 'application',
    :flush_size => 5
  }

  @config = default_config.merge(user_config)
  @logger = @config[:logger]
  @buffer = []
  @http = Net::HTTP.new(@config[:host], @config[:port])

  self
end

Instance Attribute Details

#configHash (readonly)

Current client config

Returns:

  • (Hash)

    the current value of config



8
9
10
# File 'lib/client.rb', line 8

def config
  @config
end

Instance Method Details

#counter(name, value, options = {}) ⇒ Object

Sends a counter

Parameters:

  • name (String)

    Name of the counter

  • value (Numeric)

    Increment/Decrement value, this will be truncated with ‘to_int`

  • options (Hash) (defaults to: {})

    The options to apply to the metric

Options Hash (options):

  • :tags (Hash)

    Tags to associate to the metric

  • :agg (Array<String>)

    List of aggregations to be applied by the Statful

  • :agg_freq (Integer)

    Aggregation frequency in seconds

  • :namespace (String)

    Namespace of the metric



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/client.rb', line 100

def counter(name, value, options = {})
  tags = @config[:tags]
  tags = tags.merge(options[:tags]) unless options[:tags].nil?

  aggregations = %w(sum count)
  aggregations.concat(options[:agg]) unless options[:agg].nil?

  opts = {
    :agg_freq => 10,
    :namespace => 'application'
  }.merge(MyHash[options].symbolize_keys)

  opts[:tags] = tags
  opts[:agg] = aggregations

  _put("counter.#{name}", opts[:tags], value.to_i, opts[:agg], opts[:agg_freq], @config[:sample_rate], opts[:namespace])
end

#flush_metricsObject

Flush metrics buffer



147
148
149
# File 'lib/client.rb', line 147

def flush_metrics
  flush
end

#gauge(name, value, options = {}) ⇒ Object

Sends a gauge

Parameters:

  • name (String)

    Name of the gauge

  • value (Numeric)

    Value of the metric

  • options (Hash) (defaults to: {})

    The options to apply to the metric

Options Hash (options):

  • :tags (Hash)

    Tags to associate to the metric

  • :agg (Array<String>)

    List of aggregations to be applied by Statful

  • :agg_freq (Integer)

    Aggregation frequency in seconds

  • :namespace (String)

    Namespace of the metric



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/client.rb', line 127

def gauge(name, value, options = {})
  tags = @config[:tags]
  tags = tags.merge(options[:tags]) unless options[:tags].nil?

  aggregations = %w(last)
  aggregations.concat(options[:agg]) unless options[:agg].nil?

  opts = {
    :agg_freq => 10,
    :namespace => 'application'
  }.merge(MyHash[options].symbolize_keys)

  opts[:tags] = tags
  opts[:agg] = aggregations

  _put("gauge.#{name}", opts[:tags], value, opts[:agg], opts[:agg_freq], @config[:sample_rate], opts[:namespace])
end

#newObject



11
12
13
# File 'lib/client.rb', line 11

def new
  self
end

#put(metric, tags, value, agg = [], agg_freq = 10, sample_rate = nil, namespace = 'application', timestamp = nil) ⇒ Object

Adds a new metric to the buffer

Parameters:

  • metric (String)

    Name of the metric, ex: ‘response_time`

  • value (Numeric)

    Value of the metric

  • tags (Hash)

    Tags to associate to the metric

  • agg (Array<String>) (defaults to: [])

    List of aggregations to be applied by Statful

  • agg_freq (Integer) (defaults to: 10)

    Aggregation frequency in seconds

  • sample_rate (Integer) (defaults to: nil)

    Sampling rate, between: (1-100)

  • namespace (String) (defaults to: 'application')

    Namespace of the metric

  • timestamp (Integer) (defaults to: nil)

    Timestamp of the metric



162
163
164
165
166
167
# File 'lib/client.rb', line 162

def put(metric, tags, value, agg = [], agg_freq = 10, sample_rate = nil, namespace = 'application', timestamp = nil)
  _tags = @config[:tags]
  _tags = _tags.merge(tags) unless tags.nil?

  _put(metric, _tags, value, agg, agg_freq, sample_rate, namespace, timestamp)
end

#timer(name, value, options = {}) ⇒ Object

Sends a timer

Parameters:

  • name (String)

    Name of the timer

  • value (Numeric)

    Value of the metric

  • options (Hash) (defaults to: {})

    The options to apply to the metric

Options Hash (options):

  • :tags (Hash)

    Tags to associate to the metric

  • :agg (Array<String>)

    List of aggregations to be applied by Statful

  • :agg_freq (Integer)

    Aggregation frequency in seconds

  • :namespace (String)

    Namespace of the metric



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/client.rb', line 73

def timer(name, value, options = {})
  tags = @config[:tags].merge({:unit => 'ms'})
  tags = tags.merge(options[:tags]) unless options[:tags].nil?

  aggregations = %w(avg p90 count)
  aggregations.concat(options[:agg]) unless options[:agg].nil?

  opts = {
    :agg_freq => 10,
    :namespace => 'application'
  }.merge(MyHash[options].symbolize_keys)

  opts[:tags] = tags
  opts[:agg] = aggregations

  _put("timer.#{name}", opts[:tags], value, opts[:agg], opts[:agg_freq], @config[:sample_rate], opts[:namespace])
end