Skip to content

Commit 1e8d744

Browse files
committed
no longer exclude "content-length" and "user-agent" from headers fields"
1 parent a830565 commit 1e8d744

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

helpers/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
- Change `formatHttpRequest` and `formatHttpResponse` to no longer exclude
6+
the "content-length" and "user-agent" headers for "http.request.headers"
7+
and "http.response.headers", even though their info also rendered to
8+
other specialized fields.
59
- Update ECS version to "1.6.0". Relevant [ECS changes](https://github.com/elastic/ecs/blob/master/CHANGELOG.md#160):
610
- "span.id" - This field is included by the loggers when integrating with APM.
711
- "Deprecate guidance to lowercase http.request.method."

helpers/lib/http-formatters.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,17 @@ function formatHttpRequest (ecs, req) {
9090

9191
const hasHeaders = Object.keys(headers).length > 0
9292
if (hasHeaders === true) {
93-
ecs.http.request.headers = ecs.http.request.headers || {}
94-
for (const header in headers) {
95-
if (header === 'content-length') {
96-
ecs.http.request.body = ecs.http.request.body || {}
97-
ecs.http.request.body.bytes = Number(headers[header])
98-
} else if (header === 'user-agent') {
99-
ecs.user_agent = ecs.user_agent || {}
100-
ecs.user_agent.original = headers[header]
101-
} else {
102-
// `http.response.headers` is not standardized
103-
ecs.http.request.headers[header] = headers[header]
104-
}
93+
// See https://github.com/elastic/ecs/issues/232 for discussion of
94+
// specifying headers in ECS.
95+
ecs.http.request.headers = Object.assign(ecs.http.request.headers || {}, headers)
96+
const cLen = Number(headers['content-length'])
97+
if (!isNaN(cLen)) {
98+
ecs.http.request.body = ecs.http.request.body || {}
99+
ecs.http.request.body.bytes = cLen
100+
}
101+
if (headers['user-agent']) {
102+
ecs.user_agent = ecs.user_agent || {}
103+
ecs.user_agent.original = headers['user-agent']
105104
}
106105
}
107106
}
@@ -121,15 +120,13 @@ function formatHttpResponse (ecs, res) {
121120
const headers = res.getHeaders()
122121
const hasHeaders = Object.keys(headers).length > 0
123122
if (hasHeaders === true) {
124-
ecs.http.response.headers = ecs.http.response.headers || {}
125-
for (const header in headers) {
126-
if (header === 'content-length') {
127-
ecs.http.response.body = ecs.http.response.body || {}
128-
ecs.http.response.body.bytes = Number(headers[header])
129-
} else {
130-
// `http.response.headers` is not standardized
131-
ecs.http.response.headers[header] = headers[header]
132-
}
123+
// See https://github.com/elastic/ecs/issues/232 for discussion of
124+
// specifying headers in ECS.
125+
ecs.http.response.headers = Object.assign(ecs.http.response.headers || {}, headers)
126+
const cLen = Number(headers['content-length'])
127+
if (!isNaN(cLen)) {
128+
ecs.http.response.body = ecs.http.response.body || {}
129+
ecs.http.response.body.bytes = cLen
133130
}
134131
}
135132
}

helpers/test/basic.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,20 @@ test('formatHttpRequest and formatHttpResponse should return a valid ecs object'
163163
request: {
164164
method: 'POST',
165165
headers: {
166+
'user-agent': 'cool-agent',
166167
'content-type': 'application/json',
168+
'content-length': '17',
167169
host: `localhost:${server.address().port}`,
168170
connection: 'close'
169171
},
170172
body: { bytes: 17 }
171173
},
172174
response: {
173175
status_code: 200,
174-
headers: { 'content-type': 'text/plain' },
176+
headers: {
177+
'content-type': 'text/plain',
178+
'content-length': '2'
179+
},
175180
body: { bytes: contentLen }
176181
}
177182
})

helpers/test/express.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ test('express res/req serialization', t => {
5252
request: {
5353
method: 'GET',
5454
headers: {
55+
'user-agent': 'cool-agent',
5556
host: `localhost:${server.address().port}`,
5657
connection: 'close'
5758
}

helpers/test/hapi.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ test('hapi res/req serialization', testOpts, t => {
6161
request: {
6262
method: 'GET',
6363
headers: {
64+
'user-agent': 'cool-agent',
6465
host: `localhost:${server.info.port}`,
6566
connection: 'close'
6667
}
@@ -71,6 +72,7 @@ test('hapi res/req serialization', testOpts, t => {
7172
foo: 'Bar',
7273
'content-type': 'text/html; charset=utf-8',
7374
'cache-control': 'no-cache',
75+
'content-length': '2',
7476
'accept-ranges': 'bytes'
7577
},
7678
body: {

0 commit comments

Comments
 (0)