Skip to content

Commit

Permalink
Merge pull request #286 from newrelic/common_dimension_block
Browse files Browse the repository at this point in the history
fix: entity commondimension aligned with telemetry sdk
  • Loading branch information
rogercoll committed Jan 31, 2022
2 parents d4c5319 + 042cd68 commit 5411874
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 27 deletions.
46 changes: 38 additions & 8 deletions integration/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"errors"
"sync"
"time"

"github.com/newrelic/infra-integrations-sdk/v4/data/event"
"github.com/newrelic/infra-integrations-sdk/v4/data/inventory"
Expand All @@ -12,14 +13,23 @@ 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 {
CommonDimensions map[string]string `json:"common"` // dimensions common to every entity metric
CommonDimensions Common `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 the producer of the common dimensions/attributes.
type Common struct {
Timestamp *int64 `json:"timestamp,omitempty"`
Interval *int64 `json:"interval.ms,omitempty"`
// Attributes are optional, they represent additional information that
// can be attached to an event.
Attributes map[string]interface{} `json:"attributes,omitempty"`
}

// SameAs return true when is same entity
func (e *Entity) SameAs(b *Entity) bool {
if e.Metadata == nil || b.Metadata == nil {
Expand Down Expand Up @@ -60,7 +70,25 @@ func (e *Entity) AddCommonDimension(key string, value string) {
e.lock.Lock()
defer e.lock.Unlock()

e.CommonDimensions[key] = value
e.CommonDimensions.Attributes[key] = value
}

// AddCommonTimestamp adds a new common timestamp to the entity.
func (e *Entity) AddCommonTimestamp(timestamp time.Time) {
e.lock.Lock()
defer e.lock.Unlock()

t := timestamp.Unix()
e.CommonDimensions.Timestamp = &t
}

// AddCommonInterval adds a common interval in milliseconds from a time.Duration (nanoseconds).
func (e *Entity) AddCommonInterval(timestamp time.Duration) {
e.lock.Lock()
defer e.lock.Unlock()

t := timestamp.Milliseconds()
e.CommonDimensions.Interval = &t
}

// GetMetadata returns all the Entity's metadata
Expand Down Expand Up @@ -96,12 +124,14 @@ func (e *Entity) Name() string {
// newHostEntity creates a entity without metadata.
func newHostEntity() *Entity {
return &Entity{
CommonDimensions: make(metric.Dimensions),
Metadata: nil,
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
CommonDimensions: Common{
Attributes: make(map[string]interface{}),
},
Metadata: nil,
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
}
}

Expand Down
101 changes: 85 additions & 16 deletions integration/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,26 +217,35 @@ func Test_Entity_EntitiesWithSameMetadataAreSameAs(t *testing.T) {

func TestEntity_AddCommonDimension(t *testing.T) {
tests := []struct {
name string
commons metric.Dimensions
expeceted *Entity
name string
commons metric.Dimensions
expected *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{},
CommonDimensions: Common{
Attributes: map[string]interface{}{
"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{},
CommonDimensions: Common{
Attributes: map[string]interface{}{
"k1": "v1",
"k2": "v2",
},
},
Metadata: nil,
Metrics: metric.Metrics{},
Inventory: inventory.New(),
Events: event.Events{},
lock: &sync.Mutex{},
}},
}
for _, tt := range tests {
Expand All @@ -247,7 +256,67 @@ func TestEntity_AddCommonDimension(t *testing.T) {
got.AddCommonDimension(k, v)
}

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

func TestEntity_AddCommonTimestamp(t *testing.T) {
asPtr := func(i int64) *int64 {
return &i
}
tests := []struct {
name string
timestamp time.Time
expected *Entity
}{
{"one entry", time.Unix(10000000, 0), &Entity{
CommonDimensions: Common{
Timestamp: asPtr(10000000),
},
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()
got.AddCommonTimestamp(tt.timestamp)
assert.NotNil(t, got.CommonDimensions.Timestamp)
assert.Equal(t, *tt.expected.CommonDimensions.Timestamp, *got.CommonDimensions.Timestamp)
})
}
}

func TestEntity_AddCommonInterval(t *testing.T) {
asPtr := func(i int64) *int64 {
return &i
}
tests := []struct {
name string
interval time.Duration
expected *Entity
}{
{"one entry", time.Duration(100000000), &Entity{
CommonDimensions: Common{
Interval: asPtr(100),
},
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()
got.AddCommonInterval(tt.interval)
assert.NotNil(t, got.CommonDimensions.Interval)
assert.Equal(t, *tt.expected.CommonDimensions.Interval, *got.CommonDimensions.Interval)
})
}
}
24 changes: 21 additions & 3 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
]
},
{
"common": {},
"common": {
"attributes": {
"targetName": "localhost"
}
},
"entity": {
"name": "EntityTwo",
"displayName": "",
Expand All @@ -245,7 +249,14 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
"events": []
},
{
"common": {},
"common": {
"attributes": {
"scrapedTargetKind": "user_provided",
"scrapedTargetName": "localhost:9122",
"scrapedTargetURL": "http://localhost:9122/metrics",
"targetName": "localhost:9122"
}
},
"entity": {
"name": "EntityThree",
"displayName": "",
Expand Down Expand Up @@ -393,6 +404,8 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
// add entity 2
e2, err := i.NewEntity("EntityTwo", "test", "")
assert.NoError(t, err)
// add common attributes
e2.AddCommonDimension("targetName", "localhost")
// add metric to entity 2
gauge, _ = Gauge(time.Unix(10000000, 0), "metricOne", 2)
_ = gauge.AddDimension("processName", "java")
Expand Down Expand Up @@ -423,7 +436,12 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
// add entity 3
e3, err := i.NewEntity("EntityThree", "test", "")
assert.NoError(t, err)
// add metric to entity 2
// add common attributes
e3.AddCommonDimension("scrapedTargetKind", "user_provided")
e3.AddCommonDimension("scrapedTargetName", "localhost:9122")
e3.AddCommonDimension("scrapedTargetURL", "http://localhost:9122/metrics")
e3.AddCommonDimension("targetName", "localhost:9122")
// add metric to entity 3
summary2, _ := Summary(time.Unix(10000000, 0), "metric-summary-with-nan", 1, math.NaN(), 100, math.NaN(), math.NaN())
e3.AddMetric(summary2)
// add entity 3 to integration
Expand Down

0 comments on commit 5411874

Please sign in to comment.