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

Added tracegen utility #956

Merged
merged 2 commits into from
Sep 23, 2020
Merged
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
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ updates:
directory: "/exporter/stackdriverexporter"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/tracegen"
schedule:
interval: "weekly"
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Unreleased

- Add `dockerstats` receiver as top level component (#1081)
## 🚀 New components 🚀
- add `dockerstats` receiver as top level component (#1081)
- add `tracegen` utility (#956)

## v0.10.0

Expand Down
1 change: 1 addition & 0 deletions tracegen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../Makefile.Common
48 changes: 48 additions & 0 deletions tracegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Trace generator for OpenTelemetry

This utility simulates a client generating traces, useful for testing and demonstration purposes.

## Installing

```console
$ go get github.com/open-telemetry/opentelemetry-collector-contrib/tracegen
```

## Running

First, you'll need an OpenTelemetry Collector to receive the trace data. Follow the project's instructions for a detailed setting up guide. The following configuration file should be sufficient:

```yaml
receivers:
otlp:
protocols:
grpc:
endpoint: localhost:55680

processors:

exporters:
logging:

service:
pipelines:
traces:
receivers:
- otlp
processors: []
exporters:
- logging
```

Once the OpenTelemetry Collector instance is up and running, run `tracegen`:

```console
$ tracegen -otlp-insecure -duration 5s
```

Or, to generate a specific number of traces:
```console
$ tracegen -otlp-insecure -traces 1
```

Check `-help` for all the options.
16 changes: 16 additions & 0 deletions tracegen/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/tracegen

go 1.14

require (
github.com/go-logr/logr v0.2.1
github.com/go-logr/zapr v0.2.0
github.com/grpc-ecosystem/go-grpc-middleware v1.2.1
github.com/stretchr/testify v1.6.1
go.opentelemetry.io/otel v0.11.0
go.opentelemetry.io/otel/exporters/otlp v0.11.0
go.opentelemetry.io/otel/sdk v0.11.0
go.uber.org/zap v1.16.0
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
google.golang.org/grpc v1.31.1
)
178 changes: 178 additions & 0 deletions tracegen/go.sum

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions tracegen/internal/tracegen/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright The OpenTelemetry Authors
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tracegen

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

"github.com/go-logr/logr"
"golang.org/x/time/rate"
)

// Config describes the test scenario.
type Config struct {
WorkerCount int
NumTraces int
PropagateContext bool
Rate int64
TotalDuration time.Duration
ServiceName string

// OTLP config
Endpoint string
Insecure bool
}

// Flags registers config flags.
func (c *Config) Flags(fs *flag.FlagSet) {
fs.IntVar(&c.WorkerCount, "workers", 1, "Number of workers (goroutines) to run")
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.Int64Var(&c.Rate, "rate", 0, "Approximately how many traces per second each worker should generate. Zero means no throttling.")
fs.DurationVar(&c.TotalDuration, "duration", 0, "For how long to run the test")
fs.StringVar(&c.ServiceName, "service", "tracegen", "Service name to use")

// unfortunately, at this moment, the otel-go client doesn't support configuring OTLP via env vars
fs.StringVar(&c.Endpoint, "otlp-endpoint", "localhost:55680", "Target to which the exporter is going to send spans or metrics. This MAY be configured to include a path (e.g. example.com/v1/traces)")
fs.BoolVar(&c.Insecure, "otlp-insecure", false, "Whether to enable client transport security for the exporter's grpc or http connection")
}

// Run executes the test scenario.
func Run(c *Config, logger logr.Logger) error {
if c.TotalDuration > 0 {
c.NumTraces = 0
} else if c.NumTraces <= 0 {
return fmt.Errorf("either `traces` or `duration` must be greater than 0")
}

limit := rate.Limit(c.Rate)
if c.Rate == 0 {
limit = rate.Inf
logger.Info("generation of traces isn't being throttled")
} else {
logger.Info("generation of traces is limited", "per-second", limit)
}

wg := sync.WaitGroup{}
var running uint32 = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is never used to signal the workers to stop

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it used a few lines below?

if c.Duration > 0 {
	time.Sleep(c.Duration)
	atomic.StoreUint32(&running, 0)
}

for i := 0; i < c.WorkerCount; i++ {
wg.Add(1)
w := worker{
id: i,
numTraces: c.NumTraces,
propagateContext: c.PropagateContext,
limitPerSecond: limit,
totalDuration: c.TotalDuration,
running: &running,
wg: &wg,
logger: logger.WithValues("worker", i),
}

go w.simulateTraces()
}
if c.TotalDuration > 0 {
time.Sleep(c.TotalDuration)
atomic.StoreUint32(&running, 0)
}
wg.Wait()
return nil
}
93 changes: 93 additions & 0 deletions tracegen/internal/tracegen/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tracegen

import (
"context"
"net/http"
"sync"
"sync/atomic"
"time"

"github.com/go-logr/logr"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/semconv"
"golang.org/x/time/rate"
)

type worker struct {
running *uint32 // pointer to shared flag that indicates it's time to stop the test
id int // worker id
numTraces int // how many traces the worker has to generate (only when duration==0)
propagateContext bool // whether the worker needs to propagate the trace context via HTTP headers
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
logger logr.Logger
}

const (
fakeIP string = "1.2.3.4"

fakeSpanDuration = 123 * time.Microsecond
)

func (w worker) simulateTraces() {
tracer := global.Tracer("tracegen")
limiter := rate.NewLimiter(w.limitPerSecond, 1)
var i int
for atomic.LoadUint32(w.running) == 1 {
ctx, sp := tracer.Start(context.Background(), "lets-go", trace.WithAttributes(
label.String("span.kind", "client"), // is there a semantic convention for this?
semconv.NetPeerIPKey.String(fakeIP),
semconv.PeerServiceKey.String("tracegen-server"),
))

childCtx := ctx
if w.propagateContext {
header := http.Header{}
// simulates going remote
propagation.InjectHTTP(childCtx, global.Propagators(), header)

// simulates getting a request from a client
childCtx = propagation.ExtractHTTP(childCtx, global.Propagators(), header)
}

_, child := tracer.Start(childCtx, "okey-dokey", trace.WithAttributes(
label.String("span.kind", "server"),
semconv.NetPeerIPKey.String(fakeIP),
semconv.PeerServiceKey.String("tracegen-client"),
))

limiter.Wait(context.Background())

opt := trace.WithEndTime(time.Now().Add(fakeSpanDuration))
child.End(opt)
sp.End(opt)

i++
if w.numTraces != 0 {
if i >= w.numTraces {
break
}
}
}
w.logger.Info("traces generated", "worker", w.id, "traces", i)
w.wg.Done()
}
Loading