CommonSpanEncoder implements SpanEncoder<CommonLogSpan> { @Override public String encode(CommonLogSpan commonLogSpan) throws IOException { if (commonLogSpan.getSofaTracerSpanContext() == null) { return StringUtils.EMPTY_STRING; } SofaTracerSpanContext spanContext = commonLogSpan.getSofaTracerSpanContext(); XStringBuilder xsb = new XStringBuilder(); xsb.append(Timestamp.format(commonLogSpan.getStartTime())) .append(commonLogSpan.getTagsWithStr().get(SpanTags.CURR_APP_TAG.getKey())) .append(spanContext.getTraceId()).append(spanContext.getSpanId()); this.appendStr(xsb, commonLogSpan); return xsb.toString(); }  @Override String encode(CommonLogSpan commonLogSpan);  }
@Test public void testEncode() throws Exception { SofaTracerSpan sofaTracerSpan = (SofaTracerSpan) this.sofaTracer .buildSpan("spanOperationName").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT) .withTag(SpanTags.CURR_APP_TAG.getKey(), appName).start(); SofaTracerSpanContext sofaTracerSpanContext = sofaTracerSpan.getSofaTracerSpanContext(); Exception exception = new RuntimeException("CommonSpanEncoderTest"); String errorType = "timeout_error"; String[] errorSources = new String[] { "trade", "rpc" }; Map<String, String> context = new HashMap<String, String>(); context.put("serviceName", "service"); context.put("methodName", "methodCall"); sofaTracerSpan.setBaggageItem("baggage", "value"); sofaTracerSpan.setBaggageItem("baggage1", "value1"); sofaTracerSpan.setBaggageItem("baggage2", "value2"); sofaTracerSpan.reportError(errorType, context, exception, appName, errorSources); sofaTracerSpan.finish(); TestUtil.waitForAsyncLog(); List<String> clientDigestContents = FileUtils.readLines(new File(logDirectoryPath + File.separator + clientLogType)); assertTrue(clientDigestContents.size() == 1); assertTrue(clientDigestContents.get(0).contains(sofaTracerSpanContext.getTraceId())); List<String> errorContents = FileUtils .readLines(customFileLog(TracerSystemLogEnum.MIDDLEWARE_ERROR.getDefaultLogName())); assertTrue(errorContents.size() > 1); assertTrue(errorContents.get(0).contains(sofaTracerSpanContext.getTraceId())); String fileContent = errorContents.get(0).substring(errorContents.get(0).indexOf(",") + 1); fileContent = fileContent.substring(0, fileContent.lastIndexOf(",")); List<String> params = new ArrayList<>(); params.add(sofaTracerSpan.getTagsWithStr().get(SpanTags.CURR_APP_TAG.getKey())); params.add(sofaTracerSpanContext.getTraceId()); params.add(sofaTracerSpanContext.getSpanId()); params.add(Thread.currentThread().getName()); params.add(errorType); params.add(StringUtils.arrayToString(errorSources, ARRAY_SEPARATOR, "", "")); params.add(MAP_PREFIX + StringUtils.mapToString(context)); params.add(MAP_PREFIX + StringUtils.mapToString(sofaTracerSpanContext.getBizBaggage())); Assert.assertTrue("Content Checkout error", checkResult(params, fileContent)); }