Status RequestBuilder::AppendByConsumerOperations()

in src/api_proxy/service_control/request_builder.cc [1155:1194]


Status RequestBuilder::AppendByConsumerOperations(
    const ReportRequestInfo& info,
    ::google::api::servicecontrol::v1::ReportRequest* request,
    Timestamp current_time) const {
  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));
  }
  // issue a new operation id
  op->set_operation_id(op->operation_id() + "1");

  // 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.
    for (auto it = labels_.begin(), end = labels_.end(); it != end; it++) {
      const SupportedLabel* l = *it;
      if (l->set) {
        Status status = (l->set)(*l, info, labels);
        if (!status.ok()) return status;
      }
    }

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

  return OkStatus();
}