Skip to content

Commit 7d66d59

Browse files
committed
feat: support spanId field in meta
1 parent 8b2996d commit 7d66d59

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The resource to send logs to. Defaults to `{ type: "global" }`.
6363
Type: `{ [key]: key }` *(optional)*
6464

6565
Customize additional fields to pull from log messages and include in meta. Currently
66-
supports `httpRequest`, `trace`. Defaults to `{ httpRequest: "httpRequest" }`.
66+
supports `httpRequest`, `trace`, `spanId`. Defaults to `{ httpRequest: "httpRequest" }`.
6767

6868
#### fallback
6969

docs/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Changes are grouped by:
1515

1616
## [Unreleased](https://github.com/ovhemert/pino-stackdriver/compare/v3.0.0...HEAD)
1717

18-
- ...
18+
### Added
19+
20+
- Support `spanId` field in meta [@the-ress](https://github.com/the-ress)
1921

2022
## [3.0.0](https://github.com/ovhemert/pino-stackdriver/compare/v2.1.1...v3.0.0)
2123

pino-stackdriver.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ declare namespace PinoStackdriver {
2929

3030
/**
3131
* Names of log fields to pull properties out of - see https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
32-
* @default { httpRequest: "httpRequest", trace: "trace", ... }
32+
* @default { httpRequest: "httpRequest" }
3333
*/
3434
keys?: {
3535
httpRequest?: string;
3636
trace?: string;
37+
spanId?: string;
3738
};
3839

3940
/**

src/stackdriver.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function _levelToSeverity (level) {
2525

2626
const defaultKeys = {
2727
httpRequest: 'httpRequest',
28-
trace: undefined
28+
trace: undefined,
29+
spanId: undefined
2930
}
3031

3132
function _getKey (log, data, k, keys) {
@@ -66,6 +67,7 @@ module.exports.toLogEntry = function (log, options = {}) {
6667
resource: resource || { type: 'global' },
6768
severity,
6869
trace: _getKey(log, data, 'trace', keys),
70+
spanId: _getKey(log, data, 'spanId', keys),
6971
httpRequest: _getKey(log, data, 'httpRequest', keys)
7072
},
7173
data

test/stackdriver.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ test('adds labels to log entry message', t => {
9090
test('adds httpRequest to log entry message', t => {
9191
t.plan(3)
9292

93-
const log = { level: 30, time: parseInt('1532081790730', 10), httpRequest: { url: 'http://localhost/' }, trace: 'my/trace/id', pid: 9118, hostname: 'Osmonds-MacBook-Pro.local', v: 1 }
93+
const log = { level: 30, time: parseInt('1532081790730', 10), httpRequest: { url: 'http://localhost/' }, trace: 'my/trace/id', spanId: 'my-span-id', pid: 9118, hostname: 'Osmonds-MacBook-Pro.local', v: 1 }
9494
const entry = tested.toLogEntry(log)
9595
t.ok(entry.meta.severity === 'info')
9696
t.ok(entry.meta.httpRequest.url === 'http://localhost/')
9797

98-
// by default, do not include trace
98+
// by default, do not include trace or spanId
9999
t.ok(entry.meta.trace === undefined)
100+
t.ok(entry.meta.spanId === undefined)
100101
})
101102

102103
test('adds httpRequest with custom key to log entry message', t => {
@@ -117,6 +118,15 @@ test('does not add trace to log entry message by default', t => {
117118
t.ok(entry.meta.trace === undefined)
118119
})
119120

121+
test('does not add spanId to log entry message by default', t => {
122+
t.plan(2)
123+
124+
const log = { level: 30, time: parseInt('1532081790730', 10), spanId: 'my-span-id', pid: 9118, hostname: 'Osmonds-MacBook-Pro.local', v: 1 }
125+
const entry = tested.toLogEntry(log)
126+
t.ok(entry.meta.severity === 'info')
127+
t.ok(entry.meta.spanId === undefined)
128+
})
129+
120130
test('adds trace to log entry message with option', t => {
121131
t.plan(3)
122132

@@ -127,6 +137,16 @@ test('adds trace to log entry message with option', t => {
127137
t.ok(entry.meta.httpRequest.url === 'http://localhost/')
128138
})
129139

140+
test('adds spanId to log entry message with option', t => {
141+
t.plan(3)
142+
143+
const log = { level: 30, time: parseInt('1532081790730', 10), spanId: 'my-span-id', httpRequest: { url: 'http://localhost/' }, pid: 9118, hostname: 'Osmonds-MacBook-Pro.local', v: 1 }
144+
const entry = tested.toLogEntry(log, { keys: { spanId: 'spanId' } })
145+
t.ok(entry.meta.severity === 'info')
146+
t.ok(entry.meta.spanId === 'my-span-id')
147+
t.ok(entry.meta.httpRequest.url === 'http://localhost/')
148+
})
149+
130150
test('transforms log to entry in stream', t => {
131151
t.plan(3)
132152

0 commit comments

Comments
 (0)