Skip to content

Commit

Permalink
fasten convert traceID/spanID
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kositsyn <[email protected]>
  • Loading branch information
pkositsyn committed Nov 9, 2020
1 parent f6a0e29 commit a78e918
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package esmodeltranslator

import (
"encoding/hex"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -222,26 +223,23 @@ func references(links pdata.SpanLinkSlice, parentSpanID pdata.SpanID, traceID db
}

func convertSpanID(spanID pdata.SpanID) (dbmodel.SpanID, error) {
spanIDInt := tracetranslator.BytesToUInt64SpanID(spanID.Bytes())
if spanIDInt == 0 {
if !spanID.IsValid() {
return "", errZeroSpanID
}
return dbmodel.SpanID(fmt.Sprintf("%016x", spanIDInt)), nil
src := spanID.Bytes()
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src[:])
return dbmodel.SpanID(dst), nil
}

func convertTraceID(traceID pdata.TraceID) (dbmodel.TraceID, error) {
high, low := tracetranslator.BytesToUInt64TraceID(traceID.Bytes())
if low == 0 && high == 0 {
if !traceID.IsValid() {
return "", errZeroTraceID
}
return dbmodel.TraceID(traceIDToString(high, low)), nil
}

func traceIDToString(high, low uint64) string {
if high == 0 {
return fmt.Sprintf("%016x", low)
}
return fmt.Sprintf("%016x%016x", high, low)
src := traceID.Bytes()
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src[:])
return dbmodel.TraceID(dst), nil
}

func (c *Translator) process(resource pdata.Resource) *dbmodel.Process {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func TestConvertSpan(t *testing.T) {
}, spansData[0])
}

func BenchmarkConvertSpanID(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = convertSpanID(spanID)
}
}

func TestSpanEmptyRef(t *testing.T) {
traces := traces("myservice")
span := addSpan(traces, "root", traceID, spanID)
Expand Down

0 comments on commit a78e918

Please sign in to comment.