@@ -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+
173212HeaderToMetadataFilter::HeaderToMetadataFilter (const ConfigSharedPtr config) : config_(config) {}
174213
175214HeaderToMetadataFilter::~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 }
0 commit comments