mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-04-19 19:21:41 +02:00
Create deep copy in New() to avoid access conflicts
This commit is contained in:
parent
eca963c58a
commit
0e70b663ee
@ -48,14 +48,14 @@ func (m *ccMetric) MetaMap() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MetaList returns the the list of meta data tags as sorted list of key value tags
|
// MetaList returns the the list of meta data tags as sorted list of key value tags
|
||||||
func (m *ccMetric) MetaList() (ml []*lp.Tag) {
|
func (m *ccMetric) MetaList() []*lp.Tag {
|
||||||
|
|
||||||
ml = make([]*lp.Tag, 0, len(m.meta))
|
ml := make([]*lp.Tag, 0, len(m.meta))
|
||||||
for key, value := range m.meta {
|
for key, value := range m.meta {
|
||||||
ml = append(ml, &lp.Tag{Key: key, Value: value})
|
ml = append(ml, &lp.Tag{Key: key, Value: value})
|
||||||
}
|
}
|
||||||
sort.Slice(ml, func(i, j int) bool { return ml[i].Key < ml[j].Key })
|
sort.Slice(ml, func(i, j int) bool { return ml[i].Key < ml[j].Key })
|
||||||
return
|
return ml
|
||||||
}
|
}
|
||||||
|
|
||||||
// String implements the stringer interface for data type ccMetric
|
// String implements the stringer interface for data type ccMetric
|
||||||
@ -74,14 +74,13 @@ func (m *ccMetric) TagMap() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TagList returns the the list of tags as sorted list of key value tags
|
// TagList returns the the list of tags as sorted list of key value tags
|
||||||
func (m *ccMetric) TagList() (tl []*lp.Tag) {
|
func (m *ccMetric) TagList() []*lp.Tag {
|
||||||
|
tl := make([]*lp.Tag, 0, len(m.tags))
|
||||||
tl = make([]*lp.Tag, 0, len(m.tags))
|
|
||||||
for key, value := range m.tags {
|
for key, value := range m.tags {
|
||||||
tl = append(tl, &lp.Tag{Key: key, Value: value})
|
tl = append(tl, &lp.Tag{Key: key, Value: value})
|
||||||
}
|
}
|
||||||
sort.Slice(tl, func(i, j int) bool { return tl[i].Key < tl[j].Key })
|
sort.Slice(tl, func(i, j int) bool { return tl[i].Key < tl[j].Key })
|
||||||
return
|
return tl
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fields returns the list of fields as key-value-mapping
|
// Fields returns the list of fields as key-value-mapping
|
||||||
@ -211,15 +210,23 @@ func New(
|
|||||||
) (CCMetric, error) {
|
) (CCMetric, error) {
|
||||||
m := &ccMetric{
|
m := &ccMetric{
|
||||||
name: name,
|
name: name,
|
||||||
tags: tags,
|
tags: make(map[string]string, len(tags)),
|
||||||
fields: nil,
|
meta: make(map[string]string, len(meta)),
|
||||||
|
fields: make([]*lp.Field, 0, len(fields)),
|
||||||
tm: tm,
|
tm: tm,
|
||||||
meta: meta,
|
}
|
||||||
|
|
||||||
|
// deep copy tags
|
||||||
|
for k, v := range tags {
|
||||||
|
m.tags[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// deep copy meta data tags
|
||||||
|
for k, v := range meta {
|
||||||
|
m.meta[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unsorted list of fields
|
// Unsorted list of fields
|
||||||
if len(fields) > 0 {
|
|
||||||
m.fields = make([]*lp.Field, 0, len(fields))
|
|
||||||
for k, v := range fields {
|
for k, v := range fields {
|
||||||
v := convertField(v)
|
v := convertField(v)
|
||||||
if v == nil {
|
if v == nil {
|
||||||
@ -227,7 +234,6 @@ func New(
|
|||||||
}
|
}
|
||||||
m.AddField(k, v)
|
m.AddField(k, v)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user