From 794e127f707f981fc60c7c2b48fe07fedd342288 Mon Sep 17 00:00:00 2001 From: James Neill <895886+jmsnll@users.noreply.github.com> Date: Thu, 16 Nov 2023 10:35:57 +0000 Subject: [PATCH] =?UTF-8?q?[cmd/telemetrygen]=20expose=20the=20generated?= =?UTF-8?q?=20span=20duration=20as=20a=20command=20pa=E2=80=A6=20(#29116)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description:** As originally proposed in #26991 before I got distracted Exposes the duration of generated spans as a command line parameter. It uses a `DurationVar` flag so units can be easily provided and are automatically applied. Example usage: ```bash telemetrygen traces --traces 100 --otlp-insecure --span-duration 10ns # nanoseconds telemetrygen traces --traces 100 --otlp-insecure --span-duration 10us # microseconds telemetrygen traces --traces 100 --otlp-insecure --span-duration 10ms # milliseconds telemetrygen traces --traces 100 --otlp-insecure --span-duration 10s # seconds ``` **Testing:** Ran without the argument provided `telemetrygen traces --traces 1 --otlp-insecure` and seen spans publishing with the default value. Ran again with the argument provided: `telemetrygen traces --traces 1 --otlp-insecure --span-duration 1s` And observed the expected output: ``` Resource SchemaURL: https://opentelemetry.io/schemas/1.4.0 Resource attributes: -> service.name: Str(telemetrygen) ScopeSpans #0 ScopeSpans SchemaURL: InstrumentationScope telemetrygen Span #0 Trace ID : 8b441587ffa5820688b87a6b511d634c Parent ID : 39faad428638791b ID : 88f0886894bd4ee2 Name : okey-dokey Kind : Server Start time : 2023-11-12 02:05:07.97443 +0000 UTC End time : 2023-11-12 02:05:08.97443 +0000 UTC Status code : Unset Status message : Attributes: -> net.peer.ip: Str(1.2.3.4) -> peer.service: Str(telemetrygen-client) Span #1 Trace ID : 8b441587ffa5820688b87a6b511d634c Parent ID : ID : 39faad428638791b Name : lets-go Kind : Client Start time : 2023-11-12 02:05:07.97443 +0000 UTC End time : 2023-11-12 02:05:08.97443 +0000 UTC Status code : Unset Status message : Attributes: -> net.peer.ip: Str(1.2.3.4) -> peer.service: Str(telemetrygen-server) {"kind": "exporter", "data_type": "traces", "name": "debug"} ``` **Documentation:** No documentation added. --------- Co-authored-by: Pablo Baeyens --- .chloggen/feat-fake-span-durations.yaml | 27 ++++++++++++++++ cmd/telemetrygen/internal/traces/config.go | 4 +++ cmd/telemetrygen/internal/traces/traces.go | 1 + cmd/telemetrygen/internal/traces/worker.go | 16 ++++++---- .../internal/traces/worker_test.go | 32 +++++++++++++++++++ 5 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 .chloggen/feat-fake-span-durations.yaml diff --git a/.chloggen/feat-fake-span-durations.yaml b/.chloggen/feat-fake-span-durations.yaml new file mode 100644 index 000000000000..74376aaae4dd --- /dev/null +++ b/.chloggen/feat-fake-span-durations.yaml @@ -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: [29116] + +# (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] diff --git a/cmd/telemetrygen/internal/traces/config.go b/cmd/telemetrygen/internal/traces/config.go index e8d41270d5f4..55e020e899d3 100644 --- a/cmd/telemetrygen/internal/traces/config.go +++ b/cmd/telemetrygen/internal/traces/config.go @@ -4,6 +4,8 @@ package traces import ( + "time" + "github.com/spf13/pflag" "github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common" @@ -18,6 +20,7 @@ type Config struct { StatusCode string Batch bool LoadSize int + SpanDuration time.Duration } // Flags registers config flags. @@ -32,4 +35,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.") } diff --git a/cmd/telemetrygen/internal/traces/traces.go b/cmd/telemetrygen/internal/traces/traces.go index a1136a25ee4c..a84491a378fb 100644 --- a/cmd/telemetrygen/internal/traces/traces.go +++ b/cmd/telemetrygen/internal/traces/traces.go @@ -132,6 +132,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(telemetryAttributes) diff --git a/cmd/telemetrygen/internal/traces/worker.go b/cmd/telemetrygen/internal/traces/worker.go index cbcdc09ac1e3..74b0f7bf4e3a 100644 --- a/cmd/telemetrygen/internal/traces/worker.go +++ b/cmd/telemetrygen/internal/traces/worker.go @@ -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 generated trace + 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 ) @@ -46,11 +45,15 @@ func (w worker) simulateTraces(telemetryAttributes []attribute.KeyValue) { var i int for w.running.Load() { + spanStart := time.Now() + spanEnd := spanStart.Add(w.spanDuration) + 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), ) sp.SetAttributes(telemetryAttributes...) for j := 0; j < w.loadSize; j++ { @@ -72,6 +75,7 @@ func (w worker) simulateTraces(telemetryAttributes []attribute.KeyValue) { semconv.PeerServiceKey.String("telemetrygen-client"), ), trace.WithSpanKind(trace.SpanKindServer), + trace.WithTimestamp(spanStart), ) child.SetAttributes(telemetryAttributes...) @@ -79,11 +83,11 @@ func (w worker) simulateTraces(telemetryAttributes []attribute.KeyValue) { w.logger.Fatal("limiter waited failed, retry", zap.Error(err)) } - opt := trace.WithTimestamp(time.Now().Add(fakeSpanDuration)) + endTimestamp := trace.WithTimestamp(spanEnd) child.SetStatus(w.statusCode, "") - child.End(opt) + child.End(endTimestamp) sp.SetStatus(w.statusCode, "") - sp.End(opt) + sp.End(endTimestamp) i++ if w.numTraces != 0 { diff --git a/cmd/telemetrygen/internal/traces/worker_test.go b/cmd/telemetrygen/internal/traces/worker_test.go index 7d3db1e4314b..4b05aff07d4a 100644 --- a/cmd/telemetrygen/internal/traces/worker_test.go +++ b/cmd/telemetrygen/internal/traces/worker_test.go @@ -80,6 +80,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{}