question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request: unique hash on query objects for logging

See original GitHub issue

I’m trying to write a simple metrics tool to measure which queries are performing slowly so that I can evaluate my codebase over time. As a simplified example:

var knex = require('knex')({myConfig});

var runningQueries = {};

knex.on('query', function (data) {
  // Generates a unique has using hash using keys like:
  // ['__knexUid', 'method', 'sql', 'bindings', 'options']
  var queryHash = generateHash(data);
  runningQueries[queryHash] = {
    sql: data.sql,
    start: Date.now()
  };
});

knex.on('query-response', function (response, obj, builder) {
  // This hash should match the one in the function above.
  var queryHash = generateHash(obj);
  var runTime = Date.now() - runningQueries[queryHash].start;
  // etc. etc.
  delete runningQueries[queryHash];
});

The problem I’m encountering is that multiple queries with the same properties will get jumbled together - and could potentially screw up my timings or cause me to miss queries. It would be ideal if each query object had a unique hash attached to it (or the Date.now() timestamp it was created at) to identify them uniquely.

I can add the code, but before I submit a PR I’d like your input and to make sure I’m not wasting your time. 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
funnylookinhatcommented, Jun 17, 2016

@wubzz That’s a good idea. I’ve gone ahead and updated it to __knexQueryUid to fit the __knexUid pattern.

0reactions
E-Kuerschnercommented, Nov 17, 2020

I’m using knex@0.21.2 with mssql

I’ve noticed that some queries don’t receive a __knexQueryUid. Any idea why that might be? It appears that anything using the schema api is missing this, for example:

await connection.schema.dropTableIfExists(tableName)

this type of code is resulting in query event data looking like:

{
  __knexUid: '__knexUid3',
  __knexTxId: undefined,
  sql: "if object_id('***', 'U') is not null DROP TABLE ***",
  bindings: []
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Log request string URL with # (number sign, hash, fragment ...
The problem is that the entire Query String isn't being logged when there's a # (Number Sign) in the URL (Visiting ...
Read more >
A Guide to Consistent Hashing - Toptal
Consistent Hashing is a distributed hashing scheme that operates independently of the number of servers or objects in a distributed hash table.
Read more >
Build queries by using the Logging query language
This document describes how to retrieve and analyze logs when you use the Logs Explorer. You retrieve logs by writing and executing queries....
Read more >
15.10 - Finding Uneven Distribution Using Hash Functions
Finding Uneven Distribution Using Hash Functions Use the following functions to identify uneven hash distribution of data.
Read more >
sys.dm_exec_requests (Transact-SQL) - SQL Server
Column name Data type Description blocking_session_id smallint context_info varbinary(128) CONTEXT_INFO value of the session. Is nullable. estimated_completion_time bigint Internal only. Isn't nullable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found