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

Code before:

                // Add the metric to the load metrics map.
                LoadMetricStats::Stat& stat = load_metrics[metric_name];
                stat.num_requests_with_metric += metric_value.num_requests_with_metric;
                stat.total_metric_value += metric_value.total_metric_value;

                // If we are reporting endpoint granularity, add the metric to the upstream endpoint
                // stats.
                if (upstream_endpoint_stats != nullptr) {
                  auto* endpoint_load_metric = upstream_endpoint_stats->add_load_metric_stats();
                  endpoint_load_metric->set_metric_name(metric_name);
                  endpoint_load_metric->set_num_requests_finished_with_metric(
                      metric_value.num_requests_with_metric);
                  endpoint_load_metric->set_total_metric_value(metric_value.total_metric_value);
                }
              }
            }
          }
        }

        bool should_send_locality_stats = rq_issued != 0;
        if (Runtime::runtimeFeatureEnabled("envoy.reloadable_features."
                                           "report_load_when_rq_active_is_non_zero")) {
          // If rq_active is non-zero, we should send the locality stats even if
          // rq_issued is zero(no requests have been issued in this poll
          // window), some load balancer may need rq_active for load balancing.
          should_send_locality_stats = should_send_locality_stats || (rq_active != 0);
        }

        if (should_send_locality_stats) {
          locality_stats.set_total_successful_requests(rq_success);
          locality_stats.set_total_error_requests(rq_error);
          locality_stats.set_total_requests_in_progress(rq_active);
          locality_stats.set_total_issued_requests(rq_issued);
          for (const auto& metric : load_metrics) {
            auto* load_metric_stats = locality_stats.add_load_metric_stats();
            load_metric_stats->set_metric_name(metric.first);
            load_metric_stats->set_num_requests_finished_with_metric(
                metric.second.num_requests_with_metric);
            load_metric_stats->set_total_metric_value(metric.second.total_metric_value);
          }
          cluster_stats->add_upstream_locality_stats()->MergeFrom(locality_stats);
        }
      }
    }
    cluster_stats->set_total_dropped_requests(
        cluster.info()->loadReportStats().upstream_rq_dropped_.latch());
    const uint64_t drop_overload_count =
        cluster.info()->loadReportStats().upstream_rq_drop_overload_.latch();
    if (drop_overload_count > 0) {
      auto* dropped_request = cluster_stats->add_dropped_requests();

Review comment (suggestion):
```suggestion
          // rq_issued is zero (no new requests have been issued in this poll
          // window). This is needed to report long-lived connections/requests (e.g., when web-sockets are used).
```

Code after:

                // Add the metric to the load metrics map.
                LoadMetricStats::Stat& stat = load_metrics[metric_name];
                stat.num_requests_with_metric += metric_value.num_requests_with_metric;
                stat.total_metric_value += metric_value.total_metric_value;

                // If we are reporting endpoint granularity, add the metric to the upstream endpoint
                // stats.
                if (upstream_endpoint_stats != nullptr) {
                  auto* endpoint_load_metric = upstream_endpoint_stats->add_load_metric_stats();
                  endpoint_load_metric->set_metric_name(metric_name);
                  endpoint_load_metric->set_num_requests_finished_with_metric(
                      metric_value.num_requests_with_metric);
                  endpoint_load_metric->set_total_metric_value(metric_value.total_metric_value);
                }
              }
            }
          }
        }

        bool should_send_locality_stats = rq_issued != 0;
        if (Runtime::runtimeFeatureEnabled("envoy.reloadable_features."
                                           "report_load_when_rq_active_is_non_zero")) {
          // If rq_active is non-zero, we should send the locality stats even if
          // rq_issued is zero (no new requests have been issued in this poll
          // window). This is needed to report long-lived connections/requests (e.g., when
          // web-sockets are used).
          should_send_locality_stats = should_send_locality_stats || (rq_active != 0);
        }

        if (should_send_locality_stats) {
          locality_stats.set_total_successful_requests(rq_success);
          locality_stats.set_total_error_requests(rq_error);
          locality_stats.set_total_requests_in_progress(rq_active);
          locality_stats.set_total_issued_requests(rq_issued);
          for (const auto& metric : load_metrics) {
            auto* load_metric_stats = locality_stats.add_load_metric_stats();
            load_metric_stats->set_metric_name(metric.first);
            load_metric_stats->set_num_requests_finished_with_metric(
                metric.second.num_requests_with_metric);
            load_metric_stats->set_total_metric_value(metric.second.total_metric_value);
          }
          cluster_stats->add_upstream_locality_stats()->MergeFrom(locality_stats);
        }
      }
    }
    cluster_stats->set_total_dropped_requests(
        cluster.info()->loadReportStats().upstream_rq_dropped_.latch());
    const uint64_t drop_overload_count =
        cluster.info()->loadReportStats().upstream_rq_drop_overload_.latch();
    if (drop_overload_count > 0) {