Skip to content
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"`
Comment on lines +110 to +111
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see a couple of potential improvements here:

  1. It might be more consistent if the default behavior always adds the event when PercentageOfSpan is not set
  2. What do you think about renaming it to Rate or EventRate for clarity?
  3. Unless I'm missing something, the current implementation seems to use PercentageOfSpan only to calculate the event time (L322) but does not influence whether the event is actually added

// 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
Comment on lines 185 to +186
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The internalEventTemplate already includes a rate field that controls whether an event is added to the span. I think it makes sense to use it here

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)))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I might be missing something here, but could you explain the rationale behind e.percentageOfSpan controlling the event timestamp?

} 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