Skip to content

Commit

Permalink
[cmd/telemetrygen] Improvements and fixes to telemetrygen traces (ope…
Browse files Browse the repository at this point in the history
…n-telemetry#21629)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
- Adds new option to disable the batchspanprocessor for testing large
trace sizes (enabled by default)
- Adds new option to configure size in MB of each trace generated for
load testing
- Fixes issue where large trace payloads (>2000) wouldn't generate the
correct number of traces, needed to add delay after each trace is
generated

---------

Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
  • Loading branch information
liustanley and mx-psi authored Aug 7, 2023
1 parent cf98af2 commit ce454b5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
16 changes: 16 additions & 0 deletions .chloggen/telemetrygen-traces-batch-size-options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: Adds batch option to configure whether to batch traces, and size option to configure minimum size in MB of each trace for load testing.

# One or more tracking issues related to the change
issues: [9597]

# (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:
4 changes: 4 additions & 0 deletions cmd/telemetrygen/internal/traces/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Config struct {
NumTraces int
PropagateContext bool
ServiceName string
Batch bool
LoadSize int
}

// Flags registers config flags.
Expand All @@ -23,4 +25,6 @@ func (c *Config) Flags(fs *pflag.FlagSet) {
fs.IntVar(&c.NumTraces, "traces", 1, "Number of traces to generate in each worker (ignored if duration is provided)")
fs.BoolVar(&c.PropagateContext, "marshal", false, "Whether to marshal trace context via HTTP headers")
fs.StringVar(&c.ServiceName, "service", "telemetrygen", "Service name to use")
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.")
}
22 changes: 14 additions & 8 deletions cmd/telemetrygen/internal/traces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ func Start(cfg *Config) error {
}
}()

ssp := sdktrace.NewBatchSpanProcessor(exp, sdktrace.WithBatchTimeout(time.Second))
defer func() {
logger.Info("stop the batch span processor")
if tempError := ssp.Shutdown(context.Background()); tempError != nil {
logger.Error("failed to stop the batch span processor", zap.Error(err))
}
}()
var ssp sdktrace.SpanProcessor
if cfg.Batch {
ssp = sdktrace.NewBatchSpanProcessor(exp, sdktrace.WithBatchTimeout(time.Second))
defer func() {
logger.Info("stop the batch span processor")
if tempError := ssp.Shutdown(context.Background()); tempError != nil {
logger.Error("failed to stop the batch span processor", zap.Error(err))
}
}()
}

var attributes []attribute.KeyValue
// may be overridden by `-otlp-attributes service.name="foo"`
Expand All @@ -89,7 +92,9 @@ func Start(cfg *Config) error {
sdktrace.WithResource(resource.NewWithAttributes(semconv.SchemaURL, attributes...)),
)

tracerProvider.RegisterSpanProcessor(ssp)
if cfg.Batch {
tracerProvider.RegisterSpanProcessor(ssp)
}
otel.SetTracerProvider(tracerProvider)

if err = Run(cfg, logger); err != nil {
Expand Down Expand Up @@ -131,6 +136,7 @@ func Run(c *Config, logger *zap.Logger) error {
running: running,
wg: &wg,
logger: logger.With(zap.Int("worker", i)),
loadSize: c.LoadSize,
}

go w.simulateTraces()
Expand Down
8 changes: 8 additions & 0 deletions cmd/telemetrygen/internal/traces/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package traces // import "github.com/open-telemetry/opentelemetry-collector-cont

import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
Expand All @@ -25,12 +27,15 @@ type worker struct {
limitPerSecond rate.Limit // how many spans per second to generate
wg *sync.WaitGroup // notify when done
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
)

func (w worker) simulateTraces() {
Expand All @@ -44,6 +49,9 @@ func (w worker) simulateTraces() {
),
trace.WithSpanKind(trace.SpanKindClient),
)
for j := 0; j < w.loadSize; j++ {
sp.SetAttributes(attribute.String(fmt.Sprintf("load-%v", j), string(make([]byte, charactersPerMB))))
}

childCtx := ctx
if w.propagateContext {
Expand Down

0 comments on commit ce454b5

Please sign in to comment.