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

Tencent/bk-bcs: bcs-common/common/metric/metric.go; 61 LoC

Open
#15,647 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 [Tencent/bk-bcs](https://www.github.com/Tencent/bk-bcs) at [bcs-common/common/metric/metric.go](https://github.com/Tencent/bk-bcs/blob/da6deda24df3f5accd42e0874ac5419001dc3840/bcs-common/common/metric/metric.go#L65-L125)

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 metric used in defer or goroutine at line 75

[Click here to see the code in its original context.](https://github.com/Tencent/bk-bcs/blob/da6deda24df3f5accd42e0874ac5419001dc3840/bcs-common/common/metric/metric.go#L65-L125)

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

```go
for _, metric := range m.Metrics {
blog.V(5).Infof("collect metric - > %s\n", metric.GetMeta().Name)
done := make(chan struct{})

go func(mtc *MetricContructor) {
result, err := mtc.GetResult()
if err != nil {
blog.Errorf("get metric result failed. err: %v", err)
return
}
base := metric.GetMeta()
// add special labels
if base.ConstLables == nil {
base.ConstLables = make(map[string]string)
}
base.ConstLables[module_ip_label] = m.Meta.IP
base.ConstLables[module_cluster_id_label] = m.Meta.ClusterID

var varLabelsKey, varLablesValue []string
for k, v := range result.VariableLabels {
varLabelsKey = append(varLabelsKey, k)
varLablesValue = append(varLablesValue, v)
}
var g *prometheus.GaugeVec
switch result.Value.Type {
case Float:
g = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: m.Meta.Module,
Name: base.Name,
Help: base.Help,
ConstLabels: prometheus.Labels(base.ConstLables),
}, varLabelsKey)
g.WithLabelValues(varLablesValue...).Set(result.Value.Float)
case String:
varLabelsKey = append(varLabelsKey, "bcs_metric_value")
varLablesValue = append(varLablesValue, result.Value.String)
g = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: m.Meta.Module,
Name: base.Name,
Help: base.Help,
ConstLabels: prometheus.Labels(base.ConstLables),
}, varLabelsKey)
g.WithLabelValues(varLablesValue...).Set(1)
default:
blog.Errorf("unsupported metric value type: %s", result.Value.Type)
done <- struct{}{}
return
}
g.Collect(ch)
done <- struct{}{}
}(metric)

timeout := time.After(10 * time.Second)
select {
case <-timeout:
blog.Errorf("get metric %s timeout, skip.", metric.GetMeta().Name)
continue
case <-done:
close(done)
}
}

```

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: da6deda24df3f5accd42e0874ac5419001dc3840

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading bcs-common/common/metric/metric.go around lines 65-125, focusing on the range loop and goroutine identified by the analyzer. Reproduce or inspect the reported capture behavior, then run the relevant Go tests or checks for metric collection. Done means the range-loop warning is resolved without changing metric collection or timeout behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
observability
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
56/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.