fluent-plugin-http_file_upload 0.1.2 → 0.1.3

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: 51dbdedb4b700afa5e852a48d118711c44bbc4a8
4
- data.tar.gz: fba90692103cab6e258f80b195528f695a619023
3
+ metadata.gz: 2620b143deb8788ff60dc04a35121e3d46784bca
4
+ data.tar.gz: f7fc807e21aaae3a55389957d0dab94ff948f0e5
5
5
  SHA512:
6
- metadata.gz: 0a85f8c168a2da0abaade94c77b4e498070c19f0aedec0efb0a71406223b310defc5646be09c8e07c0472bc30ac9da207f7d9c7394b8b37d860149b5d38323c5
7
- data.tar.gz: 84bef46d13d46574b4c19689a8837da566a6c8f106abae7dfc1920da00f244cf091634cfa940e92ca7cfc8bb4350e937f5cf2b9241a37a0edbc21ad53c06fbf0
6
+ metadata.gz: 2bb710641f615280bbf7169ac3d9d43a207814b6403918c2c0a9853f35ee60854102287a292b3f43cadb7750924e7d0b957e0a6bda37f88e7b029cf4ce7ac2f0
7
+ data.tar.gz: c7b88c29596196b1cb6fd3ad2112db370a27d743d7efff2a86130330bdcee2435e365b893b00699ca3d3c74b94aa0dd0cdda6eb774b593d836529fcf794ed32b
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "fluent-plugin-http_file_upload"
5
- spec.version = "0.1.2"
5
+ spec.version = "0.1.3"
6
6
  spec.authors = ["TAGOMORI Satoshi"]
7
7
  spec.email = ["tagomoris@gmail.com"]
8
8
 
@@ -1,6 +1,7 @@
1
1
  require 'fluent/output'
2
2
  require 'fluent/mixin'
3
3
 
4
+ require 'tempfile'
4
5
  require 'openssl'
5
6
  require 'uri'
6
7
  require 'httpclient'
@@ -21,6 +22,7 @@ module Fluent
21
22
  config_set_default :buffer_type, "file"
22
23
 
23
24
  config_param :uri, :string, desc: "Full URI for http upload endpoint for POST requests"
25
+
24
26
  config_param :param_name, :string, default: "file", desc: "Parameter name which contains uploaded file content"
25
27
  config_param :user_agent, :string, default: "fluent-plugin-http_file_upload", desc: "User-Agent header content"
26
28
  config_param :headers, :hash, default: {}, desc: "Additional header fields for requests"
@@ -40,8 +42,13 @@ module Fluent
40
42
 
41
43
  config_param :format, :string, default: "json", desc: "How to format records in uploaded files"
42
44
 
43
- # TODO: support compression
44
- # TODO: support gzipped transferring
45
+ SUPPORTED_COMPRESSION_TYPES = ['gzip']
46
+ config_param :compress, default: nil do |val|
47
+ unless SUPPORTED_COMPRESSION_TYPES.include?(val)
48
+ raise Fluent::ConfigError, "unsupported compression type: #{val}"
49
+ end
50
+ val
51
+ end
45
52
 
46
53
  def configure(conf)
47
54
  super
@@ -53,6 +60,11 @@ module Fluent
53
60
  if @uri.start_with?("https://")
54
61
  @client.ssl_config.verify_mode = @ssl_verify_mode
55
62
  end
63
+
64
+ case @compress
65
+ when 'gzip'
66
+ raise Fluent::ConfigError, "gzip command unavailable" unless system('gzip -h > /dev/null 2>&1')
67
+ end
56
68
  end
57
69
 
58
70
  def format(tag, time, record)
@@ -60,14 +72,62 @@ module Fluent
60
72
  end
61
73
 
62
74
  def write(chunk)
75
+ case @compress
76
+ when 'gzip'
77
+ write_gzip(chunk)
78
+ else
79
+ write_plain(chunk)
80
+ end
81
+ end
82
+
83
+ def write_plain(chunk)
63
84
  filename = Time.now.strftime(@filename)
64
85
  chunk.open do |io|
65
- io.singleton_class.class_eval{ define_method(:path){ filename } }
66
- postdata = { @param_name => io }
67
- unless @parameters.empty?
68
- postdata = @parameters.merge(postdata)
86
+ upload(io, filename)
87
+ end
88
+ end
89
+
90
+ def write_gzip(chunk)
91
+ filename = Time.now.strftime(@filename) + '.gz'
92
+ path = if chunk.respond_to?(:path)
93
+ chunk.path
94
+ else
95
+ w = Tempfile.new('chunk-gzip-temp-http_file_upload')
96
+ chunk.write_to(w)
97
+ w.close
98
+ w.path
99
+ end
100
+ tmp = Tempfile.new('gzip-temp-http_file_upload')
101
+ tmp.close # file will be removed after GC
102
+ res = system "gzip -c #{path} > #{tmp.path}"
103
+ unless res
104
+ log.warn "failed to execute gzip command: exit code '#{$?}'"
105
+ end
106
+ tmp.open
107
+ upload(tmp, filename)
108
+ tmp.close
109
+ end
110
+
111
+ StatDummy = Struct.new(:size)
112
+
113
+ def upload(io, filename)
114
+ stat_dummy = StatDummy.new(io.size)
115
+ io.singleton_class.class_eval{
116
+ define_method(:path){ filename } # override path to feed specified filename to httpclient
117
+ define_method(:lstat){ stat_dummy } # override lstat to return chunk size only (lstat doesn't work for chunk buffer file)
118
+ }
119
+ postdata = { @param_name => io }
120
+ unless @parameters.empty?
121
+ postdata = @parameters.merge(postdata)
122
+ end
123
+ res = @client.post(@uri, postdata)
124
+ if res.status == 200
125
+ log.info "upload success with code 200" # TODO: make this `debug`
126
+ else
127
+ log.error "failed to upload", uri: @uri, code: res.status, content: res.content
128
+ if res.status >= 500 && res.status < 600
129
+ raise "failed to upload with ServerError. retrying."
69
130
  end
70
- @client.post(@uri, postdata)
71
131
  end
72
132
  end
73
133
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-http_file_upload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAGOMORI Satoshi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-26 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd