Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(express): fix wrong rpcMetadata.route value with router.use(<midd… #1615

Closed
wants to merge 6 commits into from

Conversation

biuboombiuboom
Copy link

What version of OpenTelemetry are you using?

opentelemetry: 1.4.0
node: 16.14.2

What version of Node are you using?

What did you do?

    const app = express();
    const router = express.Router();
    app.use('/users', router);
    router.get('/:userId', (req, res) => {
      res.status(200).end('user-'+req.params.userId);
    });
    router.use(customMiddleware);
    const bookRouter = express.Router({ mergeParams: true });
    router.use('/:userId/books', bookRouter);
    bookRouter.get('/:bookId', (req, res) => {
      setImmediate(() => {
        res.status(200).end('book-' + req.params.bookId);
      });
    });       

request http://localhost:8080/proxy/v1/bar/1

What did you expect to see?

rpcMetadata.route='/users/:userId/books/:bookId'

What did you see instead?

rpcMetadata.route='/:userId/books/:bookId'

@chigia001
Copy link
Contributor

chigia001 commented Aug 1, 2023

@biuboombiuboom can you try remove this condition in
to see if it still pass all your test?

My bad, different issue.

@codecov
Copy link

codecov bot commented Aug 2, 2023

Codecov Report

Merging #1615 (a66a4a4) into main (154b30b) will not change coverage.
The diff coverage is n/a.

❗ Current head a66a4a4 differs from pull request most recent head 727e729. Consider uploading reports for the commit 727e729 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1615   +/-   ##
=======================================
  Coverage   96.06%   96.06%           
=======================================
  Files          14       14           
  Lines         914      914           
  Branches      199      199           
=======================================
  Hits          878      878           
  Misses         36       36           

Copy link
Contributor

@chigia001 chigia001 left a comment

Choose a reason for hiding this comment

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

I think we should simlify the test setup.

@@ -269,7 +269,7 @@ export class ExpressInstrumentation extends InstrumentationBase<
req.res?.removeListener('finish', onResponseFinish);
span.end();
}
if (!(req.route && arguments[0] instanceof Error)) {
if (!(req.route && arguments[0] instanceof Error) && layerPath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

so we are over pop item right,

If layerPath is undefined, we don't append new record _LAYERS_STORE_PROPERTY. But when next is call, in the current implementation we still pop it.

router.get('/:userId', (req, res) => {
res.status(200).end('user-' + req.params.userId);
});
router.use(customMiddleware);
Copy link
Contributor

Choose a reason for hiding this comment

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

This customMiddleware layer is the one that make we over remove one layer.

@chigia001
Copy link
Contributor

chigia001 commented Aug 3, 2023

@chigia001
Copy link
Contributor

chigia001 commented Aug 3, 2023

@biuboombiuboom I think we can refactor storeLayerPath to make it more strict on push/pop action. It also prevent instrumentation.ts from knowing of this symbol property.

export const storeLayerPath = (request: PatchedRequest, value?: string): {route: string, popLayerPath: () => void} => {
  let requestRouteStack: string[]
  if (Array.isArray(request[_LAYERS_STORE_PROPERTY]) === false) {
    requestRouteStack = [];
    Object.defineProperty(request, _LAYERS_STORE_PROPERTY, {
      enumerable: false,
      value: requestRouteStack,
    });
  } else {
    requestRouteStack = request[_LAYERS_STORE_PROPERTY] as string[]
  }
  if (value !== undefined) {
    requestRouteStack.push(value)
  }

  const stackLength = requestRouteStack.length

  const route = requestRouteStack
          .filter(path => path !== '/' && path !== '/*')
          .join('');
  let alreadyPopStack = false
  return {
    route,
    popLayerPath: () => {
      if (value !== undefined) {
        return
      }
      if (requestRouteStack.length === stackLength && alreadyPopStack === false) {
        requestRouteStack.pop()
        alreadyPopStack = true
      } else {
        diag.error(
          'express instrumentation: pop route stack incorrectly'
        );
      }
    }
  }
};

@biuboombiuboom
Copy link
Author

biuboombiuboom commented Aug 4, 2023

@biuboombiuboom I think we can refactor storeLayerPath to make it more strict on push/pop action. It also prevent instrumentation.ts from knowing of this symbol property.

export const storeLayerPath = (request: PatchedRequest, value?: string): {route: string, popLayerPath: () => void} => {
  let requestRouteStack: string[]
  if (Array.isArray(request[_LAYERS_STORE_PROPERTY]) === false) {
    requestRouteStack = [];
    Object.defineProperty(request, _LAYERS_STORE_PROPERTY, {
      enumerable: false,
      value: requestRouteStack,
    });
  } else {
    requestRouteStack = request[_LAYERS_STORE_PROPERTY] as string[]
  }
  if (value !== undefined) {
    requestRouteStack.push(value)
  }

  const stackLength = requestRouteStack.length

  const route = requestRouteStack
          .filter(path => path !== '/' && path !== '/*')
          .join('');
  let alreadyPopStack = false
  return {
    route,
    popLayerPath: () => {
      if (value !== undefined) {
        return
      }
      if (requestRouteStack.length === stackLength && alreadyPopStack === false) {
        requestRouteStack.pop()
        alreadyPopStack = true
      } else {
        diag.error(
          'express instrumentation: pop route stack incorrectly'
        );
      }
    }
  }
};

greate.
Is there a lack of processing for the ignore layer?

@blumamir
Copy link
Member

@biuboombiuboom Let me know when this is ready for review

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.

fix wrong rpcMetadata.route value when route.use(<middleware>) handle nested router
6 participants