Skip to content

Conversation

megatnt1122
Copy link
Collaborator

@megatnt1122 megatnt1122 commented Sep 11, 2025

Ticket

#1522

Description

First pass of logging improvements within user router

Tasks

  • - A description of the PR has been provided, and a diagram included if it is a new feature.
  • - Formatter has been run
  • - CHANGELOG comment has been added
  • - Labels have been assigned to the pr
  • - A reviwer has been added
  • - A user has been assigned to work on the pr
  • - If new feature a unit test has been added

Summary by Sourcery

Instrument user router endpoints with structured logging to capture request lifecycle events and contextual metadata

Enhancements:

  • Add start, success and failure logs for each user router route
  • Include client ID, correlation ID, HTTP method, route name, key parameters and error details in logs across authentication, user management, token, key and identifier operations

Copy link
Contributor

sourcery-ai bot commented Sep 11, 2025

Reviewer's Guide

This PR enhances the user router by introducing consistent, structured logging around each endpoint’s execution (start, success, failure) and refactors variable declarations to ensure contextual data is available in error handlers.

Sequence diagram for enhanced logging in user router endpoints

sequenceDiagram
    participant Client
    participant "User Router"
    participant Logger
    participant "Database/Libs"
    Client->>"User Router": HTTP GET /authn/password (or other endpoint)
    "User Router"->>Logger: Log Start (with client, correlation ID, route, etc.)
    "User Router"->>"Database/Libs": Perform endpoint logic (e.g., authenticate, query, update)
    alt Success
        "User Router"->>Logger: Log Success (with client, correlation ID, route, etc.)
        "User Router"->>Client: Send response
    else Failure
        "User Router"->>Logger: Log Failure (with client, correlation ID, route, error, stack)
        "User Router"->>Client: Send error response
    end
Loading

File-Level Changes

Change Details Files
Introduced structured logging across all user_router endpoints
  • Added console.info at the start of each route handler
  • Logged console.info on successful completion of each endpoint
  • Logged console.info or console.error on failure including error message and stack trace with contextual info
core/database/foxx/api/user_router.js
Refactored variable scope to support logging context
  • Declared client/user variables with let outside try blocks
  • Replaced inline var/const declarations with assignments to preserve values in catch blocks
core/database/foxx/api/user_router.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@megatnt1122 megatnt1122 self-assigned this Sep 11, 2025
@megatnt1122 megatnt1122 added Component: Database Relates to database microservice / data model Type: Refactor Imlplementation change, same functionality Priority: Low Lower priority work. labels Sep 11, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `core/database/foxx/api/user_router.js:271` </location>
<code_context>
+                        req.queryParams.email,
+                        "Options:",
+                        req.queryParams.options,
+                        "uuid:",
+                        req.queryParams.uuids,
+                        "is_admin:",
+                        req.queryParams.is_admin,
</code_context>

<issue_to_address>
Potential typo: 'uuid' vs 'uuids' in logging statements.

Update log statements to use 'uuids:' for consistency with the parameter name and to prevent confusion.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
                        "uuid:",
                        req.queryParams.uuids,
=======
                        "uuids:",
                        req.queryParams.uuids,
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `core/database/foxx/api/user_router.js:1957` </location>
<code_context>
 router
     .get("/list/all", function (req, res) {
+        let client = undefined;
         var qry = "for i in u sort i.name_last, i.name_first";
         var result;
+        console.info(
</code_context>

<issue_to_address>
Logging uses 'client' before it is assigned.

Assign 'client' before logging, or remove it from the log if not needed, to avoid logging 'unknown'.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@AronPerez AronPerez left a comment

Choose a reason for hiding this comment

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

Good initiative on improving observability 💪

Consider using middleware to wrap all router requests at a higher level ⛰️ - this would eliminate the repetitive logging code and make maintenance much easier

Two ideas for approaches:

  1. General middlewear [1]
  2. Router specific middlewear [1, 2]
Example util funciton

  // In index.js - handles 90% of your logging needs
  router.use((req, res, next) => {
      const start = Date.now();
      console.info(JSON.stringify({
          event: "request_start",
          ...extractContext(req)
      }));
      res.on("finish", () => {
          console.info(JSON.stringify({
              event: "request_end",
              duration: Date.now() - start,
              status: res.statusCode
          }));
      });
      next();
  });

This could reduce the PR from 2000+ lines to ~50. Let me know if you have any questions

Copy link
Collaborator

@JoshuaSBrown JoshuaSBrown left a comment

Choose a reason for hiding this comment

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

  1. Change to using null
  2. Fix the spacing here: blic key|Extra: unknown |Err: unknown |Stack: unkn
  3. Don't print access tokens or passwords

@megatnt1122 megatnt1122 force-pushed the refactor-DAPS-1522-User-Router-Logging-Improvements branch from ee33cc1 to d8ff808 Compare September 25, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Database Relates to database microservice / data model Priority: Low Lower priority work. Type: Refactor Imlplementation change, same functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants