-
Notifications
You must be signed in to change notification settings - Fork 44
[DNM] Add logOperationTime util with implementation in protocol module #398
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pino from "pino"; | ||
import { performance } from 'perf_hooks'; | ||
import { getRandomBytes32 } from "@connext/vector-utils"; | ||
|
||
/// This method is used to log the start and end time of an executed method. We are using the | ||
/// perf_hooks (performance) lib provided by node to calculate this - much more accurate than | ||
/// Date.now(). On first call, a unique label string is generated and returned. On second call, | ||
/// same label string should be passed in to measure and log the diff in time (i.e. amount of | ||
/// time method took to execute in ms). | ||
export const logOperationTime = (log: pino.BaseLogger, method: string, label?: string): string => { | ||
if (label) { | ||
const stopLabel = `${label}-stop`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if there are label collisions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently using a unique identifier below as part of generated label to prevent that, but this will be replaced by passed in methodId EDIT: But to answer question, yeah, if there are label collisions, it would overwrite the prev mark. Which would be bad, bc an async method of same name could be called twice |
||
const timestamp = performance.mark(stopLabel); | ||
const diff = performance.measure(method, label, stopLabel); | ||
const message = `${method} : finished @ ${timestamp}; took ${diff} ms`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably only need the diff here since pino adds timestamps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, this is from earlier iteration, good catch |
||
// Depending on how long method took, up severity of logging call. This is a default | ||
// arrangement that was copied over from indra. | ||
if (diff < 2) { | ||
log.debug(message); | ||
} else if (diff < 200) { | ||
log.info(message); | ||
} else { | ||
log.warn(message); | ||
} | ||
} else { | ||
// Generate the label we'll be using (for later ref) using unique hexstring. | ||
const label = `${method}-${getRandomBytes32()}`; | ||
performance.mark(label); | ||
const message = `${method}: started`; | ||
log.debug(message); | ||
} | ||
return label; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will likely want to keep the
methodId
to log throughout the other parts of the method so the logs are easily searchable. Maybe we should use thelabel
as themethodId
instead.General purpose of the
methodId
is to get a unique strings so that you can easily identify all logs that came from a given method when searching in log dna or whatever other log parserThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. This functionality is already internal to the
logOperationTime
method. I actually had it like that at first, but worried that not all methods would follow this standard of generating a methodId. Will add this back tho, thanks