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/mongodbatlas] Add Cluster Name resource attribute #21171

Merged
merged 7 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
17 changes: 17 additions & 0 deletions .chloggen/mongodbatlas-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 "`mongodb_atlas.cluster.name`" attribute which is thename of the cluster as defined in Atlas.
This attribute is disabled by default, if you want to emit it, you'll need to explicitly enable it.

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

# (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:
1 change: 1 addition & 0 deletions receiver/mongodbatlasreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ Aggregate of MongoDB Metrics MAX_SWAP_USAGE_FREE, MAX_SWAP_USAGE_USED

| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| mongodb_atlas.cluster.name | Cluster Name | Any Str | false |
| mongodb_atlas.db.name | Name of the Database | Any Str | true |
| mongodb_atlas.disk.partition | Name of a disk partition | Any Str | true |
| mongodb_atlas.host.name | Hostname of the process | Any Str | true |
Expand Down

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 @@ -128,6 +128,8 @@ all_set:
mongodbatlas.system.paging.usage.max:
enabled: true
resource_attributes:
mongodb_atlas.cluster.name:
enabled: true
mongodb_atlas.db.name:
enabled: true
mongodb_atlas.disk.partition:
Expand Down Expand Up @@ -277,6 +279,8 @@ none_set:
mongodbatlas.system.paging.usage.max:
enabled: false
resource_attributes:
mongodb_atlas.cluster.name:
enabled: false
mongodb_atlas.db.name:
enabled: false
mongodb_atlas.disk.partition:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func (s *MongoDBAtlasClient) GetLogs(ctx context.Context, groupID, hostname, log
return buf, nil
}

// retrieves the logs from the mongo API using API call: https://www.mongodb.com/docs/atlas/reference/api/clusters-get-all/#request
// GetClusters retrieves the clusters from the mongo API using API call: https://www.mongodb.com/docs/atlas/reference/api/clusters-get-all/#request
func (s *MongoDBAtlasClient) GetClusters(ctx context.Context, groupID string) ([]mongodbatlas.Cluster, error) {
options := mongodbatlas.ListOptions{}

Expand Down
4 changes: 4 additions & 0 deletions receiver/mongodbatlasreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ resource_attributes:
description: User-friendly hostname of the cluster node
enabled: false
type: string
mongodb_atlas.cluster.name:
description: Cluster Name
enabled: false
type: string
mongodb_atlas.process.port:
description: Port process is bound to
enabled: true
Expand Down
50 changes: 43 additions & 7 deletions receiver/mongodbatlasreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"strconv"
"strings"
"time"

"go.mongodb.org/atlas/mongodbatlas"
Expand Down Expand Up @@ -98,21 +99,30 @@ func (s *receiver) poll(ctx context.Context, time timeconstraints) error {
return fmt.Errorf("error retrieving projects: %w", err)
}
for _, project := range projects {
nodeClusterMap, err := s.getNodeClusterNameMap(ctx, project.ID)
if err != nil {
return fmt.Errorf("error collecting clusters from project %s: %w", project.ID, err)
}

processes, err := s.client.Processes(ctx, project.ID)
if err != nil {
return fmt.Errorf("error retrieving MongoDB Atlas processes: %w", err)
return fmt.Errorf("error retrieving MongoDB Atlas processes for project %s: %w", project.ID, err)
}
for _, process := range processes {
clusterName := ""
if v, ok := nodeClusterMap[process.UserAlias]; ok {
clusterName = v
}
dehaansa marked this conversation as resolved.
Show resolved Hide resolved

if err := s.extractProcessMetrics(ctx, time, org.Name, project, process); err != nil {
if err := s.extractProcessMetrics(ctx, time, org.Name, project, process, clusterName); err != nil {
return fmt.Errorf("error when polling process metrics from MongoDB Atlas for process %s: %w", process.ID, err)
}

if err := s.extractProcessDatabaseMetrics(ctx, time, org.Name, project, process); err != nil {
if err := s.extractProcessDatabaseMetrics(ctx, time, org.Name, project, process, clusterName); err != nil {
return fmt.Errorf("error when polling process database metrics from MongoDB Atlas for process %s: %w", process.ID, err)
}

if err := s.extractProcessDiskMetrics(ctx, time, org.Name, project, process); err != nil {
if err := s.extractProcessDiskMetrics(ctx, time, org.Name, project, process, clusterName); err != nil {
return fmt.Errorf("error when polling process disk metrics from MongoDB Atlas for process %s: %w", process.ID, err)
}
}
Expand All @@ -121,15 +131,36 @@ func (s *receiver) poll(ctx context.Context, time timeconstraints) error {
return nil
}

func (s *receiver) getNodeClusterNameMap(
ctx context.Context,
projectID string,
) (map[string]string, error) {
clusterMap := make(map[string]string)
clusters, err := s.client.GetClusters(ctx, projectID)
if err != nil {
return nil, err
}

for _, cluster := range clusters {
// URI in the form mongodb://host1.mongodb.net:27017,host2.mongodb.net:27017,host3.mongodb.net:27017
for _, node := range strings.Split(strings.TrimPrefix(cluster.MongoURI, "mongodb://"), ",") {
dehaansa marked this conversation as resolved.
Show resolved Hide resolved
// Remove the port from the node
n, _, _ := strings.Cut(node, ":")
clusterMap[n] = cluster.Name
}
}

return clusterMap, nil
}

func (s *receiver) extractProcessMetrics(
ctx context.Context,
time timeconstraints,
orgName string,
project *mongodbatlas.Project,
process *mongodbatlas.Process,
clusterName string,
) error {
// This receiver will support both logs and metrics- if one pipeline
// or the other is not configured, it will be nil.
if err := s.client.ProcessMetrics(
ctx,
s.mb,
Expand All @@ -149,6 +180,7 @@ func (s *receiver) extractProcessMetrics(
metadata.WithMongodbAtlasProjectID(project.ID),
metadata.WithMongodbAtlasHostName(process.Hostname),
metadata.WithMongodbAtlasUserAlias(process.UserAlias),
metadata.WithMongodbAtlasClusterName(clusterName),
metadata.WithMongodbAtlasProcessPort(strconv.Itoa(process.Port)),
metadata.WithMongodbAtlasProcessTypeName(process.TypeName),
metadata.WithMongodbAtlasProcessID(process.ID),
Expand All @@ -163,6 +195,7 @@ func (s *receiver) extractProcessDatabaseMetrics(
orgName string,
project *mongodbatlas.Project,
process *mongodbatlas.Process,
clusterName string,
) error {
processDatabases, err := s.client.ProcessDatabases(
ctx,
Expand Down Expand Up @@ -194,6 +227,7 @@ func (s *receiver) extractProcessDatabaseMetrics(
metadata.WithMongodbAtlasProjectID(project.ID),
metadata.WithMongodbAtlasHostName(process.Hostname),
metadata.WithMongodbAtlasUserAlias(process.UserAlias),
metadata.WithMongodbAtlasClusterName(clusterName),
metadata.WithMongodbAtlasProcessPort(strconv.Itoa(process.Port)),
metadata.WithMongodbAtlasProcessTypeName(process.TypeName),
metadata.WithMongodbAtlasProcessID(process.ID),
Expand All @@ -209,6 +243,7 @@ func (s *receiver) extractProcessDiskMetrics(
orgName string,
project *mongodbatlas.Project,
process *mongodbatlas.Process,
clusterName string,
) error {
for _, disk := range s.client.ProcessDisks(ctx, project.ID, process.Hostname, process.Port) {
if err := s.client.ProcessDiskMetrics(
Expand All @@ -222,14 +257,15 @@ func (s *receiver) extractProcessDiskMetrics(
time.end,
time.resolution,
); err != nil {
return fmt.Errorf("error when polling from MongoDB Atlas: %w", err)
return fmt.Errorf("error when polling disk metrics from MongoDB Atlas: %w", err)
}
s.mb.EmitForResource(
metadata.WithMongodbAtlasOrgName(orgName),
metadata.WithMongodbAtlasProjectName(project.Name),
metadata.WithMongodbAtlasProjectID(project.ID),
metadata.WithMongodbAtlasHostName(process.Hostname),
metadata.WithMongodbAtlasUserAlias(process.UserAlias),
metadata.WithMongodbAtlasClusterName(clusterName),
metadata.WithMongodbAtlasProcessPort(strconv.Itoa(process.Port)),
metadata.WithMongodbAtlasProcessTypeName(process.TypeName),
metadata.WithMongodbAtlasProcessID(process.ID),
Expand Down
Loading