Skip to content

Commit 6009c54

Browse files
fix: Database_observability: reuse cloud provider regexes (#5262)
1 parent d8f0b3e commit 6009c54

File tree

5 files changed

+16
-31
lines changed

5 files changed

+16
-31
lines changed

internal/component/database_observability/cloud_provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package database_observability
22

33
import (
4+
"regexp"
5+
46
"github.com/aws/aws-sdk-go-v2/aws/arn"
57
)
68

9+
var (
10+
RdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
11+
AzureMySQLRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?mysql\.database\.azure\.com`)
12+
AzurePostgreSQLRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?postgres\.database\.azure\.com`)
13+
)
14+
715
type CloudProvider struct {
816
AWS *AWSCloudProviderInfo
917
Azure *AzureCloudProviderInfo

internal/component/database_observability/mysql/cloud_provider.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ package mysql
33
import (
44
"fmt"
55
"net"
6-
"regexp"
76
"strings"
87

98
"github.com/aws/aws-sdk-go-v2/aws/arn"
109
"github.com/go-sql-driver/mysql"
1110
"github.com/grafana/alloy/internal/component/database_observability"
1211
)
1312

14-
var (
15-
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
16-
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.mysql\.database\.azure\.com`)
17-
)
18-
1913
func populateCloudProviderFromConfig(config *CloudProvider) (*database_observability.CloudProvider, error) {
2014
var cloudProvider database_observability.CloudProvider
2115
if config.AWS != nil {
@@ -41,7 +35,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
4135
host, _, err := net.SplitHostPort(cfg.Addr)
4236
if err == nil && host != "" {
4337
if strings.HasSuffix(host, "rds.amazonaws.com") {
44-
if matches := rdsRegex.FindStringSubmatch(host); len(matches) >= 4 {
38+
if matches := database_observability.RdsRegex.FindStringSubmatch(host); len(matches) >= 4 {
4539
cloudProvider.AWS = &database_observability.AWSCloudProviderInfo{
4640
ARN: arn.ARN{
4741
Resource: fmt.Sprintf("db:%s", matches[1]),
@@ -51,7 +45,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
5145
}
5246
}
5347
} else if strings.HasSuffix(host, "mysql.database.azure.com") {
54-
if matches := azureRegex.FindStringSubmatch(host); len(matches) >= 2 {
48+
if matches := database_observability.AzureMySQLRegex.FindStringSubmatch(host); len(matches) >= 2 {
5549
cloudProvider.Azure = &database_observability.AzureCloudProviderInfo{
5650
Resource: matches[1],
5751
}

internal/component/database_observability/mysql/collector/connection_info.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package collector
33
import (
44
"context"
55
"net"
6-
"regexp"
76
"strings"
87

98
"github.com/go-sql-driver/mysql"
@@ -14,11 +13,6 @@ import (
1413

1514
const ConnectionInfoName = "connection_info"
1615

17-
var (
18-
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
19-
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?mysql\.database\.azure\.com`)
20-
)
21-
2216
type ConnectionInfoArguments struct {
2317
DSN string
2418
Registry *prometheus.Registry
@@ -93,14 +87,14 @@ func (c *ConnectionInfo) Start(ctx context.Context) error {
9387
if err == nil && host != "" {
9488
if strings.HasSuffix(host, "rds.amazonaws.com") {
9589
providerName = "aws"
96-
matches := rdsRegex.FindStringSubmatch(host)
90+
matches := database_observability.RdsRegex.FindStringSubmatch(host)
9791
if len(matches) > 3 {
9892
dbInstanceIdentifier = matches[1]
9993
providerRegion = matches[3]
10094
}
10195
} else if strings.HasSuffix(host, "mysql.database.azure.com") {
10296
providerName = "azure"
103-
matches := azureRegex.FindStringSubmatch(host)
97+
matches := database_observability.AzureMySQLRegex.FindStringSubmatch(host)
10498
if len(matches) > 1 {
10599
dbInstanceIdentifier = matches[1]
106100
}

internal/component/database_observability/postgres/cloud_provider.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ package postgres
22

33
import (
44
"fmt"
5-
"regexp"
65
"strings"
76

87
"github.com/aws/aws-sdk-go-v2/aws/arn"
98
"github.com/grafana/alloy/internal/component/database_observability"
109
"github.com/grafana/alloy/internal/component/database_observability/postgres/collector"
1110
)
1211

13-
var (
14-
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
15-
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.postgres\.database\.azure\.com`)
16-
)
17-
1812
func populateCloudProviderFromConfig(config *CloudProvider) (*database_observability.CloudProvider, error) {
1913
var cloudProvider database_observability.CloudProvider
2014
if config.AWS != nil {
@@ -39,7 +33,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
3933

4034
if host, ok := parts["host"]; ok {
4135
if strings.HasSuffix(host, "rds.amazonaws.com") {
42-
matches := rdsRegex.FindStringSubmatch(host)
36+
matches := database_observability.RdsRegex.FindStringSubmatch(host)
4337
cloudProvider.AWS = &database_observability.AWSCloudProviderInfo{
4438
ARN: arn.ARN{
4539
Resource: fmt.Sprintf("db:%s", matches[1]),
@@ -48,7 +42,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
4842
},
4943
}
5044
} else if strings.HasSuffix(host, "postgres.database.azure.com") {
51-
matches := azureRegex.FindStringSubmatch(host)
45+
matches := database_observability.AzurePostgreSQLRegex.FindStringSubmatch(host)
5246
if len(matches) > 1 {
5347
cloudProvider.Azure = &database_observability.AzureCloudProviderInfo{
5448
Resource: matches[1],

internal/component/database_observability/postgres/collector/connection_info.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import (
1212

1313
const ConnectionInfoName = "connection_info"
1414

15-
var (
16-
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
17-
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?postgres\.database\.azure\.com`)
18-
)
19-
2015
var engineVersionRegex = regexp.MustCompile(`(?P<version>^[1-9]+\.[1-9]+)(?P<suffix>.*)?$`)
2116

2217
type ConnectionInfoArguments struct {
@@ -92,14 +87,14 @@ func (c *ConnectionInfo) Start(ctx context.Context) error {
9287
if host, ok := parts["host"]; ok {
9388
if strings.HasSuffix(host, "rds.amazonaws.com") {
9489
providerName = "aws"
95-
matches := rdsRegex.FindStringSubmatch(host)
90+
matches := database_observability.RdsRegex.FindStringSubmatch(host)
9691
if len(matches) > 3 {
9792
dbInstanceIdentifier = matches[1]
9893
providerRegion = matches[3]
9994
}
10095
} else if strings.HasSuffix(host, "postgres.database.azure.com") {
10196
providerName = "azure"
102-
matches := azureRegex.FindStringSubmatch(host)
97+
matches := database_observability.AzurePostgreSQLRegex.FindStringSubmatch(host)
10398
if len(matches) > 1 {
10499
dbInstanceIdentifier = matches[1]
105100
}

0 commit comments

Comments
 (0)