Skip to content

Commit

Permalink
Add awsAccountId as a label to privatelink metrics
Browse files Browse the repository at this point in the history
This will help in quickly identifying which account requires action, if
an alerts pops up.
  • Loading branch information
bergmannf committed Sep 13, 2022
1 parent 4db9d83 commit a5a7ed6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
11 changes: 8 additions & 3 deletions ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const (
ec2ServiceCode string = "ec2"
)

var TransitGatewaysQuota *prometheus.Desc = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "ec2_transitgatewaysperregion_quota"), "Quota for maximum number of Transitgateways in this account", []string{"aws_region"}, nil)
var TransitGatewaysUsage *prometheus.Desc = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "ec2_transitgatewaysperregion_usage"), "Number of Tranitgatewyas in the AWS Account", []string{"aws_region"}, nil)
var TransitGatewaysQuota *prometheus.Desc
var TransitGatewaysUsage *prometheus.Desc

type EC2Exporter struct {
sessions []*session.Session
Expand All @@ -32,9 +32,14 @@ type EC2Exporter struct {
interval time.Duration
}

func NewEC2Exporter(sessions []*session.Session, logger log.Logger, config EC2Config) *EC2Exporter {
func NewEC2Exporter(sessions []*session.Session, logger log.Logger, config EC2Config, awsAccountId string) *EC2Exporter {

level.Info(logger).Log("msg", "Initializing EC2 exporter")
accountIdLabel := map[string]string{"aws_account_id": awsAccountId}

TransitGatewaysQuota = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "ec2_transitgatewaysperregion_quota"), "Quota for maximum number of Transitgateways in this account", []string{"aws_region"}, accountIdLabel)
TransitGatewaysUsage = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "ec2_transitgatewaysperregion_usage"), "Number of Tranitgatewyas in the AWS Account", []string{"aws_region"}, accountIdLabel)

return &EC2Exporter{
sessions: sessions,
cache: *pkg.NewMetricsCache(*config.CacheTTL),
Expand Down
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ func setupCollectors(logger log.Logger, configFile string) ([]prometheus.Collect
level.Info(logger).Log("msg", "Configuring rds with regions", "regions", strings.Join(config.RdsConfig.Regions, ","))
level.Info(logger).Log("msg", "Configuring ec2 with regions", "regions", strings.Join(config.EC2Config.Regions, ","))
level.Info(logger).Log("msg", "Configuring route53 with region", "region", config.Route53Config.Region)
var vpcSessions []*session.Session
level.Info(logger).Log("msg", "Will VPC metrics be gathered?", "vpc-enabled", config.VpcConfig.Enabled)
awsAccountId, err := getAwsAccountNumber(logger, creds)
if err != nil {
return collectors, err
}
var vpcSessions []*session.Session
if config.VpcConfig.Enabled {
for _, region := range config.VpcConfig.Regions {
config := aws.NewConfig().WithRegion(region)
sess := session.Must(session.NewSession(config))
vpcSessions = append(vpcSessions, sess)
}
vpcExporter := NewVPCExporter(vpcSessions, logger, config.VpcConfig)
vpcExporter := NewVPCExporter(vpcSessions, logger, config.VpcConfig, awsAccountId)
collectors = append(collectors, vpcExporter)
go vpcExporter.CollectLoop()
}
Expand All @@ -170,15 +174,15 @@ func setupCollectors(logger log.Logger, configFile string) ([]prometheus.Collect
sess := session.Must(session.NewSession(config))
ec2Sessions = append(ec2Sessions, sess)
}
ec2Exporter := NewEC2Exporter(ec2Sessions, logger, config.EC2Config)
ec2Exporter := NewEC2Exporter(ec2Sessions, logger, config.EC2Config, awsAccountId)
collectors = append(collectors, ec2Exporter)
go ec2Exporter.CollectLoop()
}
level.Info(logger).Log("msg", "Will Route53 metrics be gathered?", "route53-enabled", config.Route53Config.Enabled)
if config.Route53Config.Enabled {
awsConfig := aws.NewConfig().WithRegion(config.Route53Config.Region)
sess := session.Must(session.NewSession(awsConfig))
r53Exporter := NewRoute53Exporter(sess, logger, config.Route53Config)
r53Exporter := NewRoute53Exporter(sess, logger, config.Route53Config, awsAccountId)
collectors = append(collectors, r53Exporter)
go r53Exporter.CollectLoop()
}
Expand Down
13 changes: 7 additions & 6 deletions route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ type Route53Exporter struct {
timeout time.Duration
}

func NewRoute53Exporter(sess *session.Session, logger log.Logger, config Route53Config) *Route53Exporter {
func NewRoute53Exporter(sess *session.Session, logger log.Logger, config Route53Config, awsAccountId string) *Route53Exporter {

level.Info(logger).Log("msg", "Initializing Route53 exporter")
accountIdLabel := map[string]string{"aws_account_id": awsAccountId}

exporter := &Route53Exporter{
sess: sess,
RecordsPerHostedZoneQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_recordsperhostedzone_quota"), "Quota for maximum number of records in a Route53 hosted zone", []string{"hostedzoneid", "hostedzonename"}, nil),
RecordsPerHostedZoneUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_recordsperhostedzone_total"), "Number of Resource records", []string{"hostedzoneid", "hostedzonename"}, nil),
HostedZonesPerAccountQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_hostedzonesperaccount_quota"), "Quota for maximum number of Route53 hosted zones in an account", []string{}, nil),
HostedZonesPerAccountUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_hostedzonesperaccount_total"), "Number of Resource records", []string{}, nil),
LastUpdateTime: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_last_updated_timestamp_seconds"), "Last time, the route53 metrics were sucessfully updated", []string{}, nil),
RecordsPerHostedZoneQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_recordsperhostedzone_quota"), "Quota for maximum number of records in a Route53 hosted zone", []string{"hostedzoneid", "hostedzonename"}, accountIdLabel),
RecordsPerHostedZoneUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_recordsperhostedzone_total"), "Number of Resource records", []string{"hostedzoneid", "hostedzonename"}, accountIdLabel),
HostedZonesPerAccountQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_hostedzonesperaccount_quota"), "Quota for maximum number of Route53 hosted zones in an account", []string{}, accountIdLabel),
HostedZonesPerAccountUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_hostedzonesperaccount_total"), "Number of Resource records", []string{}, accountIdLabel),
LastUpdateTime: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "route53_last_updated_timestamp_seconds"), "Last time, the route53 metrics were sucessfully updated", []string{}, accountIdLabel),
cache: *pkg.NewMetricsCache(*config.CacheTTL),
logger: logger,
interval: *config.Interval,
Expand Down
29 changes: 16 additions & 13 deletions vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
)

type VPCExporter struct {
awsAccountId string
sessions []*session.Session
VpcsPerRegionQuota *prometheus.Desc
VpcsPerRegionUsage *prometheus.Desc
Expand Down Expand Up @@ -54,22 +55,24 @@ type VPCCollector struct {
wg *sync.WaitGroup
}

func NewVPCExporter(sess []*session.Session, logger log.Logger, config VPCConfig) *VPCExporter {
func NewVPCExporter(sess []*session.Session, logger log.Logger, config VPCConfig, awsAccountId string) *VPCExporter {
level.Info(logger).Log("msg", "Initializing VPC exporter")
accountIdLabel := map[string]string{"aws_account_id": awsAccountId}
return &VPCExporter{
awsAccountId: awsAccountId,
sessions: sess,
VpcsPerRegionQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_vpcsperregion_quota"), "The quota of VPCs per region", []string{"aws_region"}, nil),
VpcsPerRegionUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_vpcsperregion_usage"), "The usage of VPCs per region", []string{"aws_region"}, nil),
SubnetsPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_subnetspervpc_quota"), "The quota of subnets per VPC", []string{"aws_region"}, nil),
SubnetsPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_subnetspervpc_usage"), "The usage of subnets per VPC", []string{"aws_region", "vpcid"}, nil),
RoutesPerRouteTableQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routesperroutetable_quota"), "The quota of routes per routetable", []string{"aws_region"}, nil),
RoutesPerRouteTableUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routesperroutetable_usage"), "The usage of routes per routetable", []string{"aws_region", "vpcid", "routetableid"}, nil),
InterfaceVpcEndpointsPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_interfacevpcendpointspervpc_quota"), "The quota of interface vpc endpoints per vpc", []string{"aws_region"}, nil),
InterfaceVpcEndpointsPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_interfacevpcendpointspervpc_usage"), "The usage of interface vpc endpoints per vpc", []string{"aws_region", "vpcid"}, nil),
RouteTablesPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routetablespervpc_quota"), "The quota of route tables per vpc", []string{"aws_region"}, nil),
RouteTablesPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routetablespervpc_usage"), "The usage of route tables per vpc", []string{"aws_region", "vpcid"}, nil),
IPv4BlocksPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_ipv4blockspervpc_quota"), "The quota of ipv4 blocks per vpc", []string{"aws_region"}, nil),
IPv4BlocksPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_ipv4blockspervpc_usage"), "The usage of ipv4 blocks per vpc", []string{"aws_region", "vpcid"}, nil),
VpcsPerRegionQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_vpcsperregion_quota"), "The quota of VPCs per region", []string{"aws_region"}, accountIdLabel),
VpcsPerRegionUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_vpcsperregion_usage"), "The usage of VPCs per region", []string{"aws_region"}, accountIdLabel),
SubnetsPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_subnetspervpc_quota"), "The quota of subnets per VPC", []string{"aws_region"}, accountIdLabel),
SubnetsPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_subnetspervpc_usage"), "The usage of subnets per VPC", []string{"aws_region", "vpcid"}, accountIdLabel),
RoutesPerRouteTableQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routesperroutetable_quota"), "The quota of routes per routetable", []string{"aws_region"}, accountIdLabel),
RoutesPerRouteTableUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routesperroutetable_usage"), "The usage of routes per routetable", []string{"aws_region", "vpcid", "routetableid"}, accountIdLabel),
InterfaceVpcEndpointsPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_interfacevpcendpointspervpc_quota"), "The quota of interface vpc endpoints per vpc", []string{"aws_region"}, accountIdLabel),
InterfaceVpcEndpointsPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_interfacevpcendpointspervpc_usage"), "The usage of interface vpc endpoints per vpc", []string{"aws_region", "vpcid"}, accountIdLabel),
RouteTablesPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routetablespervpc_quota"), "The quota of route tables per vpc", []string{"aws_region"}, accountIdLabel),
RouteTablesPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_routetablespervpc_usage"), "The usage of route tables per vpc", []string{"aws_region", "vpcid"}, accountIdLabel),
IPv4BlocksPerVpcQuota: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_ipv4blockspervpc_quota"), "The quota of ipv4 blocks per vpc", []string{"aws_region"}, accountIdLabel),
IPv4BlocksPerVpcUsage: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "vpc_ipv4blockspervpc_usage"), "The usage of ipv4 blocks per vpc", []string{"aws_region", "vpcid"}, accountIdLabel),
logger: logger,
timeout: *config.Timeout,
cache: *pkg.NewMetricsCache(*config.CacheTTL),
Expand Down

0 comments on commit a5a7ed6

Please sign in to comment.