Skip to content

Commit

Permalink
Use custom data type and custom JSON serialization for traceid (#1840)
Browse files Browse the repository at this point in the history
Contributes to #1177

1. The TraceID type uses custom data type so that JSON serialization
is in hex format instead of base64 (which is the default Protobuf JSON
format). Hex format is required by OTLP spec:
https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/protocol/otlp.md#request
SpanID must be also modified similarly. Will be done in a future PR
to avoid creating a huge PR.

2. Moved pdata.TraceID to its own file. Note that there is pdata.TraceID which
is different from otlp TraceID custom data type. Due to the way packages are
structured we need both to keep OTLP generated data types decoupled from pdata
data types.

The majority of the changes in this commit are simply type changes from
[]byte to TraceID.
  • Loading branch information
tigrannajaryan authored Sep 24, 2020
1 parent 430c002 commit 75b1ac1
Show file tree
Hide file tree
Showing 55 changed files with 662 additions and 530 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ genproto_sub:
sed -f proto_patch.sed $(OPENTELEMETRY_PROTO_SRC_DIR)/opentelemetry/proto/logs/v1/logs.proto \
> $(PROTO_INTERMEDIATE_DIR)/opentelemetry/proto/logs/v1/logs.proto

sed -f proto_patch.sed $(OPENTELEMETRY_PROTO_SRC_DIR)/opentelemetry/proto/metrics/v1/metrics.proto \
> $(PROTO_INTERMEDIATE_DIR)/opentelemetry/proto/metrics/v1/metrics.proto

@echo Generate Go code from .proto files in intermediate directory.
$(foreach file,$(OPENTELEMETRY_PROTO_FILES),$(call exec-command,cd $(PROTO_INTERMEDIATE_DIR) && protoc --gogofaster_out=plugins=grpc:./ -I./ -I$(GO_PKG_DIR) $(file)))

Expand Down
1 change: 1 addition & 0 deletions cmd/pdatagen/internal/log_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package internal
var logFile = &File{
Name: "log",
imports: []string{
`otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"`,
`otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/logs/v1"`,
},
testImports: []string{
Expand Down
3 changes: 2 additions & 1 deletion cmd/pdatagen/internal/trace_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package internal
var traceFile = &File{
Name: "trace",
imports: []string{
`otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"`,
`otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1"`,
},
testImports: []string{
Expand Down Expand Up @@ -204,7 +205,7 @@ var traceIDField = &primitiveTypedField{
fieldName: "TraceID",
originFieldName: "TraceId",
returnType: "TraceID",
rawType: "[]byte",
rawType: "otlpcommon.TraceID",
defaultVal: "NewTraceID(nil)",
testVal: "NewTraceID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})",
}
Expand Down
3 changes: 2 additions & 1 deletion consumer/pdata/generated_log.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions consumer/pdata/generated_trace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion consumer/pdata/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestResourceMetricsWireCompatibility(t *testing.T) {

// Now compare that the original and final ProtoBuf messages are the same.
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
assert.True(t, gogoproto.Equal(*pdataRM.orig, &gogoprotoRM))
assert.True(t, assert.EqualValues(t, *pdataRM.orig, &gogoprotoRM))
}

func TestMetricCount(t *testing.T) {
Expand Down
11 changes: 0 additions & 11 deletions consumer/pdata/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ func (td Traces) ResourceSpans() ResourceSpansSlice {
return newResourceSpansSlice(td.orig)
}

type TraceID []byte

// NewTraceID returns a new TraceID.
func NewTraceID(bytes []byte) TraceID { return bytes }

func (t TraceID) Bytes() []byte {
return t
}

func (t TraceID) String() string { return hex.EncodeToString(t) }

type SpanID []byte

// NewSpanID returns a new SpanID.
Expand Down
2 changes: 1 addition & 1 deletion consumer/pdata/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestResourceSpansWireCompatibility(t *testing.T) {

// Now compare that the original and final ProtoBuf messages are the same.
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
assert.True(t, gogoproto.Equal(*pdataRS.orig, &gogoprotoRS2))
assert.EqualValues(t, *pdataRS.orig, &gogoprotoRS2)
}

func TestTraces_ToOtlpProtoBytes(t *testing.T) {
Expand Down
34 changes: 34 additions & 0 deletions consumer/pdata/traceid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 pdata

import (
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
)

// TraceID is an alias of OTLP TraceID data type.
type TraceID otlpcommon.TraceID

func NewTraceID(bytes []byte) TraceID {
return TraceID(otlpcommon.NewTraceID(bytes))
}

func (t TraceID) Bytes() []byte {
return otlpcommon.TraceID(t).Bytes()
}

func (t TraceID) HexString() string {
return otlpcommon.TraceID(t).HexString()
}
5 changes: 3 additions & 2 deletions exporter/jaegerexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer/pdata"
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
tracev1 "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1"
"go.opentelemetry.io/collector/internal/data/testdata"
)
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestMutualTLS(t *testing.T) {
require.NoError(t, err)
defer exporter.Shutdown(context.Background())

traceID := []byte("0123456789abcdef")
traceID := otlpcommon.NewTraceID([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
spanID := []byte("01234567")
traces := pdata.TracesFromOtlp([]*tracev1.ResourceSpans{
{InstrumentationLibrarySpans: []*tracev1.InstrumentationLibrarySpans{{Spans: []*tracev1.Span{{TraceId: traceID, SpanId: spanID}}}}},
Expand All @@ -235,7 +236,7 @@ func TestMutualTLS(t *testing.T) {
require.NoError(t, err)
requestes := spanHandler.getRequests()
assert.Equal(t, 1, len(requestes))
jTraceID, err := model.TraceIDFromBytes(traceID)
jTraceID, err := model.TraceIDFromBytes(traceID.Bytes())
require.NoError(t, err)
assert.Equal(t, jTraceID, requestes[0].GetBatch().Spans[0].TraceID)
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (b *logDataBuffer) logLinks(description string, sl pdata.SpanLinkSlice) {
continue
}
b.logEntry("SpanLink #%d", i)
b.logEntry(" -> Trace ID: %s", l.TraceID().String())
b.logEntry(" -> Trace ID: %s", l.TraceID().HexString())
b.logEntry(" -> ID: %s", l.SpanID().String())
b.logEntry(" -> TraceState: %s", l.TraceState())
b.logEntry(" -> DroppedAttributesCount: %d", l.DroppedAttributesCount())
Expand Down Expand Up @@ -332,7 +332,7 @@ func (s *loggingExporter) pushTraceData(
continue
}

buf.logAttr("Trace ID", span.TraceID().String())
buf.logAttr("Trace ID", span.TraceID().HexString())
buf.logAttr("Parent ID", span.ParentSpanID().String())
buf.logAttr("ID", span.SpanID().String())
buf.logAttr("Name", span.Name())
Expand Down
Loading

0 comments on commit 75b1ac1

Please sign in to comment.