Skip to content

route: keep the matched virtual host in the stream info #40065

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

Merged
merged 15 commits into from
Jul 8, 2025

Conversation

wbpcode
Copy link
Member

@wbpcode wbpcode commented Jun 29, 2025

Commit Message: route: keep the matched virtual host in the stream info
Additional Description:

Now the virtual host will also be kept in the stream info. This is useful if there is a partial match. Then although the route is not found, we still could log some virtual host related metadata in the log.

To close #39391

Risk Level: low. The core code change is very minor.
Testing: unit/intergation.
Docs Changes: n/a.
Release Notes: added.
Platform Specific Features: n/a.

Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
@wbpcode
Copy link
Member Author

wbpcode commented Jun 29, 2025

Hi, @kyessenov , could you give first pass when you get some free time because seem you have some context of #39391. Thanks.

wbpcode added 3 commits June 29, 2025 05:58
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
@wbpcode wbpcode requested a review from mattklein123 as a code owner June 29, 2025 06:19
wbpcode added 5 commits June 29, 2025 15:05
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Copy link
Contributor

@kyessenov kyessenov left a comment

Choose a reason for hiding this comment

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

Some initial thoughts...

@@ -299,6 +299,13 @@ struct StreamInfoImpl : public StreamInfo {
return *downstream_connection_info_provider_;
}

const Router::VHostConstSharedPtr& vhost() const override {
// TODO(wbpcode): There is an edge case where the route and virtual host be override
// by the setRoute() of HTTP filter callbacks. But in that case the virtual host stored
Copy link
Contributor

Choose a reason for hiding this comment

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

That seems problematic? Filters can reprogram a route AFAIR?

Copy link
Member Author

@wbpcode wbpcode Jul 1, 2025

Choose a reason for hiding this comment

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

Basically, there should no developer will override the virtualhost in practices (although they could). This is an edge case that may never happen. So, I left a TODO here.

I actually think we can remove the reference of vhost in the route in the next PR. Then the one who override virtualhost will encounter a compile error. Then he could create a issue to require new solution for virtulhost override if there is an actual scenario.

Copy link
Member Author

Choose a reason for hiding this comment

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

I refactor the implmentation, so we can get the vhost from the route if the setRoute() method is called. This resolve the problem the comment referred.

Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
@wbpcode
Copy link
Member Author

wbpcode commented Jul 1, 2025

/retest

wbpcode added 2 commits July 1, 2025 09:22
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
@wbpcode
Copy link
Member Author

wbpcode commented Jul 2, 2025

/retest

@wbpcode wbpcode requested a review from Copilot July 2, 2025 11:02
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the routing code to preserve and expose the matched virtual host in StreamInfo, even when no route entry is found. Key changes include:

  • Introducing a new vhost() accessor and VHostRoute struct to carry both virtual host and route pointers through routing APIs.
  • Updating all call sites, mocks, and tests to use the new virtualHost()->... and stream_info.vhost().
  • Adjusting METADATA and CEL formatters to pull metadata from vhost() when present, and adding an integration test for logging vhost metadata on unmatched routes.

Reviewed Changes

Copilot reviewed 44 out of 44 changed files in this pull request and generated no comments.

Show a summary per file
File Description
envoy/stream_info/stream_info.h Add pure-virtual vhost() to StreamInfo API
source/common/stream_info/stream_info_impl.h Implement and store vhost_ in stream info
envoy/router/router.h Introduce VHostRoute struct and change Config::route API
source/common/router/* Update internal routing to return and propagate vhost()
source/extensions/formatter/metadata/* Switch metadata formatter to use vhost()
test/* Update dozens of mocks and test assertions to reflect new API
Comments suppressed due to low confidence (3)

test/extensions/formatter/metadata/metadata_test.cc:183

  • [nitpick] Consider adding a unit test for the scenario where the route lookup fails but a virtual host is still matched (i.e. route()==nullptr but vhost()!=nullptr) to ensure the formatter returns the vhost metadata as expected.
TEST_F(MetadataFormatterTest, VirtualHostMetadataNoVirtualHost) {

envoy/router/router.h:1374

  • Add a brief comment above this struct explaining that it carries both the matched virtual host and the route pointer, and that consumers should check .route before dereferencing.
struct VHostRoute {

envoy/stream_info/stream_info.h:850

  • Update the doc comment to note that vhost() may be nullptr if no virtual host was matched, so callers should guard accordingly.
  virtual const Router::VHostConstSharedPtr& vhost() const PURE;

// The method cannot return the vhost_ directly because the vhost_ has different type with
// the VHostConstSharedPtr and will create a temporary copy implicitly and result in error
// of returning reference to local temporary object.
return vhost_copy_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we change signature to be able to store vhost shared ptr in stream info?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I missed this comment. Yeah, this change is necessary to ensure the vhost pointer in the stream info is always correct even when the setRoute is used.

It's possible that the developers re-program the route in a custom filter and override the virtual host. This method ensure we can get a shared point from the route that provided by the custom filter.

kyessenov
kyessenov previously approved these changes Jul 2, 2025
Copy link
Contributor

@kyessenov kyessenov left a comment

Choose a reason for hiding this comment

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

Oooof, this is a quite a large refactor, but I think the interface for stream info / route looks clean, and that's where most of the changes come from.

@wbpcode
Copy link
Member Author

wbpcode commented Jul 2, 2025

/assign-from @envoyproxy/senior-maintainers

Copy link

@envoyproxy/senior-maintainers assignee is @phlax

🐱

Caused by: a #40065 (comment) was created by @wbpcode.

see: more, trace.

@wbpcode
Copy link
Member Author

wbpcode commented Jul 2, 2025

/assign-from @envoyproxy/senior-maintainers

Copy link

@envoyproxy/senior-maintainers assignee is @RyanTheOptimist

🐱

Caused by: a #40065 (comment) was created by @wbpcode.

see: more, trace.

@wbpcode
Copy link
Member Author

wbpcode commented Jul 2, 2025

Oooof, this is a quite a large refactor, but I think the interface for stream info / route looks clean, and that's where most of the changes come from.

Thanks for the review!!! Most changes are test code to adapt to new interfaces. The actual core code changes are limited.

Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
@wbpcode
Copy link
Member Author

wbpcode commented Jul 3, 2025

The latest commit do following changes after @kyessenov 's approval:

  1. rename the VirtualHostSharedPtr (alias of std::shared_ptr<VirtualHostImpl>) -> VirtualHostImplSharedPtr.
  2. rename the VHostConstSharedPtr to VirtualHostConstSharedPtr (use fullname of virtual host in the type per reviewer comment.).
  3. rename the vhost() to virtualHost() per reviewer comment.

wbpcode added 2 commits July 3, 2025 08:38
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
@wbpcode
Copy link
Member Author

wbpcode commented Jul 3, 2025

/retest

@wbpcode wbpcode merged commit 9172ed8 into envoyproxy:main Jul 8, 2025
25 checks passed
@wbpcode wbpcode deleted the dev-keep-virtualhost branch July 8, 2025 03:29
AnirbanNandi pushed a commit to AnirbanNandi/envoy that referenced this pull request Jul 15, 2025
…0065)

Commit Message: route: keep the matched virtual host in the stream info
Additional Description:

Now the virtual host will also be kept in the stream info. This is
useful if there is a partial match. Then although the route is not
found, we still could log some virtual host related metadata in the log.

To close envoyproxy#39391

Risk Level: low. The core code change is very minor.
Testing: unit/intergation.
Docs Changes: n/a.
Release Notes: added.
Platform Specific Features: n/a.

---------

Signed-off-by: wangbaiping(wbpcode) <wbphub@gmail.com>
Signed-off-by: Anirban Nandi <anirbannandi@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VirtualHost Metadata Missing When No Routes Matched
4 participants