fluent-plugin-http_shadow 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b47da229788f20b9d79742ffde107634b120adcd
4
- data.tar.gz: 85c3d6ea8c4a5520d2dca87b0b781bbb0cc9052a
3
+ metadata.gz: 85934dddb2f9ec1e02ec98997f0fb71dff86153e
4
+ data.tar.gz: 41cc427fce05f732a149e5196f012507fea3d5b3
5
5
  SHA512:
6
- metadata.gz: 4f623027f369842bb639e94a16eaa4ed8e6fd0eb83db983f30f16cf91c10818505196093daf7dfa483edde3f28f2fdb0c6a27540d8430e9940c162ae7fcd6d21
7
- data.tar.gz: 7e88e174de9677945838a62956ce945114de8643c1a2fea3255e097b824f7b986a21782869ac2e4b4b972dae919737ea01d272fa09e9e50a8ef971e1f404ed86
6
+ metadata.gz: 4fe72dfeaa974d5bcfe7ae08bbad021fa149b24c62440d954a854b6993679aae74fd983ddeca2da1500265107b40c2468ae926b87f8977cfd905d2d285a9b300
7
+ data.tar.gz: 84ff822909dc2b4cf154a75e8d7b62c3d4a9a97949122cbaecc254b70cd9189503512d1844ba24755071aa1c475080ee54a65e6e87111fe5dfebccbcc8eed6e4
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "fluent-plugin-http_shadow"
5
- gem.version = "0.0.1"
5
+ gem.version = "0.0.2"
6
6
  gem.summary = %q{copy http request. use shadow proxy server.}
7
7
  gem.description = %q{copy http request. use shadow proxy server.}
8
8
  gem.license = "MIT"
@@ -15,6 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
16
  gem.require_paths = ['lib']
17
17
 
18
+ gem.add_runtime_dependency "typhoeus"
19
+ gem.add_runtime_dependency "addressable"
18
20
  gem.add_development_dependency 'bundler', '~> 1.7.2'
19
21
  gem.add_development_dependency 'fluentd', '~> 0.10.58'
20
22
  gem.add_development_dependency 'pry', '~> 0.10.1'
@@ -1,34 +1,32 @@
1
- require 'json'
2
1
  module Fluent
3
- class HttpShadowOutput < Fluent::Output
2
+ class HttpShadowOutput < Fluent::BufferedOutput
4
3
  Fluent::Plugin.register_output('http_shadow', self)
5
4
 
6
5
  def initialize
7
6
  super
8
7
  require 'uri'
9
- require 'net/http'
10
8
  require 'erb'
9
+ require 'typhoeus'
10
+ require "addressable/uri"
11
11
  end
12
12
 
13
13
  config_param :host, :string, :default => nil
14
14
  config_param :host_key, :string, :default => nil
15
15
  config_param :host_hash, :hash, :default => nil
16
16
  config_param :path_format, :string
17
- config_param :method_key, :string
17
+ config_param :method_key, :string, :default => nil
18
18
  config_param :header_hash, :hash, :default => nil
19
19
  config_param :cookie_hash, :hash, :default => nil
20
20
  config_param :params_key, :string, :default => nil
21
+ config_param :max_concurrency, :integer, :default => 10
22
+ config_param :timeout, :integer, :default => 5
23
+ config_param :username, :string, :default => nil
24
+ config_param :password, :string, :default => nil
21
25
 
22
26
  def configure(conf)
23
27
  super
24
- if host
25
- @http = Net::HTTP.new(host)
26
- else
27
- @host_hash = Hash[@host_hash.map { |k,v| [k, Net::HTTP.new(v)] }]
28
- end
29
28
  @regexp = /\$\{([^}]+)\}/
30
- @path_format = @path_format.gsub(@regexp, "<%=record['" + '\1' + "'] %>")
31
- @path_format = ERB.new(@path_format)
29
+ @path_format = ERB.new(@path_format.gsub(@regexp, "<%=record['" + '\1' + "'] %>"))
32
30
 
33
31
  @headers = get_formatter(@header_hash)
34
32
  @cookies = get_formatter(@cookie_hash)
@@ -42,38 +40,49 @@ module Fluent
42
40
  super
43
41
  end
44
42
 
45
- def emit(tag, es, chain)
46
- chain.next
47
- es.each {|time,record|
48
- http = @http || @host_hash[record[@host_key]]
49
- next if http.nil?
50
- send_request(http, record)
51
- }
43
+ def format(tag, time, record)
44
+ [tag, time, record].to_msgpack
52
45
  end
53
46
 
54
- def send_request(http, record)
55
- method = record[@method_key] || 'GET'
56
-
57
- path = @path_format.result(binding)
58
- params = record[@params_key]
59
- unless params.nil?
60
- path = add_query_string(path, record, params) if method !~ /POST/i
47
+ def write(chunk)
48
+ records = []
49
+ chunk.msgpack_each do |tag, time, record|
50
+ records << record
61
51
  end
62
- req = Net::HTTP.const_get(method.capitalize).new(path)
63
- unless params.nil?
64
- req.set_form_data(params, "&") if method =~ /POST/i
52
+ send_request_parallel(records)
53
+ end
54
+
55
+ private
56
+
57
+ def send_request_parallel(records)
58
+ hydra = Typhoeus::Hydra.new(max_concurrency: @max_concurrency)
59
+ records.each do |record|
60
+ host = @host || @host_hash[record[@host_key]]
61
+ next if host.nil?
62
+ hydra.queue(get_request(host, record))
65
63
  end
66
- req = set_header(req, record)
67
- req['Cookie'] = get_cookie_string(record) if @cookie_hash
68
- response = http.request(req)
64
+ hydra.run
69
65
  end
70
66
 
71
- def add_query_string(path, record, params)
72
- uri = URI.parse(path)
73
- params_string = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
74
- join_string = uri.query.nil? ? '?' : '&'
75
- path = path + join_string + params_string
76
- path
67
+ def get_request(host, record)
68
+ method = (record[@method_key] || 'get').downcase.to_sym
69
+ path = @path_format.result(binding)
70
+
71
+ url = "http://" + host + path
72
+ uri = Addressable::URI.parse(url)
73
+ params = uri.query_values
74
+ params.merge(record[@params_key]) unless record[@params_key].nil?
75
+
76
+ option = {
77
+ timeout: @timeout * 1000,
78
+ followlocation: true,
79
+ method: method,
80
+ params: params,
81
+ headers: get_header(record)
82
+ }
83
+ option[:userpwd] = "#{@username}:#{@password}" if @username
84
+
85
+ Typhoeus::Request.new("http://" + host + uri.path, option)
77
86
  end
78
87
 
79
88
  def get_formatter(hash)
@@ -86,17 +95,27 @@ module Fluent
86
95
  formatter
87
96
  end
88
97
 
89
- def set_header(req, record)
98
+ def get_params(query, record_params)
99
+ params = query.nil? ? {} : Hash[URI::decode_www_form(query)]
100
+ if record_params
101
+ params = params.merge(record_params)
102
+ end
103
+ params
104
+ end
105
+
106
+ def get_header(record)
107
+ header = {}
90
108
  @headers.each do |k, v|
91
- req[k] = v.result(binding)
109
+ header[k] = v.result(binding)
92
110
  end
93
- req
111
+ header['Cookie'] = get_cookie_string(record) if @cookie_hash
112
+ header
94
113
  end
95
114
 
96
115
  def get_cookie_string(record)
97
116
  @cookies.map{|k, v|
98
117
  "#{k}=#{v.result(binding)}"
99
- }.join(';')
118
+ }.join('; ')
100
119
  end
101
120
  end
102
121
  end
@@ -4,29 +4,12 @@ class HttpShadowOutputTest < Test::Unit::TestCase
4
4
  Fluent::Test.setup
5
5
  end
6
6
 
7
- CONFIG = %[
8
- host staging.exsample.com
9
- path_format ${path}
10
- method_key method
11
- ]
12
-
13
- def create_driver(conf = CONFIG, tag='test')
14
- Fluent::Test::OutputTestDriver.new(Fluent::HttpShadowOutput, tag).configure(conf)
7
+ def create_driver(conf)
15
8
  end
16
9
 
17
10
  def test_configure
18
- d = create_driver
19
- assert_equal 'method', d.instance.method_key
20
- assert_equal nil, d.instance.header_hash
21
- end
22
-
23
- def test_configure_error
24
- config = %[
25
- method_key method
26
- ]
27
- assert_raise(Fluent::ConfigError) do
28
- d = create_driver(config)
29
- end
30
11
  end
31
12
 
32
13
  end
14
+
15
+
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-http_shadow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Toyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-26 00:00:00.000000000 Z
11
+ date: 2015-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
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: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement