Class: Yodeler::Client
- Inherits:
-
Object
- Object
- Yodeler::Client
- 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
-
#default_endpoint_name ⇒ Object
Returns the value of attribute default_endpoint_name.
-
#default_prefix ⇒ Object
Returns the value of attribute default_prefix.
-
#default_sample_rate ⇒ Object
Returns the value of attribute default_sample_rate.
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
-
#timestamp_format ⇒ Object
Returns the value of attribute timestamp_format.
Instance Method Summary collapse
-
#adapter(name, &block) ⇒ ~Yodeler::Adapters::Base
Syntax sugar for creating the default endpoint and set the adapter.
-
#default_endpoint ⇒ Yodeler::Endpoint
Get the default endpoint.
-
#endpoint(name = :default, &block) ⇒ Yodeler::Endpoint
Register a new endpoint.
-
#format_options(opts) ⇒ Hash
Formats/Defaults metric options.
-
#gauge(name, value, opts = {}) ⇒ Yodeler::Metric?
Set a gauge.
-
#increment(name, value = 1, opts = {}) ⇒ Yodeler::Metric?
Increment a counter.
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#publish(name, payload, opts = {}) ⇒ Yodeler::Metric?
Publish an event.
- #timestamp_generator ⇒ Object
-
#timing(name, value = nil, opts = {}) ⇒ Yodeler::Metric, ...
Measure how long something takes.
Constructor Details
#initialize ⇒ Client
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_name ⇒ Object
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_prefix ⇒ Object
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_rate ⇒ Object
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 |
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
9 10 11 |
# File 'lib/yodeler/client.rb', line 9 def endpoints @endpoints end |
#timestamp_format ⇒ Object
Returns the value of attribute timestamp_format.
8 9 10 |
# File 'lib/yodeler/client.rb', line 8 def @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?
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_endpoint ⇒ Yodeler::Endpoint
Get the default endpoint
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
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
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 (opts) endpoint_names = opts.delete(:to) || [default_endpoint_name] = opts.delete(:tags) prefix = opts.delete(:prefix) || default_prefix = opts.delete(:timestamp) || = opts.delete(:meta) { prefix: prefix, to: [endpoint_names].flatten.compact, sample_rate: opts.delete(:sample_rate) || default_sample_rate, tags: [].flatten.compact, hostname: @hostname, timestamp: , meta: } end |
#gauge(name, value, opts = {}) ⇒ Yodeler::Metric?
Set a gauge
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
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
139 140 141 |
# File 'lib/yodeler/client.rb', line 139 def publish(name, payload, opts = {}) dispatch(:event, name, payload, opts) end |
#timestamp_generator ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/yodeler/client.rb', line 24 def if .is_a?(Symbol) && TIMESTAMP_FORMATS[] TIMESTAMP_FORMATS[].call elsif .is_a?(Proc) .call else raise ArgumentError, "Time format not recognized: #{}. \nOptions are #{TIMESTAMP_FORMATS.join(', ')} or a lamba" end end |
#timing(name, value = nil, opts = {}) ⇒ Yodeler::Metric, ...
Measure how long something takes
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 |