Skip to content

Commit

Permalink
feat(autodiscovery): support extra_dbname when templating a database …
Browse files Browse the repository at this point in the history
…instance
  • Loading branch information
wparr-circle committed Nov 15, 2024
1 parent 99a996e commit 4eefd1b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
3 changes: 3 additions & 0 deletions comp/core/autodiscovery/listeners/dbm_aurora.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
38 changes: 38 additions & 0 deletions comp/core/autodiscovery/listeners/dbm_aurora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions pkg/databasemonitoring/aws/aurora.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -31,6 +32,7 @@ type Instance struct {
Port int32
IamEnabled bool
Engine string
DbName string
}

const (
Expand Down Expand Up @@ -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),
Expand Down
20 changes: 20 additions & 0 deletions pkg/databasemonitoring/aws/aurora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -81,6 +82,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
Port: 5432,
IamEnabled: true,
Engine: "aurora-postgresql",
DbName: "postgres",
},
},
},
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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",
},
},
},
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -203,6 +214,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
Port: 5432,
IamEnabled: true,
Engine: "aurora-postgresql",
DbName: "postgres",
},
},
},
Expand All @@ -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)
Expand All @@ -236,6 +249,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
Port: 5432,
IamEnabled: true,
Engine: "aurora-postgresql",
DbName: "postgres",
},
},
},
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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",
},
},
},
Expand All @@ -307,6 +326,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
Port: 5444,
IamEnabled: true,
Engine: "aurora-postgresql",
DbName: "postgres",
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/databasemonitoring/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4eefd1b

Please sign in to comment.