Status RequestBuilder::FillReportRequest()

in src/api_proxy/service_control/request_builder.cc [1093:1153]


Status RequestBuilder::FillReportRequest(const ReportRequestInfo& info,
                                         ReportRequest* request) const {
  Status status = VerifyRequiredReportFields(info);
  if (!status.ok()) {
    return status;
  }
  request->set_service_name(service_name_);
  request->set_service_config_id(service_config_id_);

  Timestamp current_time = CreateTimestamp(info.current_time);
  Operation* op = request->add_operations();
  SetOperationCommonFields(info, current_time, op);
  if (info.check_response_info.api_key_state ==
      api_key::ApiKeyState::VERIFIED) {
    ASSERT(!info.api_key.empty(),
           "API Key must be set, otherwise consumer would not be verified.");
    op->set_consumer_id(std::string(kConsumerIdApiKey) +
                        std::string(info.api_key));
  }

  // Only populate metrics if we can associate them with a method/operation.
  if (!info.operation_id.empty() && !info.operation_name.empty()) {
    Map<std::string, std::string>* labels = op->mutable_labels();
    // Set all labels with by_consumer_only is false
    for (auto it = labels_.begin(), end = labels_.end(); it != end; it++) {
      const SupportedLabel* l = *it;
      if (l->set && !l->by_consumer_only) {
        status = (l->set)(*l, info, labels);
        if (!status.ok()) return status;
      }
    }

    // Report will reject consumer metric if it's based on a invalid/unknown api
    // key, or if the service is not activated in the consumer project.
    bool send_consumer_metric = info.check_response_info.api_key_state ==
                                api_key::ApiKeyState::VERIFIED;

    // Populate all metrics.
    for (auto it = metrics_.begin(), end = metrics_.end(); it != end; it++) {
      const SupportedMetric* m = *it;
      if (send_consumer_metric || m->mark != SupportedMetric::CONSUMER) {
        if (m->set && m->mark != SupportedMetric::PRODUCER_BY_CONSUMER) {
          status = (m->set)(*m, info, op);
          if (!status.ok()) return status;
        }
      }
    }
  }

  // Fill log entries.
  for (auto it = logs_.begin(), end = logs_.end(); it != end; it++) {
    FillLogEntry(info, *it, service_config_id_, current_time,
                 op->add_log_entries());
  }

  if (!info.check_response_info.consumer_project_number.empty()) {
    return AppendByConsumerOperations(info, request, current_time);
  }

  return OkStatus();
}