github-vet / github-vet/rangeloop-pointer-findings

DataDog/datadog-agent: test/benchmarks/aggregator/memory.go; 99 LoC

Open
#6,212 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [DataDog/datadog-agent](https://www.github.com/DataDog/datadog-agent) at [test/benchmarks/aggregator/memory.go](https://github.com/DataDog/datadog-agent/blob/48c550dd038a656bc97b7ad893cc55172830cb1b/test/benchmarks/aggregator/memory.go#L111-L209)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable s used in defer or goroutine at line 164

[Click here to see the code in its original context.](https://github.com/DataDog/datadog-agent/blob/48c550dd038a656bc97b7ad893cc55172830cb1b/test/benchmarks/aggregator/memory.go#L111-L209)

Click here to show the 99 line(s) of Go which triggered the analyzer.

```go
for _, s := range series {
for _, p := range points {
tags := []string{
fmt.Sprintf("branch:%s", branchName),
fmt.Sprintf("points:%d", p),
fmt.Sprintf("series:%d", s),
}

// pre-allocate for operational memory usage benchmarking
metrics := make([]map[string][]*metrics.MetricSample, s)
for i := range metrics {
metrics[i] = preAllocateMetrics(p)
}
scs := preAllocateServiceChecks(p)
events := preAllocateEvents(p)
sent := 0

// get memory stats
initial := memstatsFunc().(runtime.MemStats)

wg.Add(1)
go func() {
defer wg.Done()

i := 0
for range ticker.C {
i += 1
i = i % p
select {
case <-quitGenerator:
return
default:
// Submit Metrics
for _, m := range metrics {
for _, generated := range m {
rawSender.SendRawMetricSample(generated[i])
sent += 1
}
}

// Submit ServiceCheck
rawSender.SendRawServiceCheck(scs[i])
sent += 1

// Submit Event
rawSender.Event(*events[i])
sent += 1
}
}
}()

wg.Add(1)
go func() {
log.Infof("[aggregator] starting memory statter (%d points per series, %d series)", p, s)
tickChan := time.NewTicker(time.Second).C
defer wg.Done()

// get memory stats
prev := initial

secs := 0
for range tickChan {
// compute metrics
current := memstatsFunc().(runtime.MemStats)
delta := float64(current.Alloc) - float64(prev.Alloc)
mallocDelta := float64(current.Mallocs) - float64(prev.Mallocs)
live := float64(current.Mallocs) - float64(current.Frees)

t := time.Now().Unix()
mAlloc := createMetric(float64(current.Alloc), tags, "benchmark.aggregator.mem.alloc", t)
mDelta := createMetric(delta, tags, "benchmark.aggregator.mem.delta", t)
mDeltaMalloc := createMetric(mallocDelta, tags, "benchmark.aggregator.mem.delta.malloc", t)
mLive := createMetric(live, tags, "benchmark.aggregator.mem.live", t)

// Append to result slice
results = append(results, mAlloc)
results = append(results, mDelta)
results = append(results, mDeltaMalloc)
results = append(results, mLive)

log.Infof("[aggregator] allocated: %10d\tdelta: %11.f mallocs: %11.f live objects:%11.f", current.Alloc, delta, mallocDelta, live)
prev = current
if secs == dur {
t := time.Now().Unix()
delta = float64(current.Alloc) - float64(initial.Alloc)
log.Infof("[aggregator] total memory delta %11.f bytes", delta)
log.Infof("[aggregator] benchmark concluded at a rate of %v pps (avg over %v secs)", sent/dur, dur)
results = append(results, createMetric(delta, tags, "benchmark.aggregator.mem.total_delta", t))
results = append(results, createMetric(float64(sent/dur), tags, "benchmark.aggregator.mem.rate", t))
quitGenerator <- true
return
}
secs += 1
}
}()

wg.Wait()
}
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 48c550dd038a656bc97b7ad893cc55172830cb1b

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.