Skip to content

Commit

Permalink
Always run OTEL collector and optionally continue running after bench…
Browse files Browse the repository at this point in the history
…marks (#29)
  • Loading branch information
lahsivjar authored Aug 29, 2023
1 parent 4c5271b commit bf84579
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
4 changes: 2 additions & 2 deletions cmd/apmbench/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ import (
var cfg struct {
Count uint
Benchtime time.Duration
Detailed bool
RunRE *regexp.Regexp
// Sorted list of agents count to be used for benchmarking
AgentsList []int
CollectorConfigYaml string
ServerMode bool
}

func init() {
cfg.AgentsList = []int{1}

flag.UintVar(&cfg.Count, "count", 1, "run benchmarks `n` times")
flag.DurationVar(&cfg.Benchtime, "benchtime", time.Second, "run each benchmark for duration `d`")
flag.BoolVar(&cfg.Detailed, "detailed", false, "Get detailed metrics recorded during benchmark")
flag.Func("run", "run only benchmarks matching `regexp`", func(restr string) error {
if restr != "" {
re, err := regexp.Compile(restr)
Expand Down Expand Up @@ -60,4 +59,5 @@ func init() {
},
)
flag.StringVar(&cfg.CollectorConfigYaml, "collector-config-yaml", "", "configuration for otel collector")
flag.BoolVar(&cfg.ServerMode, "server-mode", false, "continue running otel collector post benchmark run")
}
93 changes: 49 additions & 44 deletions cmd/apmbench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"errors"
"flag"
"log"
"os"
"os/signal"
"sync"
"testing"
"time"
Expand All @@ -26,65 +28,61 @@ func main() {
log.Fatalf("failed to setup logger: %v", err)
}

extraMetrics := func(*testing.B) error { return nil }
resetFunc := func() {}
// Create otel collector
collectorCfg := otelcollector.DefaultConfig()
if cfg.CollectorConfigYaml != "" {
logger.Info("starting otel collector...")

collectorCfg := otelcollector.DefaultConfig()
err := collectorCfg.LoadConfigFromYamlFile(cfg.CollectorConfigYaml)
if err != nil {
logger.Fatal("failed to load collector config", zap.Error(err))
}
}
collector, err := otelcollector.New(collectorCfg, logger)
if err != nil {
logger.Fatal("failed to create a new collector", zap.Error(err))
}
logger.Info("loaded collector configuration", zap.Object("config", &collectorCfg))

collector, err := otelcollector.New(collectorCfg, logger)
if err != nil {
logger.Fatal("failed to create a new collector", zap.Error(err))
}
logger.Info("loaded collector configuration", zap.Object("config", &collectorCfg))

var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start otel collector
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()

wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
logger.Info("starting otel collector...")
if err := collector.Run(ctx); err != nil {
logger.Fatal("failed to run collector", zap.Error(err))
}
}(ctx)

if err := collector.Run(ctx); err != nil {
logger.Fatal("failed to run collector", zap.Error(err))
}
}(ctx)
// Wait for otel collector to be ready
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := collector.Wait(ctx); err != nil {
logger.Fatal("failed to start collector", zap.Error(err))
}

extraMetrics = func(b *testing.B) error {
var errs []error
for _, cfg := range collectorCfg.InMemoryStoreConfig {
m, err := collector.GetAggregatedMetric(cfg)
if err != nil {
errs = append(errs, err)
continue
}
b.ReportMetric(m, cfg.Alias)
}
if len(errs) > 0 {
return errors.Join(errs...)
// Run benchmarks
extraMetrics := func(b *testing.B) error {
var errs []error
for _, cfg := range collectorCfg.InMemoryStoreConfig {
m, err := collector.GetAggregatedMetric(cfg)
if err != nil {
errs = append(errs, err)
continue
}
return nil
b.ReportMetric(m, cfg.Alias)
}
resetFunc = collector.Reset

// Wait for otel collector to be ready
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := collector.Wait(ctx); err != nil {
logger.Fatal("failed to start collector", zap.Error(err))
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}

if err := Run(
extraMetrics,
resetFunc,
collector.Reset,
Benchmark1000Transactions,
BenchmarkOTLPTraces,
BenchmarkAgentAll,
Expand All @@ -96,6 +94,13 @@ func main() {
); err != nil {
logger.Fatal("failed to run benchmarks", zap.Error(err))
}

// If server-mode is enabled then keep the otel collector running
if cfg.ServerMode {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
}
}

func init() {
Expand Down

0 comments on commit bf84579

Please sign in to comment.