Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emit metric for EC2 describeInstance calls #428

Merged
merged 1 commit into from Feb 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.",
},
),
}
}