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

Additional Support for setting fields #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
33 changes: 32 additions & 1 deletion pkg/tracegen/templated.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type SpanTemplate struct {
Service string `js:"service"`
// Name represents the name of the span. If empty, the name will be randomly generated.
Name *string `js:"name"`
// Status string sets the span status. "error", "ok" or "unset" are supported
Status *string `js:"status"`
// ParentIDX defines the index of the parent span in TraceTemplate.Spans. ParentIDX must be smaller than the
// own index. If empty, the parent is the span with the position directly before this span in TraceTemplate.Spans.
ParentIDX *int `js:"parentIdx"`
Expand Down Expand Up @@ -105,6 +107,8 @@ type Link struct {
type Event struct {
// Name of event
Name string `js:"name"`
// PercentageOfSpan if set controls where in the span the event is generated. If missing, the event is generated randomly
PercentageOfSpan float32 `js:"percentageOfSpan"`
// Attributes for this event
Attributes map[string]interface{} `js:"attributes"`
// Generate random attributes for this event
Expand Down Expand Up @@ -154,6 +158,7 @@ type internalSpanTemplate struct {
parent *internalSpanTemplate
name string
kind ptrace.SpanKind
status *ptrace.StatusCode
duration *Range
attributeSemantics *OTelSemantics
attributes map[string]interface{}
Expand All @@ -178,6 +183,7 @@ type internalLinkTemplate struct {

type internalEventTemplate struct {
rate float32
percentageOfSpan float32
exceptionOnError bool
name string
attributes map[string]interface{}
Expand Down Expand Up @@ -311,7 +317,13 @@ func (g *TemplatedGenerator) generateSpan(scopeSpans ptrace.ScopeSpans, tmpl *in
event.Attributes().EnsureCapacity(len(e.attributes) + len(e.randomAttributes))

event.SetName(e.name)
eventTime := start.Add(random.Duration(0, duration))

var eventTime time.Time
if e.percentageOfSpan > 0 {
eventTime = start.Add(time.Duration(float64(duration) * float64(e.percentageOfSpan)))
} else {
eventTime = start.Add(random.Duration(0, duration))
}
event.SetTimestamp(pcommon.NewTimestampFromTime(eventTime))

for k, v := range e.attributes {
Expand All @@ -322,6 +334,10 @@ func (g *TemplatedGenerator) generateSpan(scopeSpans ptrace.ScopeSpans, tmpl *in
}
}

if tmpl.status != nil {
span.Status().SetCode(*tmpl.status)
}

// generate links
span.Links().EnsureCapacity(len(tmpl.links))
for _, l := range tmpl.links {
Expand Down Expand Up @@ -530,6 +546,20 @@ func (g *TemplatedGenerator) initializeSpan(idx int, parent *internalSpanTemplat
}
span.attributes = util.MergeMaps(defaults.Attributes, tmpl.Attributes)

// set status
if tmpl.Status != nil {
var status ptrace.StatusCode
switch *tmpl.Status {
case "error":
status = ptrace.StatusCodeError
case "ok":
status = ptrace.StatusCodeOk
case "unset":
status = ptrace.StatusCodeUnset
}
span.status = &status
}

// set span name
if tmpl.Name != nil {
span.name = *tmpl.Name
Expand Down Expand Up @@ -637,6 +667,7 @@ func (g *TemplatedGenerator) initializeEvents(tmplEvents []Event, randomEvents,
event := internalEventTemplate{
name: e.Name,
attributes: e.Attributes,
percentageOfSpan: e.PercentageOfSpan,
randomAttributes: initializeRandomAttributes(e.RandomAttributes),
}
internalEvents = append(internalEvents, event)
Expand Down
Loading