Class: Multimeter::Registry
- Inherits:
-
Object
- Object
- Multimeter::Registry
- Defined in:
- lib/multimeter.rb
Instance Attribute Summary collapse
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#instance_id ⇒ Object
readonly
Returns the value of attribute instance_id.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #counter(name, options = {}) ⇒ Object
- #each_metric ⇒ Object (also: #each)
- #find_metric(name) ⇒ Object
- #gauge(name, options = {}, &block) ⇒ Object
- #get(name) ⇒ Object
- #histogram(name, options = {}) ⇒ Object
-
#initialize(*args) ⇒ Registry
constructor
A new instance of Registry.
- #instance_registry? ⇒ Boolean
- #meter(name, options = {}) ⇒ Object
- #sub_registries ⇒ Object
- #sub_registry(scope, instance_id = nil) ⇒ Object
- #timer(name, options = {}) ⇒ Object
- #to_h ⇒ Object
Methods included from Http
Methods included from Jmx
Constructor Details
#initialize(*args) ⇒ Registry
Returns a new instance of Registry.
377 378 379 380 381 |
# File 'lib/multimeter.rb', line 377 def initialize(*args) @group, @scope, @instance_id = args @registry = ::Yammer::Metrics::MetricsRegistry.new @sub_registries = JavaConcurrency::ConcurrentHashMap.new end |
Instance Attribute Details
#group ⇒ Object (readonly)
Returns the value of attribute group.
375 376 377 |
# File 'lib/multimeter.rb', line 375 def group @group end |
#instance_id ⇒ Object (readonly)
Returns the value of attribute instance_id.
375 376 377 |
# File 'lib/multimeter.rb', line 375 def instance_id @instance_id end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
375 376 377 |
# File 'lib/multimeter.rb', line 375 def scope @scope end |
Instance Method Details
#counter(name, options = {}) ⇒ Object
437 438 439 440 441 |
# File 'lib/multimeter.rb', line 437 def counter(name, ={}) error_translation do @registry.new_counter(create_name(name)) end end |
#each_metric ⇒ Object Also known as: each
403 404 405 406 407 408 |
# File 'lib/multimeter.rb', line 403 def each_metric return self unless block_given? @registry.all_metrics.each do |metric_name, metric| yield metric_name.name, metric end end |
#find_metric(name) ⇒ Object
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/multimeter.rb', line 415 def find_metric(name) m = get(name) unless m sub_registries.each do |registry| m = registry.find_metric(name) break if m end end m end |
#gauge(name, options = {}, &block) ⇒ Object
426 427 428 429 430 431 432 433 434 435 |
# File 'lib/multimeter.rb', line 426 def gauge(name, ={}, &block) existing_gauge = get(name) if block_given? && existing_gauge.respond_to?(:same?) && existing_gauge.same?(block) return elsif existing_gauge && block_given? raise ArgumentError, %(Cannot redeclare gauge #{name}) else @registry.new_gauge(create_name(name), ProcGauge.new(block)) end end |
#get(name) ⇒ Object
411 412 413 |
# File 'lib/multimeter.rb', line 411 def get(name) @registry.all_metrics[create_name(name)] end |
#histogram(name, options = {}) ⇒ Object
451 452 453 454 455 |
# File 'lib/multimeter.rb', line 451 def histogram(name, ={}) error_translation do @registry.new_histogram(create_name(name), !![:biased]) end end |
#instance_registry? ⇒ Boolean
383 384 385 |
# File 'lib/multimeter.rb', line 383 def instance_registry? !!@instance_id end |
#meter(name, options = {}) ⇒ Object
443 444 445 446 447 448 449 |
# File 'lib/multimeter.rb', line 443 def meter(name, ={}) error_translation do event_type = ([:event_type] || '').to_s time_unit = TIME_UNITS[[:time_unit] || :seconds] @registry.new_meter(create_name(name), event_type, time_unit) end end |
#sub_registries ⇒ Object
399 400 401 |
# File 'lib/multimeter.rb', line 399 def sub_registries @sub_registries.values.to_a end |
#sub_registry(scope, instance_id = nil) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/multimeter.rb', line 387 def sub_registry(scope, instance_id=nil) full_id = scope.dup full_id << "/#{instance_id}" if instance_id r = @sub_registries.get(full_id) unless r r = self.class.new(@group, scope, instance_id) @sub_registries.put_if_absent(full_id, r) r = @sub_registries.get(full_id) end r end |
#timer(name, options = {}) ⇒ Object
457 458 459 460 461 462 463 |
# File 'lib/multimeter.rb', line 457 def timer(name, ={}) error_translation do duration_unit = TIME_UNITS[[:duration_unit] || :milliseconds] rate_unit = TIME_UNITS[[:rate_unit] || :seconds] @registry.new_timer(create_name(name), duration_unit, rate_unit) end end |
#to_h ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/multimeter.rb', line 465 def to_h h = {@scope => {}} each_metric do |metric_name, metric| h[@scope][metric_name.to_sym] = metric.to_h end registries_by_scope = sub_registries.group_by { |r| r.scope } registries_by_scope.each do |scope, registries| if registries.size == 1 h.merge!(registries.first.to_h) else h[scope] = {} registries_by_metric = Hash.new { |h, k| h[k] = [] } registries.each do |registry| registry.each_metric do |metric_name, _| registries_by_metric[metric_name] << registry end end registries_by_metric.each do |metric_name, registries| if registries.size == 1 h[scope][metric_name.to_sym] = registries.first.get(metric_name).to_h else metrics_by_instance_id = Hash[registries.map { |r| [r.instance_id, r.get(metric_name)] }] h[scope][metric_name.to_sym] = Aggregate.new(metrics_by_instance_id).to_h end end end h end h.delete_if { |k, v| v.empty? } h end |