Class: Multimeter::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Http, Jmx
Defined in:
lib/multimeter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Http

#http!

Methods included from Jmx

#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

#groupObject (readonly)

Returns the value of attribute group.



375
376
377
# File 'lib/multimeter.rb', line 375

def group
  @group
end

#instance_idObject (readonly)

Returns the value of attribute instance_id.



375
376
377
# File 'lib/multimeter.rb', line 375

def instance_id
  @instance_id
end

#scopeObject (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, options={})
  error_translation do
    @registry.new_counter(create_name(name))
  end
end

#each_metricObject 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, options={}, &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, options={})
  error_translation do
    @registry.new_histogram(create_name(name), !!options[:biased])
  end
end

#instance_registry?Boolean

Returns:

  • (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, options={})
  error_translation do
    event_type = (options[:event_type] || '').to_s
    time_unit = TIME_UNITS[options[:time_unit] || :seconds]
    @registry.new_meter(create_name(name), event_type, time_unit)
  end
end

#sub_registriesObject



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, options={})
  error_translation do
    duration_unit = TIME_UNITS[options[:duration_unit] || :milliseconds]
    rate_unit = TIME_UNITS[options[:rate_unit] || :seconds]
    @registry.new_timer(create_name(name), duration_unit, rate_unit)
  end
end

#to_hObject



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