From 8c2431ae86e22553ed48b3bffddb152a09e39d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E9=BE=99=E5=A5=87?= Date: Wed, 4 Sep 2024 16:25:57 +0800 Subject: [PATCH] add lock --- internal/plugins/inputs/ddtrace/api_telemetry.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/plugins/inputs/ddtrace/api_telemetry.go b/internal/plugins/inputs/ddtrace/api_telemetry.go index a77ee5780d..ccaecd1244 100644 --- a/internal/plugins/inputs/ddtrace/api_telemetry.go +++ b/internal/plugins/inputs/ddtrace/api_telemetry.go @@ -22,6 +22,7 @@ import ( ) type Telemetry struct { + lock sync.RWMutex host Host application Application traceTime time.Time @@ -32,12 +33,20 @@ type Telemetry struct { } func (ob *Telemetry) toPoint() *point.Point { + ob.lock.RLock() + defer ob.lock.RUnlock() opts := point.DefaultObjectOptions() opts = append(opts, point.WithTime(ob.traceTime)) kvs := append(point.NewTags(ob.tags), point.NewKVs(ob.fields)...) return point.NewPointV2("tracing_service", kvs, opts...) } +func (ob *Telemetry) setField(key string, val interface{}) { + ob.lock.Lock() + defer ob.lock.Unlock() + ob.fields[key] = val +} + func (ob *Telemetry) Info() *inputs.MeasurementInfo { return &inputs.MeasurementInfo{ Name: "DdTrace APM Telemetry", @@ -130,7 +139,7 @@ func (ob *Telemetry) parseEvent(requestType RequestType, payload interface{}) { log.Errorf("err=%v", err) return } - ob.fields[string(requestType)] = string(bts) + ob.setField(string(requestType), string(bts)) ob.change = true case RequestTypeAppHeartbeat, RequestTypeDistributions: @@ -158,12 +167,12 @@ func (ob *Telemetry) parseEvent(requestType RequestType, payload interface{}) { for _, points := range series.Points { m += points[1] } - ob.fields[series.Metric+seriesTag] = m + ob.setField(series.Metric+seriesTag, m) } } ob.change = true case RequestTypeAppClosing: - ob.fields[string(requestType)] = "service closing" + ob.setField(string(requestType), "service closing") ob.change = true case RequestTypeMessageBatch: bts, err := json.Marshal(payload)