From 7d66d5954d764299d60fb0eec6eb845225736be9 Mon Sep 17 00:00:00 2001 From: Tereza Tomcova Date: Fri, 26 Jan 2024 18:48:58 +0100 Subject: [PATCH] feat: support `spanId` field in meta --- docs/API.md | 2 +- docs/CHANGELOG.md | 4 +++- pino-stackdriver.d.ts | 3 ++- src/stackdriver.js | 4 +++- test/stackdriver.test.js | 24 ++++++++++++++++++++++-- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/docs/API.md b/docs/API.md index d3b424c..b8aad33 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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 diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b4e5bb5..37e49ee 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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) diff --git a/pino-stackdriver.d.ts b/pino-stackdriver.d.ts index 70d2b90..516fb28 100644 --- a/pino-stackdriver.d.ts +++ b/pino-stackdriver.d.ts @@ -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; }; /** diff --git a/src/stackdriver.js b/src/stackdriver.js index 257c662..e250043 100755 --- a/src/stackdriver.js +++ b/src/stackdriver.js @@ -25,7 +25,8 @@ function _levelToSeverity (level) { const defaultKeys = { httpRequest: 'httpRequest', - trace: undefined + trace: undefined, + spanId: undefined } function _getKey (log, data, k, keys) { @@ -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 diff --git a/test/stackdriver.test.js b/test/stackdriver.test.js index 695bfcb..7c54986 100644 --- a/test/stackdriver.test.js +++ b/test/stackdriver.test.js @@ -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 => { @@ -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) @@ -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)