Skip to content

Commit

Permalink
[receiver/mongodbatlasreceiver] add provider resource attributes (ope…
Browse files Browse the repository at this point in the history
…n-telemetry#28835)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
This feature adds provider resource attributes
`mongodb_atlas.provider.name` and `mongodb_atlas.region.name` to add
additional context and filtering capabilities.

**Link to tracking Issue:** <Issue number if applicable>
open-telemetry#28833 

**Testing:** <Describe what testing was performed and which tests were
added.>
Test were automatically updated. Live testing was performed and
validated on clusters.
**Documentation:** <Describe the documentation added.>
Docs were automatically updated.
  • Loading branch information
JonathanWamsley authored and RoryCrispin committed Nov 24, 2023
1 parent f1d4165 commit e90a01a
Show file tree
Hide file tree
Showing 18 changed files with 526 additions and 332 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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: receiver/mongodbatlasreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: emit resource attributes "`mongodb_atlas.region.name`" and "`mongodb_atlas.provider.name`" on metric scrape.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [28833]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
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
4 changes: 4 additions & 0 deletions receiver/mongodbatlasreceiver/access_logs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func TestAccessLogsIntegration(t *testing.T) {
{
GroupID: testProjectID,
Name: testClusterName,
ProviderSettings: &mongodbatlas.ProviderSettings{
ProviderName: testProviderName,
RegionName: testRegionName,
},
},
},
nil)
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]any{
"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
2 changes: 2 additions & 0 deletions receiver/mongodbatlasreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,6 @@ Aggregate of MongoDB Metrics MAX_SWAP_USAGE_FREE, MAX_SWAP_USAGE_USED
| mongodb_atlas.process.type_name | Process type | Any Str | true |
| mongodb_atlas.project.id | Project ID | Any Str | true |
| mongodb_atlas.project.name | Project Name | Any Str | true |
| mongodb_atlas.provider.name | Provider Name | Any Str | false |
| mongodb_atlas.region.name | Region Name | Any Str | false |
| mongodb_atlas.user.alias | User-friendly hostname of the cluster node | Any Str | false |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ all_set:
enabled: true
mongodb_atlas.project.name:
enabled: true
mongodb_atlas.provider.name:
enabled: true
mongodb_atlas.region.name:
enabled: true
mongodb_atlas.user.alias:
enabled: true
none_set:
Expand Down Expand Up @@ -295,5 +299,9 @@ none_set:
enabled: false
mongodb_atlas.project.name:
enabled: false
mongodb_atlas.provider.name:
enabled: false
mongodb_atlas.region.name:
enabled: false
mongodb_atlas.user.alias:
enabled: false
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
8 changes: 8 additions & 0 deletions receiver/mongodbatlasreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ resource_attributes:
description: Cluster Name
enabled: false
type: string
mongodb_atlas.region.name:
description: Region Name
enabled: false
type: string
mongodb_atlas.provider.name:
description: Provider Name
enabled: false
type: string
mongodb_atlas.process.port:
description: Port process is bound to
enabled: true
Expand Down
Loading

0 comments on commit e90a01a

Please sign in to comment.