From 9e55dbc75547e5dddb9bda98b579a034b087d49d Mon Sep 17 00:00:00 2001 From: wparr-circle Date: Fri, 15 Nov 2024 15:43:45 +0000 Subject: [PATCH 1/4] feat(autodiscovery): support extra_dbname when templating a database instance Signed-off-by: wparr-circle --- .../autodiscovery/listeners/dbm_aurora.go | 3 ++ .../listeners/dbm_aurora_test.go | 38 +++++++++++++++++++ pkg/databasemonitoring/aws/aurora.go | 9 ++++- pkg/databasemonitoring/aws/aurora_test.go | 20 ++++++++++ pkg/databasemonitoring/aws/client.go | 3 +- 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/comp/core/autodiscovery/listeners/dbm_aurora.go b/comp/core/autodiscovery/listeners/dbm_aurora.go index bce4704331fa1e..c0b61641c4e612 100644 --- a/comp/core/autodiscovery/listeners/dbm_aurora.go +++ b/comp/core/autodiscovery/listeners/dbm_aurora.go @@ -277,7 +277,10 @@ func (d *DBMAuroraService) GetExtraConfig(key string) (string, error) { return strconv.FormatBool(d.instance.IamEnabled), nil case "dbclusteridentifier": return d.clusterID, nil + case "dbname": + return d.instance.DbName, nil } + return "", ErrNotSupported } diff --git a/comp/core/autodiscovery/listeners/dbm_aurora_test.go b/comp/core/autodiscovery/listeners/dbm_aurora_test.go index 1243cd9beceece..33be75f09834c9 100644 --- a/comp/core/autodiscovery/listeners/dbm_aurora_test.go +++ b/comp/core/autodiscovery/listeners/dbm_aurora_test.go @@ -279,6 +279,44 @@ func TestDBMAuroraListener(t *testing.T) { } } +func TestGetExtraConfig(t *testing.T) { + testCases := []struct { + service *DBMAuroraService + expectedExtra map[string]string + }{ + { + service: &DBMAuroraService{ + adIdentifier: dbmPostgresADIdentifier, + entityID: "f7fee36c58e3da8a", + checkName: "postgres", + clusterID: "my-cluster-1", + region: "us-east-1", + instance: &aws.Instance{ + Endpoint: "my-endpoint", + Port: 5432, + IamEnabled: true, + Engine: "aurora-postgresql", + DbName: "app", + }, + }, + expectedExtra: map[string]string{ + "dbname": "app", + "region": "us-east-1", + "managed_authentication_enabled": "true", + "dbclusteridentifier": "my-cluster-1", + }, + }, + } + + for _, tc := range testCases { + for key, value := range tc.expectedExtra { + v, err := tc.service.GetExtraConfig(key) + assert.NoError(t, err) + assert.Equal(t, value, v) + } + } +} + func contextWithTimeout(t time.Duration) gomock.Matcher { return contextWithTimeoutMatcher{ timeout: t, diff --git a/pkg/databasemonitoring/aws/aurora.go b/pkg/databasemonitoring/aws/aurora.go index 9d5270b4df9163..5c47ee10595357 100644 --- a/pkg/databasemonitoring/aws/aurora.go +++ b/pkg/databasemonitoring/aws/aurora.go @@ -11,11 +11,12 @@ package aws import ( "context" "fmt" + "hash/fnv" + "strconv" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" - "hash/fnv" - "strconv" "strings" ) @@ -31,6 +32,7 @@ type Instance struct { Port int32 IamEnabled bool Engine string + DbName string } const ( @@ -77,6 +79,9 @@ func (c *Client) GetAuroraClusterEndpoints(ctx context.Context, dbClusterIdentif if db.Engine != nil { instance.Engine = *db.Engine } + if db.DBName != nil { + instance.DbName = *db.DBName + } if _, ok := clusters[*db.DBClusterIdentifier]; !ok { clusters[*db.DBClusterIdentifier] = &AuroraCluster{ Instances: make([]*Instance, 0), diff --git a/pkg/databasemonitoring/aws/aurora_test.go b/pkg/databasemonitoring/aws/aurora_test.go index 4cb7fed6c50fc0..7f4595d1737e74 100644 --- a/pkg/databasemonitoring/aws/aurora_test.go +++ b/pkg/databasemonitoring/aws/aurora_test.go @@ -68,6 +68,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, }, }, nil).Times(1) @@ -81,6 +82,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { Port: 5432, IamEnabled: true, Engine: "aurora-postgresql", + DbName: "postgres", }, }, }, @@ -101,6 +103,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, { Endpoint: &types.Endpoint{ @@ -112,6 +115,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, { Endpoint: &types.Endpoint{ @@ -123,6 +127,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, }, }, nil).Times(1) @@ -136,18 +141,21 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { Port: 5432, IamEnabled: true, Engine: "aurora-postgresql", + DbName: "postgres", }, { Endpoint: "test-endpoint-2", Port: 5432, IamEnabled: false, Engine: "aurora-postgresql", + DbName: "postgres", }, { Endpoint: "test-endpoint-3", Port: 5444, IamEnabled: false, Engine: "aurora-postgresql", + DbName: "postgres", }, }, }, @@ -168,6 +176,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, { Endpoint: &types.Endpoint{ @@ -179,6 +188,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("terminating"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, { Endpoint: &types.Endpoint{ @@ -190,6 +200,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("terminating"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, }, }, nil).Times(1) @@ -203,6 +214,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { Port: 5432, IamEnabled: true, Engine: "aurora-postgresql", + DbName: "postgres", }, }, }, @@ -223,6 +235,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, }, }, nil).Times(1) @@ -236,6 +249,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { Port: 5432, IamEnabled: true, Engine: "aurora-postgresql", + DbName: "postgres", }, }, }, @@ -256,6 +270,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, { Endpoint: &types.Endpoint{ @@ -267,6 +282,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1a"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, { Endpoint: &types.Endpoint{ @@ -278,6 +294,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { AvailabilityZone: aws.String("us-east-1c"), DBInstanceStatus: aws.String("available"), Engine: aws.String("aurora-postgresql"), + DBName: aws.String("postgres"), }, }, }, nil).Times(1) @@ -291,12 +308,14 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { Port: 5432, IamEnabled: true, Engine: "aurora-postgresql", + DbName: "postgres", }, { Endpoint: "test-endpoint-2", Port: 5432, IamEnabled: false, Engine: "aurora-postgresql", + DbName: "postgres", }, }, }, @@ -307,6 +326,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) { Port: 5444, IamEnabled: true, Engine: "aurora-postgresql", + DbName: "postgres", }, }, }, diff --git a/pkg/databasemonitoring/aws/client.go b/pkg/databasemonitoring/aws/client.go index 7248c31e81d2b8..f1107562107d89 100644 --- a/pkg/databasemonitoring/aws/client.go +++ b/pkg/databasemonitoring/aws/client.go @@ -9,10 +9,11 @@ package aws import ( "context" + "time" + "github.com/DataDog/datadog-agent/pkg/util/ec2" awsconfig "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/rds" - "time" ) //go:generate mockgen -source=$GOFILE -package=$GOPACKAGE -destination=rdsclient_mockgen.go From aaa41225fdd3b573b7755fc6588e027c37253e29 Mon Sep 17 00:00:00 2001 From: wparr-circle Date: Mon, 9 Dec 2024 13:09:29 +0530 Subject: [PATCH 2/4] chore: add release notes --- ...e-name-in-aurora-discovery-ec366b44f47c046a.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 releasenotes/notes/support-database-name-in-aurora-discovery-ec366b44f47c046a.yaml diff --git a/releasenotes/notes/support-database-name-in-aurora-discovery-ec366b44f47c046a.yaml b/releasenotes/notes/support-database-name-in-aurora-discovery-ec366b44f47c046a.yaml new file mode 100644 index 00000000000000..c475c22785ca98 --- /dev/null +++ b/releasenotes/notes/support-database-name-in-aurora-discovery-ec366b44f47c046a.yaml @@ -0,0 +1,13 @@ +# Each section from every release note are combined when the +# CHANGELOG.rst is rendered. So the text needs to be worded so that +# it does not depend on any information only available in another +# section. This may mean repeating some details, but each section +# must be readable independently of the other. +# +# Each section note must be formatted as reStructuredText. +--- +enhancements: + - | + Extends extra configuration available for templating from Aurora Database Discovery + to include %%extra_dbname%% allowing instances which are configured with non-standard + DBName field to be discovered successfully From 8b5cbde39277a7f17a6847164201b8f643c120fb Mon Sep 17 00:00:00 2001 From: Wassim Dhif Date: Wed, 18 Dec 2024 18:09:58 +0100 Subject: [PATCH 3/4] Update pkg/databasemonitoring/aws/aurora.go --- pkg/databasemonitoring/aws/aurora.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/databasemonitoring/aws/aurora.go b/pkg/databasemonitoring/aws/aurora.go index d4278f07bcab54..210096195627ce 100644 --- a/pkg/databasemonitoring/aws/aurora.go +++ b/pkg/databasemonitoring/aws/aurora.go @@ -47,7 +47,6 @@ func (c *Client) GetAuroraClusterEndpoints(ctx context.Context, dbClusterIdentif return nil, fmt.Errorf("at least one database cluster identifier is required") } clusters := make(map[string]*AuroraCluster, 0) - for _, clusterID := range dbClusterIdentifiers { // TODO: Seth Samuel: This method is not paginated, so if there are more than 100 instances in a cluster, we will only get the first 100 // We should add pagination support to this method at some point From 0ce9ab3cccdc2a2d02d57c4afb758c2e981de914 Mon Sep 17 00:00:00 2001 From: Zhengda Lu Date: Thu, 19 Dec 2024 14:02:46 -0500 Subject: [PATCH 4/4] fix lint --- pkg/databasemonitoring/aws/aurora.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/databasemonitoring/aws/aurora.go b/pkg/databasemonitoring/aws/aurora.go index 210096195627ce..18f849bf2d9267 100644 --- a/pkg/databasemonitoring/aws/aurora.go +++ b/pkg/databasemonitoring/aws/aurora.go @@ -82,9 +82,9 @@ func (c *Client) GetAuroraClusterEndpoints(ctx context.Context, dbClusterIdentif if db.Engine != nil { instance.Engine = *db.Engine } - if db.DBName != nil { - instance.DbName = *db.DBName - } + if db.DBName != nil { + instance.DbName = *db.DBName + } if _, ok := clusters[*db.DBClusterIdentifier]; !ok { clusters[*db.DBClusterIdentifier] = &AuroraCluster{ Instances: make([]*Instance, 0),