Skip to content

Commit 5411874

Browse files
authored
Merge pull request #286 from newrelic/common_dimension_block
fix: entity commondimension aligned with telemetry sdk
2 parents d4c5319 + 042cd68 commit 5411874

File tree

3 files changed

+144
-27
lines changed

3 files changed

+144
-27
lines changed

integration/entity.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package integration
33
import (
44
"errors"
55
"sync"
6+
"time"
67

78
"github.com/newrelic/infra-integrations-sdk/v4/data/event"
89
"github.com/newrelic/infra-integrations-sdk/v4/data/inventory"
@@ -12,14 +13,23 @@ import (
1213

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

24+
// Common is the producer of the common dimensions/attributes.
25+
type Common struct {
26+
Timestamp *int64 `json:"timestamp,omitempty"`
27+
Interval *int64 `json:"interval.ms,omitempty"`
28+
// Attributes are optional, they represent additional information that
29+
// can be attached to an event.
30+
Attributes map[string]interface{} `json:"attributes,omitempty"`
31+
}
32+
2333
// SameAs return true when is same entity
2434
func (e *Entity) SameAs(b *Entity) bool {
2535
if e.Metadata == nil || b.Metadata == nil {
@@ -60,7 +70,25 @@ func (e *Entity) AddCommonDimension(key string, value string) {
6070
e.lock.Lock()
6171
defer e.lock.Unlock()
6272

63-
e.CommonDimensions[key] = value
73+
e.CommonDimensions.Attributes[key] = value
74+
}
75+
76+
// AddCommonTimestamp adds a new common timestamp to the entity.
77+
func (e *Entity) AddCommonTimestamp(timestamp time.Time) {
78+
e.lock.Lock()
79+
defer e.lock.Unlock()
80+
81+
t := timestamp.Unix()
82+
e.CommonDimensions.Timestamp = &t
83+
}
84+
85+
// AddCommonInterval adds a common interval in milliseconds from a time.Duration (nanoseconds).
86+
func (e *Entity) AddCommonInterval(timestamp time.Duration) {
87+
e.lock.Lock()
88+
defer e.lock.Unlock()
89+
90+
t := timestamp.Milliseconds()
91+
e.CommonDimensions.Interval = &t
6492
}
6593

6694
// GetMetadata returns all the Entity's metadata
@@ -96,12 +124,14 @@ func (e *Entity) Name() string {
96124
// newHostEntity creates a entity without metadata.
97125
func newHostEntity() *Entity {
98126
return &Entity{
99-
CommonDimensions: make(metric.Dimensions),
100-
Metadata: nil,
101-
Metrics: metric.Metrics{},
102-
Inventory: inventory.New(),
103-
Events: event.Events{},
104-
lock: &sync.Mutex{},
127+
CommonDimensions: Common{
128+
Attributes: make(map[string]interface{}),
129+
},
130+
Metadata: nil,
131+
Metrics: metric.Metrics{},
132+
Inventory: inventory.New(),
133+
Events: event.Events{},
134+
lock: &sync.Mutex{},
105135
}
106136
}
107137

integration/entity_test.go

+85-16
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,35 @@ func Test_Entity_EntitiesWithSameMetadataAreSameAs(t *testing.T) {
217217

218218
func TestEntity_AddCommonDimension(t *testing.T) {
219219
tests := []struct {
220-
name string
221-
commons metric.Dimensions
222-
expeceted *Entity
220+
name string
221+
commons metric.Dimensions
222+
expected *Entity
223223
}{
224224
{"empty", nil, newHostEntity()},
225225
{"one entry", metric.Dimensions{"k": "v"}, &Entity{
226-
CommonDimensions: metric.Dimensions{"k": "v"},
227-
Metadata: nil,
228-
Metrics: metric.Metrics{},
229-
Inventory: inventory.New(),
230-
Events: event.Events{},
231-
lock: &sync.Mutex{},
226+
CommonDimensions: Common{
227+
Attributes: map[string]interface{}{
228+
"k": "v",
229+
},
230+
},
231+
Metadata: nil,
232+
Metrics: metric.Metrics{},
233+
Inventory: inventory.New(),
234+
Events: event.Events{},
235+
lock: &sync.Mutex{},
232236
}},
233237
{"two entries", metric.Dimensions{"k1": "v1", "k2": "v2"}, &Entity{
234-
CommonDimensions: metric.Dimensions{"k1": "v1", "k2": "v2"},
235-
Metadata: nil,
236-
Metrics: metric.Metrics{},
237-
Inventory: inventory.New(),
238-
Events: event.Events{},
239-
lock: &sync.Mutex{},
238+
CommonDimensions: Common{
239+
Attributes: map[string]interface{}{
240+
"k1": "v1",
241+
"k2": "v2",
242+
},
243+
},
244+
Metadata: nil,
245+
Metrics: metric.Metrics{},
246+
Inventory: inventory.New(),
247+
Events: event.Events{},
248+
lock: &sync.Mutex{},
240249
}},
241250
}
242251
for _, tt := range tests {
@@ -247,7 +256,67 @@ func TestEntity_AddCommonDimension(t *testing.T) {
247256
got.AddCommonDimension(k, v)
248257
}
249258

250-
assert.Equal(t, tt.expeceted, got)
259+
assert.Equal(t, tt.expected, got)
260+
})
261+
}
262+
}
263+
264+
func TestEntity_AddCommonTimestamp(t *testing.T) {
265+
asPtr := func(i int64) *int64 {
266+
return &i
267+
}
268+
tests := []struct {
269+
name string
270+
timestamp time.Time
271+
expected *Entity
272+
}{
273+
{"one entry", time.Unix(10000000, 0), &Entity{
274+
CommonDimensions: Common{
275+
Timestamp: asPtr(10000000),
276+
},
277+
Metadata: nil,
278+
Metrics: metric.Metrics{},
279+
Inventory: inventory.New(),
280+
Events: event.Events{},
281+
lock: &sync.Mutex{},
282+
}},
283+
}
284+
for _, tt := range tests {
285+
t.Run(tt.name, func(t *testing.T) {
286+
got := newHostEntity()
287+
got.AddCommonTimestamp(tt.timestamp)
288+
assert.NotNil(t, got.CommonDimensions.Timestamp)
289+
assert.Equal(t, *tt.expected.CommonDimensions.Timestamp, *got.CommonDimensions.Timestamp)
290+
})
291+
}
292+
}
293+
294+
func TestEntity_AddCommonInterval(t *testing.T) {
295+
asPtr := func(i int64) *int64 {
296+
return &i
297+
}
298+
tests := []struct {
299+
name string
300+
interval time.Duration
301+
expected *Entity
302+
}{
303+
{"one entry", time.Duration(100000000), &Entity{
304+
CommonDimensions: Common{
305+
Interval: asPtr(100),
306+
},
307+
Metadata: nil,
308+
Metrics: metric.Metrics{},
309+
Inventory: inventory.New(),
310+
Events: event.Events{},
311+
lock: &sync.Mutex{},
312+
}},
313+
}
314+
for _, tt := range tests {
315+
t.Run(tt.name, func(t *testing.T) {
316+
got := newHostEntity()
317+
got.AddCommonInterval(tt.interval)
318+
assert.NotNil(t, got.CommonDimensions.Interval)
319+
assert.Equal(t, *tt.expected.CommonDimensions.Interval, *got.CommonDimensions.Interval)
251320
})
252321
}
253322
}

integration/integration_test.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
223223
]
224224
},
225225
{
226-
"common": {},
226+
"common": {
227+
"attributes": {
228+
"targetName": "localhost"
229+
}
230+
},
227231
"entity": {
228232
"name": "EntityTwo",
229233
"displayName": "",
@@ -245,7 +249,14 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
245249
"events": []
246250
},
247251
{
248-
"common": {},
252+
"common": {
253+
"attributes": {
254+
"scrapedTargetKind": "user_provided",
255+
"scrapedTargetName": "localhost:9122",
256+
"scrapedTargetURL": "http://localhost:9122/metrics",
257+
"targetName": "localhost:9122"
258+
}
259+
},
249260
"entity": {
250261
"name": "EntityThree",
251262
"displayName": "",
@@ -393,6 +404,8 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
393404
// add entity 2
394405
e2, err := i.NewEntity("EntityTwo", "test", "")
395406
assert.NoError(t, err)
407+
// add common attributes
408+
e2.AddCommonDimension("targetName", "localhost")
396409
// add metric to entity 2
397410
gauge, _ = Gauge(time.Unix(10000000, 0), "metricOne", 2)
398411
_ = gauge.AddDimension("processName", "java")
@@ -423,7 +436,12 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
423436
// add entity 3
424437
e3, err := i.NewEntity("EntityThree", "test", "")
425438
assert.NoError(t, err)
426-
// add metric to entity 2
439+
// add common attributes
440+
e3.AddCommonDimension("scrapedTargetKind", "user_provided")
441+
e3.AddCommonDimension("scrapedTargetName", "localhost:9122")
442+
e3.AddCommonDimension("scrapedTargetURL", "http://localhost:9122/metrics")
443+
e3.AddCommonDimension("targetName", "localhost:9122")
444+
// add metric to entity 3
427445
summary2, _ := Summary(time.Unix(10000000, 0), "metric-summary-with-nan", 1, math.NaN(), 100, math.NaN(), math.NaN())
428446
e3.AddMetric(summary2)
429447
// add entity 3 to integration

0 commit comments

Comments
 (0)