func()

in receiver/libhoneyreceiver/internal/libhoneyevent/libhoneyevent.go [279:385]


func (l *LibhoneyEvent) ToPTraceSpan(newSpan *ptrace.Span, alreadyUsedFields *[]string, cfg FieldMapConfig, logger zap.Logger) error {
	timeNs := l.MsgPackTimestamp.UnixNano()
	logger.Debug("processing trace with", zap.Int64("timestamp", timeNs))

	var parentID trc.SpanID
	if pid, ok := l.Data[cfg.Attributes.ParentID]; ok {
		parentID = spanIDFrom(pid.(string))
		newSpan.SetParentSpanID(pcommon.SpanID(parentID))
	}

	durationMs := 0.0
	for _, df := range cfg.Attributes.DurationFields {
		if duration, okay := l.Data[df]; okay {
			durationMs = duration.(float64)
			break
		}
	}
	endTimestamp := timeNs + (int64(durationMs) * 1000000)

	if tid, ok := l.Data[cfg.Attributes.TraceID]; ok {
		tid := strings.ReplaceAll(tid.(string), "-", "")
		tidByteArray, err := hex.DecodeString(tid)
		if err == nil {
			if len(tidByteArray) >= 32 {
				tidByteArray = tidByteArray[0:32]
			}
			newSpan.SetTraceID(pcommon.TraceID(tidByteArray))
		} else {
			newSpan.SetTraceID(pcommon.TraceID(traceIDFrom(tid)))
		}
	} else {
		newSpan.SetTraceID(pcommon.TraceID(generateAnID(32)))
	}

	if sid, ok := l.Data[cfg.Attributes.SpanID]; ok {
		sid := strings.ReplaceAll(sid.(string), "-", "")
		sidByteArray, err := hex.DecodeString(sid)
		if err == nil {
			if len(sidByteArray) == 32 {
				sidByteArray = sidByteArray[8:24]
			} else if len(sidByteArray) >= 16 {
				sidByteArray = sidByteArray[0:16]
			}
			newSpan.SetSpanID(pcommon.SpanID(sidByteArray))
		} else {
			newSpan.SetSpanID(pcommon.SpanID(spanIDFrom(sid)))
		}
	} else {
		newSpan.SetSpanID(pcommon.SpanID(generateAnID(16)))
	}

	newSpan.SetStartTimestamp(pcommon.Timestamp(timeNs))
	newSpan.SetEndTimestamp(pcommon.Timestamp(endTimestamp))

	if spanName, ok := l.Data[cfg.Attributes.Name]; ok {
		newSpan.SetName(spanName.(string))
	}
	if spanStatusMessage, ok := l.Data["status_message"]; ok {
		newSpan.Status().SetMessage(spanStatusMessage.(string))
	}
	newSpan.Status().SetCode(ptrace.StatusCodeUnset)

	if _, ok := l.Data[cfg.Attributes.Error]; ok {
		newSpan.Status().SetCode(ptrace.StatusCodeError)
	}

	if spanKind, ok := l.Data[cfg.Attributes.SpanKind]; ok {
		switch spanKind.(string) {
		case "server":
			newSpan.SetKind(ptrace.SpanKindServer)
		case "client":
			newSpan.SetKind(ptrace.SpanKindClient)
		case "producer":
			newSpan.SetKind(ptrace.SpanKindProducer)
		case "consumer":
			newSpan.SetKind(ptrace.SpanKindConsumer)
		case "internal":
			newSpan.SetKind(ptrace.SpanKindInternal)
		default:
			newSpan.SetKind(ptrace.SpanKindUnspecified)
		}
	}

	newSpan.Attributes().PutInt("SampleRate", int64(l.Samplerate))

	for k, v := range l.Data {
		if slices.Contains(*alreadyUsedFields, k) {
			continue
		}
		switch v := v.(type) {
		case string:
			newSpan.Attributes().PutStr(k, v)
		case int:
			newSpan.Attributes().PutInt(k, int64(v))
		case int64, int16, int32:
			intv := v.(int64)
			newSpan.Attributes().PutInt(k, intv)
		case float64:
			newSpan.Attributes().PutDouble(k, v)
		case bool:
			newSpan.Attributes().PutBool(k, v)
		default:
			logger.Warn("Span data type issue", zap.String("trace.trace_id", newSpan.TraceID().String()), zap.String("trace.span_id", newSpan.SpanID().String()), zap.String("key", k))
		}
	}
	return nil
}