Skip to content

Commit

Permalink
Merge pull request #289 from newrelic/fix_race_condition_new_metric_set
Browse files Browse the repository at this point in the history
Fix race condition when calling entityMetadata Key endpoint
  • Loading branch information
alvarocabanas committed Mar 3, 2022
2 parents 1f48f6c + 37a3d18 commit bb90381
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion integration/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type EntityMetadata struct {
Name string `json:"name"`
Namespace string `json:"type"` // For compatibility reasons we keep the type.
IDAttrs IDAttributes `json:"id_attributes"` // For entity Key uniqueness
lock sync.Locker
}

// EqualsTo returns true when both metadata are equal.
Expand Down Expand Up @@ -89,6 +90,7 @@ func newEntity(
Name: name,
Namespace: namespace,
IDAttrs: idAttributes(idAttrs...),
lock: &sync.Mutex{},
},
}

Expand All @@ -105,7 +107,6 @@ func (e *Entity) SameAs(b *Entity) bool {
if e.Metadata == nil || b.Metadata == nil {
return false
}

return e.Metadata.EqualsTo(b.Metadata)
}

Expand Down
3 changes: 3 additions & 0 deletions integration/entity_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (k EntityKey) String() string {

// Key generates the entity key based on the entity metadata.
func (m *EntityMetadata) Key() (EntityKey, error) {
m.lock.Lock()
defer m.lock.Unlock()

if len(m.Name) == 0 {
return EmptyKey, nil // Empty value means this agent's default entity identifier
}
Expand Down
27 changes: 27 additions & 0 deletions integration/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/newrelic/infra-integrations-sdk/data/attribute"
"github.com/newrelic/infra-integrations-sdk/data/event"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/infra-integrations-sdk/persist"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -248,3 +249,29 @@ func TestEntity_SameAs(t *testing.T) {
assert.True(t, e1.SameAs(e2))
assert.False(t, e1.SameAs(e3))
}

func TestEntity_NewMetricSet(t *testing.T) {
i, err := New(integrationName, integrationVersion)

entity, err := i.Entity("a-name", "a-type")
assert.NoError(t, err)

metricSetChan := make(chan *metric.Set)
go func(e *Entity) {
metricSetChan <- e.NewMetricSet("F5BigIpPoolMemberSample",
attribute.Attribute{Key: "displayName", Value: "entityName"},
attribute.Attribute{Key: "entityName", Value: "entityName"},
attribute.Attribute{Key: "poolName", Value: "description"},
attribute.Attribute{Key: "url", Value: "url"},
)
}(entity)

// We retrieve the entity at the same time to check for race conditions
go func(i *Integration) {
_, err := i.Entity("a-name", "a-type")
assert.NoError(t, err)
}(i)

metricSet := <-metricSetChan
assert.NotEmpty(t, metricSet)
}

0 comments on commit bb90381

Please sign in to comment.