Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Avantol13
Copy link
Contributor

New Features

Breaking Changes

Bug Fixes

Improvements

  • Improved logging for GraphQL queries (include timing)

Dependency updates

Deployment changes

log.error(`[ES.query] error during querying: ${err.message}`);
throw new Error(err.message);
});
var end = new Date().getTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.client.search is async so this end time won't reflect when the request ends, just when it was sent.

you'll want to add a call to finally after the .then and add this and the code below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll fix it. Is it obvious I don't with Javascript very often? 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like this

var searchResult = this.client.search({
  index: esIndex,
  body: validatedQueryBody,
}).then((resp) => resp.body, (err) => {
  log.error(`[ES.query] error during querying: ${err.message}`);
  throw new Error(err.message);
}).finally(() => {
   var end = new Date().getTime();
   var durationInMS = end - start;

   log.info('[ES.query] DurationInMS:' + durationInMS + '. index, type, query body: ', esIndex, esType, JSON.stringify(validatedQueryBody));
   return searchResult;
})

and no worries i almost commented on the log.info asking you to use f strings before realizing this was javascript

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also if you really want to squeeze performance (and look like a JS pro), use Date.now() instead of new Date().getTime()

they're functionally the same though

var durationInMS = end - start;

log.info('[ES.query] DurationInMS:' + durationInMS + '. index, type, query body: ', esIndex, esType, JSON.stringify(validatedQueryBody));
return searchResult;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't return from the finally - you can actually get rid of searchResult assignment and return this.client.search(...) like it was before. This function needs to return the Promise for the async call so callers of the functions can do their own async response handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll push and then I'm going to test in my dev env

AlbertSnows
AlbertSnows previously approved these changes Mar 26, 2024
@Avantol13 Avantol13 merged commit 182ddee into master Mar 26, 2024
@Avantol13 Avantol13 deleted the chore/logging branch March 26, 2024 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants