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

Fault injection filter #219

Merged
merged 15 commits into from
Nov 21, 2016
Merged
24 changes: 10 additions & 14 deletions source/common/http/filter/fault_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ FaultFilterConfig::FaultFilterConfig(const Json::Object& json_config, Runtime::L
const std::string& stat_prefix, Stats::Store& stats)
: runtime_(runtime), stats_{ALL_FAULT_FILTER_STATS(POOL_COUNTER_PREFIX(stats, stat_prefix))} {

abort_percent_ = 0UL;
http_status_ = 0UL;
fixed_delay_percent_ = 0UL;
fixed_duration_ms_ = 0UL;

if (json_config.hasObject("abort")) {
const Json::Object& abort = json_config.getObject("abort");
abort_percent_ = static_cast<uint64_t>(abort.getInteger("abort_percent", 0));
Expand Down Expand Up @@ -101,11 +96,7 @@ FilterHeadersStatus FaultFilter::decodeHeaders(HeaderMap& headers, bool) {

if (config_->runtime().snapshot().featureEnabled("fault.http.abort.abort_percent",
config_->abortPercent())) {
// todo check http status codes obtained from runtime
Http::HeaderMapPtr response_headers{new HeaderMapImpl{
{Headers::get().Status, std::to_string(config_->runtime().snapshot().getInteger(
"fault.http.abort.http_status", config_->abortCode()))}}};
callbacks_->encodeHeaders(std::move(response_headers), true);
abortWithHTTPStatus();
config_->stats().aborts_injected_.inc();
Copy link
Member

Choose a reason for hiding this comment

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

should go in abortWithHTTPStatus()

return FilterHeadersStatus::StopIteration;
}
Expand Down Expand Up @@ -133,17 +124,22 @@ void FaultFilter::postDelayInjection() {
// Delays can be followed by aborts
if (config_->runtime().snapshot().featureEnabled("fault.http.abort.abort_percent",
config_->abortPercent())) {
Http::HeaderMapPtr response_headers{new HeaderMapImpl{
{Headers::get().Status, std::to_string(config_->runtime().snapshot().getInteger(
"fault.http.abort.http_status", config_->abortCode()))}}};
abortWithHTTPStatus();
config_->stats().aborts_injected_.inc();
Copy link
Member

Choose a reason for hiding this comment

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

should go in abortWithHTTPStatus()

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I left it out of that function was because in future, we might add abortWithGRPCStatus and abortWithHTTP2Error . Rather than duplicating the stats increment counter in all 3 functions, I thought its better to keep it here. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I would just move it for now. We can deal with the future when it happens. :)

callbacks_->encodeHeaders(std::move(response_headers), true);
} else {
// Continue request processing
callbacks_->continueDecoding();
}
}

void FaultFilter::abortWithHTTPStatus() {
// TODO: check http status codes obtained from runtime
Http::HeaderMapPtr response_headers{new HeaderMapImpl{
{Headers::get().Status, std::to_string(config_->runtime().snapshot().getInteger(
"fault.http.abort.http_status", config_->abortCode()))}}};
callbacks_->encodeHeaders(std::move(response_headers), true);
}

void FaultFilter::resetTimerState() {
if (delay_timer_) {
delay_timer_->disableTimer();
Expand Down
9 changes: 5 additions & 4 deletions source/common/http/filter/fault_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class FaultFilterConfig {
FaultFilterStats& stats() { return stats_; }

private:
uint64_t abort_percent_; // 0-100
uint64_t http_status_; // HTTP or gRPC return codes
uint64_t fixed_delay_percent_; // 0-100
uint64_t fixed_duration_ms_; // in milliseconds
uint64_t abort_percent_{}; // 0-100
uint64_t http_status_{}; // HTTP or gRPC return codes
uint64_t fixed_delay_percent_{}; // 0-100
uint64_t fixed_duration_ms_{}; // in milliseconds
std::vector<Router::ConfigUtility::HeaderData> fault_filter_headers_;
Runtime::Loader& runtime_;
FaultFilterStats stats_;
Expand All @@ -74,6 +74,7 @@ class FaultFilter : public StreamDecoderFilter {
void onResetStream();
void resetTimerState();
void postDelayInjection();
void abortWithHTTPStatus();

FaultFilterConfigPtr config_;
StreamDecoderFilterCallbacks* callbacks_{};
Expand Down