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

[cmd/telemetrygen] expose the generated span duration as a command parameter #26991

Closed
Closed
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions .chloggen/feat_random-fake-span-durations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Exposes the span duration as a command line argument `--span-duration`

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26991]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
4 changes: 4 additions & 0 deletions cmd/telemetrygen/internal/traces/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package traces

import (
"time"

"github.com/spf13/pflag"

"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common"
Expand All @@ -18,6 +20,7 @@ type Config struct {
StatusCode string
Batch bool
LoadSize int
SpanDuration time.Duration
}

// Flags registers config flags.
Expand All @@ -29,4 +32,5 @@ func (c *Config) Flags(fs *pflag.FlagSet) {
fs.StringVar(&c.StatusCode, "status-code", "0", "Status code to use for the spans, one of (Unset, Error, Ok) or the equivalent integer (0,1,2)")
fs.BoolVar(&c.Batch, "batch", true, "Whether to batch traces")
fs.IntVar(&c.LoadSize, "size", 0, "Desired minimum size in MB of string data for each trace generated. This can be used to test traces with large payloads, i.e. when testing the OTLP receiver endpoint max receive size.")
fs.DurationVar(&c.SpanDuration, "span-duration", 123*time.Microsecond, "The duration of each generated span.")
}
1 change: 1 addition & 0 deletions cmd/telemetrygen/internal/traces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func Run(c *Config, logger *zap.Logger) error {
wg: &wg,
logger: logger.With(zap.Int("worker", i)),
loadSize: c.LoadSize,
spanDuration: c.SpanDuration,
}

go w.simulateTraces()
Expand Down
17 changes: 11 additions & 6 deletions cmd/telemetrygen/internal/traces/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ type worker struct {
totalDuration time.Duration // how long to run the test for (overrides `numTraces`)
limitPerSecond rate.Limit // how many spans per second to generate
wg *sync.WaitGroup // notify when done
loadSize int // desired minimum size in MB of string data for each trace generated
spanDuration time.Duration // duration of generated spans
logger *zap.Logger
loadSize int
}

const (
fakeIP string = "1.2.3.4"

fakeSpanDuration = 123 * time.Microsecond

charactersPerMB = 1024 * 1024 // One character takes up one byte of space, so this number comes from the number of bytes in a megabyte
)

Expand All @@ -45,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 @@ -70,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(fakeSpanDuration))
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