Skip to content

Commit

Permalink
Merge pull request #428 from jyotimahapatra/ec2
Browse files Browse the repository at this point in the history
emit metric for EC2 describeInstance calls
  • Loading branch information
k8s-ci-robot committed Feb 11, 2022
2 parents 3cd7299 + ef33d8d commit 9eed6f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/ec2provider/ec2provider.go
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/sirupsen/logrus"
"sigs.k8s.io/aws-iam-authenticator/pkg"
"sigs.k8s.io/aws-iam-authenticator/pkg/httputil"
"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
)

const (
Expand Down Expand Up @@ -193,6 +194,7 @@ func (p *ec2ProviderImpl) GetPrivateDNSName(id string) (string, error) {
}

logrus.Infof("Calling ec2:DescribeInstances for the InstanceId = %s ", id)
metrics.Get().EC2DescribeInstanceCallCount.Inc()
// Look up instance from EC2 API
output, err := p.ec2.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice([]string{id}),
Expand Down Expand Up @@ -254,6 +256,7 @@ func (p *ec2ProviderImpl) StartEc2DescribeBatchProcessing() {
func (p *ec2ProviderImpl) getPrivateDnsAndPublishToCache(instanceIdList []string) {
// Look up instance from EC2 API
logrus.Infof("Making Batch Query to DescribeInstances for %v instances ", len(instanceIdList))
metrics.Get().EC2DescribeInstanceCallCount.Inc()
output, err := p.ec2.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice(instanceIdList),
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/ec2provider/ec2provider_test.go
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
)

const (
Expand Down Expand Up @@ -61,6 +63,7 @@ func newMockedEC2ProviderImpl() *ec2ProviderImpl {
}

func TestGetPrivateDNSName(t *testing.T) {
metrics.InitMetrics(prometheus.NewRegistry())
ec2Provider := newMockedEC2ProviderImpl()
ec2Provider.ec2 = &mockEc2Client{Reservations: prepareSingleInstanceOutput()}
go ec2Provider.StartEc2DescribeBatchProcessing()
Expand Down Expand Up @@ -92,6 +95,7 @@ func prepareSingleInstanceOutput() []*ec2.Reservation {
}

func TestGetPrivateDNSNameWithBatching(t *testing.T) {
metrics.InitMetrics(prometheus.NewRegistry())
ec2Provider := newMockedEC2ProviderImpl()
reservations := prepare100InstanceOutput()
ec2Provider.ec2 = &mockEc2Client{Reservations: reservations}
Expand Down
12 changes: 10 additions & 2 deletions pkg/metrics/metrics.go
Expand Up @@ -26,8 +26,9 @@ func Get() Metrics {

// Metrics are handles to the collectors for prometheus for the various metrics we are tracking.
type Metrics struct {
ConfigMapWatchFailures prometheus.Counter
Latency *prometheus.HistogramVec
ConfigMapWatchFailures prometheus.Counter
Latency *prometheus.HistogramVec
EC2DescribeInstanceCallCount prometheus.Counter
}

func CreateMetrics(reg prometheus.Registerer) Metrics {
Expand All @@ -49,5 +50,12 @@ func CreateMetrics(reg prometheus.Registerer) Metrics {
},
[]string{"result"},
),
EC2DescribeInstanceCallCount: factory.NewCounter(
prometheus.CounterOpts{
Namespace: Namespace,
Name: "ec2_describe_instances_calls_total",
Help: "Number of EC2 describe instances calls.",
},
),
}
}

0 comments on commit 9eed6f9

Please sign in to comment.