Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions internal/component/database_observability/cloud_provider.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package database_observability

import (
"regexp"

"github.com/aws/aws-sdk-go-v2/aws/arn"
)

var (
RdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
RDSRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)

AzureMySQLRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?mysql\.database\.azure\.com`)
AzurePostgreSQLRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?postgres\.database\.azure\.com`)
)

type CloudProvider struct {
AWS *AWSCloudProviderInfo
Azure *AzureCloudProviderInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package mysql
import (
"fmt"
"net"
"regexp"
"strings"

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

var (
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.mysql\.database\.azure\.com`)
)

func populateCloudProviderFromConfig(config *CloudProvider) (*database_observability.CloudProvider, error) {
var cloudProvider database_observability.CloudProvider
if config.AWS != nil {
Expand All @@ -41,7 +35,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
host, _, err := net.SplitHostPort(cfg.Addr)
if err == nil && host != "" {
if strings.HasSuffix(host, "rds.amazonaws.com") {
if matches := rdsRegex.FindStringSubmatch(host); len(matches) >= 4 {
if matches := database_observability.RdsRegex.FindStringSubmatch(host); len(matches) >= 4 {
cloudProvider.AWS = &database_observability.AWSCloudProviderInfo{
ARN: arn.ARN{
Resource: fmt.Sprintf("db:%s", matches[1]),
Expand All @@ -51,7 +45,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
}
}
} else if strings.HasSuffix(host, "mysql.database.azure.com") {
if matches := azureRegex.FindStringSubmatch(host); len(matches) >= 2 {
if matches := database_observability.AzureMySQLRegex.FindStringSubmatch(host); len(matches) >= 2 {
cloudProvider.Azure = &database_observability.AzureCloudProviderInfo{
Resource: matches[1],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package collector
import (
"context"
"net"
"regexp"
"strings"

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

const ConnectionInfoName = "connection_info"

var (
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?mysql\.database\.azure\.com`)
)

type ConnectionInfoArguments struct {
DSN string
Registry *prometheus.Registry
Expand Down Expand Up @@ -93,14 +87,14 @@ func (c *ConnectionInfo) Start(ctx context.Context) error {
if err == nil && host != "" {
if strings.HasSuffix(host, "rds.amazonaws.com") {
providerName = "aws"
matches := rdsRegex.FindStringSubmatch(host)
matches := database_observability.RdsRegex.FindStringSubmatch(host)
if len(matches) > 3 {
dbInstanceIdentifier = matches[1]
providerRegion = matches[3]
}
} else if strings.HasSuffix(host, "mysql.database.azure.com") {
providerName = "azure"
matches := azureRegex.FindStringSubmatch(host)
matches := database_observability.AzureMySQLRegex.FindStringSubmatch(host)
if len(matches) > 1 {
dbInstanceIdentifier = matches[1]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package postgres

import (
"fmt"
"regexp"
"strings"

"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/grafana/alloy/internal/component/database_observability"
"github.com/grafana/alloy/internal/component/database_observability/postgres/collector"
)

var (
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.postgres\.database\.azure\.com`)
)

func populateCloudProviderFromConfig(config *CloudProvider) (*database_observability.CloudProvider, error) {
var cloudProvider database_observability.CloudProvider
if config.AWS != nil {
Expand All @@ -39,7 +33,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv

if host, ok := parts["host"]; ok {
if strings.HasSuffix(host, "rds.amazonaws.com") {
matches := rdsRegex.FindStringSubmatch(host)
matches := database_observability.RdsRegex.FindStringSubmatch(host)
cloudProvider.AWS = &database_observability.AWSCloudProviderInfo{
ARN: arn.ARN{
Resource: fmt.Sprintf("db:%s", matches[1]),
Expand All @@ -48,7 +42,7 @@ func populateCloudProviderFromDSN(dsn string) (*database_observability.CloudProv
},
}
} else if strings.HasSuffix(host, "postgres.database.azure.com") {
matches := azureRegex.FindStringSubmatch(host)
matches := database_observability.AzurePostgreSQLRegex.FindStringSubmatch(host)
if len(matches) > 1 {
cloudProvider.Azure = &database_observability.AzureCloudProviderInfo{
Resource: matches[1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import (

const ConnectionInfoName = "connection_info"

var (
rdsRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.([^\.]+)\.(?P<region>[^\.]+)\.rds\.amazonaws\.com`)
azureRegex = regexp.MustCompile(`(?P<identifier>[^\.]+)\.(?:privatelink\.)?postgres\.database\.azure\.com`)
)

var engineVersionRegex = regexp.MustCompile(`(?P<version>^[1-9]+\.[1-9]+)(?P<suffix>.*)?$`)

type ConnectionInfoArguments struct {
Expand Down Expand Up @@ -92,14 +87,14 @@ func (c *ConnectionInfo) Start(ctx context.Context) error {
if host, ok := parts["host"]; ok {
if strings.HasSuffix(host, "rds.amazonaws.com") {
providerName = "aws"
matches := rdsRegex.FindStringSubmatch(host)
matches := database_observability.RdsRegex.FindStringSubmatch(host)
if len(matches) > 3 {
dbInstanceIdentifier = matches[1]
providerRegion = matches[3]
}
} else if strings.HasSuffix(host, "postgres.database.azure.com") {
providerName = "azure"
matches := azureRegex.FindStringSubmatch(host)
matches := database_observability.AzurePostgreSQLRegex.FindStringSubmatch(host)
if len(matches) > 1 {
dbInstanceIdentifier = matches[1]
}
Expand Down
Loading