Module: StatsD
- Extended by:
- StatsD
- Included in:
- StatsD
- Defined in:
- lib/statsd/instrument.rb,
lib/statsd/instrument/version.rb
Overview
The StatsD module contains low-level metrics for collecting metrics and sending them to the backend.
Defined Under Namespace
Modules: Instrument
Instance Attribute Summary collapse
-
#backend ⇒ StatsD::Instrument::Backend
The backend that is being used to emit the metrics.
-
#default_sample_rate ⇒ Float
The sample rate to use if the sample rate is unspecified for a metric call.
-
#logger ⇒ Logger
The logger to use in case of any errors.
-
#prefix ⇒ String?
The prefix to apply to metric names.
Instance Method Summary collapse
-
#gauge(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a gauge metric.
-
#histogram(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a histogram metric.
-
#increment(key, value = 1, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a counter metric.
-
#key_value(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a key/value metric.
-
#measure(key, value = nil, *metric_options, &block) ⇒ Object
Emits a duration metric.
-
#set(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a set metric.
Instance Attribute Details
#backend ⇒ StatsD::Instrument::Backend
The backend that is being used to emit the metrics.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/statsd/instrument.rb', line 36 module StatsD extend self # The StatsD::Instrument module provides metaprogramming methods to instrument your methods with # StatsD metrics. E.g., yopu can create counters on how often a method is called, how often it is # successful, the duration of the methods call, etc. module Instrument # @private def self.generate_metric_name(metric_name, callee, *args) metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.') end if Process.respond_to?(:clock_gettime) # @private def self.duration start = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - start end else # @private def self.duration start = Time.now yield Time.now - start end end # Adds execution duration instrumentation to a method. # # @param method [Symbol] The name of the method to instrument. # @param name [String, #call] The name of the metric to use. You can also pass in a # callable to dynamically generate a metric name # @param metric_options (see StatsD#measure) # @return [void] def statsd_measure(method, name, *) add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args| define_method(new_method) do |*args, &block| StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, *) { send(old_method, *args, &block) } end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # For successful calls, the metric <tt>[name].success</tt> will be incremented; for failed calls, the metric # name is <tt>[name].failure</tt>. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield You can pass a block to this method if you want to define yourself what is a successful call # based on the return value of the method. # @yieldparam result The return value of the instrumented method. # @yieldreturn [Boolean] Return true iff the return value is consisered a success, false otherwise. # @return [void] # @see #statsd_count_if def statsd_count_success(method, name, *) add_to_method(method, name, :count_success) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure suffix = truthiness == false ? 'failure' : 'success' StatsD.increment("#{StatsD::Instrument.generate_metric_name(metric_name, self, *args)}.#{suffix}", 1, *) end end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # Only for successful calls, the metric will be icnremented # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield (see #statsd_count_success) # @yieldparam result (see #statsd_count_success) # @yieldreturn (see #statsd_count_success) # @return [void] # @see #statsd_count_success def statsd_count_if(method, name, *) add_to_method(method, name, :count_if) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), *) if truthiness end end end end # Adds counter instrumentation to a method. # # The metric will be incremented for every call of the instrumented method, no matter # whether what the method returns, or whether it raises an exception. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @return [void] def statsd_count(method, name, *) add_to_method(method, name, :count) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, *) send(old_method, *args, &block) end end end # Removes StatsD counter instrumentation from a method # @param method [Symbol] The method to remove instrumentation from. # @param name [String] The name of the metric that was used. # @return [void] # @see #statsd_count def statsd_remove_count(method, name) remove_from_method(method, name, :count) end # Removes StatsD conditional counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_if def statsd_remove_count_if(method, name) remove_from_method(method, name, :count_if) end # Removes StatsD success counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_success def statsd_remove_count_success(method, name) remove_from_method(method, name, :count_success) end # Removes StatsD measure instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_measure def statsd_remove_measure(method, name) remove_from_method(method, name, :measure) end private def add_to_method(method, name, action, &block) metric_name = name method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" # raw_ssl_request_for_measure_on_FedEx_without_ActiveMerchant.Shipping.#{self.class.name}.ssl_request method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" # raw_ssl_request_for_measure_on_FedEx_with_ActiveMerchant.Shipping.#{self.class.name}.ssl_request raise ArgumentError, "already instrumented #{method} for #{self.name}" if method_defined? method_name_without_statsd raise ArgumentError, "could not find method #{method} for #{self.name}" unless method_defined?(method) || private_method_defined?(method) method_scope = method_visibility(method) alias_method method_name_without_statsd, method yield method_name_without_statsd, method_name_with_statsd, metric_name alias_method method, method_name_with_statsd private(method_name_without_statsd) send(method_scope, method) private(method_name_with_statsd) end def remove_from_method(method, name, action) method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" method_scope = method_visibility(method) send(:remove_method, method_name_with_statsd) alias_method method, method_name_without_statsd send(:remove_method, method_name_without_statsd) send(method_scope, method) end def method_visibility(method) case when private_method_defined?(method) :private when protected_method_defined?(method) :protected else :public end end end attr_accessor :logger, :default_sample_rate, :prefix attr_writer :backend def backend @backend ||= StatsD::Instrument::Environment.default_backend end # Emits a duration metric. # # @overload measure(key, value, metric_options = {}) # Emits a measure metric, by providing a duration in milliseconds. # @param key [String] The name of the metric. # @param value [Float] The measured duration in milliseconds # @param metric_options [Hash] Options for the metric # @return [StatsD::Instrument::Metric] The metric that was sent to the backend. # # @overload measure(key, metric_options = {}, &block) # Emits a measure metric, after measuring the execution duration of the # block passed to this method. # @param key [String] The name of the metric. # @param metric_options [Hash] Options for the metric # @yield The method will yield the block that was passed to this emthod to measure its duration. # @return The value that was returns by the block passed to this method. # # @example # http_response = StatsD.measure('HTTP.call.duration') do # HTTP.get(url) # end def measure(key, value = nil, *, &block) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end result = nil value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given? metric = collect_metric(hash_argument().merge(type: :ms, name: key, value: value)) result = metric unless block_given? result end # Emits a counter metric. # @param key [String] The name of the metric. # @param value [Integer] The value to increment the counter by. # # You should not compensate for the sample rate using the counter increment. E.g., if # your sample rate is 0.01, you should <b>not</b> use 100 as increment to compensate for it. # The sample rate is part of the packet that is being sent to the server, and the server # should know how to handle it. # # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def increment(key, value = 1, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, 1) end collect_metric(hash_argument().merge(type: :c, name: key, value: value)) end # Emits a gauge metric. # @param key [String] The name of the metric. # @param value [Numeric] The current value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def gauge(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :g, name: key, value: value)) end # Emits a histogram metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def histogram(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :h, name: key, value: value)) end # Emits a key/value metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the statsite implementation only. def key_value(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :kv, name: key, value: value)) end # Emits a set metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def set(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :s, name: key, value: value)) end private # Converts old-style ordered arguments in an argument hash for backwards compatibility. # @param args [Array] The list of non-required arguments. # @return [Hash] The hash of optional arguments. def hash_argument(args) return {} if args.length == 0 return args.first if args.length == 1 && args.first.is_a?(Hash) order = [:sample_rate, :tags] hash = {} args.each_with_index do |value, index| hash[order[index]] = value end return hash end # Instantiates a metric, and sends it to the backend for further processing. # @param options (see StatsD::Instrument::Metric#initialize) # @return [StatsD::Instrument::Metric] The meric that was sent to the backend. def collect_metric() backend.collect_metric(metric = StatsD::Instrument::Metric.new()) metric end end |
#default_sample_rate ⇒ Float
The sample rate to use if the sample rate is unspecified for a metric call.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/statsd/instrument.rb', line 36 module StatsD extend self # The StatsD::Instrument module provides metaprogramming methods to instrument your methods with # StatsD metrics. E.g., yopu can create counters on how often a method is called, how often it is # successful, the duration of the methods call, etc. module Instrument # @private def self.generate_metric_name(metric_name, callee, *args) metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.') end if Process.respond_to?(:clock_gettime) # @private def self.duration start = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - start end else # @private def self.duration start = Time.now yield Time.now - start end end # Adds execution duration instrumentation to a method. # # @param method [Symbol] The name of the method to instrument. # @param name [String, #call] The name of the metric to use. You can also pass in a # callable to dynamically generate a metric name # @param metric_options (see StatsD#measure) # @return [void] def statsd_measure(method, name, *) add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args| define_method(new_method) do |*args, &block| StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, *) { send(old_method, *args, &block) } end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # For successful calls, the metric <tt>[name].success</tt> will be incremented; for failed calls, the metric # name is <tt>[name].failure</tt>. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield You can pass a block to this method if you want to define yourself what is a successful call # based on the return value of the method. # @yieldparam result The return value of the instrumented method. # @yieldreturn [Boolean] Return true iff the return value is consisered a success, false otherwise. # @return [void] # @see #statsd_count_if def statsd_count_success(method, name, *) add_to_method(method, name, :count_success) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure suffix = truthiness == false ? 'failure' : 'success' StatsD.increment("#{StatsD::Instrument.generate_metric_name(metric_name, self, *args)}.#{suffix}", 1, *) end end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # Only for successful calls, the metric will be icnremented # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield (see #statsd_count_success) # @yieldparam result (see #statsd_count_success) # @yieldreturn (see #statsd_count_success) # @return [void] # @see #statsd_count_success def statsd_count_if(method, name, *) add_to_method(method, name, :count_if) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), *) if truthiness end end end end # Adds counter instrumentation to a method. # # The metric will be incremented for every call of the instrumented method, no matter # whether what the method returns, or whether it raises an exception. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @return [void] def statsd_count(method, name, *) add_to_method(method, name, :count) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, *) send(old_method, *args, &block) end end end # Removes StatsD counter instrumentation from a method # @param method [Symbol] The method to remove instrumentation from. # @param name [String] The name of the metric that was used. # @return [void] # @see #statsd_count def statsd_remove_count(method, name) remove_from_method(method, name, :count) end # Removes StatsD conditional counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_if def statsd_remove_count_if(method, name) remove_from_method(method, name, :count_if) end # Removes StatsD success counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_success def statsd_remove_count_success(method, name) remove_from_method(method, name, :count_success) end # Removes StatsD measure instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_measure def statsd_remove_measure(method, name) remove_from_method(method, name, :measure) end private def add_to_method(method, name, action, &block) metric_name = name method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" # raw_ssl_request_for_measure_on_FedEx_without_ActiveMerchant.Shipping.#{self.class.name}.ssl_request method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" # raw_ssl_request_for_measure_on_FedEx_with_ActiveMerchant.Shipping.#{self.class.name}.ssl_request raise ArgumentError, "already instrumented #{method} for #{self.name}" if method_defined? method_name_without_statsd raise ArgumentError, "could not find method #{method} for #{self.name}" unless method_defined?(method) || private_method_defined?(method) method_scope = method_visibility(method) alias_method method_name_without_statsd, method yield method_name_without_statsd, method_name_with_statsd, metric_name alias_method method, method_name_with_statsd private(method_name_without_statsd) send(method_scope, method) private(method_name_with_statsd) end def remove_from_method(method, name, action) method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" method_scope = method_visibility(method) send(:remove_method, method_name_with_statsd) alias_method method, method_name_without_statsd send(:remove_method, method_name_without_statsd) send(method_scope, method) end def method_visibility(method) case when private_method_defined?(method) :private when protected_method_defined?(method) :protected else :public end end end attr_accessor :logger, :default_sample_rate, :prefix attr_writer :backend def backend @backend ||= StatsD::Instrument::Environment.default_backend end # Emits a duration metric. # # @overload measure(key, value, metric_options = {}) # Emits a measure metric, by providing a duration in milliseconds. # @param key [String] The name of the metric. # @param value [Float] The measured duration in milliseconds # @param metric_options [Hash] Options for the metric # @return [StatsD::Instrument::Metric] The metric that was sent to the backend. # # @overload measure(key, metric_options = {}, &block) # Emits a measure metric, after measuring the execution duration of the # block passed to this method. # @param key [String] The name of the metric. # @param metric_options [Hash] Options for the metric # @yield The method will yield the block that was passed to this emthod to measure its duration. # @return The value that was returns by the block passed to this method. # # @example # http_response = StatsD.measure('HTTP.call.duration') do # HTTP.get(url) # end def measure(key, value = nil, *, &block) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end result = nil value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given? metric = collect_metric(hash_argument().merge(type: :ms, name: key, value: value)) result = metric unless block_given? result end # Emits a counter metric. # @param key [String] The name of the metric. # @param value [Integer] The value to increment the counter by. # # You should not compensate for the sample rate using the counter increment. E.g., if # your sample rate is 0.01, you should <b>not</b> use 100 as increment to compensate for it. # The sample rate is part of the packet that is being sent to the server, and the server # should know how to handle it. # # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def increment(key, value = 1, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, 1) end collect_metric(hash_argument().merge(type: :c, name: key, value: value)) end # Emits a gauge metric. # @param key [String] The name of the metric. # @param value [Numeric] The current value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def gauge(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :g, name: key, value: value)) end # Emits a histogram metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def histogram(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :h, name: key, value: value)) end # Emits a key/value metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the statsite implementation only. def key_value(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :kv, name: key, value: value)) end # Emits a set metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def set(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :s, name: key, value: value)) end private # Converts old-style ordered arguments in an argument hash for backwards compatibility. # @param args [Array] The list of non-required arguments. # @return [Hash] The hash of optional arguments. def hash_argument(args) return {} if args.length == 0 return args.first if args.length == 1 && args.first.is_a?(Hash) order = [:sample_rate, :tags] hash = {} args.each_with_index do |value, index| hash[order[index]] = value end return hash end # Instantiates a metric, and sends it to the backend for further processing. # @param options (see StatsD::Instrument::Metric#initialize) # @return [StatsD::Instrument::Metric] The meric that was sent to the backend. def collect_metric() backend.collect_metric(metric = StatsD::Instrument::Metric.new()) metric end end |
#logger ⇒ Logger
The logger to use in case of any errors. The logger is also used as default logger for the LoggerBackend (although this can be overwritten).
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/statsd/instrument.rb', line 36 module StatsD extend self # The StatsD::Instrument module provides metaprogramming methods to instrument your methods with # StatsD metrics. E.g., yopu can create counters on how often a method is called, how often it is # successful, the duration of the methods call, etc. module Instrument # @private def self.generate_metric_name(metric_name, callee, *args) metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.') end if Process.respond_to?(:clock_gettime) # @private def self.duration start = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - start end else # @private def self.duration start = Time.now yield Time.now - start end end # Adds execution duration instrumentation to a method. # # @param method [Symbol] The name of the method to instrument. # @param name [String, #call] The name of the metric to use. You can also pass in a # callable to dynamically generate a metric name # @param metric_options (see StatsD#measure) # @return [void] def statsd_measure(method, name, *) add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args| define_method(new_method) do |*args, &block| StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, *) { send(old_method, *args, &block) } end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # For successful calls, the metric <tt>[name].success</tt> will be incremented; for failed calls, the metric # name is <tt>[name].failure</tt>. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield You can pass a block to this method if you want to define yourself what is a successful call # based on the return value of the method. # @yieldparam result The return value of the instrumented method. # @yieldreturn [Boolean] Return true iff the return value is consisered a success, false otherwise. # @return [void] # @see #statsd_count_if def statsd_count_success(method, name, *) add_to_method(method, name, :count_success) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure suffix = truthiness == false ? 'failure' : 'success' StatsD.increment("#{StatsD::Instrument.generate_metric_name(metric_name, self, *args)}.#{suffix}", 1, *) end end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # Only for successful calls, the metric will be icnremented # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield (see #statsd_count_success) # @yieldparam result (see #statsd_count_success) # @yieldreturn (see #statsd_count_success) # @return [void] # @see #statsd_count_success def statsd_count_if(method, name, *) add_to_method(method, name, :count_if) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), *) if truthiness end end end end # Adds counter instrumentation to a method. # # The metric will be incremented for every call of the instrumented method, no matter # whether what the method returns, or whether it raises an exception. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @return [void] def statsd_count(method, name, *) add_to_method(method, name, :count) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, *) send(old_method, *args, &block) end end end # Removes StatsD counter instrumentation from a method # @param method [Symbol] The method to remove instrumentation from. # @param name [String] The name of the metric that was used. # @return [void] # @see #statsd_count def statsd_remove_count(method, name) remove_from_method(method, name, :count) end # Removes StatsD conditional counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_if def statsd_remove_count_if(method, name) remove_from_method(method, name, :count_if) end # Removes StatsD success counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_success def statsd_remove_count_success(method, name) remove_from_method(method, name, :count_success) end # Removes StatsD measure instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_measure def statsd_remove_measure(method, name) remove_from_method(method, name, :measure) end private def add_to_method(method, name, action, &block) metric_name = name method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" # raw_ssl_request_for_measure_on_FedEx_without_ActiveMerchant.Shipping.#{self.class.name}.ssl_request method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" # raw_ssl_request_for_measure_on_FedEx_with_ActiveMerchant.Shipping.#{self.class.name}.ssl_request raise ArgumentError, "already instrumented #{method} for #{self.name}" if method_defined? method_name_without_statsd raise ArgumentError, "could not find method #{method} for #{self.name}" unless method_defined?(method) || private_method_defined?(method) method_scope = method_visibility(method) alias_method method_name_without_statsd, method yield method_name_without_statsd, method_name_with_statsd, metric_name alias_method method, method_name_with_statsd private(method_name_without_statsd) send(method_scope, method) private(method_name_with_statsd) end def remove_from_method(method, name, action) method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" method_scope = method_visibility(method) send(:remove_method, method_name_with_statsd) alias_method method, method_name_without_statsd send(:remove_method, method_name_without_statsd) send(method_scope, method) end def method_visibility(method) case when private_method_defined?(method) :private when protected_method_defined?(method) :protected else :public end end end attr_accessor :logger, :default_sample_rate, :prefix attr_writer :backend def backend @backend ||= StatsD::Instrument::Environment.default_backend end # Emits a duration metric. # # @overload measure(key, value, metric_options = {}) # Emits a measure metric, by providing a duration in milliseconds. # @param key [String] The name of the metric. # @param value [Float] The measured duration in milliseconds # @param metric_options [Hash] Options for the metric # @return [StatsD::Instrument::Metric] The metric that was sent to the backend. # # @overload measure(key, metric_options = {}, &block) # Emits a measure metric, after measuring the execution duration of the # block passed to this method. # @param key [String] The name of the metric. # @param metric_options [Hash] Options for the metric # @yield The method will yield the block that was passed to this emthod to measure its duration. # @return The value that was returns by the block passed to this method. # # @example # http_response = StatsD.measure('HTTP.call.duration') do # HTTP.get(url) # end def measure(key, value = nil, *, &block) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end result = nil value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given? metric = collect_metric(hash_argument().merge(type: :ms, name: key, value: value)) result = metric unless block_given? result end # Emits a counter metric. # @param key [String] The name of the metric. # @param value [Integer] The value to increment the counter by. # # You should not compensate for the sample rate using the counter increment. E.g., if # your sample rate is 0.01, you should <b>not</b> use 100 as increment to compensate for it. # The sample rate is part of the packet that is being sent to the server, and the server # should know how to handle it. # # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def increment(key, value = 1, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, 1) end collect_metric(hash_argument().merge(type: :c, name: key, value: value)) end # Emits a gauge metric. # @param key [String] The name of the metric. # @param value [Numeric] The current value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def gauge(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :g, name: key, value: value)) end # Emits a histogram metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def histogram(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :h, name: key, value: value)) end # Emits a key/value metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the statsite implementation only. def key_value(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :kv, name: key, value: value)) end # Emits a set metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def set(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :s, name: key, value: value)) end private # Converts old-style ordered arguments in an argument hash for backwards compatibility. # @param args [Array] The list of non-required arguments. # @return [Hash] The hash of optional arguments. def hash_argument(args) return {} if args.length == 0 return args.first if args.length == 1 && args.first.is_a?(Hash) order = [:sample_rate, :tags] hash = {} args.each_with_index do |value, index| hash[order[index]] = value end return hash end # Instantiates a metric, and sends it to the backend for further processing. # @param options (see StatsD::Instrument::Metric#initialize) # @return [StatsD::Instrument::Metric] The meric that was sent to the backend. def collect_metric() backend.collect_metric(metric = StatsD::Instrument::Metric.new()) metric end end |
#prefix ⇒ String?
The prefix to apply to metric names. This can be useful to group all the metrics for an application in a shared StatsD server.
When using a prefix a dot will be included automatically to separate the prefix from the metric name.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/statsd/instrument.rb', line 36 module StatsD extend self # The StatsD::Instrument module provides metaprogramming methods to instrument your methods with # StatsD metrics. E.g., yopu can create counters on how often a method is called, how often it is # successful, the duration of the methods call, etc. module Instrument # @private def self.generate_metric_name(metric_name, callee, *args) metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.') end if Process.respond_to?(:clock_gettime) # @private def self.duration start = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - start end else # @private def self.duration start = Time.now yield Time.now - start end end # Adds execution duration instrumentation to a method. # # @param method [Symbol] The name of the method to instrument. # @param name [String, #call] The name of the metric to use. You can also pass in a # callable to dynamically generate a metric name # @param metric_options (see StatsD#measure) # @return [void] def statsd_measure(method, name, *) add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args| define_method(new_method) do |*args, &block| StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, *) { send(old_method, *args, &block) } end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # For successful calls, the metric <tt>[name].success</tt> will be incremented; for failed calls, the metric # name is <tt>[name].failure</tt>. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield You can pass a block to this method if you want to define yourself what is a successful call # based on the return value of the method. # @yieldparam result The return value of the instrumented method. # @yieldreturn [Boolean] Return true iff the return value is consisered a success, false otherwise. # @return [void] # @see #statsd_count_if def statsd_count_success(method, name, *) add_to_method(method, name, :count_success) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure suffix = truthiness == false ? 'failure' : 'success' StatsD.increment("#{StatsD::Instrument.generate_metric_name(metric_name, self, *args)}.#{suffix}", 1, *) end end end end # Adds success and failure counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. # Only for successful calls, the metric will be icnremented # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield (see #statsd_count_success) # @yieldparam result (see #statsd_count_success) # @yieldreturn (see #statsd_count_success) # @return [void] # @see #statsd_count_success def statsd_count_if(method, name, *) add_to_method(method, name, :count_if) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| begin truthiness = result = send(old_method, *args, &block) rescue truthiness = false raise else truthiness = (yield(result) rescue false) if block_given? result ensure StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), *) if truthiness end end end end # Adds counter instrumentation to a method. # # The metric will be incremented for every call of the instrumented method, no matter # whether what the method returns, or whether it raises an exception. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @return [void] def statsd_count(method, name, *) add_to_method(method, name, :count) do |old_method, new_method, metric_name| define_method(new_method) do |*args, &block| StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, *) send(old_method, *args, &block) end end end # Removes StatsD counter instrumentation from a method # @param method [Symbol] The method to remove instrumentation from. # @param name [String] The name of the metric that was used. # @return [void] # @see #statsd_count def statsd_remove_count(method, name) remove_from_method(method, name, :count) end # Removes StatsD conditional counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_if def statsd_remove_count_if(method, name) remove_from_method(method, name, :count_if) end # Removes StatsD success counter instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_count_success def statsd_remove_count_success(method, name) remove_from_method(method, name, :count_success) end # Removes StatsD measure instrumentation from a method # @param method (see #statsd_remove_count) # @param name (see #statsd_remove_count) # @return [void] # @see #statsd_measure def statsd_remove_measure(method, name) remove_from_method(method, name, :measure) end private def add_to_method(method, name, action, &block) metric_name = name method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" # raw_ssl_request_for_measure_on_FedEx_without_ActiveMerchant.Shipping.#{self.class.name}.ssl_request method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" # raw_ssl_request_for_measure_on_FedEx_with_ActiveMerchant.Shipping.#{self.class.name}.ssl_request raise ArgumentError, "already instrumented #{method} for #{self.name}" if method_defined? method_name_without_statsd raise ArgumentError, "could not find method #{method} for #{self.name}" unless method_defined?(method) || private_method_defined?(method) method_scope = method_visibility(method) alias_method method_name_without_statsd, method yield method_name_without_statsd, method_name_with_statsd, metric_name alias_method method, method_name_with_statsd private(method_name_without_statsd) send(method_scope, method) private(method_name_with_statsd) end def remove_from_method(method, name, action) method_name_without_statsd = :"#{method}_for_#{action}_on_#{self.name}_without_#{name}" method_name_with_statsd = :"#{method}_for_#{action}_on_#{self.name}_with_#{name}" method_scope = method_visibility(method) send(:remove_method, method_name_with_statsd) alias_method method, method_name_without_statsd send(:remove_method, method_name_without_statsd) send(method_scope, method) end def method_visibility(method) case when private_method_defined?(method) :private when protected_method_defined?(method) :protected else :public end end end attr_accessor :logger, :default_sample_rate, :prefix attr_writer :backend def backend @backend ||= StatsD::Instrument::Environment.default_backend end # Emits a duration metric. # # @overload measure(key, value, metric_options = {}) # Emits a measure metric, by providing a duration in milliseconds. # @param key [String] The name of the metric. # @param value [Float] The measured duration in milliseconds # @param metric_options [Hash] Options for the metric # @return [StatsD::Instrument::Metric] The metric that was sent to the backend. # # @overload measure(key, metric_options = {}, &block) # Emits a measure metric, after measuring the execution duration of the # block passed to this method. # @param key [String] The name of the metric. # @param metric_options [Hash] Options for the metric # @yield The method will yield the block that was passed to this emthod to measure its duration. # @return The value that was returns by the block passed to this method. # # @example # http_response = StatsD.measure('HTTP.call.duration') do # HTTP.get(url) # end def measure(key, value = nil, *, &block) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end result = nil value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given? metric = collect_metric(hash_argument().merge(type: :ms, name: key, value: value)) result = metric unless block_given? result end # Emits a counter metric. # @param key [String] The name of the metric. # @param value [Integer] The value to increment the counter by. # # You should not compensate for the sample rate using the counter increment. E.g., if # your sample rate is 0.01, you should <b>not</b> use 100 as increment to compensate for it. # The sample rate is part of the packet that is being sent to the server, and the server # should know how to handle it. # # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def increment(key, value = 1, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, 1) end collect_metric(hash_argument().merge(type: :c, name: key, value: value)) end # Emits a gauge metric. # @param key [String] The name of the metric. # @param value [Numeric] The current value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) def gauge(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :g, name: key, value: value)) end # Emits a histogram metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def histogram(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :h, name: key, value: value)) end # Emits a key/value metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the statsite implementation only. def key_value(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :kv, name: key, value: value)) end # Emits a set metric. # @param key [String] The name of the metric. # @param value [Numeric] The value to record. # @param metric_options [Hash] (default: {}) Metric options # @return (see #collect_metric) # @note Supported by the datadog implementation only. def set(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :s, name: key, value: value)) end private # Converts old-style ordered arguments in an argument hash for backwards compatibility. # @param args [Array] The list of non-required arguments. # @return [Hash] The hash of optional arguments. def hash_argument(args) return {} if args.length == 0 return args.first if args.length == 1 && args.first.is_a?(Hash) order = [:sample_rate, :tags] hash = {} args.each_with_index do |value, index| hash[order[index]] = value end return hash end # Instantiates a metric, and sends it to the backend for further processing. # @param options (see StatsD::Instrument::Metric#initialize) # @return [StatsD::Instrument::Metric] The meric that was sent to the backend. def collect_metric() backend.collect_metric(metric = StatsD::Instrument::Metric.new()) metric end end |
Instance Method Details
#gauge(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a gauge metric.
315 316 317 318 319 320 321 322 |
# File 'lib/statsd/instrument.rb', line 315 def gauge(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :g, name: key, value: value)) end |
#histogram(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Supported by the datadog implementation only.
Emits a histogram metric.
330 331 332 333 334 335 336 337 |
# File 'lib/statsd/instrument.rb', line 330 def histogram(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :h, name: key, value: value)) end |
#increment(key, value = 1, *metric_options) ⇒ StatsD::Instrument::Metric
Emits a counter metric.
301 302 303 304 305 306 307 308 |
# File 'lib/statsd/instrument.rb', line 301 def increment(key, value = 1, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, 1) end collect_metric(hash_argument().merge(type: :c, name: key, value: value)) end |
#key_value(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Supported by the statsite implementation only.
Emits a key/value metric.
345 346 347 348 349 350 351 352 |
# File 'lib/statsd/instrument.rb', line 345 def key_value(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :kv, name: key, value: value)) end |
#measure(key, value, metric_options = {}) ⇒ StatsD::Instrument::Metric #measure(key, metric_options = {}) { ... } ⇒ Object
Emits a duration metric.
277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/statsd/instrument.rb', line 277 def measure(key, value = nil, *, &block) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end result = nil value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given? metric = collect_metric(hash_argument().merge(type: :ms, name: key, value: value)) result = metric unless block_given? result end |
#set(key, value, *metric_options) ⇒ StatsD::Instrument::Metric
Supported by the datadog implementation only.
Emits a set metric.
360 361 362 363 364 365 366 367 |
# File 'lib/statsd/instrument.rb', line 360 def set(key, value, *) if value.is_a?(Hash) && .empty? = [value] value = value.fetch(:value, nil) end collect_metric(hash_argument().merge(type: :s, name: key, value: value)) end |