Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/mongodbatlasreceiver] add provider resource attributes #28835

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add provider & region resource attributes to logs
  • Loading branch information
JonathanWamsley committed Nov 2, 2023
commit f46a916412298a677a6a9588520dd747de914453
2 changes: 2 additions & 0 deletions receiver/mongodbatlasreceiver/access_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ func transformAccessLogs(now pcommon.Timestamp, accessLogs []*mongodbatlas.Acces
ra := resourceLogs.Resource().Attributes()
ra.PutStr("mongodbatlas.project.name", p.Name)
ra.PutStr("mongodbatlas.project.id", p.ID)
ra.PutStr("mongodbatlas.region.name", c.ProviderSettings.RegionName)
ra.PutStr("mongodbatlas.provider.name", c.ProviderSettings.ProviderName)
ra.PutStr("mongodbatlas.org.id", p.OrgID)
ra.PutStr("mongodbatlas.cluster.name", c.Name)

Expand Down
28 changes: 20 additions & 8 deletions receiver/mongodbatlasreceiver/access_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func TestAccessLogToLogRecord(t *testing.T) {
cluster := mongodbatlas.Cluster{
GroupID: testProjectID,
Name: testClusterName,
ProviderSettings: &mongodbatlas.ProviderSettings{
ProviderName: testProviderName,
RegionName: testRegionName,
},
}

inputLogs := []*mongodbatlas.AccessLogs{
Expand Down Expand Up @@ -79,10 +83,12 @@ func TestAccessLogToLogRecord(t *testing.T) {
rl := expectedLogs.ResourceLogs().AppendEmpty()

assert.NoError(t, rl.Resource().Attributes().FromRaw(map[string]interface{}{
"mongodbatlas.project.name": testProjectName,
"mongodbatlas.project.id": testProjectID,
"mongodbatlas.org.id": testOrgID,
"mongodbatlas.cluster.name": testClusterName,
"mongodbatlas.project.name": testProjectName,
"mongodbatlas.project.id": testProjectID,
"mongodbatlas.org.id": testOrgID,
"mongodbatlas.cluster.name": testClusterName,
"mongodbatlas.region.name": testRegionName,
"mongodbatlas.provider.name": testProviderName,
}))

records := rl.ScopeLogs().AppendEmpty().LogRecords()
Expand Down Expand Up @@ -180,10 +186,12 @@ func TestAccessLogsRetrieval(t *testing.T) {
}
validateAttributes(t, expectedStringAttributes, l)
expectedResourceAttributes := map[string]string{
"mongodbatlas.cluster.name": testClusterName,
"mongodbatlas.project.name": testProjectName,
"mongodbatlas.project.id": testProjectID,
"mongodbatlas.org.id": testOrgID,
"mongodbatlas.cluster.name": testClusterName,
"mongodbatlas.project.name": testProjectName,
"mongodbatlas.project.id": testProjectID,
"mongodbatlas.org.id": testOrgID,
"mongodbatlas.region.name": testRegionName,
"mongodbatlas.provider.name": testProviderName,
}

ra := l.ResourceLogs().At(0).Resource().Attributes()
Expand Down Expand Up @@ -335,6 +343,10 @@ func testClientBase() *mockAccessLogsClient {
{
GroupID: testProjectID,
Name: testClusterName,
ProviderSettings: &mongodbatlas.ProviderSettings{
ProviderName: testProviderName,
RegionName: testRegionName,
},
},
},
nil)
Expand Down
2 changes: 2 additions & 0 deletions receiver/mongodbatlasreceiver/alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ const (
testTypeName = "OUTSIDE_METRIC_THRESHOLD"
testHostNameAndPort = "127.0.0.1:27017"
testClusterName = "Cluster1"
testRegionName = "region-name"
testProviderName = "provider-name"
)

func TestAlertsRetrieval(t *testing.T) {
Expand Down
34 changes: 23 additions & 11 deletions receiver/mongodbatlasreceiver/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,34 @@ func (s *logsReceiver) processClusters(ctx context.Context, projectCfg LogsProje
return filterClusters(clusters, projectCfg.ProjectConfig)
}

type ClusterInfo struct {
ClusterName string
RegionName string
ProviderName string
MongoDBMajorVersion string
}

func (s *logsReceiver) collectClusterLogs(clusters []mongodbatlas.Cluster, projectCfg LogsProjectConfig, pc ProjectContext) {
for _, cluster := range clusters {
clusterInfo := ClusterInfo{
ClusterName: cluster.Name,
RegionName: cluster.ProviderSettings.RegionName,
ProviderName: cluster.ProviderSettings.ProviderName,
MongoDBMajorVersion: cluster.MongoDBMajorVersion,
}

hostnames := parseHostNames(cluster.ConnectionStrings.Standard, s.log)
for _, hostname := range hostnames {
// Defaults to true if not specified
if projectCfg.EnableHostLogs == nil || *projectCfg.EnableHostLogs {
s.collectLogs(pc, hostname, "mongodb.gz", cluster.Name, cluster.MongoDBMajorVersion)
s.collectLogs(pc, hostname, "mongos.gz", cluster.Name, cluster.MongoDBMajorVersion)
s.collectLogs(pc, hostname, "mongodb.gz", clusterInfo)
s.collectLogs(pc, hostname, "mongos.gz", clusterInfo)
}

// Defaults to false if not specified
if projectCfg.EnableAuditLogs {
s.collectAuditLogs(pc, hostname, "mongodb-audit-log.gz", cluster.Name, cluster.MongoDBMajorVersion)
s.collectAuditLogs(pc, hostname, "mongos-audit-log.gz", cluster.Name, cluster.MongoDBMajorVersion)
s.collectAuditLogs(pc, hostname, "mongodb-audit-log.gz", clusterInfo)
s.collectAuditLogs(pc, hostname, "mongos-audit-log.gz", clusterInfo)
}
}
}
Expand Down Expand Up @@ -218,8 +232,8 @@ func (s *logsReceiver) getHostAuditLogs(groupID, hostname, logName string) ([]mo
return decodeAuditJSON(s.log, buf)
}

func (s *logsReceiver) collectLogs(pc ProjectContext, hostname, logName, clusterName, clusterMajorVersion string) {
logs, err := s.getHostLogs(pc.Project.ID, hostname, logName, clusterMajorVersion)
func (s *logsReceiver) collectLogs(pc ProjectContext, hostname, logName string, clusterInfo ClusterInfo) {
logs, err := s.getHostLogs(pc.Project.ID, hostname, logName, clusterInfo.MongoDBMajorVersion)
if err != nil && !errors.Is(err, io.EOF) {
s.log.Warn("Failed to retrieve host logs", zap.Error(err), zap.String("log", logName))
return
Expand All @@ -235,15 +249,14 @@ func (s *logsReceiver) collectLogs(pc ProjectContext, hostname, logName, cluster
pc,
hostname,
logName,
clusterName,
clusterMajorVersion)
clusterInfo)
err = s.consumer.ConsumeLogs(context.Background(), plog)
if err != nil {
s.log.Error("Failed to consume logs", zap.Error(err))
}
}

func (s *logsReceiver) collectAuditLogs(pc ProjectContext, hostname, logName, clusterName, clusterMajorVersion string) {
func (s *logsReceiver) collectAuditLogs(pc ProjectContext, hostname, logName string, clusterInfo ClusterInfo) {
logs, err := s.getHostAuditLogs(
pc.Project.ID,
hostname,
Expand All @@ -265,8 +278,7 @@ func (s *logsReceiver) collectAuditLogs(pc ProjectContext, hostname, logName, cl
pc,
hostname,
logName,
clusterName,
clusterMajorVersion)
clusterInfo)
if err != nil {
s.log.Warn("Failed to translate audit logs: "+logName, zap.Error(err))
return
Expand Down
18 changes: 11 additions & 7 deletions receiver/mongodbatlasreceiver/mongodb_event_to_logdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var severityMap = map[string]plog.SeverityNumber{
}

// mongoAuditEventToLogRecord converts model.AuditLog event to plog.LogRecordSlice and adds the resource attributes.
func mongodbAuditEventToLogData(logger *zap.Logger, logs []model.AuditLog, pc ProjectContext, hostname, logName, clusterName, clusterMajorVersion string) (plog.Logs, error) {
func mongodbAuditEventToLogData(logger *zap.Logger, logs []model.AuditLog, pc ProjectContext, hostname, logName string, clusterInfo ClusterInfo) (plog.Logs, error) {
ld := plog.NewLogs()
rl := ld.ResourceLogs().AppendEmpty()
sl := rl.ScopeLogs().AppendEmpty()
Expand All @@ -54,15 +54,17 @@ func mongodbAuditEventToLogData(logger *zap.Logger, logs []model.AuditLog, pc Pr
// Attributes related to the object causing the event.
resourceAttrs.PutStr("mongodb_atlas.org", pc.orgName)
resourceAttrs.PutStr("mongodb_atlas.project", pc.Project.Name)
resourceAttrs.PutStr("mongodb_atlas.cluster", clusterName)
resourceAttrs.PutStr("mongodb_atlas.cluster", clusterInfo.ClusterName)
resourceAttrs.PutStr("mongodb_atlas.region.name", clusterInfo.RegionName)
resourceAttrs.PutStr("mongodb_atlas.provider.name", clusterInfo.ProviderName)
resourceAttrs.PutStr("mongodb_atlas.host.name", hostname)

var errs []error

for _, log := range logs {
lr := sl.LogRecords().AppendEmpty()

logTsFormat := tsLayout(clusterMajorVersion)
logTsFormat := tsLayout(clusterInfo.MongoDBMajorVersion)
t, err := time.Parse(logTsFormat, log.Timestamp.Date)
if err != nil {
logger.Warn("Time failed to parse correctly", zap.Error(err))
Expand Down Expand Up @@ -143,7 +145,7 @@ func mongodbAuditEventToLogData(logger *zap.Logger, logs []model.AuditLog, pc Pr
}

// mongoEventToLogRecord converts model.LogEntry event to plog.LogRecordSlice and adds the resource attributes.
func mongodbEventToLogData(logger *zap.Logger, logs []model.LogEntry, pc ProjectContext, hostname, logName, clusterName, clusterMajorVersion string) plog.Logs {
func mongodbEventToLogData(logger *zap.Logger, logs []model.LogEntry, pc ProjectContext, hostname, logName string, clusterInfo ClusterInfo) plog.Logs {
ld := plog.NewLogs()
rl := ld.ResourceLogs().AppendEmpty()
sl := rl.ScopeLogs().AppendEmpty()
Expand All @@ -154,10 +156,12 @@ func mongodbEventToLogData(logger *zap.Logger, logs []model.LogEntry, pc Project
// Attributes related to the object causing the event.
resourceAttrs.PutStr("mongodb_atlas.org", pc.orgName)
resourceAttrs.PutStr("mongodb_atlas.project", pc.Project.Name)
resourceAttrs.PutStr("mongodb_atlas.cluster", clusterName)
resourceAttrs.PutStr("mongodb_atlas.cluster", clusterInfo.ClusterName)
resourceAttrs.PutStr("mongodb_atlas.region.name", clusterInfo.RegionName)
resourceAttrs.PutStr("mongodb_atlas.provider.name", clusterInfo.ProviderName)
resourceAttrs.PutStr("mongodb_atlas.host.name", hostname)

logTsFormat := tsLayout(clusterMajorVersion)
logTsFormat := tsLayout(clusterInfo.MongoDBMajorVersion)

for _, log := range logs {
lr := sl.LogRecords().AppendEmpty()
Expand Down Expand Up @@ -187,7 +191,7 @@ func mongodbEventToLogData(logger *zap.Logger, logs []model.LogEntry, pc Project
attrs.PutStr("component", log.Component)
attrs.PutStr("context", log.Context)
// log ID is not present on MongoDB 4.2 systems
if clusterMajorVersion != mongoDBMajorVersion4_2 {
if clusterInfo.MongoDBMajorVersion != mongoDBMajorVersion4_2 {
attrs.PutInt("id", log.ID)
}
attrs.PutStr("log_name", logName)
Expand Down
59 changes: 50 additions & 9 deletions receiver/mongodbatlasreceiver/mongodb_event_to_logdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@ func TestMongoeventToLogData4_4(t *testing.T) {
orgName: "Org",
Project: mongodbatlas.Project{Name: "Project"},
}
clusterInfo := ClusterInfo{
ClusterName: "clusterName",
RegionName: "regionName",
ProviderName: "providerName",
MongoDBMajorVersion: "4.4",
}

ld := mongodbEventToLogData(zap.NewNop(), []model.LogEntry{mongoevent}, pc, "hostname", "logName", "clusterName", "4.4")
ld := mongodbEventToLogData(zap.NewNop(), []model.LogEntry{mongoevent}, pc, "hostname", "logName", clusterInfo)
rl := ld.ResourceLogs().At(0)
resourceAttrs := rl.Resource().Attributes()
sl := rl.ScopeLogs().At(0)
lr := sl.LogRecords().At(0)
attrs := lr.Attributes()

assert.Equal(t, 1, ld.ResourceLogs().Len())
assert.Equal(t, 4, resourceAttrs.Len())
assert.Equal(t, 6, resourceAttrs.Len())
assertString(t, resourceAttrs, "mongodb_atlas.org", "Org")
assertString(t, resourceAttrs, "mongodb_atlas.project", "Project")
assertString(t, resourceAttrs, "mongodb_atlas.cluster", "clusterName")
assertString(t, resourceAttrs, "mongodb_atlas.host.name", "hostname")
assertString(t, resourceAttrs, "mongodb_atlas.region.name", "regionName")
assertString(t, resourceAttrs, "mongodb_atlas.provider.name", "providerName")

t.Logf("%+v", attrs.AsRaw())
assert.Equal(t, 8, attrs.Len())
Expand All @@ -62,18 +70,27 @@ func TestMongoeventToLogData4_2(t *testing.T) {
Project: mongodbatlas.Project{Name: "Project"},
}

ld := mongodbEventToLogData(zaptest.NewLogger(t), []model.LogEntry{mongoevent}, pc, "hostname", "logName", "clusterName", "4.2")
clusterInfo := ClusterInfo{
ClusterName: "clusterName",
RegionName: "regionName",
ProviderName: "providerName",
MongoDBMajorVersion: "4.2",
}

ld := mongodbEventToLogData(zaptest.NewLogger(t), []model.LogEntry{mongoevent}, pc, "hostname", "logName", clusterInfo)
rl := ld.ResourceLogs().At(0)
resourceAttrs := rl.Resource().Attributes()
sl := rl.ScopeLogs().At(0)
lr := sl.LogRecords().At(0)
attrs := lr.Attributes()

assert.Equal(t, 1, ld.ResourceLogs().Len())
assert.Equal(t, 4, resourceAttrs.Len())
assert.Equal(t, 6, resourceAttrs.Len())
assertString(t, resourceAttrs, "mongodb_atlas.org", "Org")
assertString(t, resourceAttrs, "mongodb_atlas.project", "Project")
assertString(t, resourceAttrs, "mongodb_atlas.cluster", "clusterName")
assertString(t, resourceAttrs, "mongodb_atlas.region.name", "regionName")
assertString(t, resourceAttrs, "mongodb_atlas.provider.name", "providerName")
assertString(t, resourceAttrs, "mongodb_atlas.host.name", "hostname")

assert.Equal(t, 4, attrs.Len())
Expand All @@ -98,8 +115,14 @@ func TestUnknownSeverity(t *testing.T) {
orgName: "Org",
Project: mongodbatlas.Project{Name: "Project"},
}
clusterInfo := ClusterInfo{
ClusterName: "clusterName",
RegionName: "regionName",
ProviderName: "providerName",
MongoDBMajorVersion: "4.4",
}

ld := mongodbEventToLogData(zap.NewNop(), []model.LogEntry{mongoevent}, pc, "hostname", "clusterName", "logName", "4.4")
ld := mongodbEventToLogData(zap.NewNop(), []model.LogEntry{mongoevent}, pc, "hostname", "clusterName", clusterInfo)
rl := ld.ResourceLogs().At(0)
logEntry := rl.ScopeLogs().At(0).LogRecords().At(0)

Expand All @@ -114,7 +137,14 @@ func TestMongoEventToAuditLogData5_0(t *testing.T) {
Project: mongodbatlas.Project{Name: "Project"},
}

ld, err := mongodbAuditEventToLogData(zaptest.NewLogger(t), []model.AuditLog{mongoevent}, pc, "hostname", "logName", "clusterName", "5.0")
clusterInfo := ClusterInfo{
ClusterName: "clusterName",
RegionName: "regionName",
ProviderName: "providerName",
MongoDBMajorVersion: "5.0",
}

ld, err := mongodbAuditEventToLogData(zaptest.NewLogger(t), []model.AuditLog{mongoevent}, pc, "hostname", "logName", clusterInfo)
require.NoError(t, err)
rl := ld.ResourceLogs().At(0)
resourceAttrs := rl.Resource().Attributes()
Expand All @@ -123,11 +153,13 @@ func TestMongoEventToAuditLogData5_0(t *testing.T) {
attrs := lr.Attributes()

assert.Equal(t, ld.ResourceLogs().Len(), 1)
assert.Equal(t, resourceAttrs.Len(), 4)
assert.Equal(t, resourceAttrs.Len(), 6)
assertString(t, resourceAttrs, "mongodb_atlas.org", "Org")
assertString(t, resourceAttrs, "mongodb_atlas.project", "Project")
assertString(t, resourceAttrs, "mongodb_atlas.cluster", "clusterName")
assertString(t, resourceAttrs, "mongodb_atlas.host.name", "hostname")
assertString(t, resourceAttrs, "mongodb_atlas.region.name", "regionName")
assertString(t, resourceAttrs, "mongodb_atlas.provider.name", "providerName")

assert.Equal(t, 14, attrs.Len())
assertString(t, attrs, "atype", "authenticate")
Expand Down Expand Up @@ -171,7 +203,14 @@ func TestMongoEventToAuditLogData4_2(t *testing.T) {
Project: mongodbatlas.Project{Name: "Project"},
}

ld, err := mongodbAuditEventToLogData(zaptest.NewLogger(t), []model.AuditLog{mongoevent}, pc, "hostname", "logName", "clusterName", "4.2")
clusterInfo := ClusterInfo{
ClusterName: "clusterName",
RegionName: "regionName",
ProviderName: "providerName",
MongoDBMajorVersion: "4.2",
}

ld, err := mongodbAuditEventToLogData(zaptest.NewLogger(t), []model.AuditLog{mongoevent}, pc, "hostname", "logName", clusterInfo)
require.NoError(t, err)
rl := ld.ResourceLogs().At(0)
resourceAttrs := rl.Resource().Attributes()
Expand All @@ -180,11 +219,13 @@ func TestMongoEventToAuditLogData4_2(t *testing.T) {
attrs := lr.Attributes()

assert.Equal(t, ld.ResourceLogs().Len(), 1)
assert.Equal(t, resourceAttrs.Len(), 4)
assert.Equal(t, resourceAttrs.Len(), 6)
assertString(t, resourceAttrs, "mongodb_atlas.org", "Org")
assertString(t, resourceAttrs, "mongodb_atlas.project", "Project")
assertString(t, resourceAttrs, "mongodb_atlas.cluster", "clusterName")
assertString(t, resourceAttrs, "mongodb_atlas.host.name", "hostname")
assertString(t, resourceAttrs, "mongodb_atlas.region.name", "regionName")
assertString(t, resourceAttrs, "mongodb_atlas.provider.name", "providerName")

assert.Equal(t, 10, attrs.Len())
assertString(t, attrs, "atype", "authenticate")
Expand Down