-
Notifications
You must be signed in to change notification settings - Fork 935
Add tracing channels #2281
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
Merged
Merged
Add tracing channels #2281
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e5c471d
Add tracing channels
jsumners-nr 52c6862
trace on names instead of exported channel
jsumners-nr a430180
add docs
jsumners-nr c3b272a
make pkg tests match ci config
jsumners-nr 3cfc8c8
add instance in payloads
jsumners-nr 142b072
refactor implementation
jsumners-nr 0d556fa
refactor tests
jsumners-nr 8c85a92
rename result field
jsumners-nr 9e09cd6
use traceSync
jsumners-nr f36aa98
remove unused parameter
jsumners-nr 2771cd0
revert return change
jsumners-nr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Diagnostics | ||
|
|
||
| Pino provides [tracing channel](tc) events that allow insight into the | ||
| internal workings of the library. The currently supported events are: | ||
|
|
||
| + `tracing:pino_asJson:start`: emitted when the final serialization process | ||
| of logs is started. The emitted event payload has the following fields: | ||
| - `instance`: the Pino instance associated with the function | ||
| - `arguments`: the arguments passed to the function | ||
| + `tracing:pino_asJson:end`: emitted at the end of the final serialization | ||
| process. The emitted event payload has the following fields: | ||
| - `instance`: the Pino instance associated with the function | ||
| - `arguments`: the arguments passed to the function | ||
| - `result`: the finalized, newline delimited, log line as a string | ||
|
|
||
| [tc]: https://nodejs.org/docs/latest/api/diagnostics_channel.html#tracingchannel-channels |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| 'use strict' | ||
|
|
||
| const test = require('node:test') | ||
| const os = require('node:os') | ||
| const diagChan = require('node:diagnostics_channel') | ||
| const { AsyncLocalStorage } = require('node:async_hooks') | ||
| const { Writable } = require('node:stream') | ||
| const tspl = require('@matteo.collina/tspl') | ||
| const pino = require('../pino') | ||
|
|
||
| const hostname = os.hostname() | ||
| const { pid } = process | ||
| const AS_JSON_START = 'tracing:pino_asJson:start' | ||
| const AS_JSON_END = 'tracing:pino_asJson:end' | ||
|
|
||
| test.beforeEach(ctx => { | ||
| ctx.pino = { | ||
| ts: 1757512800000, // 2025-09-10T10:00:00.000-05:00 | ||
| now: Date.now | ||
| } | ||
|
|
||
| Date.now = () => ctx.pino.ts | ||
|
|
||
| ctx.pino.dest = new Writable({ | ||
| objectMode: true, | ||
| write (data, enc, cb) { | ||
| cb() | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| test.afterEach(ctx => { | ||
| Date.now = ctx.pino.now | ||
| }) | ||
|
|
||
| test('asJson emits events', async (t) => { | ||
| const plan = tspl(t, { plan: 8 }) | ||
| const { dest } = t.pino | ||
| const logger = pino({}, dest) | ||
| const expectedArguments = [ | ||
| {}, | ||
| 'testing', | ||
| 30, | ||
| `,"time":${t.pino.ts}` | ||
| ] | ||
|
|
||
| let startEvent | ||
| diagChan.subscribe(AS_JSON_START, startHandler) | ||
| diagChan.subscribe(AS_JSON_END, endHandler) | ||
|
|
||
| logger.info('testing') | ||
| await plan | ||
|
|
||
| diagChan.unsubscribe(AS_JSON_START, startHandler) | ||
| diagChan.unsubscribe(AS_JSON_END, endHandler) | ||
|
|
||
| function startHandler (event) { | ||
| startEvent = event | ||
| plan.equal(Object.prototype.toString.call(event.instance), '[object Pino]') | ||
jsumners-nr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| plan.equal(event.instance === logger, true) | ||
| plan.deepStrictEqual(Array.from(event.arguments ?? []), expectedArguments) | ||
jsumners-nr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function endHandler (event) { | ||
| plan.equal(Object.prototype.toString.call(event.instance), '[object Pino]') | ||
| plan.equal(event.instance === logger, true) | ||
| plan.deepStrictEqual(Array.from(event.arguments ?? []), expectedArguments) | ||
| plan.equal( | ||
| event.result, | ||
| `{"level":30,"time":${t.pino.ts},"pid":${pid},"hostname":"${hostname}","msg":"testing"}\n` | ||
| ) | ||
|
|
||
| plan.equal(event.arguments === startEvent.arguments, true, 'same event object is supplied to both events') | ||
| } | ||
| }) | ||
|
|
||
| test('asJson context is not lost', async (t) => { | ||
| const plan = tspl(t, { plan: 2 }) | ||
| const { dest } = t.pino | ||
| const logger = pino({}, dest) | ||
| const asyncLocalStorage = new AsyncLocalStorage() | ||
| const localStore = { foo: 'bar' } | ||
|
|
||
| diagChan.subscribe(AS_JSON_START, startHandler) | ||
| diagChan.subscribe(AS_JSON_END, endHandler) | ||
|
|
||
| asyncLocalStorage.run(localStore, () => { | ||
| logger.info('testing') | ||
| }) | ||
| await plan | ||
|
|
||
| diagChan.unsubscribe(AS_JSON_START, startHandler) | ||
| diagChan.unsubscribe(AS_JSON_END, endHandler) | ||
|
|
||
| function startHandler () { | ||
| const store = asyncLocalStorage.getStore() | ||
| plan.equal(store === localStore, true) | ||
| } | ||
|
|
||
| function endHandler () { | ||
| const store = asyncLocalStorage.getStore() | ||
| plan.equal(store === localStore, true) | ||
| } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.