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

Generate "Golden Data" trace spans #967

Merged
merged 22 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3f1997c
initial dev of golden data generators
kbrockhoff May 8, 2020
f39cd56
Merge branch 'master' into golden-data-set-mgmt
kbrockhoff May 9, 2020
739a6d7
initial dev of golden data span generator
kbrockhoff May 9, 2020
ff928d5
initial dev of golden data span generator
kbrockhoff May 11, 2020
1c08888
initial dev of golden data span generator
kbrockhoff May 13, 2020
242404b
initial dev of golden data spans generator
kbrockhoff May 13, 2020
57ef7e2
fix gosec issues
kbrockhoff May 13, 2020
1297b4d
initial dev of resource spans generation
kbrockhoff May 14, 2020
f39b13f
fix some issues raised by PR reviewers
kbrockhoff May 14, 2020
81ed6ac
fix issues raised by PR reviewers
kbrockhoff May 15, 2020
d6a1912
improve attribute constants naming
kbrockhoff May 16, 2020
da7aa25
introduce supplable random number generator
kbrockhoff May 16, 2020
3083c1d
add max number of attributes test
kbrockhoff May 16, 2020
fd9cb75
Merge branch 'master' into golden-data-set-mgmt
kbrockhoff May 16, 2020
78fe561
update to latest imports standard
kbrockhoff May 16, 2020
6e0b5bb
Merge branch 'master' into golden-data-set-mgmt
kbrockhoff May 20, 2020
432cb26
added documentation to public members
kbrockhoff May 20, 2020
21642e6
Merge branch 'master' into golden-data-set-mgmt
kbrockhoff May 21, 2020
f54cf5c
Merge branch 'master' into golden-data-set-mgmt
kbrockhoff May 22, 2020
6918bd0
Merge branch 'master' into golden-data-set-mgmt
kbrockhoff May 23, 2020
88fd7ab
switch to structs for input values
kbrockhoff May 25, 2020
5019ebb
changed span names to identity PICT inputs used in generation
kbrockhoff May 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
initial dev of golden data span generator
  • Loading branch information
kbrockhoff committed May 13, 2020
commit 1c088886800db7b2c4b8c8e3023681684671dbe3
97 changes: 94 additions & 3 deletions internal/goldendataset/span_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ const (
SpanAttrInternal = "Internal"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple more important case that would be useful to cover:

  • Attributes slice that contain an AttributeKeyValue with a missing Key.
  • AttributeKeyValue with incorrect Type value (out of enum bounds).


const (
SpanChildCountNil = "Nil"
SpanChildCountEmpty = "Empty"
SpanChildCountOne = "One"
SpanChildCountTwo = "Two"
SpanChildCountEight = "Eight"
)

const (
SpanStatusNil = "Nil"
SpanStatusOk = "Ok"
Expand Down Expand Up @@ -127,7 +135,7 @@ func constructStatusMessageMap() map[string]string {
}

func GenerateSpan(traceID []byte, tracestate string, parentID []byte, spanName string, kind string, spanTypeID string,
statusStr string) *otlptrace.Span {
eventCnt string, linkCnt string, statusStr string) *otlptrace.Span {
endTime := time.Now().Add(-50 * time.Microsecond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: time.Sub may be slightly easier to understand than time.Add of negative duration.

return &otlptrace.Span{
TraceId: traceID,
Expand All @@ -140,14 +148,23 @@ func GenerateSpan(traceID []byte, tracestate string, parentID []byte, spanName s
EndTimeUnixNano: uint64(endTime.UnixNano()),
Attributes: generateSpanAttributes(spanTypeID),
DroppedAttributesCount: 0,
Events: nil,
Events: generateSpanEvents(eventCnt),
DroppedEventsCount: 0,
Links: nil,
Links: generateSpanLinks(linkCnt),
DroppedLinksCount: 0,
Status: generateStatus(statusStr),
}
}

func generateTraceID() []byte {
var r [16]byte
_, err := rand.Read(r[:])
if err != nil {
panic(err)
}
return r[:]
}

func generateSpanID() []byte {
var r [8]byte
_, err := rand.Read(r[:])
Expand Down Expand Up @@ -388,3 +405,77 @@ func generateInternalAttributes() map[string]interface{} {
attrMap[conventions.AttributeEnduserID] = "unittest"
return attrMap
}

func generateSpanEvents(eventCnt string) []*otlptrace.Span_Event {
if SpanChildCountNil == eventCnt {
return nil
}
listSize := calculateListSize(eventCnt)
eventList := make([]*otlptrace.Span_Event, listSize)
for i := 0; i < listSize; i++ {
eventList[i] = generateSpanEvent(i)
}
return eventList
}

func generateSpanLinks(linkCnt string) []*otlptrace.Span_Link {
if SpanChildCountNil == linkCnt {
return nil
}
listSize := calculateListSize(linkCnt)
linkList := make([]*otlptrace.Span_Link, listSize)
for i := 0; i < listSize; i++ {
linkList[i] = generateSpanLink(i)
}
return linkList
}

func calculateListSize(listCnt string) int {
if SpanChildCountOne == listCnt {
return 1
} else if SpanChildCountTwo == listCnt {
return 2
} else if SpanChildCountEight == listCnt {
return 8
} else {
return 0
}
}

func generateSpanEvent(index int) *otlptrace.Span_Event {
t := time.Now().Add(-75 * time.Microsecond)
return &otlptrace.Span_Event{
TimeUnixNano: uint64(t.UnixNano()),
Name: "message",
Attributes: generateEventAttributes(index),
DroppedAttributesCount: 0,
}
}

func generateEventAttributes(index int) []*otlpcommon.AttributeKeyValue {
attrMap := make(map[string]interface{})
if index%2 == 0 {
attrMap[conventions.AttributeMessageType] = "SENT"
} else {
attrMap[conventions.AttributeMessageType] = "RECEIVED"
}
attrMap[conventions.AttributeMessageID] = int64(index)
attrMap[conventions.AttributeMessageCompressedSize] = int64(17 * index)
attrMap[conventions.AttributeMessageUncompressedSize] = int64(24 * index)
return convertMapToAttributeKeyValues(attrMap)
}

func generateSpanLink(index int) *otlptrace.Span_Link {
return &otlptrace.Span_Link{
TraceId: generateTraceID(),
SpanId: generateSpanID(),
TraceState: "",
Attributes: generateLinkAttributes(index),
DroppedAttributesCount: 0,
}
}

func generateLinkAttributes(index int) []*otlpcommon.AttributeKeyValue {
attrMap := generateMessagingConsumerAttributes()
return convertMapToAttributeKeyValues(attrMap)
}
22 changes: 21 additions & 1 deletion internal/goldendataset/span_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ import (
func TestGenerateParentSpan(t *testing.T) {
traceID := generateTestTraceID()
span := GenerateSpan(traceID, "", nil, "/gotest-parent", SpanKindServer,
SpanAttrHTTPServer, SpanStatusOk)
SpanAttrHTTPServer, SpanChildCountTwo, SpanChildCountOne, SpanStatusOk)
assert.Equal(t, traceID, span.TraceId)
assert.Nil(t, span.ParentSpanId)
assert.Equal(t, 11, len(span.Attributes))
assert.Equal(t, otlptrace.Status_Ok, span.Status.Code)
}

func TestGenerateChildSpan(t *testing.T) {
traceID := generateTestTraceID()
parentID := generateTestSpanID()
span := GenerateSpan(traceID, "", parentID, "get_test_info", SpanKindClient,
SpanAttrDatabaseSQL, SpanChildCountEmpty, SpanChildCountNil, SpanStatusOk)
assert.Equal(t, traceID, span.TraceId)
assert.Equal(t, parentID, span.ParentSpanId)
assert.Equal(t, 8, len(span.Attributes))
assert.Equal(t, otlptrace.Status_Ok, span.Status.Code)
}

func generateTestTraceID() []byte {
var r [16]byte
_, err := rand.Read(r[:])
Expand All @@ -40,3 +51,12 @@ func generateTestTraceID() []byte {
}
return r[:]
}

func generateTestSpanID() []byte {
var r [8]byte
_, err := rand.Read(r[:])
if err != nil {
panic(err)
}
return r[:]
}