Skip to content

Commit 80a4b22

Browse files
authored
chore: prefer dotted field names; drop ".log" suffix on event.dataset (#150)
- Change to adding dotted field names (`"ecs.version": "1.6.0"`), rather than namespaced fields (`"ecs": {"version": "1.6.0"}`) for most fields. This is supported by the ecs-logging spec, and arguably preferred in the ECS logging docs. It is also what the ecs-logging-java libraries do. The resulting output is slightly shorter, and accidental collisions with user fields is less likely. - Stop adding ".log" suffix to `event.dataset` field. Closes: #95
1 parent 7760153 commit 80a4b22

23 files changed

+256
-448
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": "1.6.0"
2121
}
2222
----
2323

docs/morgan.asciidoc

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ similar to the following:
8585
"@timestamp": "2021-01-16T00:03:23.279Z",
8686
"log.level": "info",
8787
"message": "::1 - - [16/Jan/2021:00:03:23 +0000] \"GET / HTTP/1.1\" 200 13 \"-\" \"curl/7.64.1\"",
88-
"ecs": {
89-
"version": "1.6.0"
90-
},
88+
"ecs.version": "1.6.0",
9189
"http": {
9290
"version": "1.1",
9391
"request": {
@@ -178,19 +176,11 @@ For example, running https://github.com/elastic/ecs-logging-nodejs/blob/main/log
178176
----
179177
% node examples/express-with-apm.js | jq .
180178
{
181-
...
182-
"event": {
183-
"dataset": "express-with-elastic-apm.log"
184-
},
185-
"trace": {
186-
"id": "e097193afa9ac221017b45a1f674601c"
187-
},
188-
"transaction": {
189-
"id": "c6aa5b47e01bad72"
190-
},
191-
"service": {
192-
"name": "express-with-elastic-apm"
193-
}
179+
// The same fields as before, plus:
180+
"trace.id": "e097193afa9ac221017b45a1f674601c",
181+
"transaction.id": "c6aa5b47e01bad72",
182+
"service.name": "express-with-elastic-apm",
183+
"event.dataset": "express-with-elastic-apm"
194184
}
195185
----
196186

docs/pino.asciidoc

Lines changed: 17 additions & 28 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":"2021-01-19T22:51:12.142Z","ecs":{"version":"1.6.0"},"process":{"pid":82240},"host":{"hostname":"pink.local"},"message":"Hello world"}
72-
{"log.level":"warn","@timestamp":"2021-01-19T22:51:12.143Z","ecs":{"version":"1.6.0"},"process":{"pid":82240},"host":{"hostname":"pink.local"},"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":"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"}
7373
----
7474

7575
[float]
@@ -159,22 +159,17 @@ For example:
159159
... # run 'curl http://localhost:3000/'
160160
{
161161
"log.level": "info",
162-
"@timestamp": "2021-01-19T22:58:59.649Z",
163-
"ecs": {
164-
"version": "1.6.0"
165-
},
166-
"process": {
167-
"pid": 82670
168-
},
169-
"host": {
170-
"hostname": "pink.local"
171-
},
162+
"@timestamp": "2023-10-14T02:10:14.477Z",
163+
"process.pid": 56697,
164+
"host.hostname": "pink.local",
165+
"ecs.version": "1.6.0",
172166
"http": {
173167
"version": "1.1",
174168
"request": {
175169
"method": "GET",
176170
"headers": {
177171
"host": "localhost:3000",
172+
"user-agent": "curl/8.1.2",
178173
"accept": "*/*"
179174
}
180175
},
@@ -189,8 +184,13 @@ For example:
189184
"full": "http://localhost:3000/",
190185
"path": "/"
191186
},
187+
"client": {
188+
"address": "::ffff:127.0.0.1",
189+
"ip": "::ffff:127.0.0.1",
190+
"port": 49504
191+
},
192192
"user_agent": {
193-
"original": "curl/7.64.1"
193+
"original": "curl/8.1.2"
194194
},
195195
"message": "handled request"
196196
}
@@ -220,21 +220,10 @@ For example, running https://github.com/elastic/ecs-logging-nodejs/blob/main/log
220220
----
221221
% node examples/http-with-elastic-apm.js | jq .
222222
...
223-
"service": {
224-
"name": "http-with-elastic-apm"
225-
},
226-
"event": {
227-
"dataset": "http-with-elastic-apm.log"
228-
},
229-
"trace": {
230-
"id": "a1c23e04d7174462f330f5921c8f036d"
231-
},
232-
"transaction": {
233-
"id": "2550c4c267d4dd53"
234-
},
235-
"span": {
236-
"id": "16a8704258dbc328"
237-
},
223+
"service.name": "http-with-elastic-apm",
224+
"event.dataset": "http-with-elastic-apm",
225+
"trace.id": "79de6da334efae17ad43758573d57f1c",
226+
"transaction.id": "8bf944f94b72c54d",
238227
...
239228
----
240229

docs/winston.asciidoc

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Running this script (available https://github.com/elastic/ecs-logging-nodejs/blo
7878
[source,cmd]
7979
----
8080
% node examples/basic.js
81-
{"@timestamp":"2021-01-13T21:32:38.095Z","log.level":"info","message":"hi","ecs":{"version":"1.6.0"}}
82-
{"@timestamp":"2021-01-13T21:32:38.096Z","log.level":"error","message":"oops there is a problem","ecs":{"version":"1.6.0"},"foo":"bar"}
81+
{"@timestamp":"2023-10-14T02:14:17.302Z","log.level":"info","message":"hi","ecs.version":"1.6.0"}
82+
{"@timestamp":"2023-10-14T02:14:17.304Z","log.level":"error","message":"oops there is a problem","ecs.version":"1.6.0","foo":"bar"}
8383
----
8484

8585
The formatter handles serialization to JSON, so you don't need to add the
@@ -120,14 +120,12 @@ will yield (pretty-printed for readability):
120120
"@timestamp": "2021-01-26T17:25:07.983Z",
121121
"log.level": "info",
122122
"message": "oops",
123-
"ecs": {
124-
"version": "1.6.0"
125-
},
126123
"error": {
127124
"type": "Error",
128125
"message": "boom",
129126
"stack_trace": "Error: boom\n at Object.<anonymous> (..."
130-
}
127+
},
128+
"ecs.version": "1.6.0"
131129
}
132130
----
133131

@@ -189,18 +187,16 @@ For https://github.com/elastic/ecs-logging-nodejs/blob/main/loggers/winston/exam
189187
% node examples/http.js | jq . # using jq for pretty printing
190188
... # run 'curl http://localhost:3000/'
191189
{
192-
"@timestamp": "2021-01-13T22:00:07.442Z",
190+
"@timestamp": "2023-10-14T02:15:54.768Z",
193191
"log.level": "info",
194192
"message": "handled request",
195-
"ecs": {
196-
"version": "1.6.0"
197-
},
198193
"http": {
199194
"version": "1.1",
200195
"request": {
201196
"method": "GET",
202197
"headers": {
203198
"host": "localhost:3000",
199+
"user-agent": "curl/8.1.2",
204200
"accept": "*/*"
205201
}
206202
},
@@ -215,9 +211,15 @@ For https://github.com/elastic/ecs-logging-nodejs/blob/main/loggers/winston/exam
215211
"path": "/",
216212
"full": "http://localhost:3000/"
217213
},
214+
"client": {
215+
"address": "::ffff:127.0.0.1",
216+
"ip": "::ffff:127.0.0.1",
217+
"port": 49538
218+
},
218219
"user_agent": {
219-
"original": "curl/7.64.1"
220-
}
220+
"original": "curl/8.1.2"
221+
},
222+
"ecs.version": "1.6.0"
221223
}
222224
----
223225

@@ -239,19 +241,11 @@ For example, running https://github.com/elastic/ecs-logging-nodejs/blob/main/log
239241
----
240242
% node examples/http-with-elastic-apm.js | jq .
241243
...
242-
"event": {
243-
"dataset": "http-with-elastic-apm.log"
244-
},
245-
"trace": {
246-
"id": "74631535a02bbe6a07c298b28c7443f4"
247-
},
248-
"transaction": {
249-
"id": "505400b77aba4d9a"
250-
},
251-
"service": {
252-
"name": "http-with-elastic-apm"
253-
}
254-
...
244+
"service.name": "http-with-elastic-apm",
245+
"event.dataset": "http-with-elastic-apm",
246+
"trace.id": "7fd75f0f33ff49aba85d060b46dcad7e",
247+
"transaction.id": "6c97c7c1b468fa05"
248+
}
255249
----
256250

257251
These IDs match trace data reported by the APM agent.

loggers/morgan/CHANGELOG.md

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

3+
## Unreleased
4+
5+
- Change to adding dotted field names (`"ecs.version": "1.6.0"`), rather than
6+
namespaced fields (`"ecs": {"version": "1.6.0"}`) for most fields. This is
7+
supported by the ecs-logging spec, and arguably preferred in the ECS logging
8+
docs. It is also what the ecs-logging-java libraries do. The resulting output
9+
is slightly shorter, and accidental collisions with user fields is less
10+
likely.
11+
- Stop adding ".log" suffix to `event.dataset` field.
12+
([#95](https://github.com/elastic/ecs-logging-nodejs/issues/95))
13+
314
## v1.1.0
415

516
- Update @elastic/ecs-helpers@1.1.0 to get more robust HTTP req and res

loggers/morgan/examples/express-with-apm.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@
2222
//
2323
// If usage of Elastic APM is detected (i.e. the "elastic-apm-node" package
2424
// is being used), then log records will include trace identifiers, e.g.:
25-
// "trace": { "id": "678f2a0189f259baf2ea17db8af5a4d0" },
26-
// "transaction": { "id": "1cc6339964575165" },
27-
// "span": { "id": "f72c52ceda81777a" },
25+
// "trace.id": "678f2a0189f259baf2ea17db8af5a4d0",
26+
// "transaction.id": "1cc6339964575165",
27+
// "span.id": "f72c52ceda81777a",
2828
// to correlate log and trace data in Kibana.
2929

3030
/* eslint-disable-next-line no-unused-vars */
3131
const apm = require('elastic-apm-node').start({
3232
serviceName: 'express-with-elastic-apm',
3333
centralConfig: false,
3434
captureExceptions: false,
35-
metricsInterval: 0
35+
metricsInterval: '0s',
36+
logLevel: 'warn' // avoid APM agent log preamble
3637
})
3738

3839
const app = require('express')()

loggers/morgan/index.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ try {
4545
// APM agent is detected, then log records will include the following
4646
// fields:
4747
// - "service.name" - the configured serviceName in the agent
48-
// - "event.dataset" - set to "$serviceName.log" for correlation in Kibana
48+
// - "event.dataset" - set to "$serviceName" for correlation in Kibana
4949
// - "trace.id", "transaction.id", and "span.id" - if there is a current
5050
// active trace when the log call is made
5151
// Default true.
@@ -82,21 +82,17 @@ function ecsFormat (opts) {
8282
apm = elasticApm
8383
}
8484

85-
let serviceField
86-
let eventField
85+
const extraFields = {}
8786
if (apm) {
88-
// https://github.com/elastic/apm-agent-nodejs/pull/1949 is adding
89-
// getServiceName() in v3.11.0. Fallback to private `apm._conf`.
9087
// istanbul ignore next
9188
const serviceName = apm.getServiceName
92-
? apm.getServiceName()
89+
? apm.getServiceName() // added in elastic-apm-node@3.11.0
9390
: apm._conf.serviceName
94-
// A mis-configured APM Agent can be "started" but not have a
95-
// "serviceName".
91+
// A mis-configured APM Agent can be "started" but not have a "serviceName".
9692
// istanbul ignore else
9793
if (serviceName) {
98-
serviceField = { name: serviceName }
99-
eventField = { dataset: serviceName + '.log' }
94+
extraFields['service.name'] = serviceName
95+
extraFields['event.dataset'] = serviceName
10096
}
10197
}
10298

@@ -105,25 +101,17 @@ function ecsFormat (opts) {
105101
'@timestamp': new Date().toISOString(),
106102
'log.level': res.statusCode < 500 ? 'info' : 'error',
107103
message: fmt(token, req, res),
108-
ecs: { version }
109-
}
110-
111-
if (serviceField) {
112-
ecsFields.service = serviceField
113-
}
114-
if (eventField) {
115-
ecsFields.event = eventField
104+
'ecs.version': version,
105+
...extraFields
116106
}
117107

118108
// https://www.elastic.co/guide/en/ecs/current/ecs-tracing.html
119109
if (apm) {
120110
const tx = apm.currentTransaction
121111
// istanbul ignore else
122112
if (tx) {
123-
ecsFields.trace = ecsFields.trace || {}
124-
ecsFields.trace.id = tx.traceId
125-
ecsFields.transaction = ecsFields.transaction || {}
126-
ecsFields.transaction.id = tx.id
113+
ecsFields['trace.id'] = tx.traceId
114+
ecsFields['transaction.id'] = tx.id
127115
// Not including `span.id` because the way morgan logs (on the HTTP
128116
// Response "finished" event), any spans during the request handler
129117
// are no longer active.

loggers/morgan/test/apm.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ test('tracing integration works', t => {
132132
t.ok(traceObjs[0].metadata, 'traceObjs[0] is metadata')
133133
t.ok(traceObjs[1].transaction, 'traceObjs[1] is transaction')
134134
const tx = traceObjs[1].transaction
135-
t.equal(logObjs[0].trace.id, tx.trace_id, 'trace.id matches')
136-
t.equal(logObjs[0].transaction.id, tx.id, 'transaction.id matches')
137-
t.equal(logObjs[0].service.name, 'test-apm')
138-
t.equal(logObjs[0].event.dataset, 'test-apm.log')
135+
t.equal(logObjs[0]['trace.id'], tx.trace_id, 'trace.id matches')
136+
t.equal(logObjs[0]['transaction.id'], tx.id, 'transaction.id matches')
137+
t.equal(logObjs[0]['service.name'], 'test-apm')
138+
t.equal(logObjs[0]['event.dataset'], 'test-apm')
139139
finish()
140140
}
141141
}

loggers/pino/CHANGELOG.md

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

3+
## Unreleased
4+
5+
- Change to adding dotted field names (`"ecs.version": "1.6.0"`), rather than
6+
namespaced fields (`"ecs": {"version": "1.6.0"}`) for most fields. This is
7+
supported by the ecs-logging spec, and arguably preferred in the ECS logging
8+
docs. It is also what the ecs-logging-java libraries do. The resulting output
9+
is slightly shorter, and accidental collisions with user fields is less
10+
likely.
11+
- Stop adding ".log" suffix to `event.dataset` field.
12+
([#95](https://github.com/elastic/ecs-logging-nodejs/issues/95))
13+
314
## v1.3.0
415

516
- TypeScript types. ([#82](https://github.com/elastic/ecs-logging-nodejs/pull/82))

loggers/pino/examples/http-with-elastic-apm.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@
2222
//
2323
// If usage of Elastic APM is detected (i.e. the "elastic-apm-node" package
2424
// is being used), then log records will include trace identifiers, e.g.:
25-
// "trace": { "id": "678f2a0189f259baf2ea17db8af5a4d0" },
26-
// "transaction": { "id": "1cc6339964575165" },
27-
// "span": { "id": "f72c52ceda81777a" },
25+
// "trace.id": "678f2a0189f259baf2ea17db8af5a4d0",
26+
// "transaction.id": "1cc6339964575165",
27+
// "span.id": "f72c52ceda81777a",
2828
// to correlate log and trace data in Kibana.
2929

3030
/* eslint-disable-next-line no-unused-vars */
3131
const apm = require('elastic-apm-node').start({
3232
serviceName: 'http-with-elastic-apm',
3333
centralConfig: false,
3434
captureExceptions: false,
35-
metricsInterval: 0
35+
metricsInterval: '0s',
36+
logLevel: 'warn' // avoid APM agent log preamble
3637
})
3738

3839
const http = require('http')

loggers/pino/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Config {
1818
* fields:
1919
*
2020
* - "service.name" - the configured serviceName in the agent
21-
* - "event.dataset" - set to "$serviceName.log" for correlation in Kibana
21+
* - "event.dataset" - set to "$serviceName" for correlation in Kibana
2222
* - "trace.id", "transaction.id", and "span.id" - if there is a current
2323
* active trace when the log call is made
2424
*

0 commit comments

Comments
 (0)