Skip to content
Snippets Groups Projects
Verified Commit 3fd65b3e authored by Louis's avatar Louis :fire:
Browse files

Make query logging optional

parent 76e35292
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ if (url) { ...@@ -13,6 +13,7 @@ if (url) {
username: connectionUrl.username, username: connectionUrl.username,
password: connectionUrl.password, password: connectionUrl.password,
port: connectionUrl.port, port: connectionUrl.port,
log_queries: env('LOG_SQL_QUERIES', 'true') === 'true',
} }
} else { } else {
module.exports = { module.exports = {
...@@ -28,6 +29,7 @@ if (url) { ...@@ -28,6 +29,7 @@ if (url) {
ca: Buffer.from(env('DATABASE_CA_CERT', null) ?? '', 'base64').toString(), ca: Buffer.from(env('DATABASE_CA_CERT', null) ?? '', 'base64').toString(),
} }
}, },
log_queries: env('LOG_SQL_QUERIES', 'true') === 'true',
} }
} }
......
...@@ -41,28 +41,30 @@ Object.keys(db).forEach(modelName => { ...@@ -41,28 +41,30 @@ Object.keys(db).forEach(modelName => {
} }
}) })
sequelize.addHook('beforeQuery', (model, query) => { if (config('database.log_queries')) {
const trace = threadContext.get('profiling') sequelize.addHook('beforeQuery', (model, query) => {
if (trace) { const trace = threadContext.get('profiling')
const traceId = uuid()
const span = trace.startChild({
op: 'sql.query',
})
threadContext.set(`span_${ traceId }`, span)
query.options.trace = traceId
}
})
sequelize.addHook('afterQuery', (model, query) => {
if (query.options.trace) {
const trace = threadContext.get(`span_${ query.options.trace }`)
if (trace) { if (trace) {
trace.description = query.sql const traceId = uuid()
trace.setData('sql.params', query.options.bind) const span = trace.startChild({
trace.finish() op: 'sql.query',
})
threadContext.set(`span_${ traceId }`, span)
query.options.trace = traceId
} }
} })
})
sequelize.addHook('afterQuery', (model, query) => {
if (query.options.trace) {
const trace = threadContext.get(`span_${ query.options.trace }`)
if (trace) {
trace.description = query.sql
trace.setData('sql.params', query.options.bind)
trace.finish()
}
}
})
}
db.sequelize = sequelize db.sequelize = sequelize
db.Sequelize = Sequelize db.Sequelize = Sequelize
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment