fluent-plugin-arvancloud 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Dockerfile +7 -0
- data/Gemfile +4 -0
- data/LICENCE +202 -0
- data/README.md +1 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fluent-plugin-arvancloud.gemspec +28 -0
- data/fluentd.conf +20 -0
- data/lib/fluent/plugin/out_arvancloud.rb +431 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 61779acc0f3b8d9760a833b04bfa930ee7007a569503726bd135bbc02d821ab1
|
4
|
+
data.tar.gz: ba5552e5913133692c7919f73b70087e7108d66abc2a4492d893a548eff1bb80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5f0033f277569f3191deb79df452baef2286600d3018a709cea4e6b124d682fb98734a966b007a29e842d0066553d6c478f96230ec4f09bc53002ea3adac5144
|
7
|
+
data.tar.gz: e3932a16e088e34633d6d94ef0c1a829f9dd3a508b1a01f89f661cbc71803e044e8636297d423f9dc5b8ec2213160e847d252423cd8cb383a499b104f85b1503
|
data/Dockerfile
ADDED
data/Gemfile
ADDED
data/LICENCE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright (C) 2024 ArvanCloud Global Technologies L.L.C.
|
190
|
+
|
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 @@
|
|
1
|
+
# FluentD Plugin for Cloud Logs
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fluent/plugin/arvancloud"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
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-arvancloud'
|
6
|
+
spec.version = '0.0.1'
|
7
|
+
spec.authors = ['Arvancloud Cloud Logs team']
|
8
|
+
spec.email = ['cloudlogs@arvancloud.ir']
|
9
|
+
spec.summary = 'Arvancloud output plugin for Fluent collector'
|
10
|
+
spec.homepage = 'https://arvancloud.ir'
|
11
|
+
spec.license = 'Apache-2.0'
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7')
|
13
|
+
|
14
|
+
spec.files = Dir['**/*'].keep_if { |file| File.file?(file) }
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_runtime_dependency 'fluentd', ['>= 1', '< 2']
|
19
|
+
spec.add_runtime_dependency 'net-http-persistent', '~> 4.0.1'
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
22
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
23
|
+
spec.add_development_dependency 'yajl-ruby', '~> 1.2'
|
24
|
+
|
25
|
+
spec.metadata = {
|
26
|
+
'source_code_uri' => 'https://git.arvancloud.ir/arvancloud/fluent-plugin-arvancloud'
|
27
|
+
}
|
28
|
+
end
|
data/fluentd.conf
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
<source>
|
2
|
+
@type http
|
3
|
+
port 9880
|
4
|
+
bind 0.0.0.0
|
5
|
+
</source>
|
6
|
+
|
7
|
+
|
8
|
+
<match **>
|
9
|
+
@type arvancloud
|
10
|
+
api_key apikey b72d1b84-9ddc-569b-b3d3-862f4c0f7d1b
|
11
|
+
|
12
|
+
<buffer>
|
13
|
+
@type memory
|
14
|
+
flush_thread_count 4
|
15
|
+
flush_interval 1s
|
16
|
+
chunk_limit_size 5m
|
17
|
+
chunk_limit_records 500
|
18
|
+
</buffer>
|
19
|
+
|
20
|
+
</match>
|
@@ -0,0 +1,431 @@
|
|
1
|
+
# Copyright 2025 ArvanCloud Global Technologies L.L.C. All rights reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
require 'fluent/plugin/output'
|
15
|
+
require 'json'
|
16
|
+
|
17
|
+
module Fluent
|
18
|
+
# FluentD output plugin for Arvancloud Logs service.
|
19
|
+
class ArvancloudOutput < Output
|
20
|
+
class RetryableError < StandardError; end
|
21
|
+
|
22
|
+
# Max limits for transport regardless of Fluentd buffer.
|
23
|
+
MAX_ENTRIES_PER_BATCH = 1000
|
24
|
+
# Max limit for write entries request payload size in bytes.
|
25
|
+
MAX_BATCH_SIZE = 5_000_000
|
26
|
+
|
27
|
+
DEFAULT_ENDPOINT = 'napi.arvancloud.ir/logging'.freeze
|
28
|
+
|
29
|
+
helpers :compat_parameters
|
30
|
+
|
31
|
+
DEFAULT_BUFFER_TYPE = 'memory'.freeze
|
32
|
+
|
33
|
+
# Registering Arvancloud pluging in FluentD.
|
34
|
+
Fluent::Plugin.register_output('arvancloud', self)
|
35
|
+
|
36
|
+
# Connection settings.
|
37
|
+
config_param :host, :string, default: DEFAULT_ENDPOINT
|
38
|
+
config_param :max_retries, :integer, default: -1
|
39
|
+
config_param :max_backoff, :integer, default: 10
|
40
|
+
config_param :use_compression, :bool, default: false
|
41
|
+
config_param :compression_level, :integer, default: 5
|
42
|
+
|
43
|
+
# API Settings.
|
44
|
+
config_param :api_key, :string, secret: true
|
45
|
+
|
46
|
+
config_section :buffer do
|
47
|
+
config_set_default :@type, DEFAULT_BUFFER_TYPE
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialize
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
def configure(conf)
|
55
|
+
compat_parameters_convert(conf, :buffer)
|
56
|
+
super
|
57
|
+
end
|
58
|
+
|
59
|
+
def multi_workers_ready?
|
60
|
+
true
|
61
|
+
end
|
62
|
+
|
63
|
+
def formatted_to_msgpack_binary?
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
def start
|
68
|
+
super
|
69
|
+
@client = new_client(log, @api_key, @host, @use_compression)
|
70
|
+
end
|
71
|
+
|
72
|
+
def shutdown
|
73
|
+
super
|
74
|
+
end
|
75
|
+
|
76
|
+
def terminate
|
77
|
+
super
|
78
|
+
@client&.close
|
79
|
+
end
|
80
|
+
|
81
|
+
# This method is called when an event reaches Fluentd.
|
82
|
+
def format(tag, time, record)
|
83
|
+
Fluent::MessagePackFactory
|
84
|
+
.engine_factory
|
85
|
+
.packer
|
86
|
+
.write([tag, time, record])
|
87
|
+
.to_s
|
88
|
+
end
|
89
|
+
|
90
|
+
# NOTE! This method is called by internal thread, not Fluentd's main thread.
|
91
|
+
# 'chunk' is a buffer chunk that includes multiple formatted events.
|
92
|
+
def write(chunk)
|
93
|
+
begin
|
94
|
+
grouped_entries = group_log_entries_by_tag(chunk)
|
95
|
+
grouped_entries.each do |tag, arr|
|
96
|
+
entries = []
|
97
|
+
arr.each do |time, record|
|
98
|
+
entry = LogEntry.new
|
99
|
+
entry.log_type = tag[0]
|
100
|
+
entry.severity = compute_severity(record)
|
101
|
+
entry.timestamp = compute_timestamp(record, time)
|
102
|
+
entry.resource = { type: 'general' }
|
103
|
+
entry.payload = record
|
104
|
+
entries.push(entry)
|
105
|
+
end
|
106
|
+
@client.send_retries(entries, max_retries, max_backoff)
|
107
|
+
end
|
108
|
+
rescue Exception => e
|
109
|
+
log.error("Uncaught processing exception in Arvancloud forwarder #{e.message}")
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def group_log_entries_by_tag(chunk)
|
114
|
+
groups = {}
|
115
|
+
chunk.msgpack_each do |tag, time, record|
|
116
|
+
unless record.is_a?(Hash)
|
117
|
+
@log.warn 'Dropping log entries with malformed record: ' \
|
118
|
+
"'#{record.inspect}' from tag '#{tag}' at '#{time}'. " \
|
119
|
+
'A log record should be in JSON format.'
|
120
|
+
next
|
121
|
+
end
|
122
|
+
sanitized_tag = sanitize_tag(tag)
|
123
|
+
if sanitized_tag.nil?
|
124
|
+
@log.warn "Dropping log entries with invalid tag: '#{tag.inspect}'." \
|
125
|
+
' A tag should be a string with utf8 characters.'
|
126
|
+
next
|
127
|
+
end
|
128
|
+
# A nil local_resource_id means "fall back to legacy".
|
129
|
+
hash_key = [sanitized_tag].freeze
|
130
|
+
groups[hash_key] ||= []
|
131
|
+
groups[hash_key].push([time, record])
|
132
|
+
end
|
133
|
+
groups
|
134
|
+
end
|
135
|
+
|
136
|
+
def sanitize_tag(tag)
|
137
|
+
if @require_valid_tags &&
|
138
|
+
(!tag.is_a?(String) || tag == '' || convert_to_utf8(tag) != tag)
|
139
|
+
return nil
|
140
|
+
end
|
141
|
+
|
142
|
+
tag = convert_to_utf8(tag.to_s)
|
143
|
+
tag = '_' if tag == ''
|
144
|
+
tag
|
145
|
+
end
|
146
|
+
|
147
|
+
def convert_to_utf8(input)
|
148
|
+
if @coerce_to_utf8
|
149
|
+
input.encode(
|
150
|
+
'utf-8',
|
151
|
+
invalid: :replace,
|
152
|
+
undef: :replace,
|
153
|
+
replace: @non_utf8_replacement_string
|
154
|
+
)
|
155
|
+
else
|
156
|
+
begin
|
157
|
+
input.encode('utf-8')
|
158
|
+
rescue EncodingError
|
159
|
+
@log.error 'Encountered encoding issues potentially due to non ' \
|
160
|
+
'UTF-8 characters. To allow non-UTF-8 characters and ' \
|
161
|
+
'replace them with spaces, please set "coerce_to_utf8" ' \
|
162
|
+
'to true.'
|
163
|
+
raise
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def compute_timestamp(record, time)
|
169
|
+
if record.key?('timestamp') &&
|
170
|
+
record['timestamp'].is_a?(Hash) &&
|
171
|
+
record['timestamp'].key?('seconds') &&
|
172
|
+
record['timestamp'].key?('nanos')
|
173
|
+
ts_secs = record['timestamp']['seconds']
|
174
|
+
ts_nanos = record['timestamp']['nanos']
|
175
|
+
record.delete('timestamp')
|
176
|
+
timestamp = time_or_nil(ts_secs, ts_nanos)
|
177
|
+
elsif record.key?('timestampSeconds') &&
|
178
|
+
record.key?('timestampNanos')
|
179
|
+
ts_secs = record.delete('timestampSeconds')
|
180
|
+
ts_nanos = record.delete('timestampNanos')
|
181
|
+
timestamp = time_or_nil(ts_secs, ts_nanos)
|
182
|
+
elsif record.key?('timeNanos')
|
183
|
+
# This is deprecated since the precision is insufficient.
|
184
|
+
# Use timestampSeconds/timestampNanos instead
|
185
|
+
nanos = record.delete('timeNanos')
|
186
|
+
ts_secs = (nanos / 1_000_000_000).to_i
|
187
|
+
ts_nanos = nanos % 1_000_000_000
|
188
|
+
unless @timenanos_warning
|
189
|
+
# Warn the user this is deprecated, but only once to avoid spam.
|
190
|
+
@timenanos_warning = true
|
191
|
+
@log.warn 'timeNanos is deprecated - please use ' \
|
192
|
+
'timestampSeconds and timestampNanos instead.'
|
193
|
+
end
|
194
|
+
timestamp = time_or_nil(ts_secs, ts_nanos)
|
195
|
+
elsif record.key?('time')
|
196
|
+
# k8s ISO8601 timestamp
|
197
|
+
begin
|
198
|
+
timestamp = Time.iso8601(record.delete('time'))
|
199
|
+
rescue StandardError
|
200
|
+
timestamp = Time.at(time)
|
201
|
+
end
|
202
|
+
else
|
203
|
+
timestamp = Time.at(time)
|
204
|
+
end
|
205
|
+
timestamp.to_datetime.rfc3339(6)
|
206
|
+
end
|
207
|
+
|
208
|
+
def compute_severity(record)
|
209
|
+
if record.key?('severity')
|
210
|
+
return parse_severity(record.delete('severity'))
|
211
|
+
end
|
212
|
+
|
213
|
+
'INFO'
|
214
|
+
end
|
215
|
+
|
216
|
+
VALID_SEVERITIES = Set.new(
|
217
|
+
%w[DEBUG INFO NOTICE WARNING ERROR CRITICAL]
|
218
|
+
).freeze
|
219
|
+
|
220
|
+
# Translates other severity strings to one of the valid values above.
|
221
|
+
SEVERITY_TRANSLATIONS = {
|
222
|
+
# log4j levels (both current and obsolete).
|
223
|
+
'WARN' => 'WARNING',
|
224
|
+
'FATAL' => 'CRITICAL',
|
225
|
+
'TRACE' => 'DEBUG',
|
226
|
+
'TRACE_INT' => 'DEBUG',
|
227
|
+
'FINE' => 'DEBUG',
|
228
|
+
'FINER' => 'DEBUG',
|
229
|
+
'FINEST' => 'DEBUG',
|
230
|
+
# java.util.logging levels (only missing ones from above listed).
|
231
|
+
'SEVERE' => 'ERROR',
|
232
|
+
'CONFIG' => 'DEBUG',
|
233
|
+
# nginx levels (only missing ones from above listed).
|
234
|
+
'CRIT' => 'CRITICAL',
|
235
|
+
'EMERG' => 'CRITICAL',
|
236
|
+
# single-letter levels. Note E->ERROR and D->DEBUG.
|
237
|
+
'D' => 'DEBUG',
|
238
|
+
'I' => 'INFO',
|
239
|
+
'N' => 'NOTICE',
|
240
|
+
'W' => 'WARNING',
|
241
|
+
'E' => 'ERROR',
|
242
|
+
'C' => 'CRITICAL',
|
243
|
+
'A' => 'WARNING',
|
244
|
+
# other misc. translations.
|
245
|
+
'INFORMATION' => 'INFO',
|
246
|
+
'ERR' => 'ERROR',
|
247
|
+
'F' => 'CRITICAL'
|
248
|
+
}.freeze
|
249
|
+
|
250
|
+
def parse_severity(severity_str)
|
251
|
+
# The API is case insensitive, but uppercase to make things simpler.
|
252
|
+
severity = severity_str.to_s.upcase.strip
|
253
|
+
|
254
|
+
# If the severity is already valid, just return it.
|
255
|
+
return severity if VALID_SEVERITIES.include?(severity)
|
256
|
+
|
257
|
+
# If the severity is an integer (string) return it as an integer,
|
258
|
+
# truncated to the closest valid value (multiples of 100 between 0-800).
|
259
|
+
if /\A\d+\z/ =~ severity
|
260
|
+
begin
|
261
|
+
numeric_severity = (severity.to_i / 100) * 100
|
262
|
+
case
|
263
|
+
when numeric_severity.negative?
|
264
|
+
return 0
|
265
|
+
when numeric_severity > 800
|
266
|
+
return 800
|
267
|
+
else
|
268
|
+
return numeric_severity
|
269
|
+
end
|
270
|
+
rescue StandardError
|
271
|
+
return 'DEFAULT'
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
# Try to translate the severity.
|
276
|
+
return SEVERITY_TRANSLATIONS[severity] if SEVERITY_TRANSLATIONS.key?(severity)
|
277
|
+
|
278
|
+
# If all else fails, use 'DEFAULT'.
|
279
|
+
'DEFAULT'
|
280
|
+
end
|
281
|
+
|
282
|
+
# Group HTTP events in batches
|
283
|
+
def batch_http_events(encoded_events, max_entries_per_batch, max_request_size)
|
284
|
+
batches = []
|
285
|
+
current_batch = []
|
286
|
+
current_batch_size = 0
|
287
|
+
encoded_events.each_with_index do |encoded_event, i|
|
288
|
+
current_event_size = encoded_event.bytesize
|
289
|
+
# If this unique log size is bigger than the request size, truncate it
|
290
|
+
if current_event_size > max_request_size
|
291
|
+
encoded_event = truncate(encoded_event, max_request_size)
|
292
|
+
current_event_size = encoded_event.bytesize
|
293
|
+
end
|
294
|
+
|
295
|
+
if (i.positive? && (i % max_entries_per_batch).zero?) || (current_batch_size + current_event_size > max_request_size)
|
296
|
+
batches << current_batch
|
297
|
+
current_batch = []
|
298
|
+
current_batch_size = 0
|
299
|
+
end
|
300
|
+
|
301
|
+
current_batch_size += encoded_event.bytesize
|
302
|
+
current_batch << encoded_event
|
303
|
+
end
|
304
|
+
batches << current_batch
|
305
|
+
batches
|
306
|
+
end
|
307
|
+
|
308
|
+
# Compress logs with GZIP
|
309
|
+
def gzip_compress(payload, compression_level)
|
310
|
+
gz = StringIO.new
|
311
|
+
gz.set_encoding('BINARY')
|
312
|
+
z = Zlib::GzipWriter.new(gz, compression_level)
|
313
|
+
begin
|
314
|
+
z.write(payload)
|
315
|
+
ensure
|
316
|
+
z.close
|
317
|
+
end
|
318
|
+
gz.string
|
319
|
+
end
|
320
|
+
|
321
|
+
# Build a new CloudLogs client.
|
322
|
+
def new_client(logger, api_key, host, use_compression)
|
323
|
+
CloudLogsHTTPClient.new logger, host, use_compression, api_key
|
324
|
+
end
|
325
|
+
|
326
|
+
class LogEntry
|
327
|
+
attr_accessor :log_type, :timestamp, :severity, :resource, :payload
|
328
|
+
|
329
|
+
def as_json(_options = {})
|
330
|
+
{
|
331
|
+
logType: @log_type,
|
332
|
+
timestamp: @timestamp,
|
333
|
+
severity: @severity,
|
334
|
+
resource: @resource,
|
335
|
+
payload: @payload
|
336
|
+
}
|
337
|
+
end
|
338
|
+
|
339
|
+
def to_json(*options)
|
340
|
+
as_json(*options).to_json(*options)
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
class LogWriteRequest
|
345
|
+
attr_accessor :allowPartialProcess, :logs
|
346
|
+
|
347
|
+
def as_json(_options = {})
|
348
|
+
{
|
349
|
+
allowPartialProcess: @allowPartialProcess,
|
350
|
+
logs: @logs
|
351
|
+
}
|
352
|
+
end
|
353
|
+
|
354
|
+
def to_json(*options)
|
355
|
+
as_json(*options).to_json(*options)
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
# Top level class for Arvancloud Logs transport clients, managing retries and backoff.
|
360
|
+
class CloudLogsClient
|
361
|
+
def send_retries(entries, max_retries, max_backoff)
|
362
|
+
backoff = 1
|
363
|
+
retries = 0
|
364
|
+
begin
|
365
|
+
send(entries)
|
366
|
+
rescue RetryableError => e
|
367
|
+
if retries < max_retries || max_retries.negative?
|
368
|
+
@logger.warn('Retrying ', exception: e, backtrace: e.backtrace)
|
369
|
+
sleep backoff
|
370
|
+
backoff = 2 * backoff unless backoff > max_backoff
|
371
|
+
retries += 1
|
372
|
+
retry
|
373
|
+
end
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
def send(entries)
|
378
|
+
raise NotImplementedError, 'Cloud Logs transport client should implement the send method'
|
379
|
+
end
|
380
|
+
|
381
|
+
def close
|
382
|
+
raise NotImplementedError, 'Cloud Logs transport client should implement the close method'
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
# Cloud Logs HTTP client.
|
387
|
+
class CloudLogsHTTPClient < CloudLogsClient
|
388
|
+
require 'net/http'
|
389
|
+
require 'net/http/persistent'
|
390
|
+
|
391
|
+
def initialize(logger, host, use_compression, api_key)
|
392
|
+
@logger = logger
|
393
|
+
|
394
|
+
@uri = URI("https://#{host}/v1/entries/write")
|
395
|
+
|
396
|
+
logger.info("Starting HTTP connection to #{@uri} with compression " + (use_compression ? 'enabled' : 'disabled'))
|
397
|
+
@client = Net::HTTP::Persistent.new name: 'fluent-plugin-arvancloud'
|
398
|
+
|
399
|
+
@client.override_headers['Authorization'] = api_key
|
400
|
+
@client.override_headers['Content-Type'] = 'application/json'
|
401
|
+
if use_compression
|
402
|
+
@client.override_headers['Content-Encoding'] = 'gzip'
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
def send(entries)
|
407
|
+
request = Net::HTTP::Post.new @uri.request_uri
|
408
|
+
write_request = LogWriteRequest.new
|
409
|
+
write_request.allowPartialProcess = false
|
410
|
+
write_request.logs = entries
|
411
|
+
puts write_request.to_json
|
412
|
+
|
413
|
+
request.body = JSON(write_request)
|
414
|
+
response = @client.request @uri, request
|
415
|
+
res_code = response.code.to_i
|
416
|
+
# on a backend error or on an http 429, retry with backoff
|
417
|
+
if res_code >= 500 || res_code == 429
|
418
|
+
raise RetryableError.new "Unable to send payload: #{res_code} #{response.message}"
|
419
|
+
end
|
420
|
+
|
421
|
+
if res_code >= 400
|
422
|
+
@logger.error("Unable to send payload due to client error: #{res_code} #{response.message}")
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
def close
|
427
|
+
@client.shutdown
|
428
|
+
end
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-arvancloud
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arvancloud Cloud Logs team
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-03-06 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: fluentd
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1'
|
19
|
+
- - "<"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1'
|
29
|
+
- - "<"
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '2'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: net-http-persistent
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - "~>"
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 4.0.1
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - "~>"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 4.0.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '2.1'
|
53
|
+
type: :development
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '2.1'
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rake
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '12.0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '12.0'
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: yajl-ruby
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '1.2'
|
81
|
+
type: :development
|
82
|
+
prerelease: false
|
83
|
+
version_requirements: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '1.2'
|
88
|
+
email:
|
89
|
+
- cloudlogs@arvancloud.ir
|
90
|
+
executables:
|
91
|
+
- console
|
92
|
+
- setup
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- Dockerfile
|
97
|
+
- Gemfile
|
98
|
+
- LICENCE
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- bin/console
|
102
|
+
- bin/setup
|
103
|
+
- fluent-plugin-arvancloud.gemspec
|
104
|
+
- fluentd.conf
|
105
|
+
- lib/fluent/plugin/out_arvancloud.rb
|
106
|
+
homepage: https://arvancloud.ir
|
107
|
+
licenses:
|
108
|
+
- Apache-2.0
|
109
|
+
metadata:
|
110
|
+
source_code_uri: https://git.arvancloud.ir/arvancloud/fluent-plugin-arvancloud
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '2.7'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubygems_version: 3.6.3
|
126
|
+
specification_version: 4
|
127
|
+
summary: Arvancloud output plugin for Fluent collector
|
128
|
+
test_files: []
|