honey: add read locking to event.Fields call for NonSendingReader (#61886)

The documentation for fields already says that the returned map is not safe for concurrent modification.

However, I realized the variable assignment for the map should to be protected by a lock itself too.

commit-id:9c5542b1
This commit is contained in:
Geoffrey Gilmore 2024-04-15 08:19:38 -07:00 committed by GitHub
parent 9fab1b4e1d
commit 44bf2c4883
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,7 +16,7 @@ func NonSendingEvent() Event {
// Honeycomb, but instead aggregates it in memory. This is useful for testing and
// logging purposes.
type nonSendingEvent struct {
fieldsWritingMu sync.Mutex
fieldsWritingMu sync.RWMutex
fields map[string]any
}
@ -57,6 +57,9 @@ func (e *nonSendingEvent) AddAttributes(values []attribute.KeyValue) {
// Fields returns all the added fields of the event. The returned map is not safe to
// be modified concurrently with calls to AddField or AddAttributes.
func (e *nonSendingEvent) Fields() map[string]any {
e.fieldsWritingMu.RLock()
defer e.fieldsWritingMu.RUnlock()
return e.fields
}