Gathering detailed insights and metrics for pg-monitor
Gathering detailed insights and metrics for pg-monitor
Gathering detailed insights and metrics for pg-monitor
Gathering detailed insights and metrics for pg-monitor
npm install pg-monitor
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
151 Stars
225 Commits
3 Forks
4 Watching
2 Branches
7 Contributors
Updated on 27 Nov 2024
JavaScript (97.98%)
TypeScript (2.02%)
Cumulative downloads
Total Downloads
Last day
19.8%
10,734
Compared to previous day
Last week
-2.4%
53,395
Compared to previous week
Last month
20%
235,633
Compared to previous month
Last year
50.4%
2,712,679
Compared to previous year
1
3
Events monitor for pg-promise.
This library takes the flexible event system provided by pg-promise and outputs it on screen, with full details available, and in the most informative way.
Its purpose is to give you the full picture of how the database is used in your application, providing full details with the context, such as tasks and transactions in which queries are executed.
In addition, it simplifies events logging for your application.
$ npm install pg-monitor
The library has no direct dependency on pg-promise.
$ git clone https://github.com/vitaly-t/pg-monitor
$ npm install
$ npm test
$ npm run coverage
1const monitor = require('pg-monitor'); 2 3const initOptions = { 4 // pg-promise initialization options; 5}; 6 7const pgp = require('pg-promise')(initOptions); 8 9// attach to all pg-promise events of the initOptions object: 10monitor.attach(initOptions); 11 12// Example of attaching to just events 'query' and 'error': 13// monitor.attach(initOptions, ['query', 'error']);
Method attach is to provide the quickest way to start using the library, by attaching to a set of events automatically.
If, however, you want to have full control over event handling, you can use the manual event forwarding.
Example of forwarding events query and error manually:
1const initOptions = { 2 query(e) { 3 /* do some of your own processing, if needed */ 4 5 monitor.query(e); // monitor the event; 6 }, 7 error(err, e) { 8 /* do some of your own processing, if needed */ 9 10 monitor.error(err, e); // monitor the event; 11 } 12};
See the API below for all the methods and options that you have.
Below is a safe forwarding implemented in TypeScript, for events connect
, disconnect
and query
.
It works the same for all other events.
1import * as monitor from 'pg-monitor'; 2 3function forward(event: monitor.LogEvent, args: IArguments) { 4 // safe event forwarding into pg-monitor: 5 (monitor as any)[event].apply(monitor, [...args]); 6} 7 8const options: IInitOptions = { 9 connect() { 10 forward('connect', arguments); 11 }, 12 disconnect() { 13 forward('disconnect', arguments); 14 }, 15 query() { 16 forward('query', arguments); 17 } 18};
Alternative Syntax: attach({options, events, override});
Adds event handlers to object initOptions
that's used during pg-promise initialization:
1monitor.attach(initOptions); // mutates the options object to attach to all events
A repeated call (without calling detach first) will throw Repeated attachments not supported, must call detach first.
Optional array of event names to which to attach. Passing null
/undefined
will attach
to all known events.
Example of attaching to just events query
and error
:
1monitor.attach(initOptions, ['query', 'error']);
Query-related events supported by pg-promise: connect
, disconnect
, query
, task
, transact
and error
.
See also: Initialization Options.
By default, the method uses derivation logic - it will call the previously configured event handler, if you have one, and only then will it call the internal implementation.
If, however, you want to override your own handlers, pass override
= true
.
Example of overriding all known event handlers:
1monitor.attach({options: initOptions, override: true});
Verifies if the monitor is currently attached, and returns a boolean.
Detaches from all the events to which attached after the last successful call to attach.
Calling it while not attached will throw Event monitor not attached.
Monitors and reports event connect.
Client object passed to the event.
Database Context.
Number of times the connection has been used.
Optional. When set, it reports such connection details as user@database
.
When not set, it defaults to the value of monitor.detailed.
Monitors and reports event disconnect.
Client object passed to the event.
Database Context.
Optional. When set, it reports such connection details as user@database
.
When not set, it defaults to the value of monitor.detailed.
Monitors and reports event query.
Event context object.
Optional. When set, it reports details of the task/transaction context in which the query is executing.
When not set, it defaults to the value of monitor.detailed.
Monitors and reports event task.
Event context object.
Monitors and reports event transact.
Event context object.
Monitors and reports event error.
Error message passed to the event.
Event context object.
Optional. When set, it reports details of the task/transaction context in which the error occurred.
When not set, it defaults to the value of monitor.detailed.
This boolean property provides the default for every method that accepts optional parameter detailed
.
By default, it is set to be true
. Setting this parameter to false
will automatically
switch off all details in events that support optional details, unless they have their own
parameter detailed
passed in as true
, which then overrides this global one.
Use method setDetailed
to change the value.
Activates either a predefined or a custom color theme.
Either a predefined theme name or a custom theme object.
For details, see Color Themes.
This event is to let your application provide your own log for everything that appears on the screen.
1monitor.setLog((msg, info) => {
2 // save the screen message into your own log;
3});
The notification occurs for every single line of text that appears on the screen, so you can maintain a log file with exactly the same content.
New message line, exactly as shown on the screen, with color attributes removed.
Object with additional information about the event:
time
- Date
object that was used for the screen, or null
when it is an extra line with
the event's context details;colorText
- color-coded message text, without time in front;text
- message text without the time in front (color attributes removed);event
- name of the event being logged.ctx
- Optional, task/transaction context when available.If your intent is only to log events, while suppressing any screen output, you can do so on per-event basis, as shown below:
1info.display = false; // suppress screen output for the event;
The library provides a flexible theme support to choose any color palette that you like, with a few of them predefined for your convenience.
For details, see Color Themes.
If your application uses more than one task or transaction, it is a good idea to tag them, so they provide informative context for every query event that's being logged, i.e. so you can easily see in which task/transaction context queries are executed.
Tagging a task or transaction with pg-promise is very easy, by taking this code:
1db.task(t => { 2 // task queries; 3}); 4db.tx(t => { 5 // transaction queries; 6});
and replacing it with this one:
1db.task(tag, t => { 2 // task queries; 3}); 4db.tx(tag, t => { 5 // transaction queries; 6});
where tag
is any object or value. In most cases you would want tag
to be just
a string that represents the task/transaction name, like this:
1db.task('MyTask', t => { 2 // task queries; 3}); 4db.tx('MyTransaction', t => { 5 // transaction queries; 6});
The tag
can be anything, including your custom object, so you can use it for your own reference
when handling events. And if you want to use it as your own object, while also allowing this library
to log the task/transaction pseudo-name/alias, then have your object implement method toString()
that returns the tag name.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/26 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More