Skip to content

Commit

Permalink
Merge branch 'main' into add-GOOS=windows-lint-step
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanotti authored Jan 23, 2024
2 parents b2cc5b3 + 6a76d49 commit bdbb500
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 16 deletions.
27 changes: 27 additions & 0 deletions .chloggen/juliaj_telemetrygen-multi-child-spans.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: cmd/telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "This updates telemetrygen to create multiple child spans per trace and enhances the tool's functionality for load testing the remote tracing backend."

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

# (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: []
2 changes: 2 additions & 0 deletions cmd/telemetrygen/internal/traces/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type Config struct {
common.Config
NumTraces int
NumChildSpans int
PropagateContext bool
ServiceName string
StatusCode string
Expand All @@ -31,6 +32,7 @@ func (c *Config) Flags(fs *pflag.FlagSet) {
fs.StringVar(&c.HTTPPath, "otlp-http-url-path", "/v1/traces", "Which URL path to write to")

fs.IntVar(&c.NumTraces, "traces", 1, "Number of traces to generate in each worker (ignored if duration is provided)")
fs.IntVar(&c.NumChildSpans, "child-spans", 1, "Number of child spans to generate for each trace")
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.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)")
Expand Down
2 changes: 2 additions & 0 deletions cmd/telemetrygen/internal/traces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package traces
import (
"context"
"fmt"
"math"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -139,6 +140,7 @@ func Run(c *Config, logger *zap.Logger) error {
wg.Add(1)
w := worker{
numTraces: c.NumTraces,
numChildSpans: int(math.Max(1, float64(c.NumChildSpans))),
propagateContext: c.PropagateContext,
statusCode: statusCode,
limitPerSecond: limit,
Expand Down
37 changes: 23 additions & 14 deletions cmd/telemetrygen/internal/traces/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package traces // import "github.com/open-telemetry/opentelemetry-collector-cont
import (
"context"
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"
Expand All @@ -23,6 +24,7 @@ import (
type worker struct {
running *atomic.Bool // pointer to shared flag that indicates it's time to stop the test
numTraces int // how many traces the worker has to generate (only when duration==0)
numChildSpans int // how many child spans the worker has to generate per trace
propagateContext bool // whether the worker needs to propagate the trace context via HTTP headers
statusCode codes.Code // the status code set for the child and parent spans
totalDuration time.Duration // how long to run the test for (overrides `numTraces`)
Expand Down Expand Up @@ -69,23 +71,30 @@ func (w worker) simulateTraces(telemetryAttributes []attribute.KeyValue) {
// simulates getting a request from a client
childCtx = otel.GetTextMapPropagator().Extract(childCtx, header)
}
var endTimestamp trace.SpanEventOption

_, child := tracer.Start(childCtx, "okey-dokey", trace.WithAttributes(
semconv.NetPeerIPKey.String(fakeIP),
semconv.PeerServiceKey.String("telemetrygen-client"),
),
trace.WithSpanKind(trace.SpanKindServer),
trace.WithTimestamp(spanStart),
)
child.SetAttributes(telemetryAttributes...)
for j := 0; j < w.numChildSpans; j++ {
_, child := tracer.Start(childCtx, "okey-dokey-"+strconv.Itoa(j), trace.WithAttributes(
semconv.NetPeerIPKey.String(fakeIP),
semconv.PeerServiceKey.String("telemetrygen-client"),
),
trace.WithSpanKind(trace.SpanKindServer),
trace.WithTimestamp(spanStart),
)
child.SetAttributes(telemetryAttributes...)

if err := limiter.Wait(context.Background()); err != nil {
w.logger.Fatal("limiter waited failed, retry", zap.Error(err))
}
if err := limiter.Wait(context.Background()); err != nil {
w.logger.Fatal("limiter waited failed, retry", zap.Error(err))
}

endTimestamp := trace.WithTimestamp(spanEnd)
child.SetStatus(w.statusCode, "")
child.End(endTimestamp)
endTimestamp = trace.WithTimestamp(spanEnd)
child.SetStatus(w.statusCode, "")
child.End(endTimestamp)

// Reset the start and end for next span
spanStart = spanEnd
spanEnd = spanStart.Add(w.spanDuration)
}
sp.SetStatus(w.statusCode, "")
sp.End(endTimestamp)

Expand Down
25 changes: 25 additions & 0 deletions cmd/telemetrygen/internal/traces/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ func TestFixedNumberOfTraces(t *testing.T) {
assert.Len(t, syncer.spans, 2) // each trace has two spans
}

func TestNumberOfSpans(t *testing.T) {
// prepare
syncer := &mockSyncer{}

tracerProvider := sdktrace.NewTracerProvider()
sp := sdktrace.NewSimpleSpanProcessor(syncer)
tracerProvider.RegisterSpanProcessor(sp)
otel.SetTracerProvider(tracerProvider)

cfg := &Config{
Config: common.Config{
WorkerCount: 1,
},
NumTraces: 1,
NumChildSpans: 5,
}
expectedNumSpans := cfg.NumChildSpans + 1 // each trace has 1 + NumChildSpans spans

// test
require.NoError(t, Run(cfg, zap.NewNop()))

// verify
assert.Len(t, syncer.spans, expectedNumSpans)
}

func TestRateOfSpans(t *testing.T) {
// prepare
syncer := &mockSyncer{}
Expand Down
3 changes: 1 addition & 2 deletions processor/probabilisticsamplerprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ type Config struct {
// unique log record ID. The value of the attribute is only used if the trace ID is absent or if `attribute_source` is set to `record`.
FromAttribute string `mapstructure:"from_attribute"`

// SamplingPriority (logs only) allows to use a log record attribute designed by the `sampling_priority` key
// to be used as the sampling priority of the log record.
// SamplingPriority (logs only) enables using a log record attribute as the sampling priority of the log record.
SamplingPriority string `mapstructure:"sampling_priority"`
}

Expand Down

0 comments on commit bdbb500

Please sign in to comment.