Skip to content

Commit 7c82a8d

Browse files
authored
feat: add 'http.request.id' field, @elastic/ecs-helpers@2.1.0, bump ecs.version to 8.10.0 (#163)
Closes: #133 Closes: #76
1 parent 16a11e8 commit 7c82a8d

17 files changed

+107
-33
lines changed

docs/intro.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ popular web frameworks. A minimal log record includes the following fields:
1717
"@timestamp": "2021-01-13T21:32:38.095Z",
1818
"log.level": "info",
1919
"message": "hi",
20-
"ecs.version": "1.6.0"
20+
"ecs.version": "8.10.0"
2121
}
2222
----
2323

docs/morgan.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ similar to the following:
8686
"@timestamp": "2021-01-16T00:03:23.279Z",
8787
"log.level": "info",
8888
"message": "::1 - - [16/Jan/2021:00:03:23 +0000] \"GET / HTTP/1.1\" 200 13 \"-\" \"curl/7.64.1\"",
89-
"ecs.version": "1.6.0",
89+
"ecs.version": "8.10.0",
9090
"http": {
9191
"version": "1.1",
9292
"request": {

docs/pino.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Running this will produce log output similar to the following:
6868

6969
[source,cmd]
7070
----
71-
{"log.level":"info","@timestamp":"2023-10-14T02:07:47.901Z","process.pid":56645,"host.hostname":"pink.local","ecs.version":"1.6.0","message":"Hello world"}
72-
{"log.level":"warn","@timestamp":"2023-10-14T02:07:47.901Z","process.pid":56645,"host.hostname":"pink.local","ecs.version":"1.6.0","module":"foo","message":"From child"}
71+
{"log.level":"info","@timestamp":"2023-10-14T02:07:47.901Z","process.pid":56645,"host.hostname":"pink.local","ecs.version":"8.10.0","message":"Hello world"}
72+
{"log.level":"warn","@timestamp":"2023-10-14T02:07:47.901Z","process.pid":56645,"host.hostname":"pink.local","ecs.version":"8.10.0","module":"foo","message":"From child"}
7373
----
7474

7575
[float]
@@ -162,7 +162,7 @@ For example:
162162
"@timestamp": "2023-10-14T02:10:14.477Z",
163163
"process.pid": 56697,
164164
"host.hostname": "pink.local",
165-
"ecs.version": "1.6.0",
165+
"ecs.version": "8.10.0",
166166
"http": {
167167
"version": "1.1",
168168
"request": {

docs/winston.asciidoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Running this script (available https://github.com/elastic/ecs-logging-nodejs/blo
7979
[source,cmd]
8080
----
8181
% node examples/basic.js
82-
{"@timestamp":"2023-10-14T02:14:17.302Z","log.level":"info","message":"hi","ecs.version":"1.6.0"}
83-
{"@timestamp":"2023-10-14T02:14:17.304Z","log.level":"error","message":"oops there is a problem","ecs.version":"1.6.0","foo":"bar"}
82+
{"@timestamp":"2023-10-14T02:14:17.302Z","log.level":"info","message":"hi","ecs.version":"8.10.0"}
83+
{"@timestamp":"2023-10-14T02:14:17.304Z","log.level":"error","message":"oops there is a problem","ecs.version":"8.10.0","foo":"bar"}
8484
----
8585

8686
The formatter handles serialization to JSON, so you don't need to add the
@@ -126,7 +126,7 @@ will yield (pretty-printed for readability):
126126
"message": "boom",
127127
"stack_trace": "Error: boom\n at Object.<anonymous> (..."
128128
},
129-
"ecs.version": "1.6.0"
129+
"ecs.version": "8.10.0"
130130
}
131131
----
132132

@@ -220,7 +220,7 @@ For https://github.com/elastic/ecs-logging-nodejs/blob/main/packages/ecs-winston
220220
"user_agent": {
221221
"original": "curl/8.1.2"
222222
},
223-
"ecs.version": "1.6.0"
223+
"ecs.version": "8.10.0"
224224
}
225225
----
226226

packages/ecs-helpers/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# @elastic/ecs-helpers Changelog
22

3+
## v2.1.0
4+
5+
- Bump the `ecs.version` from "1.6.0" to "8.10.0".
6+
(https://github.com/elastic/ecs-logging-nodejs/issues/133)
7+
8+
- `formatHttpRequest()` will now attempt to determine request ID and add it
9+
to the [`http.request.id` field](https://www.elastic.co/guide/en/ecs/current/ecs-http.html#field-http-request-id).
10+
This first attempts `req.id` (Fastify, express-request-id), `req.id()`
11+
(Restify), and then falls back to the `request-id` or `x-request-id` headers.
12+
(https://github.com/elastic/ecs-logging-nodejs/issues/76)
13+
14+
Note: In previous versions a `req.id` value would be added to the `event.id`
15+
field. This has been removed. Please [open an issue](https://github.com/elastic/ecs-logging-nodejs/issues/new)
16+
if this impacts you.
17+
18+
319
## v2.0.0
420

521
- [Breaking change.] Drop the `serialize` method. Serialization will move to

packages/ecs-helpers/lib/http-formatters.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
'use strict'
1919

20+
// Likely HTTP request header names that would include a request ID value,
21+
// drawn from Fastify's [`requestIdHeader`](https://fastify.dev/docs/latest/Reference/Server/#requestidheader)
22+
// default, Restify's defaults (https://github.com/restify/node-restify/blob/9f1d249/lib/plugins/pre/reqIdHeaders.js#L5).
23+
const REQUEST_ID_HEADERS = [
24+
'request-id',
25+
'x-request-id'
26+
]
27+
2028
// Write ECS fields for the given HTTP request (expected to be
2129
// `http.IncomingMessage`-y) into the `ecs` object. This returns true iff
2230
// the given `req` was a request object it could process.
@@ -36,7 +44,6 @@ function formatHttpRequest (ecs, req) {
3644
}
3745

3846
const {
39-
id,
4047
method,
4148
url,
4249
headers,
@@ -45,11 +52,6 @@ function formatHttpRequest (ecs, req) {
4552
socket
4653
} = req
4754

48-
if (id) {
49-
ecs.event = ecs.event || {}
50-
ecs.event.id = id
51-
}
52-
5355
ecs.http = ecs.http || {}
5456
ecs.http.version = httpVersion
5557
ecs.http.request = ecs.http.request || {}
@@ -114,6 +116,39 @@ function formatHttpRequest (ecs, req) {
114116
}
115117
}
116118

119+
// Attempt to set `http.request.id` field from well-known web framework APIs
120+
// or from some likely headers.
121+
// Note: I'm not sure whether to include Hapi's `request.info.id` generated
122+
// value (https://hapi.dev/api/?v=21.3.2#-requestinfo). IIUC it does NOT
123+
// consider an incoming header.
124+
let id = null
125+
switch (typeof (req.id)) {
126+
case 'string':
127+
// Fastify https://fastify.dev/docs/latest/Reference/Request/, also
128+
// Express if using https://www.npmjs.com/package/express-request-id.
129+
id = req.id
130+
break
131+
case 'number':
132+
id = req.id.toString()
133+
break
134+
case 'function':
135+
// Restify http://restify.com/docs/request-api/#id
136+
id = req.id()
137+
break
138+
}
139+
if (!id && hasHeaders) {
140+
for (let i = 0; i < REQUEST_ID_HEADERS.length; i++) {
141+
const k = REQUEST_ID_HEADERS[i]
142+
if (headers[k]) {
143+
id = headers[k]
144+
break
145+
}
146+
}
147+
}
148+
if (id) {
149+
ecs.http.request.id = id
150+
}
151+
117152
return true
118153
}
119154

packages/ecs-helpers/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const errorFormatters = require('./error-formatters')
2121
const httpFormatters = require('./http-formatters')
2222

2323
module.exports = {
24-
version: '1.6.0',
24+
version: '8.10.0',
2525
...errorFormatters,
2626
...httpFormatters
2727
}

packages/ecs-helpers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elastic/ecs-helpers",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "ecs-logging-nodejs helpers",
55
"main": "lib/index.js",
66
"files": [

packages/ecs-helpers/test/express.test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test('express res/req serialization', t => {
3131
const app = express()
3232
let server
3333

34-
app.get('/', (req, res) => {
34+
app.get('/apath', (req, res) => {
3535
const rec = {}
3636

3737
res.setHeader('Foo', 'Bar')
@@ -45,17 +45,20 @@ test('express res/req serialization', t => {
4545

4646
t.same(rec.user_agent, { original: 'cool-agent' })
4747
t.same(rec.url, {
48-
path: '/',
49-
full: `http://127.0.0.1:${server.address().port}/`,
48+
full: `http://127.0.0.1:${server.address().port}/apath?aquery`,
49+
path: '/apath',
50+
query: 'aquery',
5051
domain: '127.0.0.1'
5152
})
5253
t.same(rec.http, {
5354
version: '1.1',
5455
request: {
56+
id: 'arequestid',
5557
method: 'GET',
5658
headers: {
5759
'user-agent': 'cool-agent',
5860
host: `127.0.0.1:${server.address().port}`,
61+
'x-request-id': 'arequestid',
5962
connection: 'close'
6063
}
6164
},
@@ -78,9 +81,13 @@ test('express res/req serialization', t => {
7881
app.listen(0, '127.0.0.1', function () {
7982
server = this
8083
const req = http.get(
81-
`http://127.0.0.1:${server.address().port}/`,
84+
`http://127.0.0.1:${server.address().port}/apath?aquery#ahash`,
8285
{
83-
headers: { 'user-agent': 'cool-agent', connection: 'close' }
86+
headers: {
87+
'user-agent': 'cool-agent',
88+
connection: 'close',
89+
'x-request-id': 'arequestid'
90+
}
8491
},
8592
function (res) {
8693
res.on('data', function () {})
@@ -91,6 +98,6 @@ test('express res/req serialization', t => {
9198
})
9299
}
93100
)
94-
req.on('error', t.ifErr)
101+
req.on('error', t.error)
95102
})
96103
})

packages/ecs-helpers/test/hapi.test.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ test('hapi res/req serialization', testOpts, t => {
6565
headers: {
6666
'user-agent': 'cool-agent',
6767
host: `127.0.0.1:${server.info.port}`,
68-
connection: 'close'
69-
}
68+
connection: 'close',
69+
'request-id': 'arequestid'
70+
},
71+
id: 'arequestid'
7072
},
7173
response: {
7274
status_code: 200,
@@ -98,8 +100,16 @@ test('hapi res/req serialization', testOpts, t => {
98100
t.comment('hapi server running on %s', server.info.uri)
99101

100102
// Make a request so we trigger a 'response' event above.
101-
const req = http.get(`http://127.0.0.1:${server.info.port}/`,
102-
{ headers: { 'user-agent': 'cool-agent', connection: 'close' } })
103+
const req = http.get(
104+
`http://127.0.0.1:${server.info.port}/`,
105+
{
106+
headers: {
107+
'user-agent': 'cool-agent',
108+
connection: 'close',
109+
'request-id': 'arequestid'
110+
}
111+
}
112+
)
103113
req.on('error', t.ifErr)
104114
})
105115
})

packages/ecs-morgan-format/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
protects against circular references and bigints.
77
(https://github.com/elastic/ecs-logging-nodejs/pull/155)
88

9+
- Set `http.request.id` field (see [ecs-helpers CHANGELOG](../ecs-helpers/CHANGELOG.md#v210)).
10+
911
## v1.4.0
1012

1113
- Add `service.version`, `service.environment`, and `service.node.name` log

packages/ecs-morgan-format/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ produce log output similar to the following:
8383
"user_agent": {
8484
"original": "curl/8.1.2"
8585
},
86-
"ecs.version": "1.6.0"
86+
"ecs.version": "8.10.0"
8787
}
8888
```
8989

packages/ecs-pino-format/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
`fast-safe-stringify` lib that resulted in a maintenance warning about
77
the `string-similarity` transitive dep.
88

9+
- Set `http.request.id` field (see [ecs-helpers CHANGELOG](../ecs-helpers/CHANGELOG.md#v210)).
10+
911
## v1.4.0
1012

1113
- Add `service.version`, `service.environment`, and `service.node.name` log

packages/ecs-pino-format/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ child.warn('From child')
4141
Running this will produce log output similar to the following:
4242

4343
```sh
44-
{"log.level":"info","@timestamp":"2023-10-16T18:08:02.601Z","process.pid":74325,"host.hostname":"pink.local","ecs.version":"1.6.0","message":"Hello world"}
45-
{"log.level":"warn","@timestamp":"2023-10-16T18:08:02.602Z","process.pid":74325,"host.hostname":"pink.local","ecs.version":"1.6.0","module":"foo","message":"From child"}
44+
{"log.level":"info","@timestamp":"2023-10-16T18:08:02.601Z","process.pid":74325,"host.hostname":"pink.local","ecs.version":"8.10.0","message":"Hello world"}
45+
{"log.level":"warn","@timestamp":"2023-10-16T18:08:02.602Z","process.pid":74325,"host.hostname":"pink.local","ecs.version":"8.10.0","module":"foo","message":"From child"}
4646
```
4747

4848
Please see the [Node.js ECS pino documentation](https://www.elastic.co/guide/en/ecs-logging/nodejs/current/pino.html) for more.

packages/ecs-pino-format/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"node": ">=10"
4040
},
4141
"dependencies": {
42-
"@elastic/ecs-helpers": "^2.0.0"
42+
"@elastic/ecs-helpers": "^2.1.0"
4343
},
4444
"devDependencies": {
4545
"@types/pino": "^6.3.9",

packages/ecs-winston-format/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
mechanism.
9292
(https://github.com/elastic/ecs-logging-nodejs/issues/108)
9393
94+
- Set `http.request.id` field (see [ecs-helpers CHANGELOG](../ecs-helpers/CHANGELOG.md#v210)).
95+
9496
## v1.4.0
9597
9698
- Add `service.version`, `service.environment`, and `service.node.name` log

packages/ecs-winston-format/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Running this script will produce log output similar to the following:
4646

4747
```sh
4848
% node examples/basic.js
49-
{"@timestamp":"2021-01-13T21:32:38.095Z","log.level":"info","message":"hi","ecs":{"version":"1.6.0"}}
50-
{"@timestamp":"2021-01-13T21:32:38.096Z","log.level":"error","message":"oops there is a problem","ecs":{"version":"1.6.0"},"foo":"bar"}
49+
{"@timestamp":"2021-01-13T21:32:38.095Z","log.level":"info","message":"hi","ecs":{"version":"8.10.0"}}
50+
{"@timestamp":"2021-01-13T21:32:38.096Z","log.level":"error","message":"oops there is a problem","ecs":{"version":"8.10.0"},"foo":"bar"}
5151
```
5252

5353
Please see the [Node.js ECS winston documentation](https://www.elastic.co/guide/en/ecs-logging/nodejs/current/winston.html) for more.

0 commit comments

Comments
 (0)