diff --git a/docs/root/intro/arch_overview/security/rbac_filter.rst b/docs/root/intro/arch_overview/security/rbac_filter.rst index 96685df1ac072..e6b2c9d9e2889 100644 --- a/docs/root/intro/arch_overview/security/rbac_filter.rst +++ b/docs/root/intro/arch_overview/security/rbac_filter.rst @@ -81,6 +81,7 @@ The following attributes are exposed to the language runtime: response.headers, string map, All response headers response.trailers, string map, All response trailers response.size, int, Size of the response body + response.total_size, int, Total size of the response including the approximate uncompressed size of the headers and the trailers response.flags, int, Additional details about the response beyond the standard response code source.address, string, Downstream connection remote address source.port, int, Downstream connection remote port diff --git a/source/extensions/filters/common/expr/context.cc b/source/extensions/filters/common/expr/context.cc index a6eaf8a459ca4..8477662815e07 100644 --- a/source/extensions/filters/common/expr/context.cc +++ b/source/extensions/filters/common/expr/context.cc @@ -131,6 +131,9 @@ absl::optional ResponseWrapper::operator[](CelValue key) const { return CelValue::CreateMap(&trailers_); } else if (value == Flags) { return CelValue::CreateInt64(info_.responseFlags()); + } else if (value == TotalSize) { + return CelValue::CreateInt64(info_.bytesSent() + headers_.value_->byteSize().value() + + trailers_.value_->byteSize().value()); } return {}; } diff --git a/source/extensions/filters/common/expr/context.h b/source/extensions/filters/common/expr/context.h index 0f79f197e6770..77bf9cab802c4 100644 --- a/source/extensions/filters/common/expr/context.h +++ b/source/extensions/filters/common/expr/context.h @@ -76,6 +76,7 @@ class HeadersWrapper : public google::api::expr::runtime::CelMap { private: friend class RequestWrapper; + friend class ResponseWrapper; const Http::HeaderMap* value_; }; diff --git a/test/extensions/filters/common/expr/context_test.cc b/test/extensions/filters/common/expr/context_test.cc index ce4217cc58fbd..db5850efd65f0 100644 --- a/test/extensions/filters/common/expr/context_test.cc +++ b/test/extensions/filters/common/expr/context_test.cc @@ -214,6 +214,14 @@ TEST(Context, ResponseAttributes) { EXPECT_EQ(123, value.value().Int64OrDie()); } + { + auto value = response[CelValue::CreateString(TotalSize)]; + EXPECT_TRUE(value.has_value()); + ASSERT_TRUE(value.value().IsInt64()); + EXPECT_EQ(148, value.value().Int64OrDie()); + } + + { auto value = response[CelValue::CreateString(Code)]; EXPECT_TRUE(value.has_value());