Skip to content

Commit 28d726e

Browse files
Merge pull request #2175 from vimalk78/log-1494-fix-syslog-output
LOG-1494: Added Formatter single_json_value
2 parents e7f2386 + 38d12aa commit 28d726e

10 files changed

+208
-0
lines changed

fluentd/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ COPY ${upstream_code}/lib/filter_parse_json_field/lib/*.rb /etc/fluent/plugin/
8383
COPY ${upstream_code}/lib/filter_elasticsearch_genid_ext/lib/filter_elasticsearch_genid_ext.rb /etc/fluent/plugin/
8484
COPY ${upstream_code}/lib/parser_viaq_host_audit/lib/*.rb /etc/fluent/plugin/
8585
COPY ${upstream_code}/lib/fluent-plugin-viaq_data_model/lib/fluent/plugin/*.rb /etc/fluent/plugin/
86+
COPY ${upstream_code}/lib/formatter-single-json-value/lib/*.rb /etc/fluent/plugin/
8687
COPY ${upstream_code}/utils/ /usr/local/bin/
8788

8889
RUN mkdir -p /etc/fluent/configs.d/user && \

fluentd/Dockerfile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ COPY --from=builder ${upstream_code}/lib/filter_parse_json_field/lib/*.rb /etc/f
9999
COPY --from=builder ${upstream_code}/lib/filter_elasticsearch_genid_ext/lib/filter_elasticsearch_genid_ext.rb /etc/fluent/plugin/
100100
COPY --from=builder ${upstream_code}/lib/parser_viaq_host_audit/lib/*.rb /etc/fluent/plugin/
101101
COPY --from=builder ${upstream_code}/lib/fluent-plugin-viaq_data_model/lib/fluent/plugin/*.rb /etc/fluent/plugin/
102+
COPY --from=builder ${upstream_code}/lib/formatter-single-json-value/lib/*.rb /etc/fluent/plugin/
102103
COPY --from=builder ${upstream_code}/utils/ /usr/local/bin/
103104

104105
RUN mkdir -p /etc/fluent/configs.d/user && \
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "https://rubygems.org"
2+
3+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4+
5+
# Specify your gem's dependencies in formatter-single-json-value.gemspec
6+
gemspec
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Vimal Kumar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Formatter::Single::Json::Value
2+
3+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/formatter/single/json/value`. To experiment with that code, run `bin/console` for an interactive prompt.
4+
5+
TODO: Delete this and the text above, and describe your gem
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
gem 'formatter-single-json-value'
13+
```
14+
15+
And then execute:
16+
17+
$ bundle
18+
19+
Or install it yourself as:
20+
21+
$ gem install formatter-single-json-value
22+
23+
## Usage
24+
25+
TODO: Write usage instructions here
26+
27+
## Development
28+
29+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30+
31+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `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).
32+
33+
## Contributing
34+
35+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/formatter-single-json-value.
36+
37+
## License
38+
39+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#require "bundler/gem_tasks"
2+
require "rake/testtask"
3+
4+
Rake::TestTask.new do |t|
5+
t.test_files = FileList['test/**/*_test.rb']
6+
t.warning = false
7+
#t.verbose = true
8+
end
9+
desc "Run tests"
10+
11+
task default: :test
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
lib = File.expand_path("../lib", __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
5+
6+
# can override for testing
7+
FLUENTD_VERSION = ENV['FLUENTD_VERSION'] || "0.12.0"
8+
9+
Gem::Specification.new do |gem|
10+
gem.name = "formatter-single-json-value"
11+
gem.version = "0.0.1"
12+
gem.authors = ["Vimal Kumar"]
13+
gem.email = ["[email protected]"]
14+
15+
gem.summary = %q{Formatter to extract a single value as string of json string}
16+
gem.description = %q{Formatter to extract a single value as string of json string}
17+
gem.license = "MIT"
18+
19+
gem.required_ruby_version = '>= 2.0.0'
20+
21+
gem.add_runtime_dependency "fluentd", "~> #{FLUENTD_VERSION}"
22+
23+
gem.add_development_dependency "bundler"
24+
gem.add_development_dependency("fluentd", "~> #{FLUENTD_VERSION}")
25+
gem.add_development_dependency("rake", ["~> 11.0"])
26+
gem.add_development_dependency("rr", ["~> 1.0"])
27+
gem.add_development_dependency("test-unit", ["~> 3.2"])
28+
gem.add_development_dependency("test-unit-rr", ["~> 1.0"])
29+
30+
31+
end
32+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Fluentd
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+
17+
require 'fluent/plugin/formatter'
18+
require 'json'
19+
20+
module Fluent
21+
module Plugin
22+
class SingleJsonValueFormatter < Formatter
23+
Plugin.register_formatter('single_json_value', self)
24+
25+
config_param :message_key, :string, default: 'message'
26+
config_param :add_newline, :bool, default: true
27+
28+
def format(tag, time, record)
29+
m = record[@message_key]
30+
if m.is_a?(Hash)
31+
text = m.to_json.dup
32+
else
33+
text = m.to_s.dup
34+
end
35+
text << "\n" if @add_newline
36+
text
37+
end
38+
end
39+
end
40+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require_relative 'test_helper'
2+
require 'fluent/test/driver/formatter'
3+
require File.join(File.dirname(__FILE__), '..', 'lib/formatter_single_json_value')
4+
5+
class SingleValueFormatterTest < ::Test::Unit::TestCase
6+
def create_driver(conf = "")
7+
Fluent::Test::Driver::Formatter.new(Fluent::Plugin::SingleJsonValueFormatter).configure(conf)
8+
end
9+
10+
def test_config_params
11+
d = create_driver
12+
assert_equal "message", d.instance.message_key
13+
end
14+
15+
def test_config_params_message_key
16+
d = create_driver('message_key' => 'foobar')
17+
assert_equal "foobar", d.instance.message_key
18+
end
19+
20+
def test_format
21+
d = create_driver
22+
formatted = d.instance.format('tag', event_time, {'message' => 'awesome'})
23+
assert_equal("awesome\n", formatted)
24+
end
25+
26+
def test_format_json
27+
d = create_driver
28+
formatted = d.instance.format('tag', event_time, {'message' => {'key' => 'value'}})
29+
assert_equal("{\"key\":\"value\"}\n", formatted)
30+
end
31+
32+
def test_format_nested_json
33+
d = create_driver
34+
formatted = d.instance.format('tag', event_time, {'message' => {'nestedkey' => {'key' => 'value'}}})
35+
assert_equal("{\"nestedkey\":{\"key\":\"value\"}}\n", formatted)
36+
end
37+
38+
def test_format_without_newline
39+
d = create_driver('add_newline' => 'false')
40+
formatted = d.instance.format('tag', event_time, {'message' => 'awesome'})
41+
assert_equal("awesome", formatted)
42+
end
43+
44+
def test_format_with_message_key
45+
d = create_driver('message_key' => 'foobar')
46+
formatted = d.instance.format('tag', event_time, {'foobar' => 'foo'})
47+
48+
assert_equal("foo\n", formatted)
49+
end
50+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'serverengine' # or test will throw error missing method windows?
2+
require "fluent/test"
3+
require 'test/unit/rr'
4+
require "fluent/test/helpers"
5+
require "fluent/test/driver/filter"
6+
7+
Test::Unit::TestCase.include(Fluent::Test::Helpers)

0 commit comments

Comments
 (0)