Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move BigEndian helper functions in tracetranslator to an internal package #3298

Merged
merged 8 commits into from
May 25, 2021
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Remove unused logstest package (#3222)
- Introduce `AppSettings` instead of `Parameters` (#3163)
- Move BigEndian helper functions in `tracetranslator` to an internal package.(#3298)

## 💡 Enhancements 💡

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tracetranslator
package idutils

import (
"encoding/binary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tracetranslator
package idutils

import (
"math"
Expand Down
19 changes: 19 additions & 0 deletions internal/idutils/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package idutils provides a set of helper functions to convert ids.
//
// Functions in big_endian_converter.go help converting uint64 ids to TraceID
// and SpanID using big endian, and vice versa.
package idutils // import "go.opentelemetry.io/collector/internal/idutils"
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)

func TestNewTracesProcessor(t *testing.T) {
Expand Down Expand Up @@ -437,7 +437,7 @@ func Test_hash(t *testing.T) {
// collisions, but, of course it is possible that they happen, a different random source
// should avoid that.
r := rand.New(rand.NewSource(1))
fullKey := tracetranslator.UInt64ToTraceID(r.Uint64(), r.Uint64()).Bytes()
fullKey := idutils.UInt64ToTraceID(r.Uint64(), r.Uint64()).Bytes()
seen := make(map[uint32]bool)
for i := 1; i <= len(fullKey); i++ {
key := fullKey[:i]
Expand Down Expand Up @@ -467,8 +467,8 @@ func genRandomTestData(numBatches, numTracesPerBatch int, serviceName string, re

for k := 0; k < numTracesPerBatch; k++ {
span := ils.Spans().At(k)
span.SetTraceID(tracetranslator.UInt64ToTraceID(r.Uint64(), r.Uint64()))
span.SetSpanID(tracetranslator.UInt64ToSpanID(r.Uint64()))
span.SetTraceID(idutils.UInt64ToTraceID(r.Uint64(), r.Uint64()))
span.SetSpanID(idutils.UInt64ToSpanID(r.Uint64()))
attributes := make(map[string]pdata.AttributeValue)
attributes[conventions.AttributeHTTPStatusCode] = pdata.NewAttributeValueInt(404)
attributes[conventions.AttributeHTTPStatusText] = pdata.NewAttributeValueString("Not Found")
Expand Down
11 changes: 6 additions & 5 deletions translator/trace/jaeger/jaegerproto_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"

"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/internal/occonventions"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
Expand Down Expand Up @@ -164,15 +165,15 @@ type instrumentationLibrary struct {

func jSpanToInternal(span *model.Span) (pdata.Span, instrumentationLibrary) {
dest := pdata.NewSpan()
dest.SetTraceID(tracetranslator.UInt64ToTraceID(span.TraceID.High, span.TraceID.Low))
dest.SetSpanID(tracetranslator.UInt64ToSpanID(uint64(span.SpanID)))
dest.SetTraceID(idutils.UInt64ToTraceID(span.TraceID.High, span.TraceID.Low))
dest.SetSpanID(idutils.UInt64ToSpanID(uint64(span.SpanID)))
dest.SetName(span.OperationName)
dest.SetStartTimestamp(pdata.TimestampFromTime(span.StartTime))
dest.SetEndTimestamp(pdata.TimestampFromTime(span.StartTime.Add(span.Duration)))

parentSpanID := span.ParentSpanID()
if parentSpanID != model.SpanID(0) {
dest.SetParentSpanID(tracetranslator.UInt64ToSpanID(uint64(parentSpanID)))
dest.SetParentSpanID(idutils.UInt64ToSpanID(uint64(parentSpanID)))
}

attrs := dest.Attributes()
Expand Down Expand Up @@ -357,8 +358,8 @@ func jReferencesToSpanLinks(refs []model.SpanRef, excludeParentID model.SpanID,
continue
}

link.SetTraceID(tracetranslator.UInt64ToTraceID(ref.TraceID.High, ref.TraceID.Low))
link.SetSpanID(tracetranslator.UInt64ToSpanID(uint64(ref.SpanID)))
link.SetTraceID(idutils.UInt64ToTraceID(ref.TraceID.High, ref.TraceID.Low))
link.SetSpanID(idutils.UInt64ToSpanID(uint64(ref.SpanID)))
i++
}

Expand Down
5 changes: 3 additions & 2 deletions translator/trace/jaeger/jaegerproto_to_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
Expand Down Expand Up @@ -843,8 +844,8 @@ func generateTraceDataTwoSpansFromTwoLibraries() pdata.Traces {
rs0ils0.InstrumentationLibrary().SetName("library1")
rs0ils0.InstrumentationLibrary().SetVersion("0.42.0")
span1 := rs0ils0.Spans().AppendEmpty()
span1.SetTraceID(tracetranslator.UInt64ToTraceID(0, 0))
span1.SetSpanID(tracetranslator.UInt64ToSpanID(0))
span1.SetTraceID(idutils.UInt64ToTraceID(0, 0))
span1.SetSpanID(idutils.UInt64ToSpanID(0))
span1.SetName("operation1")
span1.SetStartTimestamp(testSpanStartTimestamp)
span1.SetEndTimestamp(testSpanEndTimestamp)
Expand Down
11 changes: 6 additions & 5 deletions translator/trace/jaeger/jaegerthrift_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"

"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)
Expand Down Expand Up @@ -95,15 +96,15 @@ func jThriftSpansToInternal(spans []*jaeger.Span, dest pdata.SpanSlice) {
}

func jThriftSpanToInternal(span *jaeger.Span, dest pdata.Span) {
dest.SetTraceID(tracetranslator.UInt64ToTraceID(uint64(span.TraceIdHigh), uint64(span.TraceIdLow)))
dest.SetSpanID(tracetranslator.UInt64ToSpanID(uint64(span.SpanId)))
dest.SetTraceID(idutils.UInt64ToTraceID(uint64(span.TraceIdHigh), uint64(span.TraceIdLow)))
dest.SetSpanID(idutils.UInt64ToSpanID(uint64(span.SpanId)))
dest.SetName(span.OperationName)
dest.SetStartTimestamp(microsecondsToUnixNano(span.StartTime))
dest.SetEndTimestamp(microsecondsToUnixNano(span.StartTime + span.Duration))

parentSpanID := span.ParentSpanId
if parentSpanID != 0 {
dest.SetParentSpanID(tracetranslator.UInt64ToSpanID(uint64(parentSpanID)))
dest.SetParentSpanID(idutils.UInt64ToSpanID(uint64(parentSpanID)))
}

attrs := dest.Attributes()
Expand Down Expand Up @@ -184,8 +185,8 @@ func jThriftReferencesToSpanLinks(refs []*jaeger.SpanRef, excludeParentID int64,
continue
}

link.SetTraceID(tracetranslator.UInt64ToTraceID(uint64(ref.TraceIdHigh), uint64(ref.TraceIdLow)))
link.SetSpanID(tracetranslator.UInt64ToSpanID(uint64(ref.SpanId)))
link.SetTraceID(idutils.UInt64ToTraceID(uint64(ref.TraceIdHigh), uint64(ref.TraceIdLow)))
link.SetSpanID(idutils.UInt64ToSpanID(uint64(ref.SpanId)))
i++
}

Expand Down
5 changes: 3 additions & 2 deletions translator/trace/jaeger/traces_to_jaegerproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/jaegertracing/jaeger/model"

"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)
Expand Down Expand Up @@ -252,7 +253,7 @@ func getJaegerProtoSpanTags(span pdata.Span, instrumentationLibrary pdata.Instru
}

func traceIDToJaegerProto(traceID pdata.TraceID) (model.TraceID, error) {
traceIDHigh, traceIDLow := tracetranslator.TraceIDToUInt64Pair(traceID)
traceIDHigh, traceIDLow := idutils.TraceIDToUInt64Pair(traceID)
if traceIDLow == 0 && traceIDHigh == 0 {
return model.TraceID{}, errZeroTraceID
}
Expand All @@ -263,7 +264,7 @@ func traceIDToJaegerProto(traceID pdata.TraceID) (model.TraceID, error) {
}

func spanIDToJaegerProto(spanID pdata.SpanID) (model.SpanID, error) {
uSpanID := tracetranslator.SpanIDToUInt64(spanID)
uSpanID := idutils.SpanIDToUInt64(spanID)
if uSpanID == 0 {
return model.SpanID(0), errZeroSpanID
}
Expand Down
5 changes: 3 additions & 2 deletions translator/trace/zipkin/traces_to_zipkinv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
zipkinmodel "github.com/openzipkin/zipkin-go/model"

"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)
Expand Down Expand Up @@ -358,10 +359,10 @@ func isIPv6Address(ipStr string) bool {
}

func convertTraceID(t pdata.TraceID) zipkinmodel.TraceID {
h, l := tracetranslator.TraceIDToUInt64Pair(t)
h, l := idutils.TraceIDToUInt64Pair(t)
return zipkinmodel.TraceID{High: h, Low: l}
}

func convertSpanID(s pdata.SpanID) zipkinmodel.ID {
return zipkinmodel.ID(tracetranslator.SpanIDToUInt64(s))
return zipkinmodel.ID(idutils.SpanIDToUInt64(s))
}
8 changes: 4 additions & 4 deletions translator/trace/zipkin/zipkinv1_thrift_to_protospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
"google.golang.org/protobuf/types/known/timestamppb"

tracetranslator "go.opentelemetry.io/collector/translator/trace"
idutils "go.opentelemetry.io/collector/internal/idutils"
)

// v1ThriftBatchToOCProto converts Zipkin v1 spans to OC Proto.
Expand Down Expand Up @@ -55,11 +55,11 @@ func zipkinV1ThriftToOCSpan(zSpan *zipkincore.Span) (*tracepb.Span, *annotationP
// failures on the receivers in general are silent at this moment, so letting them
// proceed for now. We should validate the traceID, spanID and parentID are good with
// OC proto requirements.
traceID := tracetranslator.UInt64ToTraceID(uint64(traceIDHigh), uint64(zSpan.TraceID)).Bytes()
spanID := tracetranslator.UInt64ToSpanID(uint64(zSpan.ID)).Bytes()
traceID := idutils.UInt64ToTraceID(uint64(traceIDHigh), uint64(zSpan.TraceID)).Bytes()
spanID := idutils.UInt64ToSpanID(uint64(zSpan.ID)).Bytes()
var parentID []byte
if zSpan.ParentID != nil {
parentIDBytes := tracetranslator.UInt64ToSpanID(uint64(*zSpan.ParentID)).Bytes()
parentIDBytes := idutils.UInt64ToSpanID(uint64(*zSpan.ParentID)).Bytes()
parentID = parentIDBytes[:]
}

Expand Down
5 changes: 3 additions & 2 deletions translator/trace/zipkin/zipkinv1_to_protospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"go.opentelemetry.io/collector/consumer/pdata"
idutils "go.opentelemetry.io/collector/internal/idutils"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
)

Expand Down Expand Up @@ -414,7 +415,7 @@ func hexTraceIDToOCTraceID(hex string) ([]byte, error) {
return nil, errHexTraceIDZero
}

tidBytes := tracetranslator.UInt64ToTraceID(high, low).Bytes()
tidBytes := idutils.UInt64ToTraceID(high, low).Bytes()
return tidBytes[:], nil
}

Expand All @@ -433,7 +434,7 @@ func hexIDToOCID(hex string) ([]byte, error) {
return nil, errHexIDZero
}

idBytes := tracetranslator.UInt64ToSpanID(idValue).Bytes()
idBytes := idutils.UInt64ToSpanID(idValue).Bytes()
return idBytes[:], nil
}

Expand Down
7 changes: 4 additions & 3 deletions translator/trace/zipkin/zipkinv2_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/data"
otlptrace "go.opentelemetry.io/collector/internal/data/protogen/trace/v1"
idutils "go.opentelemetry.io/collector/internal/idutils"
"go.opentelemetry.io/collector/internal/occonventions"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
Expand Down Expand Up @@ -123,15 +124,15 @@ func V2SpansToInternalTraces(zipkinSpans []*zipkinmodel.SpanModel, parseStringTa
}

func zSpanToInternal(zspan *zipkinmodel.SpanModel, tags map[string]string, dest pdata.Span, parseStringTags bool) error {
dest.SetTraceID(tracetranslator.UInt64ToTraceID(zspan.TraceID.High, zspan.TraceID.Low))
dest.SetSpanID(tracetranslator.UInt64ToSpanID(uint64(zspan.ID)))
dest.SetTraceID(idutils.UInt64ToTraceID(zspan.TraceID.High, zspan.TraceID.Low))
dest.SetSpanID(idutils.UInt64ToSpanID(uint64(zspan.ID)))
if value, ok := tags[tracetranslator.TagW3CTraceState]; ok {
dest.SetTraceState(pdata.TraceState(value))
delete(tags, tracetranslator.TagW3CTraceState)
}
parentID := zspan.ParentID
if parentID != nil && *parentID != zspan.ID {
dest.SetParentSpanID(tracetranslator.UInt64ToSpanID(uint64(*parentID)))
dest.SetParentSpanID(idutils.UInt64ToSpanID(uint64(*parentID)))
}

dest.SetName(zspan.Name)
Expand Down