diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/GrpcSpanMessageConverter.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/GrpcSpanMessageConverter.java index 53e05b86515c..63c9307e2471 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/GrpcSpanMessageConverter.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/GrpcSpanMessageConverter.java @@ -91,8 +91,7 @@ public GrpcSpanMessageConverter(String agentId, short applicationServiceType, public GeneratedMessageV3 toMessage(SpanType message) { if (message instanceof SpanChunk) { final SpanChunk spanChunk = (SpanChunk) message; - final PSpanChunk pSpanChunk = buildPSpanChunk(spanChunk); - return pSpanChunk; + return buildPSpanChunk(spanChunk); } if (message instanceof Span) { final Span span = (Span) message; @@ -246,8 +245,8 @@ private List buildPSpanEventList(List spanEventList) { final int eventSize = spanEventList.size(); final List pSpanEventList = new ArrayList<>(eventSize); for (SpanEvent spanEvent : spanEventList) { - final PSpanEvent.Builder pSpanEvent = buildPSpanEvent(spanEvent); - pSpanEventList.add(pSpanEvent.build()); + final PSpanEvent pSpanEvent = buildPSpanEvent(spanEvent); + pSpanEventList.add(pSpanEvent); } return pSpanEventList; } @@ -298,50 +297,53 @@ PSpanChunk buildPSpanChunk(SpanChunk spanChunk) { } @VisibleForTesting - public PSpanEvent.Builder buildPSpanEvent(SpanEvent spanEvent) { - final PSpanEvent.Builder pSpanEvent = getSpanEventBuilder(); - + public PSpanEvent buildPSpanEvent(SpanEvent spanEvent) { + final PSpanEvent.Builder builder = this.pSpanEventBuilder; + try { // if (spanEvent.getStartElapsed() != 0) { -// tSpanEvent.setStartElapsed(spanEvent.getStartElapsed()); +// builder.setStartElapsed(spanEvent.getStartElapsed()); // } // tSpanEvent.setStartElapsed(spanEvent.getStartElapsed()); - if (spanEvent.getElapsedTime() != 0) { - pSpanEvent.setEndElapsed(spanEvent.getElapsedTime()); - } - pSpanEvent.setSequence(spanEvent.getSequence()); -// tSpanEvent.setRpc(spanEvent.getRpc()); - pSpanEvent.setServiceType(spanEvent.getServiceType()); + if (spanEvent.getElapsedTime() != 0) { + builder.setEndElapsed(spanEvent.getElapsedTime()); + } + builder.setSequence(spanEvent.getSequence()); +// builder.setRpc(spanEvent.getRpc()); + builder.setServiceType(spanEvent.getServiceType()); - // tSpanEvent.setAnnotations(); - if (spanEvent.getDepth() != -1) { - pSpanEvent.setDepth(spanEvent.getDepth()); - } + // tSpanEvent.setAnnotations(); + if (spanEvent.getDepth() != -1) { + builder.setDepth(spanEvent.getDepth()); + } - pSpanEvent.setApiId(spanEvent.getApiId()); + builder.setApiId(spanEvent.getApiId()); - final IntStringValue exceptionInfo = spanEvent.getExceptionInfo(); - if (exceptionInfo != null) { - PIntStringValue pIntStringValue = buildPIntStringValue(exceptionInfo); - pSpanEvent.setExceptionInfo(pIntStringValue); - } + final IntStringValue exceptionInfo = spanEvent.getExceptionInfo(); + if (exceptionInfo != null) { + PIntStringValue pIntStringValue = buildPIntStringValue(exceptionInfo); + builder.setExceptionInfo(pIntStringValue); + } - final PNextEvent nextEvent = buildNextEvent(spanEvent); - if (nextEvent != null) { - pSpanEvent.setNextEvent(nextEvent); - } - final AsyncId asyncIdObject = spanEvent.getAsyncIdObject(); - if (asyncIdObject != null) { - pSpanEvent.setAsyncEvent(asyncIdObject.getAsyncId()); - } + final PNextEvent nextEvent = buildNextEvent(spanEvent); + if (nextEvent != null) { + builder.setNextEvent(nextEvent); + } + final AsyncId asyncIdObject = spanEvent.getAsyncIdObject(); + if (asyncIdObject != null) { + builder.setAsyncEvent(asyncIdObject.getAsyncId()); + } - final List> annotations = spanEvent.getAnnotations(); - if (CollectionUtils.hasLength(annotations)) { - final List pAnnotations = buildPAnnotation(annotations); - pSpanEvent.addAllAnnotation(pAnnotations); - } + final List> annotations = spanEvent.getAnnotations(); + if (CollectionUtils.hasLength(annotations)) { + final List pAnnotations = buildPAnnotation(annotations); + builder.addAllAnnotation(pAnnotations); + } - return pSpanEvent; + return builder.build(); + } finally { + builder.clear(); + } } private PNextEvent buildNextEvent(SpanEvent spanEvent) { @@ -391,28 +393,26 @@ private PIntStringValue buildPIntStringValue(IntStringValue exceptionInfo) { @VisibleForTesting List buildPAnnotation(List> annotations) { - final List tAnnotationList = new ArrayList<>(annotations.size()); - for (Annotation annotation : annotations) { - final PAnnotation.Builder builder = getAnnotationBuilder(); + final List tAnnotationList = new ArrayList<>(annotations.size()); + for (Annotation annotation : annotations) { + PAnnotation pAnnotation = buildPAnnotation0(annotation); + tAnnotationList.add(pAnnotation); + } + return tAnnotationList; + } + + private PAnnotation buildPAnnotation0(Annotation annotation) { + final PAnnotation.Builder builder = this.pAnnotationBuilder; + try { builder.setKey(annotation.getKey()); final PAnnotationValue pAnnotationValue = grpcAnnotationValueMapper.buildPAnnotationValue(annotation); if (pAnnotationValue != null) { builder.setValue(pAnnotationValue); } - PAnnotation pAnnotation = builder.build(); - tAnnotationList.add(pAnnotation); + return builder.build(); + } finally { + builder.clear(); } - return tAnnotationList; - } - - private PAnnotation.Builder getAnnotationBuilder() { - this.pAnnotationBuilder.clear(); - return pAnnotationBuilder; - } - - private PSpanEvent.Builder getSpanEventBuilder() { - pSpanEventBuilder.clear(); - return pSpanEventBuilder; } @Override diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/compress/GrpcSpanProcessorV2Test.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/compress/GrpcSpanProcessorV2Test.java index c2f60cdc456f..267971f203c9 100644 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/compress/GrpcSpanProcessorV2Test.java +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/context/compress/GrpcSpanProcessorV2Test.java @@ -77,7 +77,7 @@ public void postProcess() { PSpan.Builder builder = PSpan.newBuilder(); for (SpanEvent spanEvent : span.getSpanEventList()) { - PSpanEvent.Builder pSpanEvent = converter.buildPSpanEvent(spanEvent); + PSpanEvent pSpanEvent = converter.buildPSpanEvent(spanEvent); builder.addSpanEvent(pSpanEvent); }