Skip to content

Commit e72553a

Browse files
authored
Encapsulate the internal representation of the ids. Ensure we don't leak it by mistake (#2946)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent f6074af commit e72553a

File tree

7 files changed

+101
-36
lines changed

7 files changed

+101
-36
lines changed

cmd/pdatagen/internal/base_fields.go

+66
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ func (ms ${structName}) ${fieldName}() ${returnType} {
7676
return ${returnType}((*ms.orig).${originFieldName})
7777
}`
7878

79+
const accessorsPrimitiveStructTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
80+
func (ms ${structName}) ${fieldName}() ${returnType} {
81+
return ${returnType}{orig: ((*ms.orig).${originFieldName})}
82+
}
83+
84+
// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
85+
func (ms ${structName}) Set${fieldName}(v ${returnType}) {
86+
(*ms.orig).${originFieldName} = v.orig
87+
}`
88+
7989
type baseField interface {
8090
generateAccessors(ms baseStruct, sb *strings.Builder)
8191

@@ -305,6 +315,62 @@ func (ptf *primitiveTypedField) generateCopyToValue(sb *strings.Builder) {
305315

306316
var _ baseField = (*primitiveTypedField)(nil)
307317

318+
// Types that has defined a custom type (e.g. "type TraceID struct {}")
319+
type primitiveStructField struct {
320+
fieldName string
321+
originFieldName string
322+
returnType string
323+
defaultVal string
324+
testVal string
325+
}
326+
327+
func (ptf *primitiveStructField) generateAccessors(ms baseStruct, sb *strings.Builder) {
328+
template := accessorsPrimitiveStructTemplate
329+
sb.WriteString(os.Expand(template, func(name string) string {
330+
switch name {
331+
case "structName":
332+
return ms.getName()
333+
case "fieldName":
334+
return ptf.fieldName
335+
case "lowerFieldName":
336+
return strings.ToLower(ptf.fieldName)
337+
case "returnType":
338+
return ptf.returnType
339+
case "originFieldName":
340+
return ptf.originFieldName
341+
default:
342+
panic(name)
343+
}
344+
}))
345+
}
346+
347+
func (ptf *primitiveStructField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
348+
sb.WriteString(os.Expand(accessorsPrimitiveTestTemplate, func(name string) string {
349+
switch name {
350+
case "structName":
351+
return ms.getName()
352+
case "defaultVal":
353+
return ptf.defaultVal
354+
case "fieldName":
355+
return ptf.fieldName
356+
case "testValue":
357+
return ptf.testVal
358+
default:
359+
panic(name)
360+
}
361+
}))
362+
}
363+
364+
func (ptf *primitiveStructField) generateSetWithTestValue(sb *strings.Builder) {
365+
sb.WriteString("\ttv.Set" + ptf.fieldName + "(" + ptf.testVal + ")")
366+
}
367+
368+
func (ptf *primitiveStructField) generateCopyToValue(sb *strings.Builder) {
369+
sb.WriteString("\tdest.Set" + ptf.fieldName + "(ms." + ptf.fieldName + "())")
370+
}
371+
372+
var _ baseField = (*primitiveStructField)(nil)
373+
308374
// oneofField is used in case where the proto defines an "oneof".
309375
type oneofField struct {
310376
copyFuncName string

cmd/pdatagen/internal/trace_structs.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -206,29 +206,26 @@ var spanStatus = &messageValueStruct{
206206
},
207207
}
208208

209-
var traceIDField = &primitiveTypedField{
209+
var traceIDField = &primitiveStructField{
210210
fieldName: "TraceID",
211211
originFieldName: "TraceId",
212212
returnType: "TraceID",
213-
rawType: "data.TraceID",
214213
defaultVal: "NewTraceID([16]byte{})",
215214
testVal: "NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})",
216215
}
217216

218-
var spanIDField = &primitiveTypedField{
217+
var spanIDField = &primitiveStructField{
219218
fieldName: "SpanID",
220219
originFieldName: "SpanId",
221220
returnType: "SpanID",
222-
rawType: "data.SpanID",
223221
defaultVal: "NewSpanID([8]byte{})",
224222
testVal: "NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})",
225223
}
226224

227-
var parentSpanIDField = &primitiveTypedField{
225+
var parentSpanIDField = &primitiveStructField{
228226
fieldName: "ParentSpanID",
229227
originFieldName: "ParentSpanId",
230228
returnType: "SpanID",
231-
rawType: "data.SpanID",
232229
defaultVal: "NewSpanID([8]byte{})",
233230
testVal: "NewSpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1})",
234231
}

consumer/pdata/generated_log.go

+4-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

consumer/pdata/generated_trace.go

+10-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

consumer/pdata/spanid.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,31 @@ import (
1919
)
2020

2121
// SpanID is an alias of OTLP SpanID data type.
22-
type SpanID data.SpanID
22+
type SpanID struct {
23+
orig data.SpanID
24+
}
2325

2426
// InvalidSpanID returns an empty (all zero bytes) SpanID.
2527
func InvalidSpanID() SpanID {
26-
return SpanID(data.NewSpanID([8]byte{}))
28+
return SpanID{orig: data.NewSpanID([8]byte{})}
2729
}
2830

2931
// NewSpanID returns a new SpanID from the given byte array.
3032
func NewSpanID(bytes [8]byte) SpanID {
31-
return SpanID(data.NewSpanID(bytes))
33+
return SpanID{orig: data.NewSpanID(bytes)}
3234
}
3335

3436
// Bytes returns the byte array representation of the SpanID.
3537
func (t SpanID) Bytes() [8]byte {
36-
return data.SpanID(t).Bytes()
38+
return t.orig.Bytes()
3739
}
3840

3941
// HexString returns hex representation of the SpanID.
4042
func (t SpanID) HexString() string {
41-
return data.SpanID(t).HexString()
43+
return t.orig.HexString()
4244
}
4345

4446
// IsEmpty returns true if id doesn't contain at least one non-zero byte.
4547
func (t SpanID) IsEmpty() bool {
46-
return data.SpanID(t).IsEmpty()
48+
return t.orig.IsEmpty()
4749
}

consumer/pdata/traceid.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,31 @@ import (
1919
)
2020

2121
// TraceID is an alias of OTLP TraceID data type.
22-
type TraceID data.TraceID
22+
type TraceID struct {
23+
orig data.TraceID
24+
}
2325

2426
// InvalidTraceID returns an empty (all zero bytes) TraceID.
2527
func InvalidTraceID() TraceID {
26-
return TraceID(data.NewTraceID([16]byte{}))
28+
return TraceID{orig: data.NewTraceID([16]byte{})}
2729
}
2830

2931
// NewTraceID returns a new TraceID from the given byte array.
3032
func NewTraceID(bytes [16]byte) TraceID {
31-
return TraceID(data.NewTraceID(bytes))
33+
return TraceID{orig: data.NewTraceID(bytes)}
3234
}
3335

3436
// Bytes returns the byte array representation of the TraceID.
3537
func (t TraceID) Bytes() [16]byte {
36-
return data.TraceID(t).Bytes()
38+
return t.orig.Bytes()
3739
}
3840

3941
// HexString returns hex representation of the TraceID.
4042
func (t TraceID) HexString() string {
41-
return data.TraceID(t).HexString()
43+
return t.orig.HexString()
4244
}
4345

4446
// IsEmpty returns true if id doesn't contain at least one non-zero byte.
4547
func (t TraceID) IsEmpty() bool {
46-
return data.TraceID(t).IsEmpty()
48+
return t.orig.IsEmpty()
4749
}

translator/trace/zipkin/zipkinv2_to_traces.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ func zTagsToSpanLinks(tags map[string]string, dest pdata.SpanLinkSlice) error {
217217
if errTrace != nil {
218218
return errTrace
219219
}
220-
link.SetTraceID(pdata.TraceID(rawTrace))
220+
link.SetTraceID(pdata.NewTraceID(rawTrace.Bytes()))
221221

222222
// Convert span id.
223223
rawSpan := data.SpanID{}
224224
errSpan := rawSpan.UnmarshalJSON([]byte(parts[1]))
225225
if errSpan != nil {
226226
return errSpan
227227
}
228-
link.SetSpanID(pdata.SpanID(rawSpan))
228+
link.SetSpanID(pdata.NewSpanID(rawSpan.Bytes()))
229229

230230
link.SetTraceState(pdata.TraceState(parts[2]))
231231

0 commit comments

Comments
 (0)