Skip to content

Commit

Permalink
[receiver/mongodbatlas] Add more resource attributes to polled alerts (
Browse files Browse the repository at this point in the history
…#17518)

add mongodbatlas.org.id and mongodbatls.project.name to polled alert resource attrs
  • Loading branch information
schmikei committed Jan 11, 2023
1 parent 5839afd commit 8418092
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: mongodbatlasreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds `mongodbatlas.project.name` and `mongodbatlas.org.id` as polled alerts resource attributes

# One or more tracking issues related to the change
issues: [17513]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
6 changes: 4 additions & 2 deletions receiver/mongodbatlasreceiver/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (a *alertsReceiver) pollAndProcess(ctx context.Context, pc *ProjectConfig,

filteredAlerts := a.applyFilters(pc, projectAlerts)
now := pcommon.NewTimestampFromTime(time.Now())
logs, err := a.convertAlerts(now, filteredAlerts)
logs, err := a.convertAlerts(now, filteredAlerts, project)
if err != nil {
a.logger.Error("error processing alerts", zap.Error(err))
break
Expand Down Expand Up @@ -356,14 +356,16 @@ func (a *alertsReceiver) shutdownPoller(ctx context.Context) error {
return a.writeCheckpoint(ctx)
}

func (a *alertsReceiver) convertAlerts(now pcommon.Timestamp, alerts []mongodbatlas.Alert) (plog.Logs, error) {
func (a *alertsReceiver) convertAlerts(now pcommon.Timestamp, alerts []mongodbatlas.Alert, project *mongodbatlas.Project) (plog.Logs, error) {
logs := plog.NewLogs()
var errs error
for _, alert := range alerts {
resourceLogs := logs.ResourceLogs().AppendEmpty()
resourceAttrs := resourceLogs.Resource().Attributes()
resourceAttrs.PutStr("mongodbatlas.group.id", alert.GroupID)
resourceAttrs.PutStr("mongodbatlas.alert.config.id", alert.AlertConfigID)
resourceAttrs.PutStr("mongodbatlas.org.id", project.OrgID)
resourceAttrs.PutStr("mongodbatlas.project.name", project.Name)
putStringToMapNotNil(resourceAttrs, "mongodbatlas.cluster.name", &alert.ClusterName)
putStringToMapNotNil(resourceAttrs, "mongodbatlas.replica_set.name", &alert.ReplicaSetName)

Expand Down
10 changes: 6 additions & 4 deletions receiver/mongodbatlasreceiver/alerts_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestAlertsReceiver(t *testing.T) {
expectedLogs, err := readLogs(filepath.Join("testdata", "alerts", "golden", payloadName))
require.NoError(t, err)

require.NoError(t, comparetest.CompareLogs(expectedLogs, logs))
require.NoError(t, comparetest.CompareLogs(expectedLogs, logs, comparetest.IgnoreObservedTimestamp()))
})
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestAlertsReceiverTLS(t *testing.T) {
expectedLogs, err := readLogs(filepath.Join("testdata", "alerts", "golden", payloadName))
require.NoError(t, err)

require.NoError(t, comparetest.CompareLogs(expectedLogs, logs))
require.NoError(t, comparetest.CompareLogs(expectedLogs, logs, comparetest.IgnoreObservedTimestamp()))
})
}
}
Expand All @@ -209,7 +209,9 @@ func TestAtlasPoll(t *testing.T) {
}

mockClient.On("GetProject", mock.Anything, testProjectName).Return(&mongodbatlas.Project{
ID: testProjectID,
ID: testProjectID,
Name: testProjectName,
OrgID: testOrgID,
}, nil)
mockClient.On("GetAlerts", mock.Anything, testProjectID, mock.Anything).Return(alerts, false, nil)

Expand Down Expand Up @@ -254,7 +256,7 @@ func TestAtlasPoll(t *testing.T) {
logs := sink.AllLogs()[0]
expectedLogs, err := readLogs(filepath.Join("testdata", "alerts", "golden", "retrieved-logs.json"))
require.NoError(t, err)
require.NoError(t, comparetest.CompareLogs(expectedLogs, logs))
require.NoError(t, comparetest.CompareLogs(expectedLogs, logs, comparetest.IgnoreObservedTimestamp()))
}

func calculateHMACb64(secret string, payload []byte) (string, error) {
Expand Down
18 changes: 16 additions & 2 deletions receiver/mongodbatlasreceiver/alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ func TestHandleRequest(t *testing.T) {
const (
testAlertID = "633335c99998645b1803c60b"
testGroupID = "5bc762b579358e3332046e6a"
testAlertConfigID = "REDACTED-alert"
testOrgID = "test-org-id"
testProjectID = "test-project-id"
testProjectName = "test-project"
testMetricName = "metric-name"
Expand Down Expand Up @@ -482,6 +484,18 @@ func TestAlertsRetrieval(t *testing.T) {
"type_name": testTypeName,
}
validateAttributes(t, expectedStringAttributes, logs)
expectedResourceAttributes := map[string]string{
"mongodbatlas.group.id": testGroupID,
"mongodbatlas.alert.config.id": testAlertConfigID,
"mongodbatlas.project.name": testProjectName,
"mongodbatlas.org.id": testOrgID,
}
ra := logs.ResourceLogs().At(0).Resource().Attributes()
for k, v := range expectedResourceAttributes {
value, ok := ra.Get(k)
require.True(t, ok)
require.Equal(t, v, value)
}
},
},
{
Expand Down Expand Up @@ -536,7 +550,7 @@ func TestAlertsRetrieval(t *testing.T) {
tc := &mockAlertsClient{}
tc.On("GetProject", mock.Anything, mock.Anything).Return(&mongodbatlas.Project{
ID: testProjectID,
OrgID: "test-org-id",
OrgID: testOrgID,
Name: testProjectName,
Links: []*mongodbatlas.Link{},
}, nil)
Expand Down Expand Up @@ -635,7 +649,7 @@ func testClient() *mockAlertsClient {
ac := &mockAlertsClient{}
ac.On("GetProject", mock.Anything, mock.Anything).Return(&mongodbatlas.Project{
ID: testProjectID,
OrgID: "test-org-id",
OrgID: testOrgID,
Name: testProjectName,
Links: []*mongodbatlas.Link{},
}, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
"stringValue": "Cluster0"
}
},
{
"key": "mongodbatlas.org.id",
"value": {
"stringValue": "test-org-id"
}
},
{
"key": "mongodbatlas.project.name",
"value": {
"stringValue": "test-project"
}
},
{
"key": "mongodbatlas.replica_set.name",
"value": {
Expand Down Expand Up @@ -173,6 +185,18 @@
"stringValue": "Cluster0"
}
},
{
"key": "mongodbatlas.org.id",
"value": {
"stringValue": "test-org-id"
}
},
{
"key": "mongodbatlas.project.name",
"value": {
"stringValue": "test-project"
}
},
{
"key": "mongodbatlas.replica_set.name",
"value": {
Expand Down

0 comments on commit 8418092

Please sign in to comment.