Skip to content

Commit

Permalink
[receiver/mongodbatlas] Alerts poll mode check hostname and port (#16286
Browse files Browse the repository at this point in the history
)

* check hostname and port before converting polled logs
  • Loading branch information
schmikei committed Nov 14, 2022
1 parent 2c9a84d commit 04b8fab
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .chloggen/mongodbatlas-check-hostname.yaml
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: bug_fix

# 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: Checks host and port before assigning attributes in `poll` mode

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

# (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:
7 changes: 6 additions & 1 deletion receiver/mongodbatlasreceiver/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ func (a *alertsReceiver) convertAlerts(now pcommon.Timestamp, alerts []mongodbat
attrs.PutStr("metric.units", alert.CurrentValue.Units)
}

// Only present for HOST, HOST_METRIC, and REPLICA_SET alerts
if alert.HostnameAndPort == "" {
continue
}

host, portStr, err := net.SplitHostPort(alert.HostnameAndPort)
if err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to split host:port %s: %w", alert.HostnameAndPort, err))
Expand Down Expand Up @@ -565,7 +570,7 @@ func (a *alertsReceiver) applyFilters(pConf *ProjectConfig, alerts []mongodbatla
lastRecordedTime = *a.record.LastRecordedTime
}
// we need to maintain two timestamps in order to not conflict while iterating
var latestInPayload time.Time = pcommon.Timestamp(0).AsTime()
var latestInPayload = pcommon.Timestamp(0).AsTime()

for _, alert := range alerts {
updatedTime, err := time.Parse(time.RFC3339, alert.Updated)
Expand Down
65 changes: 65 additions & 0 deletions receiver/mongodbatlasreceiver/alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,75 @@ func TestAlertsRetrieval(t *testing.T) {
require.Equal(t, logs.LogRecordCount(), 1)
},
},
{
name: "hostname and port missing",
config: func() *Config {
return &Config{
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(typeStr),
Granularity: defaultGranularity,
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
Alerts: AlertConfig{
Mode: alertModePoll,
Projects: []*ProjectConfig{
{
Name: testProjectName,
},
},
PageSize: defaultAlertsPageSize,
MaxPages: defaultAlertsMaxPages,
PollInterval: 1 * time.Second,
},
}
},
client: func() alertsClient {
tc := &mockAlertsClient{}
tc.On("GetProject", mock.Anything, mock.Anything).Return(&mongodbatlas.Project{
ID: testProjectID,
OrgID: "test-org-id",
Name: testProjectName,
Links: []*mongodbatlas.Link{},
}, nil)
tc.On("GetAlerts", mock.Anything, testProjectID, mock.Anything).Return(
[]mongodbatlas.Alert{
{
ID: testAlertID,
GroupID: testGroupID,
AlertConfigID: "",
EventTypeName: testTypeName,
Created: time.Now().Format(time.RFC3339),
Updated: time.Now().Format(time.RFC3339),
Enabled: new(bool),
Status: "TRACKING",
MetricName: testMetricName,
CurrentValue: &mongodbatlas.CurrentValue{
Number: new(float64),
Units: "By",
},
ClusterName: testClusterName,
HostnameAndPort: "",
Matchers: []mongodbatlas.Matcher{},
MetricThreshold: &mongodbatlas.MetricThreshold{},
Notifications: []mongodbatlas.Notification{},
},
}, false, nil)
return tc
},
validateEntries: func(t *testing.T, l plog.Logs) {
require.Equal(t, l.LogRecordCount(), 1)
rl := l.ResourceLogs().At(0)
sl := rl.ScopeLogs().At(0)
lr := sl.LogRecords().At(0)
_, hasHostname := lr.Attributes().Get("net.peer.name")
require.False(t, hasHostname)
_, hasPort := lr.Attributes().Get("net.peer.port")
require.False(t, hasPort)
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
logSink := &consumertest.LogsSink{}
alertsRcvr, err := newAlertsReceiver(zap.NewNop(), tc.config(), logSink)
require.NoError(t, err)
Expand Down

0 comments on commit 04b8fab

Please sign in to comment.