Skip to content

feat: support spanId field in meta #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The resource to send logs to. Defaults to `{ type: "global" }`.
Type: `{ [key]: key }` *(optional)*

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

#### fallback

Expand Down
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Changes are grouped by:

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

- ...
### Added

- Support `spanId` field in meta [@the-ress](https://github.com/the-ress)

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

Expand Down
3 changes: 2 additions & 1 deletion pino-stackdriver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ declare namespace PinoStackdriver {

/**
* Names of log fields to pull properties out of - see https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
* @default { httpRequest: "httpRequest", trace: "trace", ... }
* @default { httpRequest: "httpRequest" }
*/
keys?: {
httpRequest?: string;
trace?: string;
spanId?: string;
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/stackdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function _levelToSeverity (level) {

const defaultKeys = {
httpRequest: 'httpRequest',
trace: undefined
trace: undefined,
spanId: undefined
}

function _getKey (log, data, k, keys) {
Expand Down Expand Up @@ -66,6 +67,7 @@ module.exports.toLogEntry = function (log, options = {}) {
resource: resource || { type: 'global' },
severity,
trace: _getKey(log, data, 'trace', keys),
spanId: _getKey(log, data, 'spanId', keys),
httpRequest: _getKey(log, data, 'httpRequest', keys)
},
data
Expand Down
26 changes: 23 additions & 3 deletions test/stackdriver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ test('adds labels to log entry message', t => {
})

test('adds httpRequest to log entry message', t => {
t.plan(3)
t.plan(4)

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 }
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 }
const entry = tested.toLogEntry(log)
t.ok(entry.meta.severity === 'info')
t.ok(entry.meta.httpRequest.url === 'http://localhost/')

// by default, do not include trace
// by default, do not include trace or spanId
t.ok(entry.meta.trace === undefined)
t.ok(entry.meta.spanId === undefined)
})

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

test('does not add spanId to log entry message by default', t => {
t.plan(2)

const log = { level: 30, time: parseInt('1532081790730', 10), spanId: 'my-span-id', pid: 9118, hostname: 'Osmonds-MacBook-Pro.local', v: 1 }
const entry = tested.toLogEntry(log)
t.ok(entry.meta.severity === 'info')
t.ok(entry.meta.spanId === undefined)
})

test('adds trace to log entry message with option', t => {
t.plan(3)

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

test('adds spanId to log entry message with option', t => {
t.plan(3)

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 }
const entry = tested.toLogEntry(log, { keys: { spanId: 'spanId' } })
t.ok(entry.meta.severity === 'info')
t.ok(entry.meta.spanId === 'my-span-id')
t.ok(entry.meta.httpRequest.url === 'http://localhost/')
})

test('transforms log to entry in stream', t => {
t.plan(3)

Expand Down