fluent-plugin-lambda 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 952e6510f51593e371e8a325ff06aa0816b283b1
4
+ data.tar.gz: 4c0ea87380ce8c827492c4c70fd3923d7c928bd0
5
+ SHA512:
6
+ metadata.gz: eb2be51136d48e86c88e03e5971a9e400e4982ef5c7c7d1472bdf0613b7f4e1da8868ffa02ef5609cbfa828650c13768685b721bc97ac3e1e573bd33b52c4500
7
+ data.tar.gz: dc6d903d4c4287a4ae6398b1018a5bfe033996c4fad12fe30785b937b3801fe44309f51ec16aba57aeaf62396f176bb86a9564e16dc66611e6bb81f9e9650ebe
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script:
5
+ - bundle install
6
+ - bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-dd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Genki Sugawara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # fluent-plugin-lambda
2
+
3
+ Output plugin for [AWS Lambda](http://aws.amazon.com/lambda/).
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/fluent-plugin-lambda.svg)](http://badge.fury.io/rb/fluent-plugin-lambda)
6
+ [![Build Status](https://travis-ci.org/winebarrel/fluent-plugin-lambda.svg?branch=master)](https://travis-ci.org/winebarrel/fluent-plugin-lambda)
7
+
8
+ ## Installation
9
+
10
+ $ gem install fluent-plugin-lambda
11
+
12
+ ## Configuration
13
+
14
+ ```
15
+ <match lambda.**>
16
+ type lambda
17
+ #profile ...
18
+ #credentials_path ...
19
+ #aws_key_id ...
20
+ #aws_sec_key ...
21
+ region us-east-1
22
+ #endpoint ...
23
+
24
+ function_name my_func
25
+ # Pass the function name in the key of record if the function name is not set
26
+
27
+ # include_time_key false
28
+ # include_tag_key false
29
+ </match>
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ When the function name is set:
35
+
36
+ ```sh
37
+ echo '{"key":"value"}' | fluent-cat lambda.foo
38
+ ```
39
+
40
+ When the function name is not set:
41
+
42
+ ```sh
43
+ echo '{"function_name":"my_func", "key":"value"}' | fluent-cat lambda.bar
44
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'fluent-plugin-lambda'
4
+ spec.version = '0.1.0'
5
+ spec.authors = ['Genki Sugawara']
6
+ spec.email = ['sugawara@cookpad.com']
7
+ spec.description = %q{Output plugin for AWS Lambda.}
8
+ spec.summary = %q{Output plugin for AWS Lambda.}
9
+ spec.homepage = 'https://github.com/winebarrel/fluent-plugin-lambda'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ['lib']
15
+
16
+ spec.add_dependency 'fluentd'
17
+ spec.add_dependency 'aws-sdk-core', '~> 2.0.9'
18
+ spec.add_development_dependency 'bundler'
19
+ spec.add_development_dependency 'rake'
20
+ spec.add_development_dependency 'rspec', '>= 3.0.0'
21
+ end
@@ -0,0 +1,87 @@
1
+ class Fluent::LambdaOutput < Fluent::BufferedOutput
2
+ Fluent::Plugin.register_output('lambda', self)
3
+
4
+ include Fluent::SetTimeKeyMixin
5
+ include Fluent::SetTagKeyMixin
6
+
7
+ unless method_defined?(:log)
8
+ define_method('log') { $log }
9
+ end
10
+
11
+ config_param :profile, :string, :default => nil
12
+ config_param :credentials_path, :string, :default => nil
13
+ config_param :aws_key_id, :string, :default => nil
14
+ config_param :aws_sec_key, :string, :default => nil
15
+ config_param :region, :string, :default => nil
16
+ config_param :endpoint, :string, :default => nil
17
+ config_param :function_name, :string, :default => nil
18
+
19
+ config_set_default :include_time_key, false
20
+ config_set_default :include_tag_key, false
21
+
22
+ def initialize
23
+ super
24
+ require 'aws-sdk-core'
25
+ require 'json'
26
+ end
27
+
28
+ def configure(conf)
29
+ super
30
+
31
+ aws_opts = {}
32
+
33
+ if @profile
34
+ credentials_opts = {:profile_name => @profile}
35
+ credentials_opts[:path] = @credentials_path if @credentials_path
36
+ credentials = Aws::SharedCredentials.new(credentials_opts)
37
+ aws_opts[:credentials] = credentials
38
+ end
39
+
40
+ aws_opts[:access_key_id] = @aws_key_id if @aws_key_id
41
+ aws_opts[:secret_access_key] = @aws_sec_key if @aws_sec_key
42
+ aws_opts[:region] = @region if @region
43
+ aws_opts[:endpoint] = @endpoint if @endpoint
44
+
45
+ configure_aws(aws_opts)
46
+ end
47
+
48
+ def start
49
+ super
50
+
51
+ @client = create_client
52
+ end
53
+
54
+ def format(tag, time, record)
55
+ [tag, time, record].to_msgpack
56
+ end
57
+
58
+ def write(chunk)
59
+ chunk = chunk.to_enum(:msgpack_each)
60
+
61
+ chunk.select {|tag, time, record|
62
+ if @function_name or record['function_name']
63
+ true
64
+ else
65
+ log.warn("`function_name` key does not exist: #{[tag, time, record].inspect}")
66
+ false
67
+ end
68
+ }.each {|tag, time, record|
69
+ func_name = @function_name || record['function_name']
70
+
71
+ @client.invoke_async(
72
+ :function_name => func_name,
73
+ :invoke_args => JSON.dump(record),
74
+ )
75
+ }
76
+ end
77
+
78
+ private
79
+
80
+ def configure_aws(options)
81
+ Aws.config.update(options)
82
+ end
83
+
84
+ def create_client
85
+ Aws::Lambda::Client.new
86
+ end
87
+ end
@@ -0,0 +1,59 @@
1
+ describe Fluent::LambdaOutput do
2
+ let(:time) {
3
+ Time.parse('2014-09-01 01:23:45 UTC').to_i
4
+ }
5
+
6
+ context 'when events is sent' do
7
+ it 'should be call invoke_async' do
8
+ run_driver(function_name: 'my_func') do |d, client|
9
+ expect(client).to receive(:invoke_async).with(
10
+ function_name: 'my_func',
11
+ invoke_args: JSON.dump('key1' => 'foo' , 'key2' => 100),
12
+ )
13
+
14
+ expect(client).to receive(:invoke_async).with(
15
+ function_name: 'my_func',
16
+ invoke_args: JSON.dump('key1' => 'bar' , 'key2' => 200),
17
+ )
18
+
19
+ d.emit({'key1' => 'foo', 'key2' => 100}, time)
20
+ d.emit({'key1' => 'bar', 'key2' => 200}, time)
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'when events is sent with function_name' do
26
+ it 'should be call invoke_async' do
27
+ run_driver do |d, client|
28
+ expect(client).to receive(:invoke_async).with(
29
+ function_name: 'my_func1',
30
+ invoke_args: JSON.dump('function_name' => 'my_func1', 'key1' => 'foo' , 'key2' => 100),
31
+ )
32
+
33
+ expect(client).to receive(:invoke_async).with(
34
+ function_name: 'my_func2',
35
+ invoke_args: JSON.dump('function_name' => 'my_func2', 'key1' => 'bar' , 'key2' => 200),
36
+ )
37
+
38
+ d.emit({'function_name' => 'my_func1', 'key1' => 'foo', 'key2' => 100}, time)
39
+ d.emit({'function_name' => 'my_func2', 'key1' => 'bar', 'key2' => 200}, time)
40
+ end
41
+ end
42
+ end
43
+
44
+ context 'when events is sent without function_name' do
45
+ it 'should be warned' do
46
+ run_driver do |d, client|
47
+ expect(client).to_not receive(:invoke_async)
48
+
49
+ d.emit({'key1' => 'foo', 'key2' => 100}, time)
50
+ d.emit({'key1' => 'bar', 'key2' => 200}, time)
51
+
52
+ expect(d.instance.log).to receive(:warn).
53
+ with('`function_name` key does not exist: ["test.default", 1409534625, {"key1"=>"foo", "key2"=>100}]')
54
+ expect(d.instance.log).to receive(:warn).
55
+ with('`function_name` key does not exist: ["test.default", 1409534625, {"key1"=>"bar", "key2"=>200}]')
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,36 @@
1
+ require 'fluent/test'
2
+ require 'fluent/plugin/out_lambda'
3
+ require 'aws-sdk-core'
4
+ require 'time'
5
+
6
+ # Disable Test::Unit
7
+ module Test::Unit::RunCount; def run(*); end; end
8
+
9
+ RSpec.configure do |config|
10
+ config.before(:all) do
11
+ Fluent::Test.setup
12
+ end
13
+ end
14
+
15
+ def run_driver(options = {})
16
+ tag = options.delete(:tag) || 'test.default'
17
+
18
+ additional_options = options.map {|key, value|
19
+ "#{key} #{value}"
20
+ }.join("\n")
21
+
22
+ fluentd_conf = <<-EOS
23
+ type lambda
24
+ aws_key_id AKIAIOSFODNN7EXAMPLE
25
+ aws_sec_key wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
26
+ region us-west-1
27
+ #{additional_options}
28
+ EOS
29
+
30
+ driver = Fluent::Test::OutputTestDriver.new(Fluent::LambdaOutput, tag).configure(fluentd_conf)
31
+
32
+ driver.run do
33
+ client = driver.instance.instance_variable_get(:@client)
34
+ yield(driver, client)
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-lambda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.9
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.9
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ description: Output plugin for AWS Lambda.
84
+ email:
85
+ - sugawara@cookpad.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - fluent-plugin-lambda.gemspec
98
+ - lib/fluent/plugin/out_lambda.rb
99
+ - spec/out_lambda_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/winebarrel/fluent-plugin-lambda
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.0.14
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Output plugin for AWS Lambda.
125
+ test_files:
126
+ - spec/out_lambda_spec.rb
127
+ - spec/spec_helper.rb