Class: D13n::Metric::Manager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Instrumentation
Defined in:
lib/d13n/metric/manager.rb

Constant Summary collapse

CHANNELS =
['udp', 'logger', 'null']
METRIC_MAPPING =
{
  'app_state' => AppStateMetric,
  'app_http' => AppHttpMetric,
  'app_database' => AppDatabaseMetric,
  'biz_state' => BizStateMetric
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#add_instrumentation, #load_instrumentation_files, #setup_instrumentation

Constructor Details

#initialize(channel, opts = {}) ⇒ Manager

Returns a new instance of Manager.



39
40
41
42
43
44
# File 'lib/d13n/metric/manager.rb', line 39

def initialize(channel, opts={})
  self.channel = channel
  @opts = opts
  set_backend
  setup_instrumentation
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



37
38
39
# File 'lib/d13n/metric/manager.rb', line 37

def backend
  @backend
end

#channelObject

Returns the value of attribute channel.



37
38
39
# File 'lib/d13n/metric/manager.rb', line 37

def channel
  @channel
end

Class Method Details

.instanceObject



16
17
18
# File 'lib/d13n/metric/manager.rb', line 16

def self.instance
  @instance || start
end

.notice_error(exception, opts = {}) ⇒ Object



20
21
22
# File 'lib/d13n/metric/manager.rb', line 20

def self.notice_error(exception, opts={})
  Stream.notice_error(exception, opts)
end

.start(channel = 'logger', opts = {}) ⇒ Object



12
13
14
# File 'lib/d13n/metric/manager.rb', line 12

def self.start(channel='logger', opts={})
  @instance ||= new(channel, opts)
end

Instance Method Details

#default_hostObject



94
95
96
# File 'lib/d13n/metric/manager.rb', line 94

def default_host
  D13n.config[:'service.metric.host'] || 'localhost'
end

#default_portObject



98
99
100
# File 'lib/d13n/metric/manager.rb', line 98

def default_port
  D13n.config[:'service.metric.port'] || 8125
end

#default_protocolObject



102
103
104
# File 'lib/d13n/metric/manager.rb', line 102

def default_protocol
  D13n.config[:'service.metric.protocol'] || :datadog
end

#metric(type) ⇒ Object



81
82
83
84
85
86
# File 'lib/d13n/metric/manager.rb', line 81

def metric(type)
  metric_for(type)
rescue MetricNotFoundError => e
  D13n.logger.error "Instrument Metric Type #{type} Not Found in [#{METRIC_MAPPING.keys.join(',')}]"
  return nil
end

#metric_for(type) ⇒ Object



88
89
90
91
92
# File 'lib/d13n/metric/manager.rb', line 88

def metric_for(type)
  expected_metric = METRIC_MAPPING[type.to_s.downcase]
  raise MetricNotFoundError.new "#{type} of metric not found!" if expected_metric.nil?
  expected_metric
end

#set_backendObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/d13n/metric/manager.rb', line 46

def set_backend
  @backend = if @channel == 'udp'
    setup_udp_backend
  elsif @channel == 'logger'
    setup_logger_backend
  else @channel == 'null'
    setup_null_backend
  end
  D13n.logger.info "Using #{@backend.to_s} as channel in metric"
  StatsD.backend = @backend
end

#setup_logger_backendObject



66
67
68
69
70
# File 'lib/d13n/metric/manager.rb', line 66

def setup_logger_backend
  @logger = @opts.fetch(:logger, D13n.logger)
  raise InstrumentInitError.new "Missing Logger for logger backend" if @logger.nil?
  StatsD::Instrument::Backends::LoggerBackend.new(@logger)
end

#setup_null_backendObject



72
73
74
# File 'lib/d13n/metric/manager.rb', line 72

def setup_null_backend
  StatsD::Instrument::Backends::NullBackend.new
end

#setup_udp_backendObject



58
59
60
61
62
63
64
# File 'lib/d13n/metric/manager.rb', line 58

def setup_udp_backend
  @host = @opts.fetch(:host, default_host)
  @port = @opts.fetch(:port, default_port)
  @backend_uri = "#{@host}:#{@port}"
  @protocol = @opts.fetch(:protocol, default_protocol)
  StatsD::Instrument::Backends::UDPBackend.new(@backend_uri, @protocol) 
end