Skip to content

Commit 663bd8f

Browse files
committed
addressed comments from @wbpcode
Signed-off-by: Rohit Agrawal <[email protected]>
1 parent edc0348 commit 663bd8f

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

source/extensions/filters/http/header_to_metadata/header_to_metadata_filter.cc

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,45 @@ HeaderToMetadataFilterStats Config::generateStats(const std::string& stat_prefix
170170
return {ALL_HEADER_TO_METADATA_FILTER_STATS(POOL_COUNTER_PREFIX(scope, final_prefix))};
171171
}
172172

173+
void Config::chargeStat(StatsEvent event, HeaderDirection direction) const {
174+
if (!stats_.has_value()) {
175+
return;
176+
}
177+
178+
switch (event) {
179+
case StatsEvent::RulesProcessed:
180+
if (direction == HeaderDirection::Request) {
181+
stats_->request_rules_processed_.inc();
182+
} else {
183+
stats_->response_rules_processed_.inc();
184+
}
185+
break;
186+
case StatsEvent::MetadataAdded:
187+
if (direction == HeaderDirection::Request) {
188+
stats_->request_metadata_added_.inc();
189+
} else {
190+
stats_->response_metadata_added_.inc();
191+
}
192+
break;
193+
case StatsEvent::HeaderNotFound:
194+
if (direction == HeaderDirection::Request) {
195+
stats_->request_header_not_found_.inc();
196+
} else {
197+
stats_->response_header_not_found_.inc();
198+
}
199+
break;
200+
case StatsEvent::Base64DecodeFailed:
201+
stats_->base64_decode_failed_.inc();
202+
break;
203+
case StatsEvent::HeaderValueTooLong:
204+
stats_->header_value_too_long_.inc();
205+
break;
206+
case StatsEvent::RegexSubstitutionFailed:
207+
stats_->regex_substitution_failed_.inc();
208+
break;
209+
}
210+
}
211+
173212
HeaderToMetadataFilter::HeaderToMetadataFilter(const ConfigSharedPtr config) : config_(config) {}
174213

175214
HeaderToMetadataFilter::~HeaderToMetadataFilter() = default;
@@ -216,19 +255,15 @@ bool HeaderToMetadataFilter::addMetadata(StructMap& struct_map, const std::strin
216255
if (value.size() >= MAX_HEADER_VALUE_LEN) {
217256
// Too long, go away.
218257
ENVOY_LOG(debug, "metadata value is too long");
219-
if (config->stats().has_value()) {
220-
config->stats().value().header_value_too_long_.inc();
221-
}
258+
config->chargeStat(StatsEvent::HeaderValueTooLong, direction);
222259
return false;
223260
}
224261

225262
if (encode == envoy::extensions::filters::http::header_to_metadata::v3::Config::BASE64) {
226263
value = Base64::decodeWithoutPadding(value);
227264
if (value.empty()) {
228265
ENVOY_LOG(debug, "Base64 decode failed");
229-
if (config->stats().has_value()) {
230-
config->stats().value().base64_decode_failed_.inc();
231-
}
266+
config->chargeStat(StatsEvent::Base64DecodeFailed, direction);
232267
return false;
233268
}
234269
}
@@ -262,13 +297,7 @@ bool HeaderToMetadataFilter::addMetadata(StructMap& struct_map, const std::strin
262297
(*keyval.mutable_fields())[key] = std::move(val);
263298

264299
// Increment metadata_added stat if stats are enabled.
265-
if (config->stats().has_value()) {
266-
if (direction == HeaderDirection::Request) {
267-
config->stats().value().request_metadata_added_.inc();
268-
} else {
269-
config->stats().value().response_metadata_added_.inc();
270-
}
271-
}
300+
config->chargeStat(StatsEvent::MetadataAdded, direction);
272301

273302
return true;
274303
}
@@ -293,9 +322,7 @@ void HeaderToMetadataFilter::applyKeyValue(std::string&& value, const Rule& rule
293322
// If we had a non-empty input but got an empty result from regex, it could indicate a
294323
// failure.
295324
if (!original_value.empty() && value.empty()) {
296-
if (config->stats().has_value()) {
297-
config->stats().value().regex_substitution_failed_.inc();
298-
}
325+
config->chargeStat(StatsEvent::RegexSubstitutionFailed, direction);
299326
}
300327
}
301328
}
@@ -319,26 +346,14 @@ void HeaderToMetadataFilter::writeHeaderToMetadata(Http::HeaderMap& headers,
319346
absl::optional<std::string> value = rule.selector_->extract(headers);
320347

321348
// Increment rules_processed stat if stats are enabled.
322-
if (config->stats().has_value()) {
323-
if (direction == HeaderDirection::Request) {
324-
config->stats().value().request_rules_processed_.inc();
325-
} else {
326-
config->stats().value().response_rules_processed_.inc();
327-
}
328-
}
349+
config->chargeStat(StatsEvent::RulesProcessed, direction);
329350

330351
if (value && proto_rule.has_on_header_present()) {
331352
applyKeyValue(std::move(value).value_or(""), rule, proto_rule.on_header_present(),
332353
structs_by_namespace, direction);
333354
} else if (!value && proto_rule.has_on_header_missing()) {
334355
// Increment header_not_found stat if stats are enabled.
335-
if (config->stats().has_value()) {
336-
if (direction == HeaderDirection::Request) {
337-
config->stats().value().request_header_not_found_.inc();
338-
} else {
339-
config->stats().value().response_header_not_found_.inc();
340-
}
341-
}
356+
config->chargeStat(StatsEvent::HeaderNotFound, direction);
342357
applyKeyValue(std::move(value).value_or(""), rule, proto_rule.on_header_missing(),
343358
structs_by_namespace, direction);
344359
}

source/extensions/filters/http/header_to_metadata/header_to_metadata_filter.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ using KeyValuePair = envoy::extensions::filters::http::header_to_metadata::v3::C
4848
*/
4949
enum class HeaderDirection { Request, Response };
5050

51+
/**
52+
* Enum of all discrete events for which the filter records statistics.
53+
*/
54+
enum class StatsEvent {
55+
RulesProcessed,
56+
MetadataAdded,
57+
HeaderNotFound,
58+
Base64DecodeFailed,
59+
HeaderValueTooLong,
60+
RegexSubstitutionFailed,
61+
};
62+
5163
// Interface for getting values from a cookie or a header.
5264
class ValueSelector {
5365
public:
@@ -132,6 +144,12 @@ class Config : public ::Envoy::Router::RouteSpecificFilterConfig,
132144
bool doRequest() const { return request_set_; }
133145
const absl::optional<HeaderToMetadataFilterStats>& stats() const { return stats_; }
134146

147+
/**
148+
* Increment the appropriate statistic for the given event and traffic direction.
149+
* No-op if statistics were not configured.
150+
*/
151+
void chargeStat(StatsEvent event, HeaderDirection direction) const;
152+
135153
private:
136154
using ProtobufRepeatedRule = Protobuf::RepeatedPtrField<ProtoRule>;
137155

@@ -168,7 +186,8 @@ class Config : public ::Envoy::Router::RouteSpecificFilterConfig,
168186
HeaderToMetadataRules response_rules_;
169187
bool response_set_;
170188
bool request_set_;
171-
absl::optional<HeaderToMetadataFilterStats> stats_;
189+
// Mutable to allow stats charging from const contexts.
190+
mutable absl::optional<HeaderToMetadataFilterStats> stats_;
172191
};
173192

174193
using ConfigSharedPtr = std::shared_ptr<Config>;

0 commit comments

Comments
 (0)