Skip to content

Commit

Permalink
Merge pull request #254 from newrelic/feat_entity-common-dimenensions
Browse files Browse the repository at this point in the history
feat: entity common dimensions support
  • Loading branch information
varas authored Jan 15, 2021
2 parents 65ba3ca + 7044158 commit 1601955
Showing 2 changed files with 63 additions and 29 deletions.
53 changes: 24 additions & 29 deletions integration/entity.go
Original file line number Diff line number Diff line change
@@ -12,17 +12,14 @@ import (

// Entity is the producer of the data. Entity could be a host, a container, a pod, or whatever unit of meaning.
type Entity struct {
Common *Common `json:"common"`
Metadata *metadata.Metadata `json:"entity,omitempty"`
Metrics metric.Metrics `json:"metrics"`
Inventory *inventory.Inventory `json:"inventory"`
Events event.Events `json:"events"`
lock sync.Locker
CommonDimensions map[string]string `json:"common"` // dimensions common to every entity metric
Metadata *metadata.Metadata `json:"entity,omitempty"`
Metrics metric.Metrics `json:"metrics"`
Inventory *inventory.Inventory `json:"inventory"`
Events event.Events `json:"events"`
lock sync.Locker
}

// Common is a common set of attributes
type Common struct{}

// SameAs return true when is same entity
func (e *Entity) SameAs(b *Entity) bool {
if e.Metadata == nil || b.Metadata == nil {
@@ -58,6 +55,14 @@ func (e *Entity) AddInventoryItem(key string, field string, value interface{}) e
return e.Inventory.SetItem(key, field, value)
}

// AddCommonDimension adds a new dimension to every metric within the entity.
func (e *Entity) AddCommonDimension(key string, value string) {
e.lock.Lock()
defer e.lock.Unlock()

e.CommonDimensions[key] = value
}

// GetMetadata returns all the Entity's metadata
func (e *Entity) GetMetadata() metadata.Map {
return e.Metadata.Metadata
@@ -91,13 +96,12 @@ func (e *Entity) Name() string {
// newHostEntity creates a entity without metadata.
func newHostEntity() *Entity {
return &Entity{
Common: &Common{},
Metadata: nil,
// empty array or object preferred instead of null on marshaling.
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
CommonDimensions: make(metric.Dimensions),
Metadata: nil,
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
}
}

@@ -107,23 +111,14 @@ func (e *Entity) isHostEntity() bool {
}

// newEntity creates a new entity with with metadata.
func newEntity(
name,
entityType string,
displayName string) (*Entity, error) {
func newEntity(name, entityType string, displayName string) (*Entity, error) {

if name == "" || entityType == "" {
return nil, errors.New("entity name and type cannot be empty")
}

e := &Entity{
// empty array or object preferred instead of null on marshaling.
Common: &Common{},
Metadata: metadata.New(name, entityType, displayName),
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: []*event.Event{},
lock: &sync.Mutex{},
}
e := newHostEntity()
e.Metadata = metadata.New(name, entityType, displayName)

return e, nil
}
39 changes: 39 additions & 0 deletions integration/entity_test.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ import (
"testing"
"time"

"github.com/newrelic/infra-integrations-sdk/v4/data/inventory"
"github.com/newrelic/infra-integrations-sdk/v4/data/metric"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

@@ -212,3 +214,40 @@ func Test_Entity_EntitiesWithSameMetadataAreSameAs(t *testing.T) {
assert.True(t, e1.SameAs(e2))
assert.False(t, e1.SameAs(e3))
}

func TestEntity_AddCommonDimension(t *testing.T) {
tests := []struct {
name string
commons metric.Dimensions
expeceted *Entity
}{
{"empty", nil, newHostEntity()},
{"one entry", metric.Dimensions{"k": "v"}, &Entity{
CommonDimensions: metric.Dimensions{"k": "v"},
Metadata: nil,
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
}},
{"two entries", metric.Dimensions{"k1": "v1", "k2": "v2"}, &Entity{
CommonDimensions: metric.Dimensions{"k1": "v1", "k2": "v2"},
Metadata: nil,
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

got := newHostEntity()
for k, v := range tt.commons {
got.AddCommonDimension(k, v)
}

assert.Equal(t, tt.expeceted, got)
})
}
}

0 comments on commit 1601955

Please sign in to comment.