Skip to content

Commit

Permalink
fix template example, rename defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ie-pham committed Jan 16, 2024
1 parent 723b5d8 commit dcc463e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions examples/template/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const traceDefaults = {
attributeSemantics: tracing.SEMANTICS_HTTP,
attributes: {"one": "three"},
randomAttributes: {count: 2, cardinality: 5},
eventDefaults: {generateExceptionOnError: true, count: 1.0, randomAttributes: {count: 2, cardinality: 3}},
linkDefaults: {linkToPreviousSpanIndex: true, count: 1.0, randomAttributes: {count: 2, cardinality: 3}},
randomEvents: {generateExceptionOnError: true, rate: 1.0, randomAttributes: {count: 2, cardinality: 3}},
randomLinks: {rate: 1.0, randomAttributes: {count: 2, cardinality: 3}},
}

const traceTemplates = [
Expand Down Expand Up @@ -71,8 +71,8 @@ const traceTemplates = [
{service: "shop-backend", attributes: {"http.status_code": 403}},
{service: "shop-backend", name: "authenticate", attributes: {"http.request.header.accept": ["application/json"]}},
{service: "auth-service", name: "authenticate", attributes: {"http.status_code": 403}},
{service: "cart-service", name: "checkout", randomEvents: {exceptionCount: 1, count: 2, randomAttributes: {count: 5, cardinality: 2}}},
{service: "billing-service", name: "payment", randomLinks: {linkToPreviousSpanIndex: true, count: 2, randomAttributes: {count: 3, cardinality: 2}}}
{service: "cart-service", name: "checkout", randomEvents: {exceptionRate: 1, rate: 2, randomAttributes: {count: 5, cardinality: 2}}},
{service: "billing-service", name: "payment", randomLinks: {rate: 2, randomAttributes: {count: 3, cardinality: 2}}}
]
},
]
Expand Down
30 changes: 15 additions & 15 deletions pkg/tracegen/templated.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ type SpanDefaults struct {
// RandomAttributes random attributes generated for each span.
RandomAttributes *AttributeParams `js:"randomAttributes"`
// Random events generated for each span
EventDefaults EventParams `js:"eventDefaults"`
RandomEvents EventParams `js:"randomEvents"`
// Random links generated for each span
LinkDefaults LinkParams `js:"linkDefaults"`
RandomLinks LinkParams `js:"randomLinks"`
}

// SpanTemplate parameters that define how a span is created.
Expand Down Expand Up @@ -130,9 +130,8 @@ type EventParams struct {
}

type internalLinkParams struct {
LinkToPreviousSpanIndex bool
Count int
RandomAttributes *AttributeParams
Count int
RandomAttributes *AttributeParams
}

type internalEventParams struct {
Expand Down Expand Up @@ -319,6 +318,7 @@ func (g *TemplatedGenerator) generateSpan(scopeSpans ptrace.ScopeSpans, tmpl *in
exceptionEvent := generateExceptionEvent()
event := span.Events().AppendEmpty()
event.SetName(exceptionEvent.Name)
event.SetTimestamp(pcommon.NewTimestampFromTime(end))
for k, v := range exceptionEvent.Attributes {
_ = event.Attributes().PutEmpty(k).FromRaw(v)
}
Expand Down Expand Up @@ -524,15 +524,15 @@ func (g *TemplatedGenerator) initializeSpan(idx int, parent *internalSpanTemplat
}
span.attributes = util.MergeMaps(defaults.Attributes, tmpl.Attributes)

eventDefaultsRate := defaults.EventDefaults.Rate
eventDefaultsRate := defaults.RandomEvents.Rate
var eventDefaults internalEventParams
// if rate is more than 1, use whole integers
if eventDefaultsRate > 1 {
eventDefaults = internalEventParams{
GenerateExceptionOnError: defaults.EventDefaults.GenerateExceptionOnError,
RandomAttributes: defaults.EventDefaults.RandomAttributes,
GenerateExceptionOnError: defaults.RandomEvents.GenerateExceptionOnError,
RandomAttributes: defaults.RandomEvents.RandomAttributes,
Count: int(eventDefaultsRate),
ExceptionCount: int(defaults.EventDefaults.ExceptionRate),
ExceptionCount: int(defaults.RandomEvents.ExceptionRate),
}
} else {
var count, exeptionCount int
Expand All @@ -546,8 +546,8 @@ func (g *TemplatedGenerator) initializeSpan(idx int, parent *internalSpanTemplat

// if rate is less than one
eventDefaults = internalEventParams{
GenerateExceptionOnError: defaults.EventDefaults.GenerateExceptionOnError,
RandomAttributes: defaults.EventDefaults.RandomAttributes,
GenerateExceptionOnError: defaults.RandomEvents.GenerateExceptionOnError,
RandomAttributes: defaults.RandomEvents.RandomAttributes,
Count: count,
ExceptionCount: exeptionCount,
}
Expand All @@ -564,18 +564,18 @@ func (g *TemplatedGenerator) initializeSpan(idx int, parent *internalSpanTemplat
span.events = g.initializeEvents(tmpl.Events, randomEvents, eventDefaults)

// need span status to determine if an exception event should occur
span.generateExceptionEvents = defaults.EventDefaults.GenerateExceptionOnError
span.generateExceptionEvents = defaults.RandomEvents.GenerateExceptionOnError

linkDefaultsRate := defaults.LinkDefaults.Rate
linkDefaultsRate := defaults.RandomLinks.Rate
var linkDefaults internalLinkParams
if linkDefaultsRate > 1 {
linkDefaults = internalLinkParams{
RandomAttributes: defaults.LinkDefaults.RandomAttributes,
RandomAttributes: defaults.RandomLinks.RandomAttributes,
Count: int(linkDefaultsRate),
}
} else if rand.Float32() < linkDefaultsRate {
linkDefaults = internalLinkParams{
RandomAttributes: defaults.LinkDefaults.RandomAttributes,
RandomAttributes: defaults.RandomLinks.RandomAttributes,
Count: 1,
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/tracegen/templated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TestTemplatedGenerator_EventsLinks(t *testing.T) {
Defaults: SpanDefaults{
Attributes: map[string]interface{}{"fixed.attr": "some-value"},
RandomAttributes: &AttributeParams{Count: 3},
LinkDefaults: LinkParams{Rate: 0.5, RandomAttributes: &AttributeParams{Count: 3}},
EventDefaults: EventParams{GenerateExceptionOnError: true, Rate: 0.5, RandomAttributes: &AttributeParams{Count: 3}},
RandomLinks: LinkParams{Rate: 1, RandomAttributes: &AttributeParams{Count: 3}},
RandomEvents: EventParams{GenerateExceptionOnError: true, Rate: 1, RandomAttributes: &AttributeParams{Count: 3}},
},
Spans: []SpanTemplate{
// do not change order of the first one
Expand Down

0 comments on commit dcc463e

Please sign in to comment.