Create deep copy in New() to avoid access conflicts

This commit is contained in:
Holger Obermaier 2022-02-01 08:12:08 +01:00
parent eca963c58a
commit 0e70b663ee

View File

@ -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,22 +210,29 @@ 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 { for k, v := range fields {
m.fields = make([]*lp.Field, 0, len(fields)) v := convertField(v)
for k, v := range fields { if v == nil {
v := convertField(v) continue
if v == nil {
continue
}
m.AddField(k, v)
} }
m.AddField(k, v)
} }
return m, nil return m, nil