| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.rocketmq.ons.open.trace.core.common; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.apache.rocketmq.common.message.MessageType; |
| |
| public class OnsTraceDataEncoder { |
| |
| |
| public static List<OnsTraceContext> decoderFromTraceDataString(String traceData) { |
| List<OnsTraceContext> resList = new ArrayList<OnsTraceContext>(); |
| if (traceData == null || traceData.length() <= 0) { |
| return resList; |
| } |
| String[] contextList = traceData.split(String.valueOf(OnsTraceConstants.FIELD_SPLITOR)); |
| for (String context : contextList) { |
| String[] line = context.split(String.valueOf(OnsTraceConstants.CONTENT_SPLITOR)); |
| if (line[0].equals(OnsTraceType.Pub.name())) { |
| OnsTraceContext pubContext = new OnsTraceContext(); |
| pubContext.setTraceType(OnsTraceType.Pub); |
| pubContext.setTimeStamp(Long.parseLong(line[1])); |
| pubContext.setRegionId(line[2]); |
| pubContext.setGroupName(line[3]); |
| OnsTraceBean bean = new OnsTraceBean(); |
| bean.setTopic(line[4]); |
| bean.setMsgId(line[5]); |
| bean.setTags(line[6]); |
| bean.setKeys(line[7]); |
| bean.setStoreHost(line[8]); |
| bean.setBodyLength(Integer.parseInt(line[9])); |
| pubContext.setCostTime(Integer.parseInt(line[10])); |
| bean.setMsgType(MessageType.values()[Integer.parseInt(line[11])]); |
| if (line.length == 13) { |
| pubContext.setSuccess(Boolean.parseBoolean(line[12])); |
| } else if (line.length == 14) { |
| bean.setOffsetMsgId(line[12]); |
| pubContext.setSuccess(Boolean.parseBoolean(line[13])); |
| } |
| pubContext.setTraceBeans(new ArrayList<OnsTraceBean>(1)); |
| pubContext.getTraceBeans().add(bean); |
| resList.add(pubContext); |
| } else if (line[0].equals(OnsTraceType.SubBefore.name())) { |
| OnsTraceContext subBeforeContext = new OnsTraceContext(); |
| subBeforeContext.setTraceType(OnsTraceType.SubBefore); |
| subBeforeContext.setTimeStamp(Long.parseLong(line[1])); |
| subBeforeContext.setRegionId(line[2]); |
| subBeforeContext.setGroupName(line[3]); |
| subBeforeContext.setRequestId(line[4]); |
| OnsTraceBean bean = new OnsTraceBean(); |
| bean.setMsgId(line[5]); |
| bean.setRetryTimes(Integer.parseInt(line[6])); |
| bean.setKeys(line[7]); |
| subBeforeContext.setTraceBeans(new ArrayList<OnsTraceBean>(1)); |
| subBeforeContext.getTraceBeans().add(bean); |
| resList.add(subBeforeContext); |
| } else if (line[0].equals(OnsTraceType.SubAfter.name())) { |
| OnsTraceContext subAfterContext = new OnsTraceContext(); |
| subAfterContext.setTraceType(OnsTraceType.SubAfter); |
| subAfterContext.setRequestId(line[1]); |
| OnsTraceBean bean = new OnsTraceBean(); |
| bean.setMsgId(line[2]); |
| bean.setKeys(line[5]); |
| subAfterContext.setTraceBeans(new ArrayList<OnsTraceBean>(1)); |
| subAfterContext.getTraceBeans().add(bean); |
| subAfterContext.setCostTime(Integer.parseInt(line[3])); |
| subAfterContext.setSuccess(Boolean.parseBoolean(line[4])); |
| if (line.length >= 7) { |
| // add the context type |
| subAfterContext.setContextCode(Integer.parseInt(line[6])); |
| } |
| if (line.length >= 8) { |
| subAfterContext.setExactlyOnceStatus(Integer.parseInt(line[7])); |
| } |
| resList.add(subAfterContext); |
| } |
| } |
| return resList; |
| } |
| |
| |
| public static OnsTraceTransferBean encoderFromContextBean(OnsTraceContext ctx) { |
| if (ctx == null) { |
| return null; |
| } |
| OnsTraceTransferBean transferBean = new OnsTraceTransferBean(); |
| StringBuilder sb = new StringBuilder(256); |
| switch (ctx.getTraceType()) { |
| case Pub: { |
| OnsTraceBean bean = ctx.getTraceBeans().get(0); |
| sb.append(ctx.getTraceType()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getTimeStamp()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getRegionId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getGroupName()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getTopic()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getMsgId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getTags()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getKeys()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getStoreHost()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getBodyLength()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getCostTime()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getMsgType().ordinal()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getOffsetMsgId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.isSuccess()).append(OnsTraceConstants.FIELD_SPLITOR); |
| } |
| break; |
| case SubBefore: { |
| for (OnsTraceBean bean : ctx.getTraceBeans()) { |
| sb.append(ctx.getTraceType()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getTimeStamp()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getRegionId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getGroupName()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getRequestId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getMsgId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getRetryTimes()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getKeys()).append(OnsTraceConstants.FIELD_SPLITOR);// |
| } |
| } |
| break; |
| case SubAfter: { |
| for (OnsTraceBean bean : ctx.getTraceBeans()) { |
| sb.append(ctx.getTraceType()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| // .append(ctx.getTimeStamp()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getRequestId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getMsgId()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getCostTime()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.isSuccess()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(bean.getKeys()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getContextCode()).append(OnsTraceConstants.CONTENT_SPLITOR)// |
| .append(ctx.getExactlyOnceStatus()).append(OnsTraceConstants.FIELD_SPLITOR); |
| } |
| } |
| break; |
| default: |
| } |
| transferBean.setTransData(sb.toString()); |
| for (OnsTraceBean bean : ctx.getTraceBeans()) { |
| transferBean.getTransKey().add(bean.getMsgId()); |
| if (bean.getKeys() != null && bean.getKeys().length() > 0) { |
| transferBean.getTransKey().add(bean.getKeys()); |
| } |
| } |
| return transferBean; |
| } |
| } |