Example #1
0
  private void addNestedSpanEvent(TSpan span, Trace trace) {
    Assert.notNull(trace, "trace must not be null while add span event.");

    List<TSpanEvent> spanEventBoList = span.getSpanEventList();
    if (CollectionUtils.isEmpty(spanEventBoList)) {
      return;
    }

    long acceptedTime0 = acceptedTimeService.getAcceptedTime();

    spanEventBoList
        .stream()
        .filter(spanEvent -> spanEvent != null)
        .forEach(
            spanEvent -> {
              TraceEvent event = new TraceEvent();

              event.setAgentId(trace.getAgentId());
              event.setTraceId(trace.getId());

              final TransactionId transactionId =
                  TransactionIdUtils.parseTransactionId(span.getTransactionId());
              event.setTraceAgentId(transactionId.getAgentId());

              if (event.getTraceAgentId() == null) {
                event.setTraceAgentId(span.getAgentId());
              }
              event.setTraceAgentStartTime(transactionId.getAgentStartTime());
              event.setTraceTransactionSequence(transactionId.getTransactionSequence());

              event.setSequence(spanEvent.getSequence());

              event.setStartElapsed(spanEvent.getStartElapsed());
              event.setEndElapsed(spanEvent.getEndElapsed());

              event.setRpc(spanEvent.getRpc());
              event.setServiceType(spanEvent.getServiceType());

              event.setDestinationId(spanEvent.getDestinationId());

              event.setEndPoint(spanEvent.getEndPoint());
              ApiMetaData apiMetaData = findApiMetaData(trace.getAgentId(), spanEvent.getApiId());
              event.setApiId(apiMetaData.getId());

              if (spanEvent.isSetDepth()) {
                event.setDepth(spanEvent.getDepth());
              }

              if (spanEvent.isSetNextSpanId()) {
                event.setNextSpanId(spanEvent.getNextSpanId());
              }

              final TIntStringValue exceptionInfo = spanEvent.getExceptionInfo();
              if (exceptionInfo != null) {
                event.setHasException(true);
                event.setExceptionId(exceptionInfo.getIntValue());
                event.setExceptionMessage(exceptionInfo.getStringValue());
              }

              event.setCollectorAcceptTime(acceptedTime0);

              event.setTraceId(trace.getId());

              TraceEvent saveTraceEvent = traceEventRepository.save(event);

              if (spanEvent.getAnnotations() != null) {
                List<Annotation> annotations =
                    spanEvent
                        .getAnnotations()
                        .stream()
                        .filter(tAnnotation -> tAnnotation != null)
                        .map(
                            tAnnotation -> {
                              Annotation annotation = new Annotation();
                              annotation.setKey(tAnnotation.getKey());
                              Object value = transcoder.getMappingValue(tAnnotation);
                              annotation.setValueType(transcoder.getTypeCode(value));
                              annotation.setByteValue(
                                  transcoder.encode(value, annotation.getValueType()));
                              annotation.setTraceEventId(saveTraceEvent.getId());
                              return annotation;
                            })
                        .collect(Collectors.toList());
                annotationRepository.save(annotations);
              }
            });
  }
Example #2
0
  @Override
  public void insertSpanChunk(TSpanChunk spanChunk) {
    final byte[] transactionIdBytes = spanChunk.getTransactionId();
    final TransactionId transactionId = TransactionIdUtils.parseTransactionId(transactionIdBytes);

    Trace trace =
        findTrace(spanChunk.getAgentId(), spanChunk.getAgentStartTime(), spanChunk.getSpanId());

    long acceptedTime = acceptedTimeService.getAcceptedTime();
    spanChunk
        .getSpanEventList()
        .stream()
        .forEach(
            spanEvent -> {
              TraceEvent event = new TraceEvent();
              event.setAgentId(trace.getAgentId());
              event.setTraceId(trace.getId());

              event.setTraceAgentId(transactionId.getAgentId());

              if (event.getTraceAgentId() == null) {
                event.setTraceAgentId(spanChunk.getAgentId());
              }

              event.setTraceAgentStartTime(transactionId.getAgentStartTime());
              event.setTraceTransactionSequence(transactionId.getTransactionSequence());

              event.setSequence(spanEvent.getSequence());

              event.setStartElapsed(spanEvent.getStartElapsed());
              event.setEndElapsed(spanEvent.getEndElapsed());

              event.setRpc(spanEvent.getRpc());
              event.setServiceType(spanEvent.getServiceType());

              event.setDestinationId(spanEvent.getDestinationId());

              event.setEndPoint(spanEvent.getEndPoint());

              ApiMetaData apiMetaData = findApiMetaData(trace.getAgentId(), spanEvent.getApiId());
              event.setApiId(apiMetaData.getId());

              if (spanEvent.isSetDepth()) {
                event.setDepth(spanEvent.getDepth());
              }

              if (spanEvent.isSetNextSpanId()) {
                event.setNextSpanId(spanEvent.getNextSpanId());
              }

              final TIntStringValue exceptionInfo = spanEvent.getExceptionInfo();
              if (exceptionInfo != null) {
                event.setHasException(true);
                event.setExceptionId(exceptionInfo.getIntValue());
                event.setExceptionMessage(exceptionInfo.getStringValue());
              }

              event.setCollectorAcceptTime(acceptedTime);

              event.setTraceId(trace.getId());
              TraceEvent savedTraceEvent = traceEventRepository.saveAndFlush(event);

              if (spanEvent.getAnnotations() != null) {
                List<Annotation> annotations =
                    spanEvent
                        .getAnnotations()
                        .stream()
                        .filter(tAnnotation -> tAnnotation != null)
                        .map(
                            tAnnotation -> {
                              Annotation annotation = new Annotation();
                              annotation.setKey(tAnnotation.getKey());
                              Object value = transcoder.getMappingValue(tAnnotation);
                              annotation.setValueType(transcoder.getTypeCode(value));
                              annotation.setByteValue(
                                  transcoder.encode(value, annotation.getValueType()));
                              annotation.setTraceEventId(savedTraceEvent.getId());
                              return annotation;
                            })
                        .collect(Collectors.toList());
                annotationRepository.save(annotations);
              }
            });
  }