Skip to content

Commit

Permalink
fix(ECS): serialize error like objects
Browse files Browse the repository at this point in the history
serialize error like objects not only error instances.
fix absent headers object in req ecs serializer.
  • Loading branch information
commenthol committed Oct 15, 2023
1 parent 5a9742b commit 35e69f1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/ecs/LogEcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function toJson (obj, serializers) {
serializers[key](value, ecsObj)
} else {
// add all other unknown fields to extra
ecsObj.extra = ecsObj.extra || {}
ecsObj.extra[key] = normToString(value)
ecsObj.extra = ecsObj.extra || { [name]: {} }
ecsObj.extra[name][key] = normToString(value)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ecs/serializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { startTimeKey } from '../serializers/res.js'
const isNotObject = (any) => !any || typeof any !== 'object'

const ecsError = (err, ecsObj) => {
if (!(err instanceof Error)) {
if (!(err?.message)) {
return
}

Expand Down Expand Up @@ -63,7 +63,7 @@ const ecsReq = (req, ecsObj) => {
authorization,
'user-agent': userAgent,
...headers
} = req.headers
} = req.headers || {}

ecsObj.http = ecsObj.http || {}
ecsObj.http.request = {
Expand Down
53 changes: 51 additions & 2 deletions test/LogEcs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ describe('LogEcs', function () {
}
})
})

it('shall serialize an error like object', function () {
const ecsFields = {}
const err = { name: 'TypeError', message: 'boom' }
err.code = 'oneFinger'
ecsError(err, ecsFields)
assert.equal(ecsFields.error.stack_trace, undefined)
ecsFields.error.stack_trace = '#'
assert.deepStrictEqual(ecsFields, {
error: {
type: 'Object',
message: 'boom',
stack_trace: '#'
}
})
})
})

describe('ecsReq', function () {
Expand Down Expand Up @@ -126,6 +142,39 @@ describe('LogEcs', function () {
}
})
})

it('shall serialize a request without headers', function () {
const _req = {
...req,
originalUrl: '/mount/test/path?test=1',
httpVersion: '2.0',
headers: null
}

const ecsFields = {}
ecsReq(_req, ecsFields)
assert.deepStrictEqual(ecsFields, {
client: {
ip: '127.0.0.1',
port: 3333
},
url: {
path: '/mount/test/path',
query: 'test=1'
},
http: {
request: {
id: 'f90a5d9e-52e6-482e-a6ab-d1c5da1fe9c6',
method: 'GET',
version: '2.0',
headers: {}
}
},
user_agent: {
original: undefined
}
})
})
})

describe('ecsRes', function () {
Expand Down Expand Up @@ -241,15 +290,15 @@ describe('LogEcs', function () {
const res = log.error({ largeString })
assert.strictEqual(
res,
'{"log":{"level":"ERROR","logger":"test","diff_ms":0},"@timestamp":"1970-01-01T00:00:00.000Z","extra":{"largeString":"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"}}'
'{"log":{"level":"ERROR","logger":"test","diff_ms":0},"@timestamp":"1970-01-01T00:00:00.000Z","extra":{"test":{"largeString":"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"}}}'
)
})

it('should remove infinite number and function', function () {
const res = log.error({ number: Infinity, fn: () => {} })
assert.strictEqual(
res,
'{"log":{"level":"ERROR","logger":"test","diff_ms":0},"@timestamp":"1970-01-01T00:00:00.000Z","extra":{"number":"Infinity"}}'
'{"log":{"level":"ERROR","logger":"test","diff_ms":0},"@timestamp":"1970-01-01T00:00:00.000Z","extra":{"test":{"number":"Infinity"}}}'
)
})

Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/logEcs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35e69f1

Please sign in to comment.