Skip to content

Commit

Permalink
Merge 29aab47 into d478715
Browse files Browse the repository at this point in the history
  • Loading branch information
tmwllc committed Sep 20, 2018
2 parents d478715 + 29aab47 commit 298ff72
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ export default class Encoder {
}
})
el = cache
} else if (typeof el === 'string') {
let jsonObj
try {
jsonObj = JSON.parse(el)
} catch (err) {
// string is not JSON, ignore it
}
if (jsonObj) {
return internalSwap(jsonObj)
}
}
return el
}
Expand Down
28 changes: 28 additions & 0 deletions test/encoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ describe('encoder', () => {
)
})

it('should redact values from JSON string and convert JSON to object', () => {
let encoded = new Encoder({redactList: [new RegExp('(\blng\b)|(\blat\b)|firstName|lastName|phone', 'gi')]}).encode({
traceId: 'abc',
spanId: 'def',
parentId: 'ghi',
operation: 'op1',
start: 1489522409134,
duration: 123,
tags: {
redact: 'val1',
tag2: 'val2'
},
logs: [{
firstName: 'Tom',
event:"Handled Request",
level:"info",
message:"Handled Request POST:/v2/reservations/8FC5735C312A44A1933365EDECFCA36D-1518675144764[461]",
logEvent:"Handled Request",
request: {
headers: {},
body:"{\"status\":\"active\",\"shopper\":{\"firstName\":\"Tom\",\"lastName\":\"Wallace\",\"phone\":\"1234567891\"}}"
}
}]
})
encoded.should.equal('{"traceId":"abc","spanId":"def","parentId":"ghi","operation":"op1","start":1489522409134,"duration":123,"tags":{"redact":"val1","tag2":"val2"},"logs":[{"firstName":"***","event":"Handled Request","level":"info","message":"Handled Request POST:/v2/reservations/8FC5735C312A44A1933365EDECFCA36D-1518675144764[461]","logEvent":"Handled Request","request":{"headers":{},"body":{"status":"active","shopper":{"firstName":"***","lastName":"***","phone":"***"}}}}]}' + os.EOL
)
})

it('should redact values', () => {
let encoded = new Encoder({redactList: [/redact/]}).encode({
traceId: 'abc',
Expand Down

0 comments on commit 298ff72

Please sign in to comment.