beholder-fluentd-plugin 1.0.0
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/LICENSE +202 -0
- data/README.md +89 -0
- data/bin/console +14 -0
- data/bin/setup +9 -0
- data/bin/test +5 -0
- data/lib/fluent/plugin/out_loki.rb +368 -0
- metadata +153 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c03a4c467ac2112d3ac1af80b66bf88b3f0cf27fec9c5bc8f919f04b3ccf3bdc
|
|
4
|
+
data.tar.gz: 2d9c3b5aaf2d762215f74b5cdc7ff46113b5226449ac4f5b9283169ec6c735e8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5ff735035ba9b1d5ec466c457827e04a7f40ffebf34492491cb23ac751fa0bb7b52abbaed13158134f47b91afb6e32ff0272882235c55239f2396b095f7444b4
|
|
7
|
+
data.tar.gz: dc1209a43605b50a4d5b15fc995256097644a8ad0eaabc081500c15218c8e8be0a583750682e7e6315f90567ae1ef0eeb1bb938a7b3d79b74373e309915469b5
|
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,89 @@
|
|
|
1
|
+
# Fluentd output plugin
|
|
2
|
+
|
|
3
|
+
[Fluentd](https://fluentd.org/) is a data collector for unified logging layer, it can be configured with the Loki output plugin, provided in this folder, to ship logs to Loki.
|
|
4
|
+
|
|
5
|
+
See [docs/client/fluentd/README.md](../../docs/sources/clients/fluentd/_index.md) for detailed information.
|
|
6
|
+
|
|
7
|
+
## Development
|
|
8
|
+
|
|
9
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
10
|
+
|
|
11
|
+
To install this gem onto your local machine, run `ruby -S bundle exec rake install`. To release a new version, update the version number in `fluent-plugin-grafana-loki.gemspec`, and then run `ruby -S bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
12
|
+
|
|
13
|
+
To create the gem: `ruby -S gem build fluent-plugin-grafana-loki.gemspec`
|
|
14
|
+
|
|
15
|
+
Useful additions:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
ruby -S gem install rubocop
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Testing
|
|
22
|
+
|
|
23
|
+
Start Loki using:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
docker run -it -p 3100:3100 grafana/loki:latest
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Verify that Loki accept and stores logs:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
curl -H "Content-Type: application/json" -XPOST -s "http://localhost:3100/loki/api/v1/push" --data-raw "{\"streams\": [{\"stream\": {\"job\": \"test\"}, \"values\": [[\"$(date +%s)000000000\", \"fizzbuzz\"]]}]}"
|
|
33
|
+
curl "http://localhost:3100/loki/api/v1/query_range" --data-urlencode 'query={job="test"}' --data-urlencode 'step=300' | jq .data.result
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The expected output is:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
[
|
|
40
|
+
{
|
|
41
|
+
"stream": {
|
|
42
|
+
"job": "test"
|
|
43
|
+
},
|
|
44
|
+
"values": [
|
|
45
|
+
[
|
|
46
|
+
"1588337198000000000",
|
|
47
|
+
"fizzbuzz"
|
|
48
|
+
]
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Start and send test logs with Fluentd using:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
LOKI_URL=http://{{ IP }}:3100 make fluentd-test
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Verify that syslogs are being feeded into Loki:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
curl "http://localhost:3100/loki/api/v1/query_range" --data-urlencode 'query={job="fluentd"}' --data-urlencode 'step=300' | jq .data.result
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The expected output is:
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
[
|
|
70
|
+
{
|
|
71
|
+
"stream": {
|
|
72
|
+
"job": "fluentd"
|
|
73
|
+
},
|
|
74
|
+
"values": [
|
|
75
|
+
[
|
|
76
|
+
"1588336950379591919",
|
|
77
|
+
"log=\"May 1 14:42:30 ibuprofen avahi-daemon[859]: New relevant interface vethb503225.IPv6 for mDNS.\""
|
|
78
|
+
],
|
|
79
|
+
...
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Copyright
|
|
86
|
+
|
|
87
|
+
* Copyright(c) 2018- Grafana Labs
|
|
88
|
+
* License
|
|
89
|
+
* Apache License, Version 2.0
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'gems/fluent/plugin/out_loki'
|
|
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
data/bin/test
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Copyright 2018- Grafana Labs
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# You may obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
# See the License for the specific language governing permissions and
|
|
16
|
+
# limitations under the License.
|
|
17
|
+
|
|
18
|
+
require 'fluent/env'
|
|
19
|
+
require 'fluent/plugin/output'
|
|
20
|
+
require 'net/http'
|
|
21
|
+
require 'yajl'
|
|
22
|
+
require 'time'
|
|
23
|
+
|
|
24
|
+
module Fluent
|
|
25
|
+
module Plugin
|
|
26
|
+
# Subclass of Fluent Plugin Output
|
|
27
|
+
class LokiOutput < Fluent::Plugin::Output # rubocop:disable Metrics/ClassLength
|
|
28
|
+
Fluent::Plugin.register_output('loki', self)
|
|
29
|
+
|
|
30
|
+
class LogPostError < StandardError; end
|
|
31
|
+
|
|
32
|
+
helpers :compat_parameters, :record_accessor
|
|
33
|
+
|
|
34
|
+
attr_accessor :record_accessors
|
|
35
|
+
|
|
36
|
+
DEFAULT_BUFFER_TYPE = 'memory'
|
|
37
|
+
|
|
38
|
+
desc 'Loki API base URL'
|
|
39
|
+
config_param :url, :string, default: 'https://logs-prod-us-central1.grafana.net'
|
|
40
|
+
|
|
41
|
+
desc 'Authentication: basic auth credentials'
|
|
42
|
+
config_param :username, :string, default: nil
|
|
43
|
+
config_param :password, :string, default: nil, secret: true
|
|
44
|
+
|
|
45
|
+
desc 'Authentication: Authorization header with Bearer token scheme'
|
|
46
|
+
config_param :bearer_token_file, :string, default: nil
|
|
47
|
+
|
|
48
|
+
desc 'TLS: parameters for presenting a client certificate'
|
|
49
|
+
config_param :cert, :string, default: nil
|
|
50
|
+
config_param :key, :string, default: nil
|
|
51
|
+
|
|
52
|
+
desc 'TLS: CA certificate file for server certificate verification'
|
|
53
|
+
config_param :ca_cert, :string, default: nil
|
|
54
|
+
|
|
55
|
+
desc 'TLS: disable server certificate verification'
|
|
56
|
+
config_param :insecure_tls, :bool, default: false
|
|
57
|
+
|
|
58
|
+
desc 'Loki tenant id'
|
|
59
|
+
config_param :tenant, :string, default: nil
|
|
60
|
+
|
|
61
|
+
desc 'extra labels to add to all log streams'
|
|
62
|
+
config_param :extra_labels, :hash, default: {}
|
|
63
|
+
|
|
64
|
+
desc 'format to use when flattening the record to a log line'
|
|
65
|
+
config_param :line_format, :enum, list: %i[json key_value], default: :key_value
|
|
66
|
+
|
|
67
|
+
desc 'extract kubernetes labels as loki labels'
|
|
68
|
+
config_param :extract_kubernetes_labels, :bool, default: false
|
|
69
|
+
|
|
70
|
+
desc 'comma separated list of needless record keys to remove'
|
|
71
|
+
config_param :remove_keys, :array, default: %w[], value_type: :string
|
|
72
|
+
|
|
73
|
+
desc 'if a record only has 1 key, then just set the log line to the value and discard the key.'
|
|
74
|
+
config_param :drop_single_key, :bool, default: false
|
|
75
|
+
|
|
76
|
+
desc 'whether or not to include the fluentd_thread label when multiple threads are used for flushing'
|
|
77
|
+
config_param :include_thread_label, :bool, default: true
|
|
78
|
+
|
|
79
|
+
config_section :buffer do
|
|
80
|
+
config_set_default :@type, DEFAULT_BUFFER_TYPE
|
|
81
|
+
config_set_default :chunk_keys, []
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
85
|
+
def configure(conf)
|
|
86
|
+
compat_parameters_convert(conf, :buffer)
|
|
87
|
+
super
|
|
88
|
+
@uri = URI.parse("#{@url}/loki/api/v1/push")
|
|
89
|
+
unless @uri.is_a?(URI::HTTP) || @uri.is_a?(URI::HTTPS)
|
|
90
|
+
raise Fluent::ConfigError, 'URL parameter must have HTTP/HTTPS scheme'
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
@record_accessors = {}
|
|
94
|
+
conf.elements.select { |element| element.name == 'label' }.each do |element|
|
|
95
|
+
element.each_pair do |k, v|
|
|
96
|
+
element.has_key?(k) # rubocop:disable Style/PreferredHashMethods #to suppress unread configuration warning
|
|
97
|
+
v = k if v.empty?
|
|
98
|
+
@record_accessors[k] = record_accessor_create(v)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
@remove_keys_accessors = []
|
|
102
|
+
@remove_keys.each do |key|
|
|
103
|
+
@remove_keys_accessors.push(record_accessor_create(key))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# If configured, load and validate client certificate (and corresponding key)
|
|
107
|
+
if client_cert_configured?
|
|
108
|
+
load_client_cert
|
|
109
|
+
validate_client_cert_key
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
if !@bearer_token_file.nil? && !File.exist?(@bearer_token_file)
|
|
113
|
+
raise "bearer_token_file #{@bearer_token_file} not found"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
@auth_token_bearer = nil
|
|
117
|
+
unless @bearer_token_file.nil?
|
|
118
|
+
raise "bearer_token_file #{@bearer_token_file} not found" unless File.exist?(@bearer_token_file)
|
|
119
|
+
|
|
120
|
+
# Read the file once, assume long-lived authentication token.
|
|
121
|
+
@auth_token_bearer = File.read(@bearer_token_file)
|
|
122
|
+
raise "bearer_token_file #{@bearer_token_file} is empty" if @auth_token_bearer.empty?
|
|
123
|
+
|
|
124
|
+
log.info "will use Bearer token from bearer_token_file #{@bearer_token_file} in Authorization header"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
raise "CA certificate file #{@ca_cert} not found" if !@ca_cert.nil? && !File.exist?(@ca_cert)
|
|
128
|
+
end
|
|
129
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
130
|
+
|
|
131
|
+
def client_cert_configured?
|
|
132
|
+
!@key.nil? && !@cert.nil?
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def load_client_cert
|
|
136
|
+
@cert = OpenSSL::X509::Certificate.new(File.read(@cert)) if @cert
|
|
137
|
+
@key = OpenSSL::PKey.read(File.read(@key)) if @key
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def validate_client_cert_key
|
|
141
|
+
if !@key.is_a?(OpenSSL::PKey::RSA) && !@key.is_a?(OpenSSL::PKey::DSA)
|
|
142
|
+
raise "Unsupported private key type #{key.class}"
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def multi_workers_ready?
|
|
147
|
+
true
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# flush a chunk to loki
|
|
151
|
+
def write(chunk)
|
|
152
|
+
# streams by label
|
|
153
|
+
payload = generic_to_loki(chunk)
|
|
154
|
+
body = { 'streams' => payload }
|
|
155
|
+
|
|
156
|
+
tenant = extract_placeholders(@tenant, chunk) if @tenant
|
|
157
|
+
|
|
158
|
+
# add ingest path to loki url
|
|
159
|
+
res = loki_http_request(body, tenant)
|
|
160
|
+
|
|
161
|
+
if res.is_a?(Net::HTTPSuccess)
|
|
162
|
+
log.debug "POST request was responded to with status code #{res.code}"
|
|
163
|
+
return
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
res_summary = "#{res.code} #{res.message} #{res.body}"
|
|
167
|
+
log.warn "failed to write post to #{@uri} (#{res_summary})"
|
|
168
|
+
log.debug Yajl.dump(body)
|
|
169
|
+
|
|
170
|
+
# Only retry 429 and 500s
|
|
171
|
+
raise(LogPostError, res_summary) if res.is_a?(Net::HTTPTooManyRequests) || res.is_a?(Net::HTTPServerError)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def http_request_opts(uri)
|
|
175
|
+
opts = {
|
|
176
|
+
use_ssl: uri.scheme == 'https'
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
# Optionally disable server server certificate verification.
|
|
180
|
+
if @insecure_tls
|
|
181
|
+
opts = opts.merge(
|
|
182
|
+
verify_mode: OpenSSL::SSL::VERIFY_NONE
|
|
183
|
+
)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Optionally present client certificate
|
|
187
|
+
if !@cert.nil? && !@key.nil?
|
|
188
|
+
opts = opts.merge(
|
|
189
|
+
cert: @cert,
|
|
190
|
+
key: @key
|
|
191
|
+
)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# For server certificate verification: set custom CA bundle.
|
|
195
|
+
# Only takes effect when `insecure_tls` is not set.
|
|
196
|
+
unless @ca_cert.nil?
|
|
197
|
+
opts = opts.merge(
|
|
198
|
+
ca_file: @ca_cert
|
|
199
|
+
)
|
|
200
|
+
end
|
|
201
|
+
opts
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def generic_to_loki(chunk)
|
|
205
|
+
# log.debug("GenericToLoki: converting #{chunk}")
|
|
206
|
+
streams = chunk_to_loki(chunk)
|
|
207
|
+
payload_builder(streams)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
private
|
|
211
|
+
|
|
212
|
+
def loki_http_request(body, tenant)
|
|
213
|
+
req = Net::HTTP::Post.new(
|
|
214
|
+
@uri.request_uri
|
|
215
|
+
)
|
|
216
|
+
req.add_field('Content-Type', 'application/json')
|
|
217
|
+
req.add_field('Authorization', "Bearer #{@auth_token_bearer}") unless @auth_token_bearer.nil?
|
|
218
|
+
req.add_field('X-Scope-OrgID', tenant) if tenant
|
|
219
|
+
req.body = Yajl.dump(body)
|
|
220
|
+
req.basic_auth(@username, @password) if @username
|
|
221
|
+
|
|
222
|
+
opts = http_request_opts(@uri)
|
|
223
|
+
|
|
224
|
+
msg = "sending #{req.body.length} bytes to loki"
|
|
225
|
+
msg += " (tenant: \"#{tenant}\")" if tenant
|
|
226
|
+
log.debug msg
|
|
227
|
+
|
|
228
|
+
Net::HTTP.start(@uri.host, @uri.port, **opts) { |http| http.request(req) }
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def numeric?(val)
|
|
232
|
+
!Float(val).nil?
|
|
233
|
+
rescue StandardError
|
|
234
|
+
false
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def format_labels(data_labels)
|
|
238
|
+
formatted_labels = {}
|
|
239
|
+
# merge extra_labels with data_labels. If there are collisions extra_labels win.
|
|
240
|
+
data_labels = {} if data_labels.nil?
|
|
241
|
+
data_labels = data_labels.merge(@extra_labels)
|
|
242
|
+
# sanitize label values
|
|
243
|
+
data_labels.each { |k, v| formatted_labels[k] = v.gsub('"', '\\"') if v.is_a?(String) }
|
|
244
|
+
formatted_labels
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def payload_builder(streams)
|
|
248
|
+
payload = []
|
|
249
|
+
streams.each do |k, v|
|
|
250
|
+
# create a stream for each label set.
|
|
251
|
+
# Additionally sort the entries by timestamp just in case we
|
|
252
|
+
# got them out of order.
|
|
253
|
+
entries = v.sort_by.with_index { |hsh, i| [hsh['ts'], i] }
|
|
254
|
+
payload.push(
|
|
255
|
+
'stream' => format_labels(k),
|
|
256
|
+
'values' => entries.map { |e| [e['ts'].to_s, e['line']] }
|
|
257
|
+
)
|
|
258
|
+
end
|
|
259
|
+
payload
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def to_nano(time)
|
|
263
|
+
# time is a Fluent::EventTime object, or an Integer which represents unix timestamp (seconds from Epoch)
|
|
264
|
+
# https://docs.fluentd.org/plugin-development/api-plugin-output#chunk-each-and-block
|
|
265
|
+
if time.is_a?(Fluent::EventTime)
|
|
266
|
+
time.to_i * (10**9) + time.nsec
|
|
267
|
+
else
|
|
268
|
+
time.to_i * (10**9)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
273
|
+
def record_to_line(record)
|
|
274
|
+
line = ''
|
|
275
|
+
if @drop_single_key && record.keys.length == 1
|
|
276
|
+
line = record[record.keys[0]]
|
|
277
|
+
else
|
|
278
|
+
case @line_format
|
|
279
|
+
when :json
|
|
280
|
+
line = Yajl.dump(record)
|
|
281
|
+
when :key_value
|
|
282
|
+
formatted_labels = []
|
|
283
|
+
record.each do |k, v|
|
|
284
|
+
# Remove non UTF-8 characters by force-encoding the string
|
|
285
|
+
v = v.encode('utf-8', invalid: :replace, undef: :replace, replace: '?') if v.is_a?(String)
|
|
286
|
+
# Escape double quotes and backslashes by prefixing them with a backslash
|
|
287
|
+
v = v.to_s.gsub(/(["\\])/, '\\\\\1')
|
|
288
|
+
if v.include?(' ') || v.include?('=')
|
|
289
|
+
formatted_labels.push(%(#{k}="#{v}"))
|
|
290
|
+
else
|
|
291
|
+
formatted_labels.push(%(#{k}=#{v}))
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
line = formatted_labels.join(' ')
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
line
|
|
298
|
+
end
|
|
299
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
300
|
+
|
|
301
|
+
# convert a line to loki line with labels
|
|
302
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
303
|
+
def line_to_loki(record)
|
|
304
|
+
chunk_labels = {}
|
|
305
|
+
line = ''
|
|
306
|
+
if record.is_a?(Hash)
|
|
307
|
+
@record_accessors&.each do |name, accessor|
|
|
308
|
+
new_key = name.gsub(%r{[.\-/]}, '_')
|
|
309
|
+
chunk_labels[new_key] = accessor.call(record)
|
|
310
|
+
accessor.delete(record)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
if @extract_kubernetes_labels && record.key?('kubernetes')
|
|
314
|
+
kubernetes_labels = record['kubernetes']['labels']
|
|
315
|
+
kubernetes_labels&.each_key do |l|
|
|
316
|
+
new_key = l.gsub(%r{[.\-/]}, '_')
|
|
317
|
+
chunk_labels[new_key] = kubernetes_labels[l]
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# remove needless keys.
|
|
322
|
+
@remove_keys_accessors&.each do |deleter|
|
|
323
|
+
deleter.delete(record)
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
line = record_to_line(record)
|
|
327
|
+
else
|
|
328
|
+
line = record.to_s
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# add buffer flush thread title as a label if there are multiple flush threads
|
|
332
|
+
# this prevents "entry out of order" errors in loki by making the label constellation
|
|
333
|
+
# unique per flush thread
|
|
334
|
+
# note that flush thread != fluentd worker. if you use multiple workers you still need to
|
|
335
|
+
# add the worker id as a label
|
|
336
|
+
if @include_thread_label && @buffer_config.flush_thread_count > 1
|
|
337
|
+
chunk_labels['fluentd_thread'] = Thread.current[:_fluentd_plugin_helper_thread_title].to_s
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# return both the line content plus the labels found in the record
|
|
341
|
+
{
|
|
342
|
+
line: line,
|
|
343
|
+
labels: chunk_labels
|
|
344
|
+
}
|
|
345
|
+
end
|
|
346
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
347
|
+
|
|
348
|
+
# iterate through each chunk and create a loki stream entry
|
|
349
|
+
def chunk_to_loki(chunk)
|
|
350
|
+
streams = {}
|
|
351
|
+
chunk.each do |time, record|
|
|
352
|
+
# each chunk has a unique set of labels
|
|
353
|
+
result = line_to_loki(record)
|
|
354
|
+
chunk_labels = result[:labels]
|
|
355
|
+
# initialize a new stream with the chunk_labels if it does not exist
|
|
356
|
+
streams[chunk_labels] = [] if streams[chunk_labels].nil?
|
|
357
|
+
# NOTE: timestamp must include nanoseconds
|
|
358
|
+
# append to matching chunk_labels key
|
|
359
|
+
streams[chunk_labels].push(
|
|
360
|
+
'ts' => to_nano(time),
|
|
361
|
+
'line' => result[:line]
|
|
362
|
+
)
|
|
363
|
+
end
|
|
364
|
+
streams
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: beholder-fluentd-plugin
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- cglrn
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-09-16 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: 1.9.3
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '2'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 1.9.3
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '2'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: bundler
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
type: :development
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: rake
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '12.0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '12.0'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: rspec
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3.0'
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '3.0'
|
|
75
|
+
- !ruby/object:Gem::Dependency
|
|
76
|
+
name: rubocop-rspec
|
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
type: :development
|
|
83
|
+
prerelease: false
|
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
- !ruby/object:Gem::Dependency
|
|
90
|
+
name: simplecov
|
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
type: :development
|
|
97
|
+
prerelease: false
|
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
- !ruby/object:Gem::Dependency
|
|
104
|
+
name: test-unit
|
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
type: :development
|
|
111
|
+
prerelease: false
|
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: '0'
|
|
117
|
+
description: Output plugin to ship logs to a Grafana Loki server
|
|
118
|
+
email:
|
|
119
|
+
- caglareren@yahoo.com
|
|
120
|
+
executables: []
|
|
121
|
+
extensions: []
|
|
122
|
+
extra_rdoc_files: []
|
|
123
|
+
files:
|
|
124
|
+
- LICENSE
|
|
125
|
+
- README.md
|
|
126
|
+
- bin/console
|
|
127
|
+
- bin/setup
|
|
128
|
+
- bin/test
|
|
129
|
+
- lib/fluent/plugin/out_loki.rb
|
|
130
|
+
homepage: https://github.com/grafana/loki/
|
|
131
|
+
licenses:
|
|
132
|
+
- Apache-2.0
|
|
133
|
+
metadata: {}
|
|
134
|
+
post_install_message:
|
|
135
|
+
rdoc_options: []
|
|
136
|
+
require_paths:
|
|
137
|
+
- lib
|
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
139
|
+
requirements:
|
|
140
|
+
- - "~>"
|
|
141
|
+
- !ruby/object:Gem::Version
|
|
142
|
+
version: '2.7'
|
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
|
+
requirements:
|
|
145
|
+
- - ">="
|
|
146
|
+
- !ruby/object:Gem::Version
|
|
147
|
+
version: '0'
|
|
148
|
+
requirements: []
|
|
149
|
+
rubygems_version: 3.0.3
|
|
150
|
+
signing_key:
|
|
151
|
+
specification_version: 4
|
|
152
|
+
summary: Output plugin to ship logs to a Grafana Loki server
|
|
153
|
+
test_files: []
|