vitaly-t / pg-monitor Goto Github PK
View Code? Open in Web Editor NEW:computer: Events monitor for pg-promise
License: MIT License
:computer: Events monitor for pg-promise
License: MIT License
This is an awesome module.
Would it be possible to add an option to display the milliseconds in the timestamp? During debugging it sometimes comes in handy.
I added it as a default to my fork. However, when I tried figuring out how to add it as an option. I got stuck.
Thank you!
I'm using this awesome module with massive.js. I'm wondering if it is possible to get a duration of a specific statement? Now I get event info displayed in this form:
14:59:07 connect(playground@playground)
14:59:07 SELECT COUNT(1) FROM "users"
WHERE TRUE LIMIT 1
14:59:07 disconnect(playground@playground)
14:59:07 connect(playground@playground)
14:59:07 SELECT * FROM "users"
WHERE "id" = 1 ORDER BY "id" LIMIT 10
14:59:07 disconnect(playground@playground)
14:59:07 connect(playground@playground)
14:59:07 SELECT * FROM "books"
WHERE "user_id" = 1 ORDER BY "id"
14:59:07 disconnect(playground@playground)
@vitaly-t, today I tried the recent version of pg-monitor, after the last commit - typescript 2 support.
I had problems to import the library because of the module declaration. In the way it is, it's happening the following:
import * as pgMonitor from 'pg-monitor';
pgMonitor.pgMonitor.attach(options); ..
This situation is described in typescript - namespaces and modules doc.
Complementing, I tried to use as:
import { pgMonitor } from 'pg-monitor';
pgMonitor.attach(options); ..
This will result an exception during runtime - "TypeError: Cannot read property 'attach' of undefined"
I will open a PR, ok?
We really like the way logs are formatted by pg-monitor, but we also need to customize the logger. Currently, if you provide a custom log function, it will always call removeColors
before it passes the message in. We want the colors though! We just also want to customize logging.
I played around with some options, and I wanted to get your thoughts to see if I should submit a PR.
I could change setLog
to allow it to take either of:
log: function(msg, info)
Currentoptions: { log: function(msg, info), removeColors: Boolean }
New!Setting options.removeColors
to false
could result in either:
msg
is colored -but- info.text
is plainmsg
is colored -and- info.text
is coloredAlready, the two are slightly different in that info.text
has .trim()
called on the string, whereas msg
does not; so, I'm not sure if you think they should also be differentiated in this way as well.
Usage would change to be able to look like this:
monitor.setLog({
removeColors: false,
log(msg, info) { /* ... */ }
});
This allows the best of both worlds, with output like this:
What do you think?
I've been using TAPE piped to Faucet for TAP testing and summarization. Console statements seem to interfere with the summarizer.
node transaction-test.js | faucet
I used monitor.log
to direct output to a log file:
monitor.log = function(msg, info){
wstream.write(msg + "\n");
};
BUT, I couldn't find a way around commenting out the console.log statement in pg-monitor:
https://github.com/vitaly-t/pg-monitor/blob/master/lib/index.js#L335
I've already been through the previous question #3.
I have the configurations setup according to the documentation, but I don't see any output to the console.
This is what I'm using right now
import pg from 'pg-promise';
import monitor from 'pg-monitor';
const options = {
user: process.env.dbUser,
database: process.env.dbName,
password: process.env.dbPassword,
port: process.env.dbPort,
max: process.env.dbMaxClient,
idleTimeoutMillis: process.env.dbIdleTimeout
};
const db = pg()(options);
monitor.attach(options);
This library was written as an addition to one of the earliest versions of pg-promise
. At the time it was done a bit in a rush, and today its code looks a bit dated and messy.
I have done TypeScript conversion for a number of other modules, but this one was left for last, because its primary function is that of a development
dependency, plus it has no bugs, hence the lower priority.
This will also create an opportunity for some breaking changes to improve the protocol.
There is a vulnerability in dependency cli-color. It should be bumped up to version 2.0.4 to fix this.
medikoo/cli-color#43
Hi @vitaly-t
I was wondering if there is a way to tag a query that is not a task or transaction? I would like to provider a context id so that I know what http request has caused the query execution when I log it with pg-monitor. For example:
db.none('UPDATE ...', 'my-http-request-id')
...
const log = (msg, info) => {
logger.debug(`${info.event}: ${info.text}`, {
sql: true,
event: info.event,
requestId: info.tag,
});
info.display = false;
};
Thanks in advance
Sorry, I'm a total newbie at node.js - any help you can give would be appreciated.
I'm using express with massive.js, and wanted to use pg-monitor. I installed it and added these lines to app.js:
const monitor = require('pg-monitor');
monitor.attach({});
However, nothing appears on the console (or anywhere else that I can see) when I execute a query via massive. I've tried a few different things, but no luck. Is there a step I'm missing?
Thanks!
Concretely, the error code would be nice.
Please consider adding a license to your code, so that developers looking to use it know the terms of that use.
A popular and common license you could consider would be: https://mit-license.org/
can i pass cutom logger we have customized bunyan logger and using it in all the application need to pass that logger to pg-monitor to log.info all the queries passed to pg promise
Hello! I want to get rid of these messages from my errors.log
:
05:58:40 error: duplicate key value violates unique constraint "usernames_pkey"
task: INSERT INTO "usernames" ("username", "email") VALUES
('profile-creation-0', '[email protected]')
This how I handle the error in my controller:
try {
await t.none(sql.usernames.insert, { username, email });
} catch (err) {
if (typeof err === 'object'
&& err.code == '23505'
&& err.table == 'usernames'
&& err.constraint == 'usernames_pkey') {
return ({
error: {
type: 'validation',
username: 'Username taken.'
}
});
}
throw err;
}
This is my diagnostics.js
:
const stack = require('callsite');
const os = require('os');
const fs = require('fs');
const path = require('path');
const monitor = require('pg-monitor');
module.exports = ({ mode, logFile }, initOptions) => {
const $DEV = mode === 'development';
const dirname = path.dirname(stack()[3].getFileName());
logFile = path.resolve(dirname, logFile);
monitor.setTheme('matrix');
monitor.setLog((msg, info) => {
// In a PROD environment we will only receive event 'error',
// because this is how we set it up below.
// And the check below is for DEV environment only, as we want to log
// errors only, or else the file will grow out of proportion in no time.
if (info.event === 'error') {
let logText = os.EOL + msg; // line break + next error message;
if (info.time) {
// If it is a new error being reported,
// and not an additional error line;
logText = os.EOL + logText; // add another line break in front;
}
fs.appendFileSync(logFile, logText); // add error handling as required;
}
// We absolutely must not let the monitor write anything into the console
// while in a PROD environment, and not just because nobody will be able
// to see it there, but mainly because the console is incredibly slow and
// hugely resource-consuming, suitable only for debugging.
if (!$DEV) {
// If it is not a DEV environment:
info.display = false; // display nothing;
}
});
if ($DEV) {
// In a DEV environment, we attach to all supported events:
monitor.attach(initOptions);
} else {
// In a PROD environment we should only attach to the type of events
// that we intend to log. And we are only logging event 'error' here:
monitor.attach(initOptions, ['error']);
}
}
Thanks in advance. I'm loving pg-promise
, pg-monitor
and pg-minify
. Great work!
It would be great if there was an option to add a general prefix to the output. For instance:
var monitor = require("pg-monitor");
monitor.setPrefix("postgres: ");
monitor.attach();
// proceed with the call to pgpLib
Instead of
15:46:35 connect(clima@150601)
15:46:35 select * from maps_create('[{"id":1,"code":"energia_centrais"}]')
15:46:35 disconnect(clima@150601)
the output would be
15:46:35 postgres: connect(clima@150601)
15:46:35 postgres: select * from maps_create('[{"id":1,"code":"energia_centrais"}]')
15:46:35 postgres: disconnect(clima@150601)
Even better: allow simple customization of the date format:
monitor.setDateFormat("YYMMDD HH:MM:SS");
monitor.setDateFormat("HH:MM:SS");
etc
Similar to spex and pg-promise, I also need pg-monitor to include its typings via package.json.
I've created a pull request to implement this feature.
In light of the soon-coming version 8.x of pg-promise
, add displaying tags when they are numbers.
Currently, only strings are displayed. But tags can be simple numbers sometimes also.
Here is the code I am using to initialize pg-monitor:
// postgres setup
const pg_options = {
host: 'localhost',
port: 5432,
database: 'test',
user: 'dm',
password: 'asdfasdf'
};
global.db = pgp(pg_options);
// postgres monitoring
require("pg-monitor").attach(pg_options);
Is there something that I am doing wrong?
Optional array of event names to which to attach. Passing null/undefined will attach to all known events.
Yet when I do
monitor.attach(null);
or
I get:
TypeError: Initialization object 'options' must be specified.
I think an explicit example with no options would be nice to see.
I'm a bit stumped on how to get output from pg-monitor
. I initialize pg-promise
and pg-monitor
and then use the db connection in Express.js route handlers. However, there is no console output. An example:
const pgp = require('pg-promise');
const monitor = require('pg-monitor');
const express = require('express');
const db = pgp()({ /* connection parameters */});
monitor.attach({});
const app = express();
app.use(/* routes using db.any etc to query the database */);
app.listen(8080, () => console.log('listening on 8080'));
// outputs "listening on 8080" and then nothing, even if HTTP requests are made and DB activity occurs
Is app.listen
somehow blocking the output from pg-monitor
or am I missing something else? I've been reading and re-reading the docs but I can't find anything obviously wrong ๐
The issue is caused by an older version of [email protected] if bump up the version it will resolve the CVE
None of defaults works in white bg screen. Ex. matrix white => black works fine. I have used over 20 years white bg and black fg. I'm sure I'm not only who has got enough dark bg and green or amber fg.
Of course you can make own theme, but it would be nice if some of defaults is usable also this kind of screens.
To prevent console.log
when custom logger provided, I must mutate incoming config to prevent default behaviour. It isnt documented and wery stange setup method
monitor.setLog((msg, info) => {
const { time, event, ...args } = info
info.display = false
logger.debug(event, args)
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.