diff --git a/docs/root/version_history/current.rst b/docs/root/version_history/current.rst index 5261f906b4ec3..bca7e1b937c09 100644 --- a/docs/root/version_history/current.rst +++ b/docs/root/version_history/current.rst @@ -32,6 +32,7 @@ Bug Fixes --------- *Changes expected to improve the state of the world and are unlikely to have negative effects* +* access log: fix `%UPSTREAM_CLUSTER%` when used in http upstream access logs. Previously, it was always logging as an unset value. * xray: fix the AWS X-Ray tracer bug where span's error, fault and throttle information was not reported properly as per the `AWS X-Ray documentation `_. Before this fix, server error was reported under 'annotations' section of the segment data. Removed Config or Runtime diff --git a/source/common/router/upstream_request.cc b/source/common/router/upstream_request.cc index 3dd9cf0394129..03efc074b9a96 100644 --- a/source/common/router/upstream_request.cc +++ b/source/common/router/upstream_request.cc @@ -62,6 +62,11 @@ UpstreamRequest::UpstreamRequest(RouterFilterInterface& parent, } stream_info_.healthCheck(parent_.callbacks()->streamInfo().healthCheck()); + absl::optional cluster_info = + parent_.callbacks()->streamInfo().upstreamClusterInfo(); + if (cluster_info.has_value()) { + stream_info_.setUpstreamClusterInfo(*cluster_info); + } } UpstreamRequest::~UpstreamRequest() { diff --git a/test/common/router/router_upstream_log_test.cc b/test/common/router/router_upstream_log_test.cc index e9d2260504d90..6831f32dd3963 100644 --- a/test/common/router/router_upstream_log_test.cc +++ b/test/common/router/router_upstream_log_test.cc @@ -83,6 +83,12 @@ class RouterUpstreamLogTest : public testing::Test { public: void init(absl::optional upstream_log) { envoy::extensions::filters::http::router::v3::Router router_proto; + static const std::string cluster_name = "cluster_0"; + + cluster_info_ = std::make_shared>(); + ON_CALL(*cluster_info_, name()).WillByDefault(ReturnRef(cluster_name)); + ON_CALL(*cluster_info_, observabilityName()).WillByDefault(ReturnRef(cluster_name)); + ON_CALL(callbacks_.stream_info_, upstreamClusterInfo()).WillByDefault(Return(cluster_info_)); if (upstream_log) { ON_CALL(*context_.access_log_manager_.file_, write(_)) @@ -160,6 +166,7 @@ class RouterUpstreamLogTest : public testing::Test { EXPECT_CALL(context_.cluster_manager_.thread_local_cluster_.conn_pool_.host_->outlier_detector_, putHttpResponseCode(response_code)); + // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage) response_decoder->decodeHeaders(std::move(response_headers), false); Http::ResponseTrailerMapPtr response_trailers( @@ -245,6 +252,7 @@ class RouterUpstreamLogTest : public testing::Test { NiceMock callbacks_; std::shared_ptr config_; std::shared_ptr router_; + std::shared_ptr> cluster_info_; NiceMock stream_info_; }; @@ -365,5 +373,27 @@ name: accesslog EXPECT_EQ(output_.front(), "110 49 41"); } +// Test UPSTREAM_CLUSTER log formatter. +TEST_F(RouterUpstreamLogTest, UpstreamCluster) { + const std::string yaml = R"EOF( +name: accesslog +typed_config: + "@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog + log_format: + text_format_source: + inline_string: "%UPSTREAM_CLUSTER%" + path: "/dev/null" + )EOF"; + + envoy::config::accesslog::v3::AccessLog upstream_log; + TestUtility::loadFromYaml(yaml, upstream_log); + + init(absl::optional(upstream_log)); + run(); + + EXPECT_EQ(output_.size(), 1U); + EXPECT_EQ(output_.front(), "cluster_0"); +} + } // namespace Router } // namespace Envoy