Module: FFWD
- Defined in:
- lib/ffwd/event.rb,
lib/ffwd.rb,
lib/ffwd/core.rb,
lib/ffwd/utils.rb,
lib/ffwd/metric.rb,
lib/ffwd/plugin.rb,
lib/ffwd/schema.rb,
lib/ffwd/tunnel.rb,
lib/ffwd/channel.rb,
lib/ffwd/handler.rb,
lib/ffwd/logging.rb,
lib/ffwd/retrier.rb,
lib/ffwd/version.rb,
lib/ffwd/protocol.rb,
lib/ffwd/lifecycle.rb,
lib/ffwd/connection.rb,
lib/ffwd/statistics.rb,
lib/ffwd/core/emitter.rb,
lib/ffwd/core/reporter.rb,
lib/ffwd/event_emitter.rb,
lib/ffwd/core/interface.rb,
lib/ffwd/core/processor.rb,
lib/ffwd/metric_emitter.rb,
lib/ffwd/plugin_channel.rb,
lib/ffwd/circular_buffer.rb,
lib/ffwd/producing_client.rb
Overview
$LICENSE Copyright 2013-2014 Spotify AB. All rights reserved.
The contents of this file are licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Defined Under Namespace
Modules: Debug, Lifecycle, Logging, Plugin, PluginLoader, Processor, Reporter, Schema, Statistics, TCP, Tunnel, UDP
Classes: Channel, CircularBuffer, ClassLogger, Connection, Core, Event, EventEmitter, EventStruct, FakeLogger, Handler, Metric, MetricEmitter, MetricStruct, PluginChannel, ProducingClient, Retrier
Constant Summary
collapse
- DEFAULT_PLUGIN_DIRECTORIES =
[
'./plugins'
]
- SCHEMA_DEFAULT_SUPPORT =
[
:dump_metric,
:dump_event
]
- DEFUALT_SCHEMA =
'default'
- DEFAULT_CONTENT_TYPE =
'application/json'
- FAMILIES =
{:tcp => Tunnel::TCP, :udp => Tunnel::UDP}
- DEFAULT_TIMEOUT =
10
- VERSION =
"0.1.7"
- DEFAULT_FLUSH_PERIOD =
10
- DEFAULT_EVENT_LIMIT =
10000
- DEFAULT_METRIC_LIMIT =
10000
- DEFAULT_FLUSH_SIZE =
1000
Class Method Summary
collapse
-
.activated_plugins_warning ⇒ Object
-
.current_host ⇒ Object
-
.dump2hex(data) ⇒ Object
-
.dump_activated_plugins(plugins) ⇒ Object
-
.dump_loaded_plugins ⇒ Object
-
.is_reporter?(var) ⇒ Boolean
-
.load_config_dir(dir, config) ⇒ Object
-
.load_yaml(path) ⇒ Object
-
.log ⇒ Object
-
.log_config ⇒ Object
-
.log_disable ⇒ Object
-
.log_disabled? ⇒ Boolean
-
.log_reload ⇒ Object
-
.main(args) ⇒ Object
-
.merge_configurations(target, source) ⇒ Object
-
.merge_hashes(a, b) ⇒ Object
-
.merge_sets(a, b) ⇒ Object
Merge two sets (arrays actually).
-
.opts ⇒ Object
-
.parse_options(args) ⇒ Object
-
.parse_protocol(original) ⇒ Object
-
.parse_schema(opts, support = SCHEMA_DEFAULT_SUPPORT) ⇒ Object
-
.parser ⇒ Object
-
.producing_client(channel, producer, opts) ⇒ Object
-
.retry(opts = {}, &block) ⇒ Object
-
.setup_log ⇒ Object
-
.setup_plugins(config) ⇒ Object
-
.timing(&block) ⇒ Object
-
.tunnel(family, port, core, plugin, log, connection, args) ⇒ Object
Class Method Details
.activated_plugins_warning ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/ffwd.rb', line 202
def self.activated_plugins_warning
puts " NO ACTIVATED PLUGINS!"
puts ""
puts " 1) Did you specify a valid configuration?"
puts " Example ways to configure:"
puts " ffwd -c /etc/ffwd.conf"
puts " ffwd -d /etc/ffwd.d/"
puts ""
puts " 2) Are your plugins loaded?"
puts " Check with:"
puts " ffwd -c .. --plugins"
puts ""
puts " 3) Did any errors happen when loading the plugins?"
puts " Check with:"
puts " ffwd -c .. --debug"
puts ""
puts " 4) If you think you've stumbled on a bug, report it to:"
puts " https://github.com/spotify/ffwd"
end
|
.current_host ⇒ Object
36
37
38
|
# File 'lib/ffwd/utils.rb', line 36
def self.current_host
Socket.gethostname
end
|
.dump2hex(data) ⇒ Object
47
48
49
|
# File 'lib/ffwd/utils.rb', line 47
def self.dump2hex data
data.bytes.map { |byte| byte.to_s(16) }.join
end
|
.dump_activated_plugins(plugins) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/ffwd.rb', line 187
def self.dump_activated_plugins plugins
plugins.each do |kind, kind_plugins|
puts " #{kind}:"
if kind_plugins.empty?
puts " (no active plugins)"
next
end
kind_plugins.each do |p|
puts " #{p.name}: #{p.config}"
end
end
end
|
.dump_loaded_plugins ⇒ Object
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
|
# File 'lib/ffwd.rb', line 146
def self.dump_loaded_plugins
FFWD::Plugin.loaded.each do |name, plugin|
unless description = plugin.description
description = "no description"
end
puts " Plugin '#{name}' (#{description})"
puts " Source: #{plugin.source}"
puts " Supported modes: #{plugin.capabilities.join(', ')}"
unless plugin.options.empty?
puts " Available Options:"
plugin.options.each do |opt|
if modes = opt[:modes]
modes = modes.join(', ')
else
modes = "all modes"
end
puts " :#{opt[:name]} (#{modes})"
unless (default = opt[:default]).nil?
puts " Default: #{default.inspect}"
else
puts " Default: (no default)"
end
if help = opt[:help]
if help.is_a? Array
help.each do |h|
puts " #{h}"
end
else
puts " #{help}"
end
end
end
end
end
end
|
.is_reporter?(var) ⇒ Boolean
32
33
34
|
# File 'lib/ffwd/utils.rb', line 32
def self.is_reporter? var
var.respond_to? :report!
end
|
.load_config_dir(dir, config) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/ffwd.rb', line 59
def self.load_config_dir dir, config
Dir.entries(dir).sort.each do |entry|
entry_path = File.join dir, entry
next unless File.file? entry_path
if entry.start_with? "."
log.debug "Ignoring: #{entry_path} (hidden file)"
next
end
c = load_yaml entry_path
if c.nil?
log.warn "Ignoring: #{entry_path} (invalid yaml)"
next
end
yield c
end
end
|
.load_yaml(path) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/ffwd.rb', line 32
def self.load_yaml path
return YAML.load_file path
rescue => e
log.error "Failed to load config: #{path} (#{e})"
return nil
end
|
.log ⇒ Object
35
36
37
|
# File 'lib/ffwd/logging.rb', line 35
def self.log
@log ||= setup_log
end
|
.log_config ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/ffwd/logging.rb', line 64
def self.log_config
return @log_config unless @log_config.nil?
@log_config = {
:file => nil,
:shift_age => 1,
:level => Logger::INFO,
:stream => STDOUT,
:progname => 'FFWD',
}
end
|
.log_disable ⇒ Object
76
77
78
|
# File 'lib/ffwd/logging.rb', line 76
def self.log_disable
@log_disable = true
end
|
.log_disabled? ⇒ Boolean
80
81
82
|
# File 'lib/ffwd/logging.rb', line 80
def self.log_disabled?
@log_disable || false
end
|
.log_reload ⇒ Object
39
40
41
|
# File 'lib/ffwd/logging.rb', line 39
def self.log_reload
@log = setup_log
end
|
.main(args) ⇒ Object
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
|
# File 'lib/ffwd.rb', line 222
def self.main args
parse_options args
if opts[:show_version]
puts "ffwd version: #{FFWD::VERSION}"
return 0
end
FFWD.log_config[:level] = opts[:debug] ? Logger::DEBUG : Logger::INFO
config = {}
opts[:config_paths].each do |path|
unless File.file? path
puts "Configuration path does not exist: #{path}"
puts ""
puts parser.help
return 1
end
return 0 unless source = load_yaml(path)
merge_configurations config, source
end
if config_dir = opts[:config_dir]
unless File.directory? config_dir
puts "Configuration directory does not exist: #{path}"
puts ""
puts parser.help
return 1
end
load_config_dir(config_dir, config) do |c|
merge_configurations config, c
end
end
if config[:logging]
if opts[:debug]
puts "Ignoring :logging directive, --debug in effect"
else
config[:logging].each do |key, value|
FFWD.log_config[key] = value
end
end
end
FFWD.log_reload
if FFWD.log_config[:file]
puts "Logging to file: #{FFWD.log_config[:file]}"
end
blacklist = config[:blacklist] || {}
directories = ((config[:plugin_directories] || []) +
(opts[:plugin_directories] || []))
PluginLoader.plugin_directories = directories
PluginLoader.load FFWD::Plugin, blacklist[:plugins] || []
PluginLoader.load FFWD::Processor, blacklist[:processors] || []
PluginLoader.load FFWD::Schema, blacklist[:schemas] || []
stop_early = false
stop_early ||= opts[:list_plugins]
stop_early ||= opts[:list_schemas]
stop_early ||= opts[:dump_config]
plugins = setup_plugins config
all_empty = plugins.values.map(&:empty?).all?
if opts[:list_plugins]
puts ""
puts "Loaded Plugins:"
dump_loaded_plugins
puts ""
if all_empty
activated_plugins_warning
else
puts "Activated Plugins:"
dump_activated_plugins plugins
end
puts ""
end
if opts[:list_schemas]
puts "Available Schemas:"
FFWD::Schema.loaded.each do |key, schema|
name, content_type = key
puts "Schema '#{name}' #{content_type} (#{schema.source})"
end
end
if opts[:dump_config]
puts "Configuration:"
puts config
end
if stop_early
return 0
end
if all_empty
puts ""
activated_plugins_warning
puts ""
return 1
end
core = FFWD::Core.new plugins, config
core.run
return 0
end
|
.merge_configurations(target, source) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/ffwd.rb', line 39
def self.merge_configurations target, source
if target.is_a? Hash
raise "source not a Hash: #{source}" unless source.is_a? Hash
source.each do |key, value|
target[key] = merge_configurations target[key], source[key]
end
return target
end
if target.is_a? Array
raise "source not an Array: #{source}" unless source.is_a? Array
return target + source
end
return source
end
|
.merge_hashes(a, b) ⇒ Object
27
28
29
30
|
# File 'lib/ffwd/utils.rb', line 27
def self.merge_hashes(a, b)
return a.merge(b) if a and b
b || a || {}
end
|
.merge_sets(a, b) ⇒ Object
Merge two sets (arrays actually)
21
22
23
24
|
# File 'lib/ffwd/utils.rb', line 21
def self.merge_sets(a, b)
return Set.new(a + b).to_a if a and b
a || b || []
end
|
.opts ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/ffwd.rb', line 81
def self.opts
@@opts ||= {:debug => false, :config => nil, :config_dir => nil,
:list_plugins => false, :list_schemas => false,
:dump_config => false, :show_version => false,
:config_paths => [],
:plugin_directories => DEFAULT_PLUGIN_DIRECTORIES}
end
|
.parse_options(args) ⇒ Object
127
128
129
|
# File 'lib/ffwd.rb', line 127
def self.parse_options args
parser.parse args
end
|
.parse_protocol(original) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/ffwd/protocol.rb', line 20
def self.parse_protocol(original)
string = original.downcase
return UDP if string == "udp"
return TCP if string == "tcp"
throw "Unknown protocol '#{original}'"
end
|
.parse_schema(opts, support = SCHEMA_DEFAULT_SUPPORT) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/ffwd/schema.rb', line 25
def self.parse_schema opts, support=SCHEMA_DEFAULT_SUPPORT
name = opts[:schema] || DEFUALT_SCHEMA
content_type = opts[:content_type] || DEFAULT_CONTENT_TYPE
key = [name, content_type]
schema = FFWD::Schema.loaded[key]
if schema.nil?
raise "No schema '#{name}' for content type '#{content_type}'"
end
unless schema.support? support
raise "Schema #{schema} does not support all of: #{support}"
end
return schema.mod
end
|
.parser ⇒ Object
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
|
# File 'lib/ffwd.rb', line 89
def self.parser
@@parser ||= OptionParser.new do |o|
o.banner = "Usage: ffwd [options]"
o.on "-d", "--[no-]debug" do |d|
opts[:debug] = d
end
o.on "-c", "--config <path>", "Load the specified configuration file." do |path|
opts[:config_paths] << path
end
o.on "-d", "--config-directory <path>", "Load configuration files from the specified directory." do |path|
opts[:config_dir] = path
end
o.on "--plugins", "Print loaded and activated plugins." do
opts[:list_plugins] = true
end
o.on "--schemas", "Print available schemas." do
opts[:list_schemas] = true
end
o.on "--dump", "Dump the configuration that has been loaded." do
opts[:dump_config] = true
end
o.on "--plugin-directory <dir>", "Load plugins from the specified directory." do |dir|
opts[:plugin_directories] << dir
end
o.on "-v", "--version", "Show version." do
opts[:show_version] = true
end
end
end
|
.producing_client(channel, producer, opts) ⇒ Object
180
181
182
183
184
185
|
# File 'lib/ffwd/producing_client.rb', line 180
def self.producing_client channel, producer, opts
flush_period = opts[:flush_period] || DEFAULT_FLUSH_PERIOD
event_limit = opts[:event_limit] || DEFAULT_EVENT_LIMIT
metric_limit = opts[:metric_limit] || DEFAULT_METRIC_LIMIT
ProducingClient.new channel, producer, flush_period, event_limit, metric_limit
end
|
.retry(opts = {}, &block) ⇒ Object
68
69
70
71
|
# File 'lib/ffwd/retrier.rb', line 68
def self.retry opts={}, &block
timeout = opts[:timeout] || DEFAULT_TIMEOUT
Retrier.new(timeout, &block)
end
|
.setup_log ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/ffwd/logging.rb', line 43
def self.setup_log
if log_config[:file]
file = log_config[:file]
shift_age = log_config[:shift_age]
return ::Logger.new(file, shift_age=shift_age).tap do |l|
l.level = log_config[:level]
l.progname = log_config[:progname]
end
end
if log_config[:stream]
return ::Logger.new(log_config[:stream]).tap do |l|
l.level = log_config[:level]
l.progname = log_config[:progname]
end
end
raise "cannot setup loggin with options: #{log_config}"
end
|
.setup_plugins(config) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/ffwd.rb', line 131
def self.setup_plugins config
plugins = {}
plugins[:tunnel] = FFWD::Plugin.load_plugins(
log, "Tunnel", config[:tunnel], :tunnel)
plugins[:input] = FFWD::Plugin.load_plugins(
log, "Input", config[:input], :input)
plugins[:output] = FFWD::Plugin.load_plugins(
log, "Output", config[:output], :output)
plugins
end
|
.timing(&block) ⇒ Object
40
41
42
43
44
45
|
# File 'lib/ffwd/utils.rb', line 40
def self.timing &block
start = Time.now
block.call
stop = Time.now
((stop - start) * 1000).round(3)
end
|
.tunnel(family, port, core, plugin, log, connection, args) ⇒ Object
22
23
24
25
26
|
# File 'lib/ffwd/tunnel.rb', line 22
def self.tunnel family, port, core, plugin, log, connection, args
impl = FAMILIES[family]
raise "Unsupported family: #{family}" if impl.nil?
return impl.new port, core, plugin, log, connection, args
end
|