Skip to content

Commit defe0d3

Browse files
authored
Reverting changes that added details to inventory (#2020)
1 parent 3e8acbb commit defe0d3

File tree

7 files changed

+31
-351
lines changed

7 files changed

+31
-351
lines changed

pkg/integrations/legacy/runner.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,9 @@ func EmitDataSet(
707707
if err != nil {
708708
return fmt.Errorf("couldn't determine a unique entity Key: %s", err.Error())
709709
}
710-
// Custom attributes are from infra agent config
711-
customAttr := ctx.Config().CustomAttributes.DataMap()
712710

713711
if len(dataSet.Inventory) > 0 {
714-
inventoryDataSet := BuildInventoryDataSet(elog, dataSet.Inventory, labels, customAttr, integrationUser, pluginName, pluginVersion, agentIdentifier, entityKey.String())
712+
inventoryDataSet := BuildInventoryDataSet(elog, dataSet.Inventory, labels, integrationUser, pluginName, entityKey.String())
715713
emitter.EmitInventory(inventoryDataSet, entity.NewWithoutID(entityKey))
716714
}
717715

pkg/integrations/legacy/runner_test.go

+16-85
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func newFakePlugin(ctx customContext, pluginVersion int) externalPlugin {
509509
Context: ctx,
510510
},
511511
pluginInstance: &PluginV1Instance{
512-
Labels: map[string]string{"role": "fileserver", "environment": "development", "agent_role": "overwrite agent role"},
512+
Labels: map[string]string{"role": "fileserver", "environment": "development"},
513513
plugin: &Plugin{
514514
Name: "new-plugin",
515515
ProtocolVersion: pluginVersion,
@@ -573,7 +573,7 @@ func newFakePluginWithEnvVars(pluginVersion int) externalPlugin {
573573
Context: ctx,
574574
},
575575
pluginInstance: &PluginV1Instance{
576-
Labels: map[string]string{"role": "fileserver", "environment": "development", "agent_role": "overwrite agent role"},
576+
Labels: map[string]string{"role": "fileserver", "environment": "development"},
577577
plugin: &Plugin{
578578
Name: "new-plugin",
579579
ProtocolVersion: pluginVersion,
@@ -676,36 +676,22 @@ func (rs *RunnerSuite) TestPluginHandleOutputV1(c *C) {
676676
c.Assert(err, IsNil)
677677

678678
c.Assert(rd, NotNil)
679+
c.Assert(len(rd.Data), Equals, 4)
680+
c.Assert(rd.Data[0].SortKey(), Equals, "first")
681+
682+
invData := rd.Data[3].(protocol.InventoryData)
683+
c.Assert(invData["id"], Equals, "integrationUser")
684+
c.Assert(invData["value"], Equals, "test")
679685

680686
c.Assert(event, NotNil)
681687
c.Assert(event["event_type"], Equals, "LoadBalancerSample")
682688
c.Assert(event["id"], Equals, "first")
683689
c.Assert(event["value"], Equals, "random")
684690
c.Assert(event["integrationUser"], Equals, "test")
685691

686-
expectedLabelValues := map[string]string{
687-
"first": "fake",
688-
"labels/my_group": "test group",
689-
"labels/role": "fileserver",
690-
"labels/environment": "development",
691-
"labels/agent_role": "overwrite agent role",
692-
"integrationUser": "test",
693-
"integrationName": "test",
694-
"integrationVersion": "1.0.0",
695-
}
696-
697-
c.Assert(len(rd.Data), Equals, 8)
698-
699-
for _, item := range rd.Data {
700-
if invData, ok := item.(protocol.InventoryData); ok {
701-
id, _ := invData["id"].(string)
702-
value, _ := invData["value"].(string)
703-
704-
if expectedValue, exists := expectedLabelValues[id]; exists {
705-
c.Assert(value, Equals, expectedValue)
706-
} else {
707-
c.Fatalf("Expected label: %v not found in Inventory", id)
708-
}
692+
for _, labelKey := range labelKeys {
693+
if rd.Data[1].SortKey() != labelKey && rd.Data[2].SortKey() != labelKey {
694+
c.Errorf("There isn't label '%s'' in the inventory", labelKey)
709695
}
710696
}
711697
}
@@ -740,7 +726,6 @@ func (rs *RunnerSuite) TestPluginHandleOutputEventsV1(c *C) {
740726
// labels from pluginInstance
741727
c.Assert(event["label.environment"], Equals, "development")
742728
c.Assert(event["label.role"], Equals, "fileserver")
743-
c.Assert(event["label.agent_role"], Equals, "overwrite agent role")
744729

745730
// labels from databind
746731
c.Assert(event["label.expected"], Equals, "extra label")
@@ -846,7 +831,7 @@ func (rs *RunnerSuite) TestEventsPluginRunV1(c *C) {
846831
c.Assert(err, IsNil)
847832

848833
c.Assert(rd, NotNil)
849-
c.Assert(len(rd.Data), Equals, 7)
834+
c.Assert(len(rd.Data), Equals, 3)
850835
c.Assert(rd.Data[0].SortKey(), Equals, "first")
851836

852837
c.Assert(event, NotNil)
@@ -1045,7 +1030,7 @@ func (rs *RunnerSuite) TestHandleOutputV1(c *C) {
10451030
c.Assert(err, IsNil)
10461031

10471032
// labels are added as inventory
1048-
c.Assert(len(rd.Data)-len(labelKeys), Equals, 7)
1033+
c.Assert(len(rd.Data)-len(labelKeys), Equals, 3)
10491034

10501035
firstData := rd.Data[0]
10511036
inv := firstData.(protocol.InventoryData)
@@ -1560,16 +1545,12 @@ func TestParsePayloadV3(t *testing.T) {
15601545
type fakeEmitter struct {
15611546
lastEventData map[string]interface{}
15621547
lastEntityKey string
1563-
inventory types.PluginInventoryDataset
15641548
}
15651549

15661550
func (f *fakeEmitter) EmitInventoryWithPluginId(data types.PluginInventoryDataset, entityKey string, pluginId ids.PluginID) {
1567-
f.inventory = data
15681551
}
15691552

1570-
func (f *fakeEmitter) EmitInventory(data types.PluginInventoryDataset, entity entity.Entity) {
1571-
f.inventory = data
1572-
}
1553+
func (f *fakeEmitter) EmitInventory(data types.PluginInventoryDataset, entity entity.Entity) {}
15731554

15741555
func (f *fakeEmitter) EmitEvent(eventData map[string]interface{}, entityKey entity.Key) {
15751556
f.lastEventData = eventData
@@ -1605,29 +1586,6 @@ func TestEmitPayloadV2NoDisplayNameNoEntityName(t *testing.T) {
16051586
assert.EqualValues(t, "Motorbike", emitter.lastEventData["entityName"])
16061587
assert.EqualValues(t, "motorbike:street_hawk", emitter.lastEventData["entityKey"])
16071588

1608-
expectedLabelValues := map[string]string{
1609-
"integrationUser": "testuser",
1610-
"integrationName": "test/test",
1611-
"integrationVersion": "x.y.z",
1612-
"reportingAgent": "my-agent-id",
1613-
"motor": "", // we are type converting to string so motor does not have a value
1614-
}
1615-
1616-
assert.EqualValues(t, len(emitter.inventory), 5)
1617-
1618-
for _, item := range emitter.inventory {
1619-
if invData, ok := item.(protocol.InventoryData); ok {
1620-
id, _ := invData["id"].(string)
1621-
value, _ := invData["value"].(string)
1622-
1623-
if expectedValue, exists := expectedLabelValues[id]; exists {
1624-
assert.EqualValues(t, value, expectedValue)
1625-
} else {
1626-
assert.Fail(t, "Expected label not found in Inventory", "Label: %s not found in inventory", id)
1627-
}
1628-
}
1629-
}
1630-
16311589
// Local entity, no displayName, no entityName
16321590
assert.NoError(t, EmitDataSet(ctx, &emitter, "test/test", "x.y.z", "testuser", rd.DataSets[2], extraAnnotations, labels, entityRewrite, version))
16331591
_, ok := emitter.lastEventData["displayName"]
@@ -1636,27 +1594,6 @@ func TestEmitPayloadV2NoDisplayNameNoEntityName(t *testing.T) {
16361594
assert.False(t, ok)
16371595
// but entityKey is the agent key
16381596
assert.EqualValues(t, "my-agent-id", emitter.lastEventData["entityKey"])
1639-
1640-
// Check inventory data
1641-
assert.EqualValues(t, len(emitter.inventory), 5)
1642-
1643-
for _, item := range emitter.inventory {
1644-
if invData, ok := item.(protocol.InventoryData); ok {
1645-
id, _ := invData["id"].(string)
1646-
value, _ := invData["value"].(string)
1647-
1648-
if expectedValue, exists := expectedLabelValues[id]; exists {
1649-
assert.EqualValues(t, value, expectedValue)
1650-
} else {
1651-
assert.Fail(t, "Expected label not found in Inventory", "Label: %s not found in inventory", id)
1652-
}
1653-
}
1654-
}
1655-
}
1656-
1657-
func createMockConfigWithDataMap(attrs map[string]interface{}) *config.Config {
1658-
customAttrs := config.CustomAttributeMap(attrs)
1659-
return &config.Config{CustomAttributes: customAttrs} //nolint:all
16601597
}
16611598

16621599
func TestEmitDataSet_OnAddHostnameDecoratesWithHostname(t *testing.T) {
@@ -1689,7 +1626,6 @@ func TestEmitDataSet_OnAddHostnameDecoratesWithHostname(t *testing.T) {
16891626
ctx.On("EntityKey").Return(agentIdentifier)
16901627
ctx.On("HostnameResolver").Return(newFixedHostnameResolver(hn, "short"))
16911628
ctx.On("IDLookup").Return(newFixedIDLookup())
1692-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
16931629

16941630
em := &fakeEmitter{}
16951631
extraAnnotations := map[string]string{}
@@ -1741,7 +1677,6 @@ func TestEmitDataSet_EntityNameLocalhostIsNotReplacedWithHostnameV2(t *testing.T
17411677
ctx.On("EntityKey").Return(agID)
17421678
ctx.On("HostnameResolver").Return(newFixedHostnameResolver("foo.bar", "short"))
17431679
ctx.On("IDLookup").Return(newFixedIDLookup())
1744-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
17451680

17461681
em := &fakeEmitter{}
17471682
extraAnnotations := map[string]string{}
@@ -1789,7 +1724,6 @@ func TestEmitDataSet_EntityNameLocalhostIsReplacedWithHostnameV3(t *testing.T) {
17891724
ctx.On("EntityKey").Return(agID)
17901725
ctx.On("HostnameResolver").Return(newFixedHostnameResolver("foo.bar", "short"))
17911726
ctx.On("IDLookup").Return(newFixedIDLookup())
1792-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
17931727

17941728
em := &fakeEmitter{}
17951729
extraAnnotations := map[string]string{}
@@ -1838,7 +1772,6 @@ func TestEmitDataSet_MetricHostnameIsReplacedIfLocalhostV3(t *testing.T) {
18381772
ctx.On("EntityKey").Return(agID)
18391773
ctx.On("HostnameResolver").Return(newFixedHostnameResolver("foo.bar", "short"))
18401774
ctx.On("IDLookup").Return(newFixedIDLookup())
1841-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
18421775

18431776
em := &fakeEmitter{}
18441777
extraAnnotations := map[string]string{}
@@ -1888,7 +1821,6 @@ func TestEmitDataSet_ReportingFieldsAreReplacedIfLocalhostV3(t *testing.T) {
18881821
ctx.On("EntityKey").Return(agID)
18891822
ctx.On("HostnameResolver").Return(newFixedHostnameResolver("foo.bar", "short"))
18901823
ctx.On("IDLookup").Return(newFixedIDLookup())
1891-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
18921824

18931825
em := &fakeEmitter{}
18941826
extraAnnotations := map[string]string{}
@@ -1937,7 +1869,6 @@ func TestEmitDataSet_LogsEntityViolationsOncePerEntity(t *testing.T) {
19371869
ctx.On("EntityKey").Return(agID)
19381870
ctx.On("HostnameResolver").Return(newFixedHostnameResolver("foo.bar", "short"))
19391871
ctx.On("IDLookup").Return(newFixedIDLookup())
1940-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
19411872

19421873
em := &fakeEmitter{}
19431874
extraAnnotations := map[string]string{}
@@ -1975,7 +1906,6 @@ func TestEmitDataSet_DoNotOverrideExistingMetrics(t *testing.T) {
19751906
ctx.On("EntityKey").Return("agent-id")
19761907
ctx.On("HostnameResolver").Return(newFixedHostnameResolver("long", "short"))
19771908
ctx.On("IDLookup").Return(newFixedIDLookup())
1978-
ctx.On("Config").Return(createMockConfigWithDataMap(make(map[string]interface{})))
19791909
em := &fakeEmitter{}
19801910
extraAnnotations := map[string]string{
19811911
"cluster_name": "K8sDiscoveredCluster",
@@ -2277,7 +2207,8 @@ func TestLogFields(t *testing.T) {
22772207
"TEMP_DIR": "a/path",
22782208
})
22792209
assert.Equal(t, fields["labels"], map[string]string{
2280-
"agent_role": "overwrite agent role", "environment": "development", "role": "fileserver",
2210+
"role": "fileserver",
2211+
"environment": "development",
22812212
})
22822213

22832214
if runtime.GOOS == "windows" {

pkg/integrations/legacy/utils.go

+7-36
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ import (
1313
"github.com/sirupsen/logrus"
1414
)
1515

16-
const (
17-
integrationUserID = "integrationUser"
18-
integrationNameID = "integrationName"
19-
integrationVersionID = "integrationVersion"
20-
reportingAgentID = "reportingAgent"
21-
)
22-
2316
func BuildInventoryDataSet(
2417
entryLog log.Entry,
2518
inventoryData map[string]protocol.InventoryData,
2619
labels map[string]string,
27-
customAttr map[string]string,
2820
integrationUser string,
2921
pluginName string,
30-
pluginVersion string,
31-
reportingAgent string,
3222
entityKey string) types.PluginInventoryDataset {
3323
var inventoryDataSet types.PluginInventoryDataset
3424

@@ -42,39 +32,20 @@ func BuildInventoryDataSet(
4232
}
4333
}
4434

45-
addLabeledData := func(id string, value string) {
35+
for key, value := range labels {
4636
inventoryDataSet = append(inventoryDataSet, protocol.InventoryData{
47-
"id": id,
37+
"id": fmt.Sprintf("labels/%s", key),
4838
"value": value,
4939
"entityKey": entityKey,
5040
})
5141
}
5242

53-
for key, value := range customAttr {
54-
// Do not set in the case of duplicate key
55-
if _, exists := labels[key]; !exists {
56-
addLabeledData(fmt.Sprintf("labels/%s", key), value)
57-
}
58-
}
59-
60-
for key, value := range labels {
61-
addLabeledData(fmt.Sprintf("labels/%s", key), value)
62-
}
63-
6443
if integrationUser != "" {
65-
addLabeledData(integrationUserID, integrationUser)
66-
}
67-
68-
if pluginName != "" {
69-
addLabeledData(integrationNameID, pluginName)
70-
}
71-
72-
if pluginVersion != "" {
73-
addLabeledData(integrationVersionID, pluginVersion)
74-
}
75-
76-
if reportingAgent != "" {
77-
addLabeledData(reportingAgentID, reportingAgent)
44+
inventoryDataSet = append(inventoryDataSet, protocol.InventoryData{
45+
"id": "integrationUser",
46+
"value": integrationUser,
47+
"entityKey": entityKey,
48+
})
7849
}
7950

8051
return inventoryDataSet

0 commit comments

Comments
 (0)