TraceTransferBean TraceUtil::CovertTraceContextToTransferBean()

in src/trace/TraceUtil.cpp [37:123]


TraceTransferBean TraceUtil::CovertTraceContextToTransferBean(TraceContext* ctx) {
  std::ostringstream ss;
  std::vector<TraceBean> beans = ctx->getTraceBeans();
  switch (ctx->getTraceType()) {
    case Pub: {
      std::vector<TraceBean>::iterator it = beans.begin();
      ss << TraceUtil::CovertTraceTypeToString(ctx->getTraceType()) << TraceConstant::CONTENT_SPLITOR;
      ss << ctx->getTimeStamp() << TraceConstant::CONTENT_SPLITOR;
      ss << ctx->getRegionId() << TraceConstant::CONTENT_SPLITOR;
      ss << ctx->getGroupName() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getTopic() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getMsgId() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getTags() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getKeys() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getStoreHost() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getBodyLength() << TraceConstant::CONTENT_SPLITOR;
      ss << ctx->getCostTime() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getMsgType() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getOffsetMsgId() << TraceConstant::CONTENT_SPLITOR;
      ss << (ctx->getStatus() ? "true" : "false") << TraceConstant::FIELD_SPLITOR;
    } break;

    case SubBefore: {
      std::vector<TraceBean>::iterator it = beans.begin();
      for (; it != beans.end(); ++it) {
        ss << TraceUtil::CovertTraceTypeToString(ctx->getTraceType()) << TraceConstant::CONTENT_SPLITOR;
        ss << ctx->getTimeStamp() << TraceConstant::CONTENT_SPLITOR;
        ss << ctx->getRegionId() << TraceConstant::CONTENT_SPLITOR;
        ss << ctx->getGroupName() << TraceConstant::CONTENT_SPLITOR;
        ss << ctx->getRequestId() << TraceConstant::CONTENT_SPLITOR;
        ss << it->getMsgId() << TraceConstant::CONTENT_SPLITOR;
        ss << it->getRetryTimes() << TraceConstant::CONTENT_SPLITOR;
        // this is a bug caused by broker.
        std::string defaultKey = "dKey";
        if (!it->getKeys().empty()) {
          defaultKey = it->getKeys();
        }
        ss << defaultKey << TraceConstant::FIELD_SPLITOR;
      }
    } break;

    case SubAfter: {
      std::vector<TraceBean>::iterator it = beans.begin();
      ss << TraceUtil::CovertTraceTypeToString(ctx->getTraceType()) << TraceConstant::CONTENT_SPLITOR;
      ss << ctx->getRequestId() << TraceConstant::CONTENT_SPLITOR;
      ss << it->getMsgId() << TraceConstant::CONTENT_SPLITOR;
      ss << ctx->getCostTime() << TraceConstant::CONTENT_SPLITOR;
      ss << (ctx->getStatus() ? "true" : "false") << TraceConstant::CONTENT_SPLITOR;
      // this is a bug caused by broker.
      std::string defaultKey = "dKey";
      if (!it->getKeys().empty()) {
        defaultKey = it->getKeys();
      }
      ss << defaultKey << TraceConstant::FIELD_SPLITOR;
    } break;

    default:
      break;
  }

  TraceTransferBean transferBean;
  transferBean.setTransData(ss.str());

  switch (ctx->getTraceType()) {
    case Pub:
    case SubAfter: {
      std::vector<TraceBean>::iterator it = beans.begin();
      transferBean.setTransKey(it->getMsgId());
      if (it->getKeys() != "") {
        transferBean.setTransKey(it->getKeys());
      }
    } break;
    case SubBefore: {
      std::vector<TraceBean>::iterator it = beans.begin();
      for (; it != beans.end(); ++it) {
        transferBean.setTransKey((*it).getMsgId());
        if ((*it).getKeys() != "") {
          transferBean.setTransKey((*it).getKeys());
        }
      }
    } break;
    default:
      break;
  }

  return transferBean;
}