Skip to content

Commit

Permalink
[cmd/telemetrygen] calculate and set span start and end times precisely
Browse files Browse the repository at this point in the history
- previous approach depended on a second call of `time.Now()` which differed in value from the spans start time and introduced variance to the duration
- both start and end times are now set for both parent and child spans
- added test to ensure start and end times match the duration provided exactly
  • Loading branch information
James Neill committed Sep 20, 2023
1 parent 9ef99c7 commit e8099c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/telemetrygen/internal/traces/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ func (w worker) simulateTraces() {
limiter := rate.NewLimiter(w.limitPerSecond, 1)
var i int
for w.running.Load() {
spanStart := time.Now()
spanEnd := spanStart.Add(w.spanDuration)
endTimestampOption := trace.WithTimestamp(spanEnd)

ctx, sp := tracer.Start(context.Background(), "lets-go", trace.WithAttributes(
semconv.NetPeerIPKey.String(fakeIP),
semconv.PeerServiceKey.String("telemetrygen-server"),
),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithTimestamp(spanStart),
)

for j := 0; j < w.loadSize; j++ {
sp.SetAttributes(attribute.String(fmt.Sprintf("load-%v", j), string(make([]byte, charactersPerMB))))
}
Expand All @@ -69,17 +75,17 @@ func (w worker) simulateTraces() {
semconv.PeerServiceKey.String("telemetrygen-client"),
),
trace.WithSpanKind(trace.SpanKindServer),
trace.WithTimestamp(spanStart),
)

if err := limiter.Wait(context.Background()); err != nil {
w.logger.Fatal("limiter waited failed, retry", zap.Error(err))
}

opt := trace.WithTimestamp(time.Now().Add(w.spanDuration))
child.SetStatus(w.statusCode, "")
child.End(opt)
child.End(endTimestampOption)
sp.SetStatus(w.statusCode, "")
sp.End(opt)
sp.End(endTimestampOption)

i++
if w.numTraces != 0 {
Expand Down
32 changes: 32 additions & 0 deletions cmd/telemetrygen/internal/traces/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ func TestRateOfSpans(t *testing.T) {
assert.True(t, len(syncer.spans) <= 20, "there should have been less than 20 spans, had %d", len(syncer.spans))
}

func TestSpanDuration(t *testing.T) {
// prepare
syncer := &mockSyncer{}

tracerProvider := sdktrace.NewTracerProvider()
sp := sdktrace.NewSimpleSpanProcessor(syncer)
tracerProvider.RegisterSpanProcessor(sp)
otel.SetTracerProvider(tracerProvider)

targetDuration := 1 * time.Second
cfg := &Config{
Config: common.Config{
Rate: 10,
TotalDuration: time.Second / 2,
WorkerCount: 1,
},
SpanDuration: targetDuration,
}

// sanity check
require.Len(t, syncer.spans, 0)

// test
require.NoError(t, Run(cfg, zap.NewNop()))

for _, span := range syncer.spans {
startTime, endTime := span.StartTime(), span.EndTime()
spanDuration := endTime.Sub(startTime)
assert.Equal(t, targetDuration, spanDuration)
}
}

func TestUnthrottled(t *testing.T) {
// prepare
syncer := &mockSyncer{}
Expand Down

0 comments on commit e8099c3

Please sign in to comment.