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

request_id_extension: Add noop impl by default #10687

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,8 @@ envoy_cc_library(
deps = [
"//include/envoy/http:request_id_extension_interface",
"//include/envoy/server:request_id_extension_config_interface",
"//include/envoy/upstream:upstream_interface",
"//source/common/common:assert_lib",
"//source/common/common:utility_lib",
"//source/common/config:utility_lib",
"//source/common/http:header_map_lib",
"//source/common/http:header_utility_lib",
"//source/common/http:headers_lib",
"//source/common/http:utility_lib",
"//source/common/protobuf:utility_lib",
"//source/common/stream_info:stream_info_lib",
"//source/common/tracing:http_tracer_lib",
"//source/common/runtime:runtime_lib",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
],
)
19 changes: 19 additions & 0 deletions source/common/http/request_id_extension_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
namespace Envoy {
namespace Http {

namespace {

// NoopRequestIDExtension is the implementation used outside of HTTP context.
class NoopRequestIDExtension : public RequestIDExtension {
Copy link
Member

Choose a reason for hiding this comment

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

nit: it would be nice to get coverage of the other methods here in the unit test you added.

public:
void set(RequestHeaderMap&, bool) override {}
void setInResponse(ResponseHeaderMap&, const RequestHeaderMap&) override {}
bool modBy(const RequestHeaderMap&, uint64_t&, uint64_t) override { return false; }
TraceStatus getTraceStatus(const RequestHeaderMap&) override { return TraceStatus::NoTrace; }
void setTraceStatus(RequestHeaderMap&, TraceStatus) override {}
};

} // namespace

RequestIDExtensionSharedPtr RequestIDExtensionFactory::fromProto(
const envoy::extensions::filters::network::http_connection_manager::v3::RequestIDExtension&
config,
Expand All @@ -30,5 +44,10 @@ RequestIDExtensionFactory::defaultInstance(Envoy::Runtime::RandomGenerator& rand
return std::make_shared<UUIDRequestIDExtension>(random);
}

RequestIDExtensionSharedPtr RequestIDExtensionFactory::noopInstance() {
static RequestIDExtensionSharedPtr global = std::make_shared<NoopRequestIDExtension>();
return global;
}

} // namespace Http
} // namespace Envoy
5 changes: 5 additions & 0 deletions source/common/http/request_id_extension_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class RequestIDExtensionFactory {
*/
static RequestIDExtensionSharedPtr defaultInstance(Envoy::Runtime::RandomGenerator& random);

/**
* Return a globally shared instance of the noop RequestIDExtension implementation.
*/
static RequestIDExtensionSharedPtr noopInstance();

/**
* Read a RequestIDExtension definition from proto and create it.
*/
Expand Down
1 change: 1 addition & 0 deletions source/common/stream_info/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ envoy_cc_library(
"//include/envoy/stream_info:stream_info_interface",
"//source/common/common:assert_lib",
"//source/common/common:dump_state_utils",
"//source/common/http:request_id_extension_lib",
"@envoy_api//envoy/config/core/v3:pkg_cc_proto",
],
)
Expand Down
10 changes: 7 additions & 3 deletions source/common/stream_info/stream_info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "common/common/assert.h"
#include "common/common/dump_state_utils.h"
#include "common/http/request_id_extension_impl.h"
#include "common/stream_info/filter_state_impl.h"

namespace Envoy {
Expand All @@ -20,12 +21,14 @@ struct StreamInfoImpl : public StreamInfo {
StreamInfoImpl(TimeSource& time_source)
: time_source_(time_source), start_time_(time_source.systemTime()),
start_time_monotonic_(time_source.monotonicTime()),
filter_state_(std::make_shared<FilterStateImpl>(FilterState::LifeSpan::FilterChain)) {}
filter_state_(std::make_shared<FilterStateImpl>(FilterState::LifeSpan::FilterChain)),
request_id_extension_(Http::RequestIDExtensionFactory::noopInstance()) {}
Copy link
Member

Choose a reason for hiding this comment

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

looking at this, I think these constructors can mostly be collapsed with each one invoking the previous with fewer arguments. This would be a nice follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, will send a follow up pr. Does it need a TODO for the time being?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #10691, will rebase once this one is merged.


StreamInfoImpl(Http::Protocol protocol, TimeSource& time_source)
: time_source_(time_source), start_time_(time_source.systemTime()),
start_time_monotonic_(time_source.monotonicTime()), protocol_(protocol),
filter_state_(std::make_shared<FilterStateImpl>(FilterState::LifeSpan::FilterChain)) {}
filter_state_(std::make_shared<FilterStateImpl>(FilterState::LifeSpan::FilterChain)),
request_id_extension_(Http::RequestIDExtensionFactory::noopInstance()) {}

StreamInfoImpl(Http::Protocol protocol, TimeSource& time_source,
std::shared_ptr<FilterState>& parent_filter_state)
Expand All @@ -34,7 +37,8 @@ struct StreamInfoImpl : public StreamInfo {
filter_state_(std::make_shared<FilterStateImpl>(
FilterStateImpl::LazyCreateAncestor(parent_filter_state,
FilterState::LifeSpan::DownstreamConnection),
FilterState::LifeSpan::FilterChain)) {}
FilterState::LifeSpan::FilterChain)),
request_id_extension_(Http::RequestIDExtensionFactory::noopInstance()) {}

SystemTime startTime() const override { return start_time_; }

Expand Down
18 changes: 18 additions & 0 deletions test/common/stream_info/stream_info_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ TEST_F(StreamInfoImplTest, RequestHeadersTest) {
EXPECT_EQ(&headers, stream_info.getRequestHeaders());
}

TEST_F(StreamInfoImplTest, DefaultRequestIDExtensionTest) {
StreamInfoImpl stream_info(test_time_.timeSystem());
EXPECT_TRUE(stream_info.getRequestIDExtension());

auto rid_extension = stream_info.getRequestIDExtension();

Http::RequestHeaderMapImpl request_headers;
Http::ResponseHeaderMapImpl response_headers;
rid_extension->set(request_headers, false);
rid_extension->set(request_headers, true);
rid_extension->setInResponse(response_headers, request_headers);
uint64_t out = 123;
EXPECT_FALSE(rid_extension->modBy(request_headers, out, 10000));
EXPECT_EQ(out, 123);
rid_extension->setTraceStatus(request_headers, Http::TraceStatus::Forced);
EXPECT_EQ(rid_extension->getTraceStatus(request_headers), Http::TraceStatus::NoTrace);
}

} // namespace
} // namespace StreamInfo
} // namespace Envoy
6 changes: 6 additions & 0 deletions test/integration/tcp_proxy_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ TEST_P(TcpProxyIntegrationTest, AccessLog) {
"upstreamhost=%UPSTREAM_HOST% downstream=%DOWNSTREAM_REMOTE_ADDRESS_WITHOUT_PORT% "
"sent=%BYTES_SENT% received=%BYTES_RECEIVED%\n");
access_log->mutable_typed_config()->PackFrom(access_log_config);
auto* runtime_filter = access_log->mutable_filter()->mutable_runtime_filter();
runtime_filter->set_runtime_key("unused-key");
auto* percent_sampled = runtime_filter->mutable_percent_sampled();
percent_sampled->set_numerator(100);
percent_sampled->set_denominator(
envoy::type::FractionalPercent::DenominatorType::FractionalPercent_DenominatorType_HUNDRED);
config_blob->PackFrom(tcp_proxy_config);
});
initialize();
Expand Down