Skip to content

Commit

Permalink
feat: support spanId field in meta
Browse files Browse the repository at this point in the history
  • Loading branch information
the-ress committed Jan 26, 2024
1 parent 8b2996d commit 7d66d59
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
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
24 changes: 22 additions & 2 deletions test/stackdriver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ test('adds labels to log entry message', t => {
test('adds httpRequest to log entry message', t => {
t.plan(3)

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

0 comments on commit 7d66d59

Please sign in to comment.