github-vet / github-vet/rangeloop-pointer-findings
Tencent/bk-bcs: bcs-common/common/metric/metric.go; 61 LoC
- 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/4f7af6157ef2a6eca524da40868bec4087bcff11/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/4f7af6157ef2a6eca524da40868bec4087bcff11/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: 4f7af6157ef2a6eca524da40868bec4087bcff11
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.