Skip to content

Commit 156a59d

Browse files
authored
Separate model parts into more independent pieces (jaegertracing#6581)
## Which problem is this PR solving? - Part of jaegertracing#6571 - Follow-up to jaegertracing/jaeger-idl#118 ## Description of the changes - Separate model files into more independent pieces that are easier to move to jaeger-idl ## How was this change tested? - CI Signed-off-by: Yuri Shkuro <[email protected]>
1 parent 4fbffc9 commit 156a59d

9 files changed

+314
-256
lines changed

model/flags.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2019 The Jaeger Authors.
2+
// Copyright (c) 2017 Uber Technologies, Inc.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package model
6+
7+
const (
8+
// SampledFlag is the bit set in Flags in order to define a span as a sampled span
9+
SampledFlag = Flags(1)
10+
// DebugFlag is the bit set in Flags in order to define a span as a debug span
11+
DebugFlag = Flags(2)
12+
// FirehoseFlag is the bit in Flags in order to define a span as a firehose span
13+
FirehoseFlag = Flags(8)
14+
)
15+
16+
// Flags is a bit map of flags for a span
17+
type Flags uint32
18+
19+
// ------- Flags -------
20+
21+
// SetSampled sets the Flags as sampled
22+
func (f *Flags) SetSampled() {
23+
f.setFlags(SampledFlag)
24+
}
25+
26+
// SetDebug set the Flags as sampled
27+
func (f *Flags) SetDebug() {
28+
f.setFlags(DebugFlag)
29+
}
30+
31+
// SetFirehose set the Flags as firehose enabled
32+
func (f *Flags) SetFirehose() {
33+
f.setFlags(FirehoseFlag)
34+
}
35+
36+
func (f *Flags) setFlags(bit Flags) {
37+
*f |= bit
38+
}
39+
40+
// IsSampled returns true if the Flags denote sampling
41+
func (f Flags) IsSampled() bool {
42+
return f.checkFlags(SampledFlag)
43+
}
44+
45+
// IsDebug returns true if the Flags denote debugging
46+
// Debugging can be useful in testing tracing availability or correctness
47+
func (f Flags) IsDebug() bool {
48+
return f.checkFlags(DebugFlag)
49+
}
50+
51+
// IsFirehoseEnabled returns true if firehose is enabled
52+
// Firehose is used to decide whether to index a span or not
53+
func (f Flags) IsFirehoseEnabled() bool {
54+
return f.checkFlags(FirehoseFlag)
55+
}
56+
57+
func (f Flags) checkFlags(bit Flags) bool {
58+
return f&bit == bit
59+
}

model/flags_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2019 The Jaeger Authors.
2+
// Copyright (c) 2017 Uber Technologies, Inc.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package model_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/jaegertracing/jaeger/model"
13+
)
14+
15+
func TestIsDebug(t *testing.T) {
16+
flags := model.Flags(0)
17+
flags.SetDebug()
18+
assert.True(t, flags.IsDebug())
19+
flags = model.Flags(0)
20+
assert.False(t, flags.IsDebug())
21+
22+
flags = model.Flags(32)
23+
assert.False(t, flags.IsDebug())
24+
flags.SetDebug()
25+
assert.True(t, flags.IsDebug())
26+
}
27+
28+
func TestIsFirehoseEnabled(t *testing.T) {
29+
flags := model.Flags(0)
30+
assert.False(t, flags.IsFirehoseEnabled())
31+
flags.SetDebug()
32+
flags.SetSampled()
33+
assert.False(t, flags.IsFirehoseEnabled())
34+
flags.SetFirehose()
35+
assert.True(t, flags.IsFirehoseEnabled())
36+
37+
flags = model.Flags(8)
38+
assert.True(t, flags.IsFirehoseEnabled())
39+
}
40+
41+
func TestIsSampled(t *testing.T) {
42+
flags := model.Flags(0)
43+
flags.SetSampled()
44+
assert.True(t, flags.IsSampled())
45+
flags = model.Flags(0)
46+
flags.SetDebug()
47+
assert.False(t, flags.IsSampled())
48+
}

model/ids.go

-34
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strconv"
1313

1414
"github.com/gogo/protobuf/jsonpb"
15-
"go.opentelemetry.io/collector/pdata/pcommon"
1615
)
1716

1817
const (
@@ -146,23 +145,6 @@ func (t *TraceID) UnmarshalJSON(data []byte) error {
146145
return t.Unmarshal(b)
147146
}
148147

149-
// ToOTELTraceID converts the TraceID to OTEL's representation of a trace identitfier.
150-
// This was taken from
151-
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/internal/coreinternal/idutils/big_endian_converter.go.
152-
func (t *TraceID) ToOTELTraceID() pcommon.TraceID {
153-
traceID := [16]byte{}
154-
binary.BigEndian.PutUint64(traceID[:8], t.High)
155-
binary.BigEndian.PutUint64(traceID[8:], t.Low)
156-
return traceID
157-
}
158-
159-
func TraceIDFromOTEL(traceID pcommon.TraceID) TraceID {
160-
return TraceID{
161-
High: binary.BigEndian.Uint64(traceID[:traceIDShortBytesLen]),
162-
Low: binary.BigEndian.Uint64(traceID[traceIDShortBytesLen:]),
163-
}
164-
}
165-
166148
// ------- SpanID -------
167149

168150
// NewSpanID creates a new SpanID from a 64bit unsigned int.
@@ -261,19 +243,3 @@ func (s *SpanID) UnmarshalJSON(data []byte) error {
261243
func (s *SpanID) UnmarshalJSONPB(_ *jsonpb.Unmarshaler, b []byte) error {
262244
return s.UnmarshalJSON(b)
263245
}
264-
265-
// ToOTELSpanID converts the SpanID to OTEL's representation of a span identitfier.
266-
// This was taken from
267-
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/internal/coreinternal/idutils/big_endian_converter.go.
268-
func (s SpanID) ToOTELSpanID() pcommon.SpanID {
269-
spanID := [8]byte{}
270-
binary.BigEndian.PutUint64(spanID[:], uint64(s))
271-
return pcommon.SpanID(spanID)
272-
}
273-
274-
// ToOTELSpanID converts OTEL's SpanID to the model representation of a span identitfier.
275-
// This was taken from
276-
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/internal/coreinternal/idutils/big_endian_converter.go.
277-
func SpanIDFromOTEL(spanID pcommon.SpanID) SpanID {
278-
return SpanID(binary.BigEndian.Uint64(spanID[:]))
279-
}

model/ids_proto_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2019 The Jaeger Authors.
2+
// Copyright (c) 2018 Uber Technologies, Inc.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package model_test
6+
7+
import (
8+
"bytes"
9+
"testing"
10+
11+
"github.com/gogo/protobuf/jsonpb"
12+
"github.com/gogo/protobuf/proto"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
16+
"github.com/jaegertracing/jaeger/model"
17+
"github.com/jaegertracing/jaeger/model/prototest"
18+
)
19+
20+
// TraceID/SpanID fields are defined as bytes in proto, backed by custom types in Go.
21+
// Unfortunately, that means they require manual implementations of proto & json serialization.
22+
// To ensure that it's the same as the default protobuf serialization, file jaeger_test.proto
23+
// contains a copy of SpanRef message without any gogo options. This test file is compiled with
24+
// plain protoc -go_out (without gogo). This test performs proto and JSON marshaling/unmarshaling
25+
// to ensure that the outputs of manual and standard serialization are identical.
26+
func TestTraceSpanIDMarshalProto(t *testing.T) {
27+
testCases := []struct {
28+
name string
29+
marshal func(proto.Message) ([]byte, error)
30+
unmarshal func([]byte, proto.Message) error
31+
expected string
32+
}{
33+
{
34+
name: "protobuf",
35+
marshal: proto.Marshal,
36+
unmarshal: proto.Unmarshal,
37+
},
38+
{
39+
name: "JSON",
40+
marshal: func(m proto.Message) ([]byte, error) {
41+
out := new(bytes.Buffer)
42+
err := new(jsonpb.Marshaler).Marshal(out, m)
43+
if err != nil {
44+
return nil, err
45+
}
46+
return out.Bytes(), nil
47+
},
48+
unmarshal: func(in []byte, m proto.Message) error {
49+
return jsonpb.Unmarshal(bytes.NewReader(in), m)
50+
},
51+
expected: `{"traceId":"AAAAAAAAAAIAAAAAAAAAAw==","spanId":"AAAAAAAAAAs="}`,
52+
},
53+
}
54+
for _, testCase := range testCases {
55+
t.Run(testCase.name, func(t *testing.T) {
56+
ref1 := model.SpanRef{TraceID: model.NewTraceID(2, 3), SpanID: model.NewSpanID(11)}
57+
ref2 := prototest.SpanRef{
58+
// TODO: would be cool to fuzz that test
59+
TraceId: []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3},
60+
SpanId: []byte{0, 0, 0, 0, 0, 0, 0, 11},
61+
}
62+
d1, err := testCase.marshal(&ref1)
63+
require.NoError(t, err)
64+
d2, err := testCase.marshal(&ref2)
65+
require.NoError(t, err)
66+
assert.Equal(t, d2, d1)
67+
if testCase.expected != "" {
68+
assert.Equal(t, testCase.expected, string(d1))
69+
}
70+
// test unmarshal
71+
var ref1u model.SpanRef
72+
err = testCase.unmarshal(d2, &ref1u)
73+
require.NoError(t, err)
74+
assert.Equal(t, ref1, ref1u)
75+
})
76+
}
77+
}

model/ids_test.go

-135
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,14 @@
55
package model_test
66

77
import (
8-
"bytes"
98
"testing"
109

11-
"github.com/gogo/protobuf/jsonpb"
12-
"github.com/gogo/protobuf/proto"
1310
"github.com/stretchr/testify/assert"
1411
"github.com/stretchr/testify/require"
15-
"go.opentelemetry.io/collector/pdata/pcommon"
1612

1713
"github.com/jaegertracing/jaeger/model"
18-
"github.com/jaegertracing/jaeger/model/prototest"
1914
)
2015

21-
// TraceID/SpanID fields are defined as bytes in proto, backed by custom types in Go.
22-
// Unfortunately, that means they require manual implementations of proto & json serialization.
23-
// To ensure that it's the same as the default protobuf serialization, file jaeger_test.proto
24-
// contains a copy of SpanRef message without any gogo options. This test file is compiled with
25-
// plain protoc -go_out (without gogo). This test performs proto and JSON marshaling/unmarshaling
26-
// to ensure that the outputs of manual and standard serialization are identical.
27-
func TestTraceSpanIDMarshalProto(t *testing.T) {
28-
testCases := []struct {
29-
name string
30-
marshal func(proto.Message) ([]byte, error)
31-
unmarshal func([]byte, proto.Message) error
32-
expected string
33-
}{
34-
{
35-
name: "protobuf",
36-
marshal: proto.Marshal,
37-
unmarshal: proto.Unmarshal,
38-
},
39-
{
40-
name: "JSON",
41-
marshal: func(m proto.Message) ([]byte, error) {
42-
out := new(bytes.Buffer)
43-
err := new(jsonpb.Marshaler).Marshal(out, m)
44-
if err != nil {
45-
return nil, err
46-
}
47-
return out.Bytes(), nil
48-
},
49-
unmarshal: func(in []byte, m proto.Message) error {
50-
return jsonpb.Unmarshal(bytes.NewReader(in), m)
51-
},
52-
expected: `{"traceId":"AAAAAAAAAAIAAAAAAAAAAw==","spanId":"AAAAAAAAAAs="}`,
53-
},
54-
}
55-
for _, testCase := range testCases {
56-
t.Run(testCase.name, func(t *testing.T) {
57-
ref1 := model.SpanRef{TraceID: model.NewTraceID(2, 3), SpanID: model.NewSpanID(11)}
58-
ref2 := prototest.SpanRef{
59-
// TODO: would be cool to fuzz that test
60-
TraceId: []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3},
61-
SpanId: []byte{0, 0, 0, 0, 0, 0, 0, 11},
62-
}
63-
d1, err := testCase.marshal(&ref1)
64-
require.NoError(t, err)
65-
d2, err := testCase.marshal(&ref2)
66-
require.NoError(t, err)
67-
assert.Equal(t, d2, d1)
68-
if testCase.expected != "" {
69-
assert.Equal(t, testCase.expected, string(d1))
70-
}
71-
// test unmarshal
72-
var ref1u model.SpanRef
73-
err = testCase.unmarshal(d2, &ref1u)
74-
require.NoError(t, err)
75-
assert.Equal(t, ref1, ref1u)
76-
})
77-
}
78-
}
79-
8016
func TestSpanIDFromBytes(t *testing.T) {
8117
errTests := [][]byte{
8218
{0, 0, 0, 0},
@@ -118,74 +54,3 @@ func TestTraceIDFromBytes(t *testing.T) {
11854
assert.Equal(t, test.expected, traceID)
11955
}
12056
}
121-
122-
func TestToOTELTraceID(t *testing.T) {
123-
modelTraceID := model.TraceID{
124-
Low: 3,
125-
High: 2,
126-
}
127-
otelTraceID := modelTraceID.ToOTELTraceID()
128-
expected := []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}
129-
require.Equal(t, pcommon.TraceID(expected), otelTraceID)
130-
}
131-
132-
func TestTraceIDFromOTEL(t *testing.T) {
133-
otelTraceID := pcommon.TraceID([]byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3})
134-
expected := model.TraceID{
135-
Low: 3,
136-
High: 2,
137-
}
138-
require.Equal(t, expected, model.TraceIDFromOTEL(otelTraceID))
139-
}
140-
141-
func TestToOTELSpanID(t *testing.T) {
142-
tests := []struct {
143-
name string
144-
spanID model.SpanID
145-
expected pcommon.SpanID
146-
}{
147-
{
148-
name: "zero span ID",
149-
spanID: model.NewSpanID(0),
150-
expected: pcommon.NewSpanIDEmpty(),
151-
},
152-
{
153-
name: "non-zero span ID",
154-
spanID: model.NewSpanID(1),
155-
expected: pcommon.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}),
156-
},
157-
}
158-
159-
for _, test := range tests {
160-
t.Run(test.name, func(t *testing.T) {
161-
actual := test.spanID.ToOTELSpanID()
162-
assert.Equal(t, test.expected, actual)
163-
})
164-
}
165-
}
166-
167-
func TestSpanIDFromOTEL(t *testing.T) {
168-
tests := []struct {
169-
name string
170-
otelSpanID pcommon.SpanID
171-
expected model.SpanID
172-
}{
173-
{
174-
name: "zero span ID",
175-
otelSpanID: pcommon.NewSpanIDEmpty(),
176-
expected: model.NewSpanID(0),
177-
},
178-
{
179-
name: "non-zero span ID",
180-
otelSpanID: pcommon.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}),
181-
expected: model.NewSpanID(1),
182-
},
183-
}
184-
185-
for _, test := range tests {
186-
t.Run(test.name, func(t *testing.T) {
187-
actual := model.SpanIDFromOTEL(test.otelSpanID)
188-
assert.Equal(t, test.expected, actual)
189-
})
190-
}
191-
}

0 commit comments

Comments
 (0)