fluent-plugin-file-with-header 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE +202 -0
- data/README.md +43 -0
- data/Rakefile +13 -0
- data/fluent-plugin-file-with-header.gemspec +26 -0
- data/lib/fluent/plugin/out_file_with_header.rb +334 -0
- data/test/helper.rb +8 -0
- data/test/plugin/test_out_file_with_header.rb +18 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2b9b933eb2bce3f00fc363ded7338d3a275df0563c07971c33e78477c90ca3dd
|
4
|
+
data.tar.gz: 971413afec87a4c7c699038e127566c270ee840a591237d2823e79da39d94b38
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c326cd71f79221cbcef1d41d712d0fd52449129f390214601979bfe846339bbcff65d35e652317725f4497e1ee721b3b3d88a0d55daaaf8aa02ea8afb651e004
|
7
|
+
data.tar.gz: f985d29509062570e5a9bc69b9734c63081dc74809b31b13932955d76549121c509b13a3ad940fcc009144e12cc094b1fbb040a5d00d0211d174fa1ddabd3760
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# fluent-plugin-file-with-header
|
2
|
+
|
3
|
+
[Fluentd](https://fluentd.org/) output plugin to do something.
|
4
|
+
|
5
|
+
TODO: write description for you plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
### RubyGems
|
10
|
+
|
11
|
+
```
|
12
|
+
$ gem install fluent-plugin-file-with-header
|
13
|
+
```
|
14
|
+
|
15
|
+
### Bundler
|
16
|
+
|
17
|
+
Add following line to your Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem "fluent-plugin-file-with-header"
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```
|
26
|
+
$ bundle
|
27
|
+
```
|
28
|
+
|
29
|
+
## Configuration
|
30
|
+
|
31
|
+
You can generate configuration template:
|
32
|
+
|
33
|
+
```
|
34
|
+
$ fluent-plugin-config-format output file-with-header
|
35
|
+
```
|
36
|
+
|
37
|
+
You can copy and paste generated documents here.
|
38
|
+
|
39
|
+
## Copyright
|
40
|
+
|
41
|
+
* Copyright(c) 2020- Onoda Shigeaki
|
42
|
+
* License
|
43
|
+
* Apache License, Version 2.0
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs.push("lib", "test")
|
8
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
9
|
+
t.verbose = true
|
10
|
+
t.warning = true
|
11
|
+
end
|
12
|
+
|
13
|
+
task default: [:test]
|
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "fluent-plugin-file-with-header"
|
6
|
+
spec.version = "0.1.0"
|
7
|
+
spec.authors = ["pomcho555"]
|
8
|
+
spec.email = ["ono.fda03@gmail.com"]
|
9
|
+
|
10
|
+
spec.summary = "Output file with header."
|
11
|
+
spec.homepage = "https://github.com/pomcho555/fluent-plugin-file-with-header"
|
12
|
+
spec.license = "Apache-2.0"
|
13
|
+
|
14
|
+
test_files, files = `git ls-files -z`.split("\x0").partition do |f|
|
15
|
+
f.match(%r{^(test|spec|features)/})
|
16
|
+
end
|
17
|
+
spec.files = files
|
18
|
+
spec.executables = files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = test_files
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
23
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
24
|
+
spec.add_development_dependency "test-unit", "~> 3.0"
|
25
|
+
spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
|
26
|
+
end
|
@@ -0,0 +1,334 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2020- pomcho555
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
require 'fileutils'
|
17
|
+
require 'zlib'
|
18
|
+
require 'time'
|
19
|
+
|
20
|
+
require 'fluent/plugin/output'
|
21
|
+
require 'fluent/config/error'
|
22
|
+
# TODO remove ...
|
23
|
+
require 'fluent/plugin/file_util'
|
24
|
+
|
25
|
+
module Fluent::Plugin
|
26
|
+
class FileWithHeaderOutput < Fluent::Plugin::Output
|
27
|
+
Fluent::Plugin.register_output("file_with_header", self)
|
28
|
+
|
29
|
+
helpers :formatter, :inject, :compat_parameters
|
30
|
+
|
31
|
+
SUPPORTED_COMPRESS = [:text, :gz, :gzip]
|
32
|
+
SUPPORTED_COMPRESS_MAP = {
|
33
|
+
text: nil,
|
34
|
+
gz: :gzip,
|
35
|
+
gzip: :gzip,
|
36
|
+
}
|
37
|
+
|
38
|
+
DEFAULT_TIMEKEY = 60 * 60 * 24
|
39
|
+
|
40
|
+
desc "The Path of the file."
|
41
|
+
config_param :path, :string
|
42
|
+
|
43
|
+
desc "Specify to add file suffix for bare file path or not."
|
44
|
+
config_param :add_path_suffix, :bool, default: true
|
45
|
+
desc "The file suffix added to bare file path."
|
46
|
+
config_param :path_suffix, :string, default: '.log'
|
47
|
+
desc "The flushed chunk is appended to existence file or not."
|
48
|
+
config_param :append, :bool, default: false
|
49
|
+
desc "Compress flushed file."
|
50
|
+
config_param :compress, :enum, list: SUPPORTED_COMPRESS, default: :text
|
51
|
+
desc "Execute compression again even when buffer chunk is already compressed."
|
52
|
+
config_param :recompress, :bool, default: false
|
53
|
+
desc "Create symlink to temporary buffered file when buffer_type is file (disabled on Windows)."
|
54
|
+
config_param :symlink_path, :string, default: nil
|
55
|
+
# This plugin add this param from out_file
|
56
|
+
config_param :headers, :string, default: nil
|
57
|
+
config_section :format do
|
58
|
+
config_set_default :@type, 'out_file'
|
59
|
+
end
|
60
|
+
|
61
|
+
config_section :buffer do
|
62
|
+
config_set_default :@type, 'file'
|
63
|
+
config_set_default :chunk_keys, ['time']
|
64
|
+
config_set_default :timekey, DEFAULT_TIMEKEY
|
65
|
+
end
|
66
|
+
|
67
|
+
attr_reader :dir_perm
|
68
|
+
attr_accessor :last_written_path # for tests
|
69
|
+
|
70
|
+
module SymlinkBufferMixin
|
71
|
+
def metadata(timekey: nil, tag: nil, variables: nil)
|
72
|
+
metadata = super
|
73
|
+
|
74
|
+
@latest_metadata ||= new_metadata(timekey: 0)
|
75
|
+
if metadata.timekey && (metadata.timekey >= @latest_metadata.timekey)
|
76
|
+
@latest_metadata = metadata
|
77
|
+
end
|
78
|
+
|
79
|
+
metadata
|
80
|
+
end
|
81
|
+
|
82
|
+
def output_plugin_for_symlink=(output_plugin)
|
83
|
+
@_output_plugin_for_symlink = output_plugin
|
84
|
+
end
|
85
|
+
|
86
|
+
def symlink_path=(path)
|
87
|
+
@_symlink_path = path
|
88
|
+
end
|
89
|
+
|
90
|
+
def generate_chunk(metadata)
|
91
|
+
chunk = super
|
92
|
+
# "symlink" feature is to link from symlink_path to the latest file chunk. Records with latest
|
93
|
+
# timekey will be appended into that file chunk. On the other side, resumed file chunks might NOT
|
94
|
+
# have timekey, especially in the cases that resumed file chunks are generated by Fluentd v0.12.
|
95
|
+
# These chunks will be enqueued immediately, and will be flushed soon.
|
96
|
+
if chunk.metadata == @latest_metadata
|
97
|
+
sym_path = @_output_plugin_for_symlink.extract_placeholders(@_symlink_path, chunk)
|
98
|
+
FileUtils.mkdir_p(File.dirname(sym_path), mode: @_output_plugin_for_symlink.dir_perm)
|
99
|
+
FileUtils.ln_sf(chunk.path, sym_path)
|
100
|
+
end
|
101
|
+
chunk
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def configure(conf)
|
106
|
+
compat_parameters_convert(conf, :formatter, :buffer, :inject, default_chunk_key: "time")
|
107
|
+
|
108
|
+
configured_time_slice_format = conf['time_slice_format']
|
109
|
+
|
110
|
+
if conf.elements(name: 'buffer').empty?
|
111
|
+
conf.add_element('buffer', 'time')
|
112
|
+
end
|
113
|
+
buffer_conf = conf.elements(name: 'buffer').first
|
114
|
+
# Fluent::PluginId#configure is not called yet, so we can't use #plugin_root_dir here.
|
115
|
+
if !buffer_conf.has_key?('path') && !(conf['@id'] && system_config.root_dir)
|
116
|
+
# v0.14 file buffer handles path as directory if '*' is missing
|
117
|
+
# 'dummy_path' is not to raise configuration error for 'path' in file buffer plugin,
|
118
|
+
# but raise it in this plugin.
|
119
|
+
buffer_conf['path'] = conf['path'] || '/tmp/dummy_path'
|
120
|
+
end
|
121
|
+
|
122
|
+
if conf.has_key?('utc') || conf.has_key?('localtime')
|
123
|
+
param_name = conf.has_key?('utc') ? 'utc' : 'localtime'
|
124
|
+
log.warn "'#{param_name}' is deprecated for output plugin. This parameter is used for formatter plugin in compatibility layer. If you want to use same feature, use timekey_use_utc parameter in <buffer> directive instead"
|
125
|
+
end
|
126
|
+
|
127
|
+
super
|
128
|
+
|
129
|
+
@compress_method = SUPPORTED_COMPRESS_MAP[@compress]
|
130
|
+
|
131
|
+
if @path.include?('*') && !@buffer_config.timekey
|
132
|
+
raise Fluent::ConfigError, "path including '*' must be used with buffer chunk key 'time'"
|
133
|
+
end
|
134
|
+
|
135
|
+
path_suffix = @add_path_suffix ? @path_suffix : ''
|
136
|
+
path_timekey = if @chunk_key_time
|
137
|
+
@as_secondary ? @primary_instance.buffer_config.timekey : @buffer_config.timekey
|
138
|
+
else
|
139
|
+
nil
|
140
|
+
end
|
141
|
+
@path_template = generate_path_template(@path, path_timekey, @append, @compress_method, path_suffix: path_suffix, time_slice_format: configured_time_slice_format)
|
142
|
+
|
143
|
+
if @as_secondary
|
144
|
+
# When this plugin is configured as secondary & primary plugin has tag key, but this plugin may not have it.
|
145
|
+
# Increment placeholder can make another output file per chunk tag/keys even if original path doesn't include it.
|
146
|
+
placeholder_validators(:path, @path_template).select{|v| v.type == :time }.each do |v|
|
147
|
+
v.validate!
|
148
|
+
end
|
149
|
+
else
|
150
|
+
placeholder_validate!(:path, @path_template)
|
151
|
+
|
152
|
+
max_tag_index = get_placeholders_tag(@path_template).max || 1
|
153
|
+
max_tag_index = 1 if max_tag_index < 1
|
154
|
+
dummy_tag = (['a'] * max_tag_index).join('.')
|
155
|
+
dummy_record_keys = get_placeholders_keys(@path_template) || ['message']
|
156
|
+
dummy_record = Hash[dummy_record_keys.zip(['data'] * dummy_record_keys.size)]
|
157
|
+
|
158
|
+
test_chunk1 = chunk_for_test(dummy_tag, Fluent::EventTime.now, dummy_record)
|
159
|
+
test_path = extract_placeholders(@path_template, test_chunk1)
|
160
|
+
unless ::Fluent::FileUtil.writable_p?(test_path)
|
161
|
+
raise Fluent::ConfigError, "out_file: `#{test_path}` is not writable"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
@formatter = formatter_create
|
166
|
+
|
167
|
+
if @symlink_path && @buffer.respond_to?(:path)
|
168
|
+
if @as_secondary
|
169
|
+
raise Fluent::ConfigError, "symlink_path option is unavailable in <secondary>: consider to use secondary_file plugin"
|
170
|
+
end
|
171
|
+
if Fluent.windows?
|
172
|
+
log.warn "symlink_path is unavailable on Windows platform. disabled."
|
173
|
+
@symlink_path = nil
|
174
|
+
else
|
175
|
+
@buffer.extend SymlinkBufferMixin
|
176
|
+
@buffer.symlink_path = @symlink_path
|
177
|
+
@buffer.output_plugin_for_symlink = self
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
@dir_perm = system_config.dir_permission || Fluent::DEFAULT_DIR_PERMISSION
|
182
|
+
@file_perm = system_config.file_permission || Fluent::DEFAULT_FILE_PERMISSION
|
183
|
+
@need_lock = system_config.workers > 1
|
184
|
+
end
|
185
|
+
|
186
|
+
def multi_workers_ready?
|
187
|
+
true
|
188
|
+
end
|
189
|
+
|
190
|
+
def format(tag, time, record)
|
191
|
+
r = inject_values_to_record(tag, time, record)
|
192
|
+
@formatter.format(tag, time, r)
|
193
|
+
end
|
194
|
+
|
195
|
+
def write(chunk)
|
196
|
+
path = extract_placeholders(@path_template, chunk)
|
197
|
+
FileUtils.mkdir_p File.dirname(path), mode: @dir_perm
|
198
|
+
|
199
|
+
writer = case
|
200
|
+
when @compress_method.nil?
|
201
|
+
method(:write_without_compression)
|
202
|
+
when @compress_method == :gzip
|
203
|
+
if @buffer.compress != :gzip || @recompress
|
204
|
+
method(:write_gzip_with_compression)
|
205
|
+
else
|
206
|
+
method(:write_gzip_from_gzipped_chunk)
|
207
|
+
end
|
208
|
+
else
|
209
|
+
raise "BUG: unknown compression method #{@compress_method}"
|
210
|
+
end
|
211
|
+
|
212
|
+
if @append
|
213
|
+
writer.call(path, chunk)
|
214
|
+
else
|
215
|
+
find_filepath_available(path, with_lock: @need_lock) do |actual_path|
|
216
|
+
writer.call(actual_path, chunk)
|
217
|
+
path = actual_path
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
@last_written_path = path
|
222
|
+
end
|
223
|
+
|
224
|
+
def write_without_compression(path, chunk)
|
225
|
+
File.open(path, "ab", @file_perm) do |f|
|
226
|
+
if f.stat.size == 0
|
227
|
+
f.write(headers)
|
228
|
+
f.write("\n")
|
229
|
+
end
|
230
|
+
chunk.write_to(f)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def write_gzip_with_compression(path, chunk)
|
235
|
+
File.open(path, "ab", @file_perm) do |f|
|
236
|
+
gz = Zlib::GzipWriter.new(f)
|
237
|
+
if f.stat.size == 0
|
238
|
+
f.write(headers)
|
239
|
+
f.write("\n")
|
240
|
+
end
|
241
|
+
chunk.write_to(gz, compressed: :text)
|
242
|
+
gz.close
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
def write_gzip_from_gzipped_chunk(path, chunk)
|
247
|
+
if f.stat.size == 0
|
248
|
+
f.write(headers)
|
249
|
+
f.write("\n")
|
250
|
+
end
|
251
|
+
File.open(path, "ab", @file_perm) do |f|
|
252
|
+
chunk.write_to(f, compressed: :gzip)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def timekey_to_timeformat(timekey)
|
257
|
+
case timekey
|
258
|
+
when nil then ''
|
259
|
+
when 0...60 then '%Y%m%d%H%M%S' # 60 exclusive
|
260
|
+
when 60...3600 then '%Y%m%d%H%M'
|
261
|
+
when 3600...86400 then '%Y%m%d%H'
|
262
|
+
else '%Y%m%d'
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
def compression_suffix(compress)
|
267
|
+
case compress
|
268
|
+
when :gzip then '.gz'
|
269
|
+
when nil then ''
|
270
|
+
else
|
271
|
+
raise ArgumentError, "unknown compression type #{compress}"
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
# /path/to/dir/file.* -> /path/to/dir/file.%Y%m%d
|
276
|
+
# /path/to/dir/file.*.data -> /path/to/dir/file.%Y%m%d.data
|
277
|
+
# /path/to/dir/file -> /path/to/dir/file.%Y%m%d.log
|
278
|
+
# %Y%m%d -> %Y%m%d_** (non append)
|
279
|
+
# + .gz (gzipped)
|
280
|
+
## TODO: remove time_slice_format when end of support of compat_parameters
|
281
|
+
def generate_path_template(original, timekey, append, compress, path_suffix: '', time_slice_format: nil)
|
282
|
+
comp_suffix = compression_suffix(compress)
|
283
|
+
index_placeholder = append ? '' : '_**'
|
284
|
+
if original.index('*')
|
285
|
+
raise "BUG: configuration error must be raised for path including '*' without timekey" unless timekey
|
286
|
+
time_placeholders_part = time_slice_format || timekey_to_timeformat(timekey)
|
287
|
+
original.gsub('*', time_placeholders_part + index_placeholder) + comp_suffix
|
288
|
+
else
|
289
|
+
if timekey
|
290
|
+
if time_slice_format
|
291
|
+
"#{original}.#{time_slice_format}#{index_placeholder}#{path_suffix}#{comp_suffix}"
|
292
|
+
else
|
293
|
+
time_placeholders = timekey_to_timeformat(timekey)
|
294
|
+
if time_placeholders.scan(/../).any?{|ph| original.include?(ph) }
|
295
|
+
raise Fluent::ConfigError, "insufficient timestamp placeholders in path" if time_placeholders.scan(/../).any?{|ph| !original.include?(ph) }
|
296
|
+
"#{original}#{index_placeholder}#{path_suffix}#{comp_suffix}"
|
297
|
+
else
|
298
|
+
"#{original}.#{time_placeholders}#{index_placeholder}#{path_suffix}#{comp_suffix}"
|
299
|
+
end
|
300
|
+
end
|
301
|
+
else
|
302
|
+
"#{original}#{index_placeholder}#{path_suffix}#{comp_suffix}"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def find_filepath_available(path_with_placeholder, with_lock: false) # for non-append
|
308
|
+
raise "BUG: index placeholder not found in path: #{path_with_placeholder}" unless path_with_placeholder.index('_**')
|
309
|
+
i = 0
|
310
|
+
dir_path = locked = nil
|
311
|
+
while true
|
312
|
+
path = path_with_placeholder.sub('_**', "_#{i}")
|
313
|
+
i += 1
|
314
|
+
next if File.exist?(path)
|
315
|
+
|
316
|
+
if with_lock
|
317
|
+
dir_path = path + '.lock'
|
318
|
+
locked = Dir.mkdir(dir_path) rescue false
|
319
|
+
next unless locked
|
320
|
+
# ensure that other worker doesn't create a file (and release lock)
|
321
|
+
# between previous File.exist? and Dir.mkdir
|
322
|
+
next if File.exist?(path)
|
323
|
+
end
|
324
|
+
|
325
|
+
break
|
326
|
+
end
|
327
|
+
yield path
|
328
|
+
ensure
|
329
|
+
if dir_path && locked && Dir.exist?(dir_path)
|
330
|
+
Dir.rmdir(dir_path) rescue nil
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path("../../", __FILE__))
|
2
|
+
require "test-unit"
|
3
|
+
require "fluent/test"
|
4
|
+
require "fluent/test/driver/output"
|
5
|
+
require "fluent/test/helpers"
|
6
|
+
|
7
|
+
Test::Unit::TestCase.include(Fluent::Test::Helpers)
|
8
|
+
Test::Unit::TestCase.extend(Fluent::Test::Helpers)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "helper"
|
2
|
+
require "fluent/plugin/out_file_with_header.rb"
|
3
|
+
|
4
|
+
class FileWithHeaderOutputTest < Test::Unit::TestCase
|
5
|
+
setup do
|
6
|
+
Fluent::Test.setup
|
7
|
+
end
|
8
|
+
|
9
|
+
test "failure" do
|
10
|
+
flunk
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def create_driver(conf)
|
16
|
+
Fluent::Test::Driver::Output.new(Fluent::Plugin::FileWithHeaderOutput).configure(conf)
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-file-with-header
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pomcho555
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fluentd
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.14.10
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '2'
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 0.14.10
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '2'
|
75
|
+
description:
|
76
|
+
email:
|
77
|
+
- ono.fda03@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- fluent-plugin-file-with-header.gemspec
|
87
|
+
- lib/fluent/plugin/out_file_with_header.rb
|
88
|
+
- test/helper.rb
|
89
|
+
- test/plugin/test_out_file_with_header.rb
|
90
|
+
homepage: https://github.com/pomcho555/fluent-plugin-file-with-header
|
91
|
+
licenses:
|
92
|
+
- Apache-2.0
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubygems_version: 3.0.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Output file with header.
|
113
|
+
test_files:
|
114
|
+
- test/helper.rb
|
115
|
+
- test/plugin/test_out_file_with_header.rb
|