Repository: envoyproxy/envoy
File: source/extensions/filters/http/ext_authz/ext_authz.cc (C++)

Code before:
    }
    return filter_return_ == FilterReturn::StopDecoding ? Http::FilterTrailersStatus::StopIteration
                                                        : Http::FilterTrailersStatus::Continue;
  }

  return Http::FilterTrailersStatus::Continue;
}

Http::Filter1xxHeadersStatus Filter::encode1xxHeaders(Http::ResponseHeaderMap&) {
  return Http::Filter1xxHeadersStatus::Continue;
}

Http::FilterHeadersStatus Filter::encodeHeaders(Http::ResponseHeaderMap& headers, bool) {
  ENVOY_STREAM_LOG(trace,
                   "ext_authz filter has {} response header(s) to add and {} response header(s) to "
                   "set to the encoded response:",
                   *encoder_callbacks_, response_headers_to_add_.size(),
                   response_headers_to_set_.size());
  bool omitted_response_headers = false;
  if (!response_headers_to_add_.empty()) {
    ENVOY_STREAM_LOG(
        trace, "ext_authz filter added header(s) to the encoded response:", *encoder_callbacks_);
    for (const auto& [key, value] : response_headers_to_add_) {
      if (headers.size() >= headers.maxHeadersCount()) {
        omitted_response_headers = true;
        ENVOY_STREAM_LOG(warn, "Cannot add new response header, header map is full.",
                         *encoder_callbacks_);
        break;
      }
      ENVOY_STREAM_LOG(trace, "'{}':'{}'", *encoder_callbacks_, key.get(), value);
      headers.addCopy(key, value);
    }
  }

  if (!response_headers_to_set_.empty()) {
    ENVOY_STREAM_LOG(
        trace, "ext_authz filter set header(s) to the encoded response:", *encoder_callbacks_);
    for (const auto& [key, value] : response_headers_to_set_) {
      if (headers.get(key).empty() && headers.size() >= headers.maxHeadersCount()) {
        omitted_response_headers = true;
        ENVOY_STREAM_LOG(warn, "Cannot add new response header, header map is full.",
                         *encoder_callbacks_);
        // Continue because there could be other existing headers that can be set without increasing
        // the number of headers.
        continue;
      }
      ENVOY_STREAM_LOG(trace, "'{}':'{}'", *encoder_callbacks_, key.get(), value);
      headers.setCopy(key, value);
    }
  }


Review comment (suggestion):
Use one of the logging variants that logs every n instances or time period; this could result in too many logs.

Code after:
    }
    return filter_return_ == FilterReturn::StopDecoding ? Http::FilterTrailersStatus::StopIteration
                                                        : Http::FilterTrailersStatus::Continue;
  }

  return Http::FilterTrailersStatus::Continue;
}

Http::Filter1xxHeadersStatus Filter::encode1xxHeaders(Http::ResponseHeaderMap&) {
  return Http::Filter1xxHeadersStatus::Continue;
}

Http::FilterHeadersStatus Filter::encodeHeaders(Http::ResponseHeaderMap& headers, bool) {
  ENVOY_STREAM_LOG(trace,
                   "ext_authz filter has {} response header(s) to add and {} response header(s) to "
                   "set to the encoded response:",
                   *encoder_callbacks_, response_headers_to_add_.size(),
                   response_headers_to_set_.size());
  bool omitted_response_headers = false;
  if (!response_headers_to_add_.empty()) {
    ENVOY_STREAM_LOG(
        trace, "ext_authz filter added header(s) to the encoded response:", *encoder_callbacks_);
    for (const auto& [key, value] : response_headers_to_add_) {
      if (headers.size() >= headers.maxHeadersCount()) {
        omitted_response_headers = true;
        break;
      }
      ENVOY_STREAM_LOG(trace, "'{}':'{}'", *encoder_callbacks_, key.get(), value);
      headers.addCopy(key, value);
    }
  }

  if (!response_headers_to_set_.empty()) {
    ENVOY_STREAM_LOG(
        trace, "ext_authz filter set header(s) to the encoded response:", *encoder_callbacks_);
    for (const auto& [key, value] : response_headers_to_set_) {
      if (headers.get(key).empty() && headers.size() >= headers.maxHeadersCount()) {
        omitted_response_headers = true;
        // Continue because there could be other existing headers that can be set without increasing
        // the number of headers.
        continue;
      }
      ENVOY_STREAM_LOG(trace, "'{}':'{}'", *encoder_callbacks_, key.get(), value);
      headers.setCopy(key, value);
    }
  }

  if (!response_headers_to_add_if_absent_.empty()) {
    for (const auto& [key, value] : response_headers_to_add_if_absent_) {
      ENVOY_STREAM_LOG(trace, "'{}':'{}'", *encoder_callbacks_, key.get(), value);
      if (auto header_entry = headers.get(key); header_entry.empty()) {