github-vet / github-vet/rangeloop-pointer-findings
circonus-labs/irondb-prometheus-adapter: handlers/prometheus2_0.go; 139 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [circonus-labs/irondb-prometheus-adapter](https://www.github.com/circonus-labs/irondb-prometheus-adapter) at [handlers/prometheus2_0.go](https://github.com/circonus-labs/irondb-prometheus-adapter/blob/aa026c9e0fcc3b5c2cc286df9ee1e3f64ec8296e/handlers/prometheus2_0.go#L185-L323)
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 q used in defer or goroutine at line 282
[Click here to see the code in its original context.](https://github.com/circonus-labs/irondb-prometheus-adapter/blob/aa026c9e0fcc3b5c2cc286df9ee1e3f64ec8296e/handlers/prometheus2_0.go#L185-L323)
Click here to show the 139 line(s) of Go which triggered the analyzer.
```go
for _, q := range req.GetQueries() {
// foreach query, perform the query and generate a query result
var (
// for each query we will be making a queryresponse
qr = new(prompb.QueryResult)
snowthTagQuery strings.Builder
streamTags = []string{}
)
// always include the check_uuid in the tag query, will reduce search space
snowthTagQuery.WriteString("and(__check_uuid:")
snowthTagQuery.WriteString(prp.checkUUID.String())
snowthTagQuery.WriteString(",")
for i, m := range q.GetMatchers() {
// for each of the matchers within the query
// take each matcher and formulate a stream tag filter
if i > 0 {
snowthTagQuery.WriteByte(',')
}
var (
name string = m.GetName()
value string = m.GetValue()
)
if name == "__name__" {
name = "__name"
}
var (
matcherName = base64.StdEncoding.EncodeToString(
[]byte(name))
matcherValue = base64.StdEncoding.EncodeToString(
[]byte(value))
)
// based on the matcher type, we need to build out our tag query
switch m.Type {
case prompb.LabelMatcher_EQ:
// query equal
tag := fmt.Sprintf(`b"%s":b"%s"`, matcherName, matcherValue)
snowthTagQuery.WriteString(tag)
streamTags = append(streamTags, tag)
case prompb.LabelMatcher_NEQ:
// query not equal
tag := fmt.Sprintf(`b"%s":b"%s"`, matcherName, matcherValue)
snowthTagQuery.WriteString("not(")
snowthTagQuery.WriteString(tag)
snowthTagQuery.WriteByte(')')
case prompb.LabelMatcher_RE:
// query regular expression
tag := fmt.Sprintf(`b"%s":b/%s/`, matcherName, matcherValue)
snowthTagQuery.WriteString(tag)
streamTags = append(streamTags, tag)
case prompb.LabelMatcher_NRE:
// query not regular expression
tag := fmt.Sprintf(`b"%s":b/%s/`, matcherName, matcherValue)
snowthTagQuery.WriteString("not(")
snowthTagQuery.WriteString(tag)
snowthTagQuery.WriteByte(')')
}
}
// close our and(
snowthTagQuery.WriteByte(')')
var (
tagResp []gosnowth.FindTagsItem
err error
)
start := time.Now()
ctx.Logger().Warnf("timing find query: %s", snowthTagQuery.String())
tagResp, err = snowthClient.FindTags(node, prp.accountID, snowthTagQuery.String(), "", "")
ctx.Logger().Warnf("timing find query: %s, duration: %v", snowthTagQuery.String(), time.Now().Sub(start))
if err != nil {
ctx.Logger().Errorf("failed to find tags: %s", err.Error())
continue
}
ctx.Logger().Warnf("doing %d rollups for query: %s", len(tagResp), snowthTagQuery.String())
var tsChan = make(chan *prompb.TimeSeries, 100)
var step = 60 * time.Second
if q.Hints != nil && q.Hints.StepMs > 0 {
step = time.Duration(q.Hints.StepMs) * time.Millisecond
}
var wg sync.WaitGroup
for _, v := range tagResp {
wg.Add(1)
go func() {
defer wg.Done()
// for all of our tag responses, grab the rollups
start := time.Now()
values, err := snowthClient.ReadRollupValues(
node, prp.checkUUID.String(), v.MetricName, []string{}, step,
time.Unix(0, q.StartTimestampMs*int64(time.Millisecond)),
time.Unix(0, q.EndTimestampMs*int64(time.Millisecond)),
)
ctx.Logger().Warnf("timing rollup query: %s, %s, %d, %d, %d, result length: %d, duration: %v",
prp.checkUUID.String(), v.MetricName, int64(step), q.StartTimestampMs*int64(time.Millisecond),
q.EndTimestampMs*int64(time.Millisecond), len(values), time.Now().Sub(start))
ctx.Logger().Debugf("rollup results: %+v", values)
timeSeries := new(prompb.TimeSeries)
if err != nil {
ctx.Logger().Errorf("failed to read rollup: %s", err.Error())
tsChan <- timeSeries
return
}
timeSeries.Labels = metricNameToLabelPairs(v.MetricName)
for _, v := range values {
// convert value to time series
timeSeries.Samples = append(timeSeries.Samples,
&prompb.Sample{
Value: v.Value,
Timestamp: v.Timestamp * 1000, // prom does ms
})
}
// add the timeseries to the query result
tsChan <- timeSeries
}()
}
go func() {
wg.Wait()
close(tsChan)
}()
for timeSeries := range tsChan {
ctx.Logger().Warnf("time series added to resultset, #samples: %d", len(timeSeries.Samples))
qr.Timeseries = append(qr.Timeseries, timeSeries)
}
// add this result to our results
resp.Results = append(resp.Results, qr)
}
```
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: aa026c9e0fcc3b5c2cc286df9ee1e3f64ec8296e
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.