From e5b7245ce21d6c997313a1879bc4308b5d34291a Mon Sep 17 00:00:00 2001 From: "jason.crouse" Date: Fri, 6 Sep 2024 13:46:28 -0400 Subject: [PATCH 1/4] remove uses of rand.Seed, and pass in an explicit random generator anywhere we called rand methods --- .gitignore | 1 + generatorreceiver/generator_receiver.go | 19 +++++----- .../internal/generator/trace_generator.go | 8 ++-- .../generator/trace_generator_test.go | 2 +- .../internal/topology/kubernetes.go | 38 +++++++++---------- .../internal/topology/latency_percentiles.go | 12 +++--- generatorreceiver/internal/topology/metric.go | 11 +++--- .../topology/resource_attribute_set.go | 5 ++- .../topology/resource_attribute_set_test.go | 3 +- .../internal/topology/service_route.go | 6 +-- .../internal/topology/tag_map.go | 4 +- .../internal/topology/tag_set_test.go | 8 ++-- 12 files changed, 59 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index b40c47f..c89bd77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build vendor/ dist/ +.idea/ diff --git a/generatorreceiver/generator_receiver.go b/generatorreceiver/generator_receiver.go index 92c8931..3732b6d 100644 --- a/generatorreceiver/generator_receiver.go +++ b/generatorreceiver/generator_receiver.go @@ -61,10 +61,6 @@ func (g generatorReceiver) Start(ctx context.Context, host component.Host) error // rand is used to generate seeds the underlying *rand.Rand generatorRand := rand.New(rand.NewSource(g.randomSeed)) - // Metrics generator uses the global rand.Rand - // TODO: LS-60180 - rand.Seed is deprecated, use rand.NewSource - rand.Seed(generatorRand.Int63()) - if g.server != nil { err := g.server.Start(ctx, host) if err != nil { @@ -79,7 +75,7 @@ func (g generatorReceiver) Start(ctx context.Context, host component.Host) error continue } k.Cfg = topoFile.Config - k.CreatePods(s.ServiceName) + k.CreatePods(s.ServiceName, generatorRand) } } @@ -88,7 +84,7 @@ func (g generatorReceiver) Start(ctx context.Context, host component.Host) error // Service defined metrics for _, m := range s.Metrics { - metricTicker := g.startMetricGenerator(ctx, s.ServiceName, m) + metricTicker := g.startMetricGenerator(ctx, s.ServiceName, m, generatorRand) g.tickers = append(g.tickers, metricTicker) } @@ -104,7 +100,7 @@ func (g generatorReceiver) Start(ctx context.Context, host component.Host) error // keep the same flags as the resources. k8sMetrics[i].EmbeddedFlags = resource.EmbeddedFlags - metricTicker := g.startMetricGenerator(ctx, s.ServiceName, k8sMetrics[i]) + metricTicker := g.startMetricGenerator(ctx, s.ServiceName, k8sMetrics[i], generatorRand) g.tickers = append(g.tickers, metricTicker) } } @@ -150,14 +146,19 @@ func (g generatorReceiver) Start(ctx context.Context, host component.Host) error return nil } -func (g *generatorReceiver) startMetricGenerator(ctx context.Context, serviceName string, m topology.Metric) *time.Ticker { +func (g *generatorReceiver) startMetricGenerator( + ctx context.Context, + serviceName string, + m topology.Metric, + random *rand.Rand, +) *time.Ticker { // TODO: do we actually need to generate every second? metricTicker := time.NewTicker(topology.DefaultMetricTickerPeriod) go func() { g.logger.Info("generating metrics", zap.String("service", serviceName), zap.String("name", m.Name), zap.String("flag_set", m.EmbeddedFlags.FlagSet), zap.String("flag_unset", m.EmbeddedFlags.FlagUnset)) metricGen := generator.NewMetricGenerator(g.randomSeed) for range metricTicker.C { - m.Pod.RestartIfNeeded(m.EmbeddedFlags, g.logger) + m.Pod.RestartIfNeeded(m.EmbeddedFlags, g.logger, random) if metrics, report := metricGen.Generate(&m, serviceName); report { err := g.metricConsumer.ConsumeMetrics(ctx, metrics) diff --git a/generatorreceiver/internal/generator/trace_generator.go b/generatorreceiver/internal/generator/trace_generator.go index 7c0f713..97ee17d 100644 --- a/generatorreceiver/internal/generator/trace_generator.go +++ b/generatorreceiver/internal/generator/trace_generator.go @@ -77,7 +77,7 @@ func (g *TraceGenerator) createSpanForServiceRouteCall(traces *ptrace.Traces, se resourceAttributeSet := serviceTier.GetResourceAttributeSet(traceId) attrs := resource.Attributes() - resourceAttributeSet.GetAttributes().InsertTags(&attrs) + resourceAttributeSet.GetAttributes(g.random).InsertTags(&attrs, g.random) rspan.ScopeSpans() ils := rspan.ScopeSpans().AppendEmpty() @@ -94,7 +94,7 @@ func (g *TraceGenerator) createSpanForServiceRouteCall(traces *ptrace.Traces, se ts := serviceTier.GetTagSet(routeName, traceId) // ts is single TagSet consisting of tags from the service AND route attr := span.Attributes() - ts.Tags.InsertTags(&attr) // add service and route tags to span attributes + ts.Tags.InsertTags(&attr, g.random) // add service and route tags to span attributes for _, tg := range ts.TagGenerators { tg.Init(g.random) @@ -106,9 +106,9 @@ func (g *TraceGenerator) createSpanForServiceRouteCall(traces *ptrace.Traces, se // TODO: this is still a bit weird - we're calling each downstream route // after a sample of the current route's latency, which doesn't really // make sense - but maybe it's realistic enough? - endTime := startTimeNanos + route.SampleLatency(traceId) + endTime := startTimeNanos + route.SampleLatency(traceId, g.random) for _, c := range route.DownstreamCalls { - var childStartTimeNanos = startTimeNanos + route.SampleLatency(traceId) + var childStartTimeNanos = startTimeNanos + route.SampleLatency(traceId, g.random) childSpan := g.createSpanForServiceRouteCall(traces, c.Service, c.Route, childStartTimeNanos, traceId, newSpanId) val, ok := childSpan.Attributes().Get("error") diff --git a/generatorreceiver/internal/generator/trace_generator_test.go b/generatorreceiver/internal/generator/trace_generator_test.go index 979878a..02b3419 100644 --- a/generatorreceiver/internal/generator/trace_generator_test.go +++ b/generatorreceiver/internal/generator/trace_generator_test.go @@ -109,7 +109,7 @@ func TestTraceGenerator_createSpanForServiceRouteCall2(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { traces := ptrace.NewTraces() - g := NewTraceGenerator(testTopology, rand.New(rand.NewSource(rand.Int63())), tt.args.serviceName, tt.args.routeName) + g := NewTraceGenerator(testTopology, rand.New(rand.NewSource(123)), tt.args.serviceName, tt.args.routeName) genTraceID := g.genTraceId() rootSpan := g.createSpanForServiceRouteCall(&traces, g.service, g.route, tt.args.startTimeNanos, genTraceID, pcommon.NewSpanIDEmpty()) convertedSpanStartTime := pcommon.NewTimestampFromTime(time.Unix(0, tt.args.startTimeNanos)) diff --git a/generatorreceiver/internal/topology/kubernetes.go b/generatorreceiver/internal/topology/kubernetes.go index d9d1bbb..129c210 100644 --- a/generatorreceiver/internal/topology/kubernetes.go +++ b/generatorreceiver/internal/topology/kubernetes.go @@ -75,20 +75,20 @@ type ResourceUsage struct { Jitter float64 `json:"jitter" yaml:"jitter"` } -func (k *Kubernetes) CreatePods(serviceName string) { +func (k *Kubernetes) CreatePods(serviceName string, random *rand.Rand) { k.mutex.Lock() defer k.mutex.Unlock() - k.ReplicaSetName = serviceName + "-" + generateK8sName(10) + k.ReplicaSetName = serviceName + "-" + generateK8sName(10, random) k.Namespace = serviceName k.Service = serviceName k.pods = make([]*Pod, k.GetPodCount()) for i := 0; i < len(k.pods); i++ { k.pods[i] = &Pod{ StartTime: time.Now(), - PodName: k.ReplicaSetName + "-" + generateK8sName(5), + PodName: k.ReplicaSetName + "-" + generateK8sName(5, random), Container: serviceName, Kubernetes: k, - RestartDuration: k.RestartDurationWithJitter(), + RestartDuration: k.RestartDurationWithJitter(random), } } } @@ -103,7 +103,7 @@ func (k *Kubernetes) GetPodCount() int { } } -func (p *Pod) RestartIfNeeded(flags flags.EmbeddedFlags, logger *zap.Logger) bool { +func (p *Pod) RestartIfNeeded(flags flags.EmbeddedFlags, logger *zap.Logger, random *rand.Rand) bool { if p == nil || p.Kubernetes.Restart.Every == 0 { return false } @@ -115,35 +115,35 @@ func (p *Pod) RestartIfNeeded(flags flags.EmbeddedFlags, logger *zap.Logger) boo if flagTime.After(p.StartTime) { // consider that the pod started at the time that a flag was enabled/disabled. // TODO: restart with some jitter - p.restart(logger) + p.restart(logger, random) return true } else if time.Since(p.StartTime) >= p.RestartDuration { // TODO: restart with some jitter - p.restart(logger) + p.restart(logger, random) return true } return false } -func (p *Pod) restart(logger *zap.Logger) { +func (p *Pod) restart(logger *zap.Logger, random *rand.Rand) { // this is locked by RestartIfNeeded p.StartTime = time.Now() - p.RestartDuration = p.Kubernetes.RestartDurationWithJitter() - p.PodName = p.Kubernetes.ReplicaSetName + "-" + generateK8sName(5) + p.RestartDuration = p.Kubernetes.RestartDurationWithJitter(random) + p.PodName = p.Kubernetes.ReplicaSetName + "-" + generateK8sName(5, random) logger.Info("pod restarted", zap.String("service", p.Kubernetes.Service), zap.String("pod", p.PodName)) } -func (k *Kubernetes) randomPod() *Pod { - return k.pods[rand.Intn(len(k.pods))] +func (k *Kubernetes) randomPod(random *rand.Rand) *Pod { + return k.pods[random.Intn(len(k.pods))] } // only called from tag generator! -func (k *Kubernetes) GetRandomK8sTags() map[string]string { +func (k *Kubernetes) GetRandomK8sTags(random *rand.Rand) map[string]string { k.mutex.Lock() defer k.mutex.Unlock() - pod := k.randomPod() + pod := k.randomPod(random) // ref: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/k8s.md return k.GetK8sTags(pod) } @@ -187,8 +187,8 @@ func (p *Pod) ReplaceTags(tags map[string]string) map[string]string { return replaced } -func (k *Kubernetes) RestartDurationWithJitter() time.Duration { - return k.Restart.Every + time.Duration(float64(k.Restart.Jitter)*(rand.Float64()-0.5)) +func (k *Kubernetes) RestartDurationWithJitter(random *rand.Rand) time.Duration { + return k.Restart.Every + time.Duration(float64(k.Restart.Jitter)*(random.Float64()-0.5)) } func (k *Kubernetes) GenerateMetrics() []Metric { @@ -664,12 +664,12 @@ func (k *Kubernetes) GenerateMetrics() []Metric { return metrics } -func generateK8sName(n int) string { +func generateK8sName(nameLength int, random *rand.Rand) string { var letters = []rune("bcdfghjklmnpqrstvwxz2456789") - b := make([]rune, n) + b := make([]rune, nameLength) for i := range b { - b[i] = letters[rand.Intn(len(letters))] + b[i] = letters[random.Intn(len(letters))] } return string(b) } diff --git a/generatorreceiver/internal/topology/latency_percentiles.go b/generatorreceiver/internal/topology/latency_percentiles.go index 52bcbef..5d2bb18 100644 --- a/generatorreceiver/internal/topology/latency_percentiles.go +++ b/generatorreceiver/internal/topology/latency_percentiles.go @@ -28,7 +28,7 @@ type LatencyPercentiles struct { flags.EmbeddedFlags `json:",inline" yaml:",inline"` } -func (l *LatencyPercentiles) Sample() int64 { +func (l *LatencyPercentiles) Sample(random *rand.Rand) int64 { if l == nil { // This results from having a list where // items are !ShouldGenerate() which leaves @@ -38,9 +38,9 @@ func (l *LatencyPercentiles) Sample() int64 { uniform := func(timeA, timeB time.Duration) int64 { minimum := float64(timeA.Nanoseconds()) maximum := float64(timeB.Nanoseconds()) - return int64(minimum + (maximum-minimum)*rand.Float64()) + return int64(minimum + (maximum-minimum)*random.Float64()) } - genNumber := rand.Float64() + genNumber := random.Float64() switch { case genNumber < 0.5: return uniform(l.durations.p0, l.durations.p50) @@ -90,7 +90,7 @@ func (l *LatencyPercentiles) loadDurations() error { type LatencyConfigs []*LatencyPercentiles -func (lcfg *LatencyConfigs) Sample(traceID pcommon.TraceID) int64 { +func (lcfg *LatencyConfigs) Sample(traceID pcommon.TraceID, random *rand.Rand) int64 { var defaultCfg *LatencyPercentiles var enabled []*LatencyPercentiles for _, cfg := range *lcfg { @@ -104,8 +104,8 @@ func (lcfg *LatencyConfigs) Sample(traceID pcommon.TraceID) int64 { picked := pickBasedOnWeight(enabled, traceID) if picked != nil { - return picked.Sample() + return picked.Sample(random) } } - return defaultCfg.Sample() + return defaultCfg.Sample(random) } diff --git a/generatorreceiver/internal/topology/metric.go b/generatorreceiver/internal/topology/metric.go index d1fec81..0354c80 100644 --- a/generatorreceiver/internal/topology/metric.go +++ b/generatorreceiver/internal/topology/metric.go @@ -62,11 +62,11 @@ type Metric struct { Shape Shape `json:"shape" yaml:"shape"` ShapeInterface ShapeInterface `json:"-" yaml:"-"` Tags map[string]string `json:"tags" yaml:"tags"` - TagGenerator TagGenerator `json:"tagGenerator,omitempty" yaml:"tagGenerator,omitempty"` + TagGenerator TagGenerator `json:"tagGenerator,omitempty" yaml:"tagGenerator,omitempty"` Jitter float64 `json:"jitter" yaml:"jitter"` flags.EmbeddedFlags `json:",inline" yaml:",inline"` Pod *Pod - Random *rand.Rand + Random *rand.Rand } func (m *Metric) GetTags() map[string]string { @@ -74,12 +74,11 @@ func (m *Metric) GetTags() map[string]string { return m.Pod.ReplaceTags(m.Tags) } - tags := make(map[string]string) - for k,v := range m.Tags { + for k, v := range m.Tags { tags[k] = v } - for k,v := range m.TagGenerator.GetRefreshedTags() { + for k, v := range m.TagGenerator.GetRefreshedTags() { tags[k] = v } @@ -159,7 +158,7 @@ func (m *Metric) GetValue() float64 { v := m.Min + (m.Max-m.Min)*factor // jitter deviation is calculated in percentage that ranges from [-m.Jitter/2, m.Jitter/2)% - j := 1 + rand.Float64()*m.Jitter - m.Jitter/2 + j := 1 + m.Random.Float64()*m.Jitter - m.Jitter/2 v = v * j diff --git a/generatorreceiver/internal/topology/resource_attribute_set.go b/generatorreceiver/internal/topology/resource_attribute_set.go index f04165b..9ae2cdd 100644 --- a/generatorreceiver/internal/topology/resource_attribute_set.go +++ b/generatorreceiver/internal/topology/resource_attribute_set.go @@ -2,6 +2,7 @@ package topology import ( "github.com/lightstep/telemetry-generator/generatorreceiver/internal/flags" + "math/rand" ) type ResourceAttributeSet struct { @@ -11,13 +12,13 @@ type ResourceAttributeSet struct { flags.EmbeddedFlags `json:",inline" yaml:",inline"` } -func (r *ResourceAttributeSet) GetAttributes() *TagMap { +func (r *ResourceAttributeSet) GetAttributes(random *rand.Rand) *TagMap { tm := make(TagMap) for k, v := range r.ResourceAttributes { tm[k] = v } if k8s := r.Kubernetes; k8s != nil { - for k, v := range k8s.GetRandomK8sTags() { + for k, v := range k8s.GetRandomK8sTags(random) { tm[k] = v } } diff --git a/generatorreceiver/internal/topology/resource_attribute_set_test.go b/generatorreceiver/internal/topology/resource_attribute_set_test.go index 05f27cf..6459a00 100644 --- a/generatorreceiver/internal/topology/resource_attribute_set_test.go +++ b/generatorreceiver/internal/topology/resource_attribute_set_test.go @@ -68,11 +68,10 @@ func TestResourceAttributeSet_GetAttributes(t *testing.T) { } if k := resourceAttrSet.Kubernetes; k != nil { - rand.Seed(123) + randomGenerator := rand.New(rand.NewSource(123)) k.CreatePods(tt.service) // k8s.pod.name structure was copied from CreatePods() - rand.Seed(123) tt.expected["k8s.pod.name"] = tt.service + "-" + generateK8sName(10) + "-" + generateK8sName(5) } diff --git a/generatorreceiver/internal/topology/service_route.go b/generatorreceiver/internal/topology/service_route.go index a6066c5..56e21c2 100644 --- a/generatorreceiver/internal/topology/service_route.go +++ b/generatorreceiver/internal/topology/service_route.go @@ -91,10 +91,10 @@ func (r *ServiceRoute) load(route string) error { return nil } -func (r *ServiceRoute) SampleLatency(traceID pcommon.TraceID) int64 { +func (r *ServiceRoute) SampleLatency(traceID pcommon.TraceID, random *rand.Rand) int64 { if r.LatencyConfigs == nil { - return rand.Int63n(r.MaxLatencyMillis * 1000000) + return random.Int63n(r.MaxLatencyMillis * 1000000) } else { - return r.LatencyConfigs.Sample(traceID) + return r.LatencyConfigs.Sample(traceID, random) } } diff --git a/generatorreceiver/internal/topology/tag_map.go b/generatorreceiver/internal/topology/tag_map.go index 039c4e2..2995662 100644 --- a/generatorreceiver/internal/topology/tag_map.go +++ b/generatorreceiver/internal/topology/tag_map.go @@ -9,7 +9,7 @@ import ( type TagMap map[string]interface{} -func (tm *TagMap) InsertTags(attr *pcommon.Map) { +func (tm *TagMap) InsertTags(attr *pcommon.Map, random *rand.Rand) { for key, val := range *tm { switch val := val.(type) { case float64: @@ -24,7 +24,7 @@ func (tm *TagMap) InsertTags(attr *pcommon.Map) { case bool: attr.PutBool(key, val) case []string: - attr.PutStr(key, val[rand.Intn(len(val))]) + attr.PutStr(key, val[random.Intn(len(val))]) default: attr.PutStr(key, fmt.Sprint(val)) } diff --git a/generatorreceiver/internal/topology/tag_set_test.go b/generatorreceiver/internal/topology/tag_set_test.go index 5dbd895..af9f6ed 100644 --- a/generatorreceiver/internal/topology/tag_set_test.go +++ b/generatorreceiver/internal/topology/tag_set_test.go @@ -26,16 +26,16 @@ func TestTagMap_InsertTag(t *testing.T) { attr := pcommon.NewMap() - rand.Seed(123) - ts.Tags.InsertTags(&attr) + randomGenerator := rand.New(rand.NewSource(123)) + + ts.Tags.InsertTags(&attr, randomGenerator) expectedAttr := pcommon.NewMap() expectedAttr.PutBool("key1", true) expectedAttr.PutStr("key2", "hi") expectedAttr.PutDouble("key3", 123.123) expectedAttr.PutInt("key4", 10) - rand.Seed(123) - expectedAttr.PutStr("key5", csvTags[rand.Intn(len(csvTags))]) + expectedAttr.PutStr("key5", csvTags[randomGenerator.Intn(len(csvTags))]) require.Equal(t, attr.AsRaw(), expectedAttr.AsRaw()) } From d07c7e47ba6e63e51089aa40bbe612386ce8eb01 Mon Sep 17 00:00:00 2001 From: "jason.crouse" Date: Fri, 6 Sep 2024 14:24:20 -0400 Subject: [PATCH 2/4] fix missing args --- generatorreceiver/internal/topology/kubernetes_test.go | 10 ++++++---- .../internal/topology/latency_percentiles_test.go | 5 +++-- .../internal/topology/resource_attribute_set_test.go | 10 ++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/generatorreceiver/internal/topology/kubernetes_test.go b/generatorreceiver/internal/topology/kubernetes_test.go index 8578e2d..5af3c32 100644 --- a/generatorreceiver/internal/topology/kubernetes_test.go +++ b/generatorreceiver/internal/topology/kubernetes_test.go @@ -2,6 +2,7 @@ package topology import ( "github.com/stretchr/testify/require" + "math/rand" "testing" "time" ) @@ -59,13 +60,14 @@ func TestMultiPod(t *testing.T) { }, PodCount: nPods, } - k.CreatePods("some") + random := rand.New(rand.NewSource(123)) + k.CreatePods("some", random) // we should see more than one pod name in the tags // (odds of this test failing randomly are 1 in 7**100 =~ 3 in 10^85 names := make(map[string]bool) for i := 0; i < 100; i++ { - tags := k.GetRandomK8sTags() + tags := k.GetRandomK8sTags(random) names[tags["k8s.pod.name"]] = true } require.Greater(t, len(names), 1, "multiple pod names should be generated") @@ -90,7 +92,7 @@ func TestMultiPod(t *testing.T) { }, }, } - k.CreatePods("some") + k.CreatePods("some", random) require.Equal(t, 2, k.GetPodCount(), "pod count defaults to config value") k = &Kubernetes{ ClusterName: "some-cluster", @@ -99,6 +101,6 @@ func TestMultiPod(t *testing.T) { Jitter: minute, }, } - k.CreatePods("some") + k.CreatePods("some", random) require.Equal(t, 1, k.GetPodCount(), "pod count defaults to 1 if no config value") } diff --git a/generatorreceiver/internal/topology/latency_percentiles_test.go b/generatorreceiver/internal/topology/latency_percentiles_test.go index b0d6091..6205232 100644 --- a/generatorreceiver/internal/topology/latency_percentiles_test.go +++ b/generatorreceiver/internal/topology/latency_percentiles_test.go @@ -1,6 +1,7 @@ package topology import ( + "math/rand" "testing" "time" @@ -52,7 +53,7 @@ func TestLatencyPercentiles_WorksWithoutWeights(t *testing.T) { err := cfg.loadDurations() require.NoError(t, err) } - - latency := cfgs.Sample(pcommon.NewTraceIDEmpty()) + random := rand.New(rand.NewSource(123)) + latency := cfgs.Sample(pcommon.NewTraceIDEmpty(), random) require.Equal(t, 100*time.Millisecond, time.Duration(latency)) } diff --git a/generatorreceiver/internal/topology/resource_attribute_set_test.go b/generatorreceiver/internal/topology/resource_attribute_set_test.go index 6459a00..f48dbcd 100644 --- a/generatorreceiver/internal/topology/resource_attribute_set_test.go +++ b/generatorreceiver/internal/topology/resource_attribute_set_test.go @@ -67,15 +67,17 @@ func TestResourceAttributeSet_GetAttributes(t *testing.T) { ResourceAttributes: tt.resourceAttributes, } + random := rand.New(rand.NewSource(123)) + if k := resourceAttrSet.Kubernetes; k != nil { - randomGenerator := rand.New(rand.NewSource(123)) - k.CreatePods(tt.service) + + k.CreatePods(tt.service, random) // k8s.pod.name structure was copied from CreatePods() - tt.expected["k8s.pod.name"] = tt.service + "-" + generateK8sName(10) + "-" + generateK8sName(5) + tt.expected["k8s.pod.name"] = tt.service + "-" + generateK8sName(10, random) + "-" + generateK8sName(5, random) } - require.Equal(t, tt.expected, *resourceAttrSet.GetAttributes()) + require.Equal(t, tt.expected, *resourceAttrSet.GetAttributes(random)) }) } } From f23fa356ae43d25dafbe60e56d727e6af8c06824 Mon Sep 17 00:00:00 2001 From: "jason.crouse" Date: Fri, 6 Sep 2024 15:39:40 -0400 Subject: [PATCH 3/4] fix this other random lint error --- generatorreceiver/internal/topology/topology.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/generatorreceiver/internal/topology/topology.go b/generatorreceiver/internal/topology/topology.go index 363e89e..0c6eb47 100644 --- a/generatorreceiver/internal/topology/topology.go +++ b/generatorreceiver/internal/topology/topology.go @@ -1,6 +1,9 @@ package topology -import "fmt" +import ( + "errors" + "fmt" +) type Topology struct { Services map[string]*ServiceTier `json:"services" yaml:"services"` @@ -37,7 +40,7 @@ func (t *Topology) traverseServiceGraph(service string, route string, seenCalls seenCalls[service+route] = true for _, c := range downstreamCalls { if seenCalls[c.Service+c.Route] { - return fmt.Errorf(printServiceCycle(orderedCalls, c.Service+c.Route)) + return errors.New(printServiceCycle(orderedCalls, c.Service+c.Route)) } err := t.traverseServiceGraph(c.Service, c.Route, seenCalls, append(orderedCalls, c.Service+c.Route)) From d012bdeac23be468bd82665eee47f91fec3751ce Mon Sep 17 00:00:00 2001 From: "jason.crouse" Date: Fri, 6 Sep 2024 15:56:54 -0400 Subject: [PATCH 4/4] need to reset the generator for tests so the same series of randoms are generated --- .../internal/topology/resource_attribute_set_test.go | 6 +++--- generatorreceiver/internal/topology/tag_map.go | 3 ++- generatorreceiver/internal/topology/tag_set_test.go | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/generatorreceiver/internal/topology/resource_attribute_set_test.go b/generatorreceiver/internal/topology/resource_attribute_set_test.go index f48dbcd..2112ce2 100644 --- a/generatorreceiver/internal/topology/resource_attribute_set_test.go +++ b/generatorreceiver/internal/topology/resource_attribute_set_test.go @@ -67,16 +67,16 @@ func TestResourceAttributeSet_GetAttributes(t *testing.T) { ResourceAttributes: tt.resourceAttributes, } - random := rand.New(rand.NewSource(123)) - if k := resourceAttrSet.Kubernetes; k != nil { - + random := rand.New(rand.NewSource(123)) k.CreatePods(tt.service, random) // k8s.pod.name structure was copied from CreatePods() + random = rand.New(rand.NewSource(123)) tt.expected["k8s.pod.name"] = tt.service + "-" + generateK8sName(10, random) + "-" + generateK8sName(5, random) } + random := rand.New(rand.NewSource(123)) require.Equal(t, tt.expected, *resourceAttrSet.GetAttributes(random)) }) } diff --git a/generatorreceiver/internal/topology/tag_map.go b/generatorreceiver/internal/topology/tag_map.go index 2995662..b8da5c0 100644 --- a/generatorreceiver/internal/topology/tag_map.go +++ b/generatorreceiver/internal/topology/tag_map.go @@ -24,7 +24,8 @@ func (tm *TagMap) InsertTags(attr *pcommon.Map, random *rand.Rand) { case bool: attr.PutBool(key, val) case []string: - attr.PutStr(key, val[random.Intn(len(val))]) + r := random.Intn(len(val)) + attr.PutStr(key, val[r]) default: attr.PutStr(key, fmt.Sprint(val)) } diff --git a/generatorreceiver/internal/topology/tag_set_test.go b/generatorreceiver/internal/topology/tag_set_test.go index af9f6ed..b0b9ca3 100644 --- a/generatorreceiver/internal/topology/tag_set_test.go +++ b/generatorreceiver/internal/topology/tag_set_test.go @@ -35,6 +35,8 @@ func TestTagMap_InsertTag(t *testing.T) { expectedAttr.PutStr("key2", "hi") expectedAttr.PutDouble("key3", 123.123) expectedAttr.PutInt("key4", 10) + + randomGenerator = rand.New(rand.NewSource(123)) expectedAttr.PutStr("key5", csvTags[randomGenerator.Intn(len(csvTags))]) require.Equal(t, attr.AsRaw(), expectedAttr.AsRaw())