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.0"
DEFAULT_FLUSH_PERIOD =
10
DEFAULT_EVENT_LIMIT =
10000
DEFAULT_METRIC_LIMIT =
10000
DEFAULT_FLUSH_SIZE =
1000

Class Method Summary collapse

Class Method Details

.current_hostObject



36
37
38
# File 'lib/ffwd/utils.rb', line 36

def self.current_host
  Socket.gethostname
end

.is_reporter?(var) ⇒ Boolean

Returns:

  • (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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ffwd.rb', line 58

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



31
32
33
34
35
36
# File 'lib/ffwd.rb', line 31

def self.load_yaml path
  return YAML.load_file path
rescue => e
  log.error "Failed to load config: #{path} (#{e})"
  return nil
end

.logObject



35
36
37
# File 'lib/ffwd/logging.rb', line 35

def self.log
  @log ||= setup_log
end

.log_configObject



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_disableObject



76
77
78
# File 'lib/ffwd/logging.rb', line 76

def self.log_disable
  @log_disable = true
end

.log_disabled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ffwd/logging.rb', line 80

def self.log_disabled?
  @log_disable || false
end

.log_reloadObject



39
40
41
# File 'lib/ffwd/logging.rb', line 39

def self.log_reload
  @log = setup_log
end

.main(args) ⇒ Object



140
141
142
143
144
145
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
# File 'lib/ffwd.rb', line 140

def self.main args
  parse_options args

  FFWD.log_config[:level] = opts[:debug] ? Logger::DEBUG : Logger::INFO

  config = {:debug => nil}

  if config_path = opts[:config_path]
    unless File.file? config_path
      puts "Configuration path does not exist: #{config_path}"
      puts ""
      puts parser.help
      return 1
    end

    unless source = load_yaml(config_path)
      return 0
    end

    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]
    config[:logging].each do |key, value|
      FFWD.log_config[key] = value
    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

  if opts[:list_plugins]
    puts "Available Plugins:"

    FFWD::Plugin.loaded.each do |name, plugin|
      puts "Plugin '#{name}' (#{plugin.source})"
      puts "  supports: #{plugin.capabilities.join(' ')}"
    end

    puts "Activated Plugins:"

    plugins.each do |kind, kind_plugins|
      puts "#{kind}:"

      if kind_plugins.empty?
        puts "  (no active)"
      end

      kind_plugins.each do |p|
        puts "  #{p.name}: #{p.config}"
      end
    end
  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 "Dumping Configuration:"
    puts config
  end

  if stop_early
    return 0
  end

  core = FFWD::Core.new plugins, config
  core.run
  return 0
end

.merge_configurations(target, source) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ffwd.rb', line 38

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

  # override source
  return source
end

.merge_hashes(a, b) ⇒ Object

Merge two hashes.



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

.optsObject



80
81
82
83
84
85
# File 'lib/ffwd.rb', line 80

def self.opts
  @@opts ||= {:debug => false, :config => nil, :config_dir => nil,
              :list_plugins => false, :list_schemas => false,
              :dump_config => false,
              :plugin_directories => DEFAULT_PLUGIN_DIRECTORIES}
end

.parse_options(args) ⇒ Object



121
122
123
# File 'lib/ffwd.rb', line 121

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

.parserObject



87
88
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
# File 'lib/ffwd.rb', line 87

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>" do |path|
      opts[:config_path] = path
    end

    o.on "-d", "--config-dir <path>" do |path|
      opts[:config_dir] = path
    end

    o.on "--list-plugins", "Print available plugins." do
      opts[:list_plugins] = true
    end

    o.on "--list-schemas", "Print available schemas." do
      opts[:list_schemas] = true
    end

    o.on "--dump-config", "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
  end
end

.producing_client(channel, producer, opts) ⇒ Object



175
176
177
178
179
180
# File 'lib/ffwd/producing_client.rb', line 175

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_logObject



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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ffwd.rb', line 125

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