void FillLogEntry()

in src/api_proxy/service_control/request_builder.cc [831:943]


void FillLogEntry(const ReportRequestInfo& info, const std::string& name,
                  const std::string& config_id, const Timestamp& current_time,
                  LogEntry* log_entry) {
  log_entry->set_name(name);
  *log_entry->mutable_timestamp() = current_time;
  auto severity = (get_status_code(info) >= 400) ? google::logging::type::ERROR
                                                 : google::logging::type::INFO;
  log_entry->set_severity(severity);

  // Add trace if available.
  if (!info.trace_id.empty()) {
    if (!info.tracing_project_id.empty()) {
      log_entry->set_trace(
          TraceResourceName(info.trace_id, info.tracing_project_id));
    } else if (!info.gcp_project_id.empty()) {
      log_entry->set_trace(
          TraceResourceName(info.trace_id, info.gcp_project_id));
    }
  }

  // Fill in http request.
  auto* http_request = log_entry->mutable_http_request();
  http_request->set_protocol(protocol::ToString(info.frontend_protocol));
  http_request->set_status(get_status_code(info));
  if (!info.method.empty()) {
    http_request->set_request_method(info.method);
  }
  if (!info.url.empty()) {
    http_request->set_request_url(info.url);
  }
  if (info.request_size >= 0) {
    http_request->set_request_size(info.request_size);
  }
  if (info.response_size >= 0) {
    http_request->set_response_size(info.response_size);
  }
  if (!info.client_ip.empty()) {
    http_request->set_remote_ip(info.client_ip);
  }
  if (!info.referer.empty()) {
    http_request->set_referer(info.referer);
  }
  if (info.latency.request_time_ms >= 0) {
    const google::protobuf::Duration duration =
        google::protobuf::util::TimeUtil::MillisecondsToDuration(
            info.latency.request_time_ms);
    http_request->mutable_latency()->CopyFrom(duration);
  }

  // Fill in JSON struct.
  auto* fields = log_entry->mutable_struct_payload()->mutable_fields();
  (*fields)[kLogFieldNameTimestamp].set_number_value(
      static_cast<double>(current_time.seconds()) +
      static_cast<double>(current_time.nanos()) / 1000000000.0);
  (*fields)[kLogFieldNameConfigId].set_string_value(config_id);
  (*fields)[kLogFieldNameServiceAgent].set_string_value(
      kServiceAgentPrefix + utils::Version::instance().get());

  (*fields)[kLogFieldNameApiKeyState].set_string_value(
      api_key::ToString(info.check_response_info.api_key_state));
  (*fields)[kLogFieldNameHttpStatusCode].set_number_value(
      info.http_response_code);

  if (!info.response_code_detail.empty()) {
    (*fields)[kLogFieldNameResponseCodeDetail].set_string_value(
        info.response_code_detail);
  }

  if (!info.producer_project_id.empty()) {
    (*fields)[kLogFieldNameProducerProjectId].set_string_value(
        info.producer_project_id);
  }
  if (!info.api_key.empty()) {
    (*fields)[kLogFieldNameApiKey].set_string_value(info.api_key);
  }
  if (!info.api_name.empty()) {
    (*fields)[kLogFieldNameApiName].set_string_value(info.api_name);
  }
  if (!info.api_version.empty()) {
    (*fields)[kLogFieldNameApiVersion].set_string_value(info.api_version);
  }
  if (!info.api_method.empty()) {
    (*fields)[kLogFieldNameApiMethod].set_string_value(info.api_method);
  }
  if (!info.location.empty()) {
    (*fields)[kLogFieldNameLocation].set_string_value(info.location);
  }
  if (!info.log_message.empty()) {
    (*fields)[kLogFieldNameLogMessage].set_string_value(info.log_message);
  }
  if (!info.request_headers.empty()) {
    (*fields)[kLogFieldNameRequestHeaders].set_string_value(
        info.request_headers);
  }
  if (!info.response_headers.empty()) {
    (*fields)[kLogFieldNameResponseHeaders].set_string_value(
        info.response_headers);
  }
  if (!info.jwt_payloads.empty()) {
    (*fields)[kLogFieldNameJwtPayloads].set_string_value(info.jwt_payloads);
  }
  if (!info.status.ok() && info.status.message().length() > 0) {
    (*fields)[kLogFieldNameErrorCause].set_string_value(info.status.message());
  }

  if (info.grpc_response_code.has_value()) {
    const std::string grpc_status_string =
        Envoy::Grpc::Utility::grpcStatusToString(
            static_cast<Envoy::Grpc::Status::GrpcStatus>(
                info.grpc_response_code.value()));
    (*fields)[kLogFieldNameGrpcStatusCode].set_string_value(grpc_status_string);
  }
}