Repository: envoyproxy/envoy
File: source/common/router/router.cc (C++)

Code before:
      put_result_code = grpc_to_http_status;
    }
  } else {
    // Check cluster's http_protocol_options if different code should be reported to
    // outlier detector.
    absl::optional<bool> matched = cluster_->processHttpForOutlierDetection(*headers);
    if (matched.has_value()) {
      // Outlier detector distinguishes only two values:
      // Anything >= 500 is error.
      // Anything < 500 is success.
      if (!matched.value()) {
        // Matcher returned non-match.
        // report success to outlier detector.
        put_result_code = 200;
      } else {
        // Matcher returned match (treat the response as error).
        // If the original status code was error (>= 500), then report the
        // original status code to the outlier detector.
        if (response_code < 500) {
          put_result_code = 500;
        }
      }
    }
  }

  maybeProcessOrcaLoadReport(*headers, upstream_request);

  upstream_request.upstreamHost()->outlierDetector().putResult(
      put_result_code >= 500 ? Upstream::Outlier::Result::ExtOriginRequestFailed
                             : Upstream::Outlier::Result::ExtOriginRequestSuccess,
      put_result_code);

  if (headers->EnvoyImmediateHealthCheckFail() != nullptr) {
    upstream_request.upstreamHost()->healthChecker().setUnhealthy(
        Upstream::HealthCheckHostMonitor::UnhealthyType::ImmediateHealthCheckFail);
  }

  bool could_not_retry = false;

  // Check if this upstream request was already retried, for instance after
  // hitting a per try timeout. Don't retry it if we already have.
  if (retry_state_) {
    if (upstream_request.retried()) {
      // We already retried this request (presumably for a per try timeout) so
      // we definitely won't retry it again. Check if we would have retried it
      // if we could.
      bool retry_as_early_data; // Not going to be used as we are not retrying.
      could_not_retry = retry_state_->wouldRetryFromHeaders(*headers, *downstream_headers_,
                                                            retry_as_early_data) !=
                        RetryState::RetryDecision::NoRetry;
    } else {

Review comment (refactor):
nit; can you move this line about the ORCA report to after we do all this outlier detection jazz? i.e move it 4-5 lines later.

Code after:
    }
  }

  ASSERT(final_upstream_request);
  // Now put the final request back on this list.
  LinkedList::moveIntoList(std::move(final_upstream_request), upstream_requests_);
}

void Filter::onUpstreamHeaders(uint64_t response_code, Http::ResponseHeaderMapPtr&& headers,
                               UpstreamRequest& upstream_request, bool end_stream) {
  ENVOY_STREAM_LOG(debug, "upstream headers complete: end_stream={}", *callbacks_, end_stream);

  ASSERT(!host_selection_cancelable_);

  // When grpc-status appears in response headers, convert grpc-status to HTTP status code
  // for outlier detection. This does not currently change any stats or logging and does not
  // handle the case when an error grpc-status is sent as a trailer.
  absl::optional<Grpc::Status::GrpcStatus> grpc_status;
  uint64_t grpc_to_http_status = 0;
  uint64_t response_code_for_outlier_detection = response_code;
  if (grpc_request_) {
    grpc_status = Grpc::Common::getGrpcStatus(*headers);
    if (grpc_status.has_value()) {
      grpc_to_http_status = Grpc::Utility::grpcToHttpStatus(grpc_status.value());
      response_code_for_outlier_detection = grpc_to_http_status;
    }
  } else {
    // Check cluster's http_protocol_options if different code should be reported to
    // outlier detector.
    absl::optional<bool> matched = cluster_->processHttpForOutlierDetection(*headers);
    if (matched.has_value()) {
      // Outlier detector distinguishes only two values:
      // Anything >= 500 is error.
      // Anything < 500 is success.
      if (!matched.value()) {
        // Matcher returned non-match.
        // report success to outlier detector.
        response_code_for_outlier_detection = 200;
      } else {
        // Matcher returned match (treat the response as error).
        // If the original status code was error (>= 500), then report the
        // original status code to the outlier detector.
        if (response_code < 500) {
          response_code_for_outlier_detection = 500;
        }
      }
    }
  }

  upstream_request.upstreamHost()->outlierDetector().putResult(
      response_code_for_outlier_detection >= 500