Class: D13n::Metric::Manager
- Inherits:
-
Object
- Object
- D13n::Metric::Manager
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
#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
#backend ⇒ Object
Returns the value of attribute backend.
37
38
39
|
# File 'lib/d13n/metric/manager.rb', line 37
def backend
@backend
end
|
#channel ⇒ Object
Returns the value of attribute channel.
37
38
39
|
# File 'lib/d13n/metric/manager.rb', line 37
def channel
@channel
end
|
Class Method Details
.instance ⇒ Object
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_host ⇒ Object
94
95
96
|
# File 'lib/d13n/metric/manager.rb', line 94
def default_host
D13n.config[:'service.metric.host'] || 'localhost'
end
|
#default_port ⇒ Object
98
99
100
|
# File 'lib/d13n/metric/manager.rb', line 98
def default_port
D13n.config[:'service.metric.port'] || 8125
end
|
#default_protocol ⇒ Object
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_backend ⇒ Object
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_backend ⇒ Object
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_backend ⇒ Object
72
73
74
|
# File 'lib/d13n/metric/manager.rb', line 72
def setup_null_backend
StatsD::Instrument::Backends::NullBackend.new
end
|
#setup_udp_backend ⇒ Object
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
|