-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Transit gateway metrics and ec2 exporter (#46)
* implement Transit gateway metrics and ec2 exporter * Re-enable other exporters * Replace context.TODO so timeouts are respected * * add missing log statement * update docs * change name of transitgateway metric to be in line with others
- Loading branch information
Showing
4 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/aws/aws-sdk-go/service/servicequotas" | ||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const ( | ||
transitGatewayPerAccountQuotaCode string = "L-A2478D36" | ||
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) | ||
|
||
type EC2Exporter struct { | ||
sessions []*session.Session | ||
|
||
logger log.Logger | ||
timeout time.Duration | ||
} | ||
|
||
func NewEC2Exporter(sessions []*session.Session, logger log.Logger, timeout time.Duration) *EC2Exporter { | ||
|
||
level.Info(logger).Log("msg", "Initializing EC2 exporter") | ||
return &EC2Exporter{ | ||
sessions: sessions, | ||
|
||
logger: logger, | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
func (e *EC2Exporter) Collect(ch chan<- prometheus.Metric) { | ||
ctx, ctxCancel := context.WithTimeout(context.Background(), e.timeout) | ||
defer ctxCancel() | ||
wg := &sync.WaitGroup{} | ||
wg.Add(len(e.sessions)) | ||
|
||
for _, sess := range e.sessions { | ||
go collectInRegion(sess, e.logger, wg, ch, ctx) | ||
} | ||
wg.Wait() | ||
} | ||
|
||
func collectInRegion(sess *session.Session, logger log.Logger, wg *sync.WaitGroup, ch chan<- prometheus.Metric, ctx context.Context) { | ||
defer wg.Done() | ||
ec2Svc := ec2.New(sess) | ||
serviceQuotaSvc := servicequotas.New(sess) | ||
|
||
quota, err := getQuotaValueWithContext(serviceQuotaSvc, ec2ServiceCode, transitGatewayPerAccountQuotaCode, ctx) | ||
if err != nil { | ||
level.Error(logger).Log("msg", "Could not retrieve Transit Gateway quota", "error", err.Error()) | ||
exporterMetrics.IncrementErrors() | ||
return | ||
} | ||
|
||
gateways, err := getAllTransitGatewaysWithContext(ec2Svc, ctx) | ||
if err != nil { | ||
level.Error(logger).Log("msg", "Could not retrieve Transit Gateway quota", "error", err.Error()) | ||
exporterMetrics.IncrementErrors() | ||
return | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric(TransitGatewaysUsage, prometheus.GaugeValue, float64(len(gateways)), *sess.Config.Region) | ||
ch <- prometheus.MustNewConstMetric(TransitGatewaysQuota, prometheus.GaugeValue, quota, *sess.Config.Region) | ||
|
||
} | ||
|
||
func (e *EC2Exporter) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- TransitGatewaysQuota | ||
ch <- TransitGatewaysUsage | ||
} | ||
|
||
func getAllTransitGatewaysWithContext(client *ec2.EC2, ctx context.Context) ([]*ec2.TransitGateway, error) { | ||
results := []*ec2.TransitGateway{} | ||
describeGatewaysInput := &ec2.DescribeTransitGatewaysInput{ | ||
DryRun: aws.Bool(false), | ||
MaxResults: aws.Int64(1000), | ||
} | ||
|
||
describeGatewaysOutput, err := client.DescribeTransitGatewaysWithContext(ctx, describeGatewaysInput) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
results = append(results, describeGatewaysOutput.TransitGateways...) | ||
|
||
for describeGatewaysOutput.NextToken != nil { | ||
describeGatewaysInput.SetNextToken(*describeGatewaysOutput.NextToken) | ||
describeGatewaysOutput, err := client.DescribeTransitGatewaysWithContext(ctx, describeGatewaysInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
results = append(results, describeGatewaysOutput.TransitGateways...) | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func getQuotaValueWithContext(client *servicequotas.ServiceQuotas, serviceCode string, quotaCode string, ctx context.Context) (float64, error) { | ||
sqOutput, err := client.GetServiceQuotaWithContext(ctx, &servicequotas.GetServiceQuotaInput{ | ||
QuotaCode: aws.String(quotaCode), | ||
ServiceCode: aws.String(serviceCode), | ||
}) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return *sqOutput.Quota.Value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters