Class: Yodeler::Client

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

Constant Summary collapse

TIMESTAMP_FORMATS =
{
  iso8601: -> { Time.now.utc.iso8601 },
  epoch:   -> { Time.now.to_i }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
18
19
20
21
22
# File 'lib/yodeler/client.rb', line 16

def initialize
  @endpoints = {}
  @default_sample_rate = 1.0
  @default_prefix = nil
  @hostname = Socket.gethostname
  @timestamp_format = :iso8601
end

Instance Attribute Details

#default_endpoint_nameObject

Returns the value of attribute default_endpoint_name.



5
6
7
# File 'lib/yodeler/client.rb', line 5

def default_endpoint_name
  @default_endpoint_name
end

#default_prefixObject

Returns the value of attribute default_prefix.



6
7
8
# File 'lib/yodeler/client.rb', line 6

def default_prefix
  @default_prefix
end

#default_sample_rateObject

Returns the value of attribute default_sample_rate.



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

def default_sample_rate
  @default_sample_rate
end

#endpointsObject (readonly)

Returns the value of attribute endpoints.



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

def endpoints
  @endpoints
end

#timestamp_formatObject

Returns the value of attribute timestamp_format.



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

def timestamp_format
  @timestamp_format
end

Instance Method Details

#adapter(name, &block) ⇒ ~Yodeler::Adapters::Base

Syntax sugar for creating the default endpoint and set the adapter

Useful if you just have one endpoint and don’t care about its name like during testing or for simple metric reporting scenarios … is this useful or is it a big ol’ booger?

Parameters:

  • name (Symbol)

    registered adapter name

  • &block (Type)

    configuration for adapter

Returns:

  • (~Yodeler::Adapters::Base)

    the adapter



60
61
62
63
# File 'lib/yodeler/client.rb', line 60

def adapter(name, &block)
  endpoint if @endpoints.empty?
  default_endpoint.use(name, &block)
end

#default_endpointYodeler::Endpoint

Get the default endpoint

Returns:



47
48
49
# File 'lib/yodeler/client.rb', line 47

def default_endpoint
  @endpoints[default_endpoint_name]
end

#endpoint(name = :default, &block) ⇒ Yodeler::Endpoint

Register a new endpoint

Parameters:

  • name (Symbol|String) (defaults to: :default)

    of endpoint, must be unique

Returns:



38
39
40
41
42
# File 'lib/yodeler/client.rb', line 38

def endpoint(name = :default, &block)
  fail DuplicateEndpointNameError.new(name: name) if @endpoints[name]
  @default_endpoint_name ||= name
  @endpoints[name] = Endpoint.new(name, &block)
end

#format_options(opts) ⇒ Hash

Formats/Defaults metric options

Parameters:

  • opts (Hash)

    metric options

Options Hash (opts):

  • :tags (Array<String,Symbol>, String, Symbol) — default: []

    array of tags to apply to metric/event

  • :sample_rate (Float) — default: 1.0

    The sample rate to use

  • :to (Array<Symbol>, Symbol)

    array of endpoint names to send the metric to. If not set will send to #default_endpoint_name

Returns:

  • (Hash)

    formatted, defaulted options



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/yodeler/client.rb', line 153

def format_options(opts)
  endpoint_names  = opts.delete(:to) || [default_endpoint_name]
  tags            = opts.delete(:tags)
  prefix          = opts.delete(:prefix) || default_prefix
  timestamp       = opts.delete(:timestamp) || timestamp_generator
  meta            = opts.delete(:meta)

  {
    prefix:       prefix,
    to:           [endpoint_names].flatten.compact,
    sample_rate:  opts.delete(:sample_rate) || default_sample_rate,
    tags:         [tags].flatten.compact,
    hostname:     @hostname,
    timestamp:    timestamp,
    meta:         meta
  }
end

#gauge(name, value, opts = {}) ⇒ Yodeler::Metric?

Set a gauge

Examples:

client.gauge('users.count', 20_000_000)
client.gauge('users.count', 20_000_000, { tags: %w(something) })

Parameters:

  • name (~String)

    of the metric

  • value (~Fixnum)

    of the metric

  • opts={} (Hash)

    Examples #format_options

Returns:



75
76
77
# File 'lib/yodeler/client.rb', line 75

def gauge(name, value, opts = {})
  dispatch(:gauge, name, value, opts)
end

#increment(name, value = 1, opts = {}) ⇒ Yodeler::Metric?

Increment a counter

Examples:

client.increment 'user.signup'
client.increment 'user.signup', {}
client.increment 'user.signup', 1, {}

Parameters:

  • name (~String)

    of the metric

  • value=1 (~Fixnum)

    of the metric

  • opts={} (Hash)

    Examples #format_options

Returns:



90
91
92
93
94
95
96
# File 'lib/yodeler/client.rb', line 90

def increment(name, value = 1, opts = {})
  if value.is_a?(Hash)
    opts = value
    value = 1
  end
  dispatch(:increment, name, value, opts)
end

#publish(name, payload, opts = {}) ⇒ Yodeler::Metric?

Publish an event

Examples:

client.publish('item.sold', purchase.to_json)
client.publish('user.sign_up', {name: user.name, avatar: user.image})

Parameters:

  • name (~String)

    of the metric

  • value (~Hash)

    of the metric

  • opts={} (Hash)

    Examples #format_options

Returns:



139
140
141
# File 'lib/yodeler/client.rb', line 139

def publish(name, payload, opts = {})
  dispatch(:event, name, payload, opts)
end

#timestamp_generatorObject



24
25
26
27
28
29
30
31
32
# File 'lib/yodeler/client.rb', line 24

def timestamp_generator
  if timestamp_format.is_a?(Symbol) && TIMESTAMP_FORMATS[timestamp_format]
    TIMESTAMP_FORMATS[timestamp_format].call
  elsif timestamp_format.is_a?(Proc)
    timestamp_format.call
  else
    raise ArgumentError, "Time format not recognized: #{timestamp_format}. \nOptions are #{TIMESTAMP_FORMATS.join(', ')} or a lamba"
  end
end

#timing(name, value = nil, opts = {}) ⇒ Yodeler::Metric, ...

Measure how long something takes

Examples:

client.timing 'eat.sandwich', 250
client.timing('eat.pizza') do
  user.eat(pizza) #=> THAT WAS QUICK FATSO!
end

Parameters:

  • name (~String)

    of the metric

  • value (~Fixnum) (defaults to: nil)

    time in ms

  • opts={} (Hash)

    Examples #format_options

Returns:

  • (Yodeler::Metric, nil, Object)

    the dispatched metric, nil if not sampled if a block is given the result of the block is returned



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/yodeler/client.rb', line 112

def timing(name, value = nil, opts = {})
  if value.is_a?(Hash)
    opts = value
    value = nil
  end

  retval = nil
  if block_given?
    start = Time.now.to_i
    retval = yield
    value = Time.now.to_i - start
  end

  metric = dispatch(:timing, name, value, opts)
  retval || metric
end