mirror of
https://github.com/meineerde/rackstash.git
synced 2026-02-01 01:37:12 +00:00
Add Hash encoder which normalizes the raw event hash to a JSON-compatible Hash
This commit is contained in:
parent
b6d15a8da7
commit
2e7083218c
@ -6,7 +6,7 @@
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'rackstash/adapters/adapter'
|
||||
require 'rackstash/encoders/raw'
|
||||
require 'rackstash/encoders/hash'
|
||||
|
||||
module Rackstash
|
||||
module Adapters
|
||||
@ -46,15 +46,15 @@ module Rackstash
|
||||
end
|
||||
end
|
||||
|
||||
# By default, we use an {Rackstash::Encoders::Raw} to encode the events.
|
||||
# This ensures that the raw event is passed through to to the callable by
|
||||
# default.
|
||||
# By default, we use an {Rackstash::Encoders::Hash} to encode the events.
|
||||
# This ensures that all of the data in the logged event is passed through
|
||||
# to the callable by default.
|
||||
#
|
||||
# You can define a custom encoder in the responsible {Flow}.
|
||||
#
|
||||
# @return [Rackstash::Encoders::Raw] a new Raw encoder
|
||||
# @return [Rackstash::Encoders::Hash] a new Hash encoder
|
||||
def default_encoder
|
||||
Rackstash::Encoders::Raw.new
|
||||
Rackstash::Encoders::Hash.new
|
||||
end
|
||||
|
||||
# Write a single log line by calling the defined `callable` given in
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
# This software may be modified and distributed under the terms
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'rackstash/encoders/hash'
|
||||
require 'rackstash/encoders/json'
|
||||
require 'rackstash/encoders/lograge'
|
||||
require 'rackstash/encoders/logstash'
|
||||
|
||||
30
lib/rackstash/encoders/hash.rb
Normal file
30
lib/rackstash/encoders/hash.rb
Normal file
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
#
|
||||
# Copyright 2017 Holger Just
|
||||
#
|
||||
# This software may be modified and distributed under the terms
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'rackstash/encoders/helpers/message'
|
||||
require 'rackstash/encoders/helpers/timestamp'
|
||||
|
||||
module Rackstash
|
||||
module Encoders
|
||||
# The Hash encoder formats the log event as a raw `Hash` containing all data
|
||||
# exposed by the buffer. This can be used by special log targets which are
|
||||
# designed to handle hashes as opposed to formatted strings.
|
||||
class Hash
|
||||
include Rackstash::Encoders::Helpers::Message
|
||||
include Rackstash::Encoders::Helpers::Timestamp
|
||||
|
||||
# @param event [Hash] a log event as produced by the {Flow}
|
||||
# @return [Hash] the normalized event
|
||||
def encode(event)
|
||||
normalize_message(event)
|
||||
normalize_timestamp(event)
|
||||
|
||||
event
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -123,10 +123,10 @@ module Rackstash
|
||||
case value
|
||||
when nil
|
||||
return
|
||||
when Hash
|
||||
when ::Hash
|
||||
return if value.empty?
|
||||
return serialize_hash(value, prefix: key)
|
||||
when Array
|
||||
when ::Array
|
||||
return if value.empty?
|
||||
return serialize_array(value, prefix: key)
|
||||
when Float
|
||||
|
||||
@ -57,7 +57,7 @@ module Rackstash
|
||||
case value
|
||||
when nil
|
||||
nil
|
||||
when Array
|
||||
when ::Array
|
||||
"[#{value.map(&:to_s).join(',')}] "
|
||||
else
|
||||
"[#{value}] "
|
||||
|
||||
@ -7,9 +7,12 @@
|
||||
|
||||
module Rackstash
|
||||
module Encoders
|
||||
# The Raw encoder formats the log event as a raw `Hash` containing all data
|
||||
# exposed by the buffer. This can be used by special log targets which are
|
||||
# designed to handle hashes as opposed to formatted strings.
|
||||
# The Raw encoder passes along the raw unformatted event hash. It still
|
||||
# contains an `Array` of {Message} objects in the `"message"` key and a
|
||||
# `Time` object in the `"@timestamp"` key.
|
||||
#
|
||||
# When expecting a Hash in an adapter, usually it's more useful to use the
|
||||
# {Rackstash::Encoders::Hash} encoder instead.
|
||||
class Raw
|
||||
# @param event [Hash] a log event as produced by the {Flow}
|
||||
# @return [Hash] the passed `event`
|
||||
|
||||
@ -32,8 +32,8 @@ describe Rackstash::Adapters::Callable do
|
||||
end
|
||||
|
||||
describe '.default_encoder' do
|
||||
it 'returns a Raw encoder' do
|
||||
expect(adapter.default_encoder).to be_instance_of Rackstash::Encoders::Raw
|
||||
it 'returns a Hash encoder' do
|
||||
expect(adapter.default_encoder).to be_instance_of Rackstash::Encoders::Hash
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
33
spec/rackstash/encoders/hash_spec.rb
Normal file
33
spec/rackstash/encoders/hash_spec.rb
Normal file
@ -0,0 +1,33 @@
|
||||
# frozen_string_literal: true
|
||||
#
|
||||
# Copyright 2017 Holger Just
|
||||
#
|
||||
# This software may be modified and distributed under the terms
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
require 'rackstash/encoders/hash'
|
||||
|
||||
describe Rackstash::Encoders::Hash do
|
||||
let(:encoder) { described_class.new }
|
||||
|
||||
describe '#encode' do
|
||||
it 'normalized the message' do
|
||||
event = { 'message' => ["hello\n", "world\n", "foo", "bar"] }
|
||||
expect(encoder.encode(event)).to eql 'message' => "hello\nworld\nfoobar"
|
||||
end
|
||||
|
||||
it 'normalizes the timestamp' do
|
||||
time = Time.now
|
||||
event = { 'message' => ['foo', 'bar'], '@timestamp' => time }
|
||||
|
||||
expect(encoder.encode(event)).to eql 'message' => 'foobar', '@timestamp' => time.getutc.iso8601(6)
|
||||
end
|
||||
|
||||
it 'passes the normalized event hash through' do
|
||||
event = { 'foo' => 'bar', 'baz' => :boing }
|
||||
expect(encoder.encode(event)).to eql 'foo' => 'bar', 'baz' => :boing, 'message' => ''
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user