-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from firespring/story/TP-17814
Add a normalizer to deal with certain nested context objects dumping with different formats that cause Logstash and Loggly to complain.
- Loading branch information
Showing
8 changed files
with
217 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Hedgelog | ||
class Normalizer | ||
def normalize(data) | ||
# Need to Marshal.dump/Marshal.load to deep copy the input so that scrubbing doesn't change global state | ||
d = Marshal.load(Marshal.dump(data)) | ||
normalize_hash(d) | ||
end | ||
|
||
def normalize_struct(struct) | ||
normalize_hash(Hash[struct.each_pair.to_a]) | ||
end | ||
|
||
def normalize_hash(hash) | ||
Hash[hash.map do |key, val| | ||
[key, normalize_thing(val)] | ||
end] | ||
end | ||
|
||
def normalize_array(array) | ||
array.to_json | ||
end | ||
|
||
private | ||
|
||
def normalize_thing(thing) | ||
return '' if thing.nil? | ||
return normalize_struct(thing) if thing.is_a?(Struct) | ||
return normalize_array(thing) if thing.is_a?(Array) | ||
return normalize_hash(thing) if thing.is_a?(Hash) | ||
thing | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
require 'hedgelog/normalizer' | ||
|
||
describe Hedgelog::Normalizer do | ||
subject(:instance) { described_class.new } | ||
let(:struct_class) { Struct.new(:foo, :bar) } | ||
let(:struct) { struct_class.new(1234, 'dummy') } | ||
let(:hash) { {message: 'dummy=1234'} } | ||
let(:array) { ['dummy string', {message: 'dummy=1234'}] } | ||
describe '#normalize' do | ||
it 'returns the normalized data' do | ||
expect(instance.normalize(hash)).to include(message: 'dummy=1234') | ||
end | ||
|
||
it 'does not modify external state' do | ||
myvar = 'dummy=1234' | ||
orig_myvar = myvar.clone | ||
|
||
data = {foo: myvar} | ||
instance.normalize(data) | ||
expect(myvar).to eq orig_myvar | ||
end | ||
end | ||
describe '#normalize_hash' do | ||
it 'returns a normalized hash' do | ||
expect(instance.normalize_hash(hash)).to include(message: 'dummy=1234') | ||
end | ||
end | ||
describe '#normalize_struct' do | ||
it 'returns struct as a normalized hash' do | ||
expect(instance.normalize_struct(struct)).to include(foo: 1234, bar: 'dummy') | ||
end | ||
end | ||
describe '#normalize_array' do | ||
it 'returns array as a json string' do | ||
normalized_array = instance.normalize_array(array) | ||
expect(normalized_array).to be_a String | ||
expect(normalized_array).to eq '["dummy string",{"message":"dummy=1234"}]' | ||
end | ||
end | ||
context 'When a hash contains different types of data' do | ||
let(:data) { hash } | ||
before :each do | ||
# add other types to the hash | ||
data[:hash] = hash.clone | ||
data[:struct] = struct | ||
data[:array] = array | ||
data[:string] = 'dummy' | ||
data[:number] = 1234 | ||
end | ||
it 'normalizes recursively' do | ||
result = instance.normalize_hash(data) | ||
expect(result[:hash]).to include(message: 'dummy=1234') | ||
expect(result[:message]).to include('dummy=1234') | ||
expect(result[:struct]).to be_a Hash | ||
expect(result[:struct]).to include(foo: 1234, bar: 'dummy') | ||
expect(result[:array]).to eq '["dummy string",{"message":"dummy=1234"}]' | ||
expect(result[:string]).to eq 'dummy' | ||
expect(result[:number]).to eq 1234 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'rspec/expectations' | ||
require 'benchmark/ips' | ||
|
||
module Matchers | ||
module Benchmark | ||
extend RSpec::Matchers::DSL | ||
|
||
matcher :perform do | ||
match do |block_arg| | ||
report = ::Benchmark.ips(quiet: true) do |bm| | ||
bm.config(time: 5, warmup: 2) | ||
bm.report('first') { @second_block.call } | ||
bm.report('second') { block_arg.call } | ||
end | ||
|
||
@first_bm, @second_bm = *report.entries | ||
|
||
@first_bm.ips >= target_ips(@second_bm) | ||
end | ||
|
||
chain :times_slower do |slower| | ||
@slower = slower | ||
end | ||
|
||
chain :than do |&blk| | ||
@second_block = blk | ||
end | ||
|
||
failure_message do | ||
"expected function to perform #{target_ips(@second_bm)} IPS, but it only performed #{@first_bm.ips} IPS" | ||
end | ||
|
||
def target_ips(result) | ||
result.ips / (@slower || 1) | ||
end | ||
|
||
def supports_block_expectations? | ||
true | ||
end | ||
|
||
diffable | ||
end | ||
end | ||
end |