From 0dcb0f040ebd8c167e62cc4d54efd9ce42eb5fb4 Mon Sep 17 00:00:00 2001 From: Charles Cheng Date: Thu, 12 Dec 2024 22:11:32 +0000 Subject: [PATCH] Add support for latency metrics --- .../ecs-agent/acs/session/session.go | 1 - .../ecs-agent/api/ecs/client/ecs_client.go | 12 ++++++------ .../ecs-agent/api/ecs/client/ecs_client_option.go | 10 ++++++++++ .../amazon-ecs-agent/ecs-agent/api/ecs/interface.go | 4 ---- ecs-agent/acs/session/session.go | 1 - ecs-agent/api/ecs/client/ecs_client.go | 12 ++++++------ ecs-agent/api/ecs/client/ecs_client_option.go | 10 ++++++++++ ecs-agent/api/ecs/client/ecs_client_option_test.go | 8 ++++++++ ecs-agent/api/ecs/interface.go | 4 ---- 9 files changed, 40 insertions(+), 22 deletions(-) diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/session.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/session.go index 4cc8dc7c922..fb82c9a60e0 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/session.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/acs/session/session.go @@ -251,7 +251,6 @@ func (s *session) startSessionOnce(ctx context.Context) error { }) return err } - s.metricsFactory.New(metrics.DiscoverPollEndpointDurationName).WithGauge(s.ecsClient.GetDiscoverPollEndpointDuration()).Done(nil) client := s.clientFactory.New( s.acsURL(acsEndpoint), diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client.go index eee36f04a89..9a789f0c470 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client.go @@ -39,6 +39,7 @@ import ( "github.com/aws/amazon-ecs-agent/ecs-agent/httpclient" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" "github.com/aws/amazon-ecs-agent/ecs-agent/utils" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" ) @@ -77,7 +78,7 @@ type ecsClient struct { shouldExcludeIPv6PortBinding bool sascCustomRetryBackoff func(func() error) error stscAttachmentCustomRetryBackoff func(func() error) error - discoverPollEndpointDuration time.Duration + metricsFactory metrics.EntryFactory } // NewECSClient creates a new ECSClient interface object. @@ -113,6 +114,9 @@ func NewECSClient( if client.submitStateChangeClient == nil { client.submitStateChangeClient = newSubmitStateChangeClient(&ecsConfig) } + if client.metricsFactory == nil { + client.metricsFactory = metrics.NewNopEntryFactory() + } return client, nil } @@ -778,7 +782,7 @@ func (client *ecsClient) discoverPollEndpoint(containerInstanceArn string, return nil, err } - client.discoverPollEndpointDuration = time.Since(discoverPollEndpointStartTime) + client.metricsFactory.New(metrics.DiscoverPollEndpointDurationName).WithGauge(time.Since(discoverPollEndpointStartTime)).Done(nil) // Cache the response from ECS. client.pollEndpointCache.Set(containerInstanceArn, output) return output, nil @@ -872,7 +876,3 @@ func trimString(inputString string, maxLen int) string { return inputString } } - -func (client *ecsClient) GetDiscoverPollEndpointDuration() time.Duration { - return client.discoverPollEndpointDuration -} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_option.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_option.go index ee9ad5c8686..14ac8382b0a 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_option.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client_option.go @@ -16,6 +16,7 @@ package ecsclient import ( "github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs" "github.com/aws/amazon-ecs-agent/ecs-agent/async" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" ) // ECSClientOption allows for configuration of an ecsClient. @@ -87,3 +88,12 @@ func WithSubmitStateChangeClient(s ecs.ECSSubmitStateSDK) ECSClientOption { client.submitStateChangeClient = s } } + +// WithMetricsFactory is an ECSClientOption that configures +// ecsClient.metricsFactory with the value passed as a parameter. +// This is especially useful for emitting metrics in the ECS Client +func WithMetricsFactory(metricsFactory metrics.EntryFactory) ECSClientOption { + return func(client *ecsClient) { + client.metricsFactory = metricsFactory + } +} diff --git a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/interface.go b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/interface.go index 6c962dc2b90..fb3b3fab53f 100644 --- a/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/interface.go +++ b/agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/interface.go @@ -14,8 +14,6 @@ package ecs import ( - "time" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/request" @@ -64,8 +62,6 @@ type ECSClient interface { UpdateContainerInstancesState(instanceARN, status string) error // GetHostResources retrieves a map that map the resource name to the corresponding resource GetHostResources() (map[string]*ecs.Resource, error) - // GetDiscoverPollEndpointDuration retrieves the time it takes for the DiscoverPollEndpoint call - GetDiscoverPollEndpointDuration() time.Duration } // ECSSDK is an interface that specifies the subset of the AWS Go SDK's ECS diff --git a/ecs-agent/acs/session/session.go b/ecs-agent/acs/session/session.go index 4cc8dc7c922..fb82c9a60e0 100644 --- a/ecs-agent/acs/session/session.go +++ b/ecs-agent/acs/session/session.go @@ -251,7 +251,6 @@ func (s *session) startSessionOnce(ctx context.Context) error { }) return err } - s.metricsFactory.New(metrics.DiscoverPollEndpointDurationName).WithGauge(s.ecsClient.GetDiscoverPollEndpointDuration()).Done(nil) client := s.clientFactory.New( s.acsURL(acsEndpoint), diff --git a/ecs-agent/api/ecs/client/ecs_client.go b/ecs-agent/api/ecs/client/ecs_client.go index eee36f04a89..9a789f0c470 100644 --- a/ecs-agent/api/ecs/client/ecs_client.go +++ b/ecs-agent/api/ecs/client/ecs_client.go @@ -39,6 +39,7 @@ import ( "github.com/aws/amazon-ecs-agent/ecs-agent/httpclient" "github.com/aws/amazon-ecs-agent/ecs-agent/logger" "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" "github.com/aws/amazon-ecs-agent/ecs-agent/utils" "github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" ) @@ -77,7 +78,7 @@ type ecsClient struct { shouldExcludeIPv6PortBinding bool sascCustomRetryBackoff func(func() error) error stscAttachmentCustomRetryBackoff func(func() error) error - discoverPollEndpointDuration time.Duration + metricsFactory metrics.EntryFactory } // NewECSClient creates a new ECSClient interface object. @@ -113,6 +114,9 @@ func NewECSClient( if client.submitStateChangeClient == nil { client.submitStateChangeClient = newSubmitStateChangeClient(&ecsConfig) } + if client.metricsFactory == nil { + client.metricsFactory = metrics.NewNopEntryFactory() + } return client, nil } @@ -778,7 +782,7 @@ func (client *ecsClient) discoverPollEndpoint(containerInstanceArn string, return nil, err } - client.discoverPollEndpointDuration = time.Since(discoverPollEndpointStartTime) + client.metricsFactory.New(metrics.DiscoverPollEndpointDurationName).WithGauge(time.Since(discoverPollEndpointStartTime)).Done(nil) // Cache the response from ECS. client.pollEndpointCache.Set(containerInstanceArn, output) return output, nil @@ -872,7 +876,3 @@ func trimString(inputString string, maxLen int) string { return inputString } } - -func (client *ecsClient) GetDiscoverPollEndpointDuration() time.Duration { - return client.discoverPollEndpointDuration -} diff --git a/ecs-agent/api/ecs/client/ecs_client_option.go b/ecs-agent/api/ecs/client/ecs_client_option.go index ee9ad5c8686..14ac8382b0a 100644 --- a/ecs-agent/api/ecs/client/ecs_client_option.go +++ b/ecs-agent/api/ecs/client/ecs_client_option.go @@ -16,6 +16,7 @@ package ecsclient import ( "github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs" "github.com/aws/amazon-ecs-agent/ecs-agent/async" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" ) // ECSClientOption allows for configuration of an ecsClient. @@ -87,3 +88,12 @@ func WithSubmitStateChangeClient(s ecs.ECSSubmitStateSDK) ECSClientOption { client.submitStateChangeClient = s } } + +// WithMetricsFactory is an ECSClientOption that configures +// ecsClient.metricsFactory with the value passed as a parameter. +// This is especially useful for emitting metrics in the ECS Client +func WithMetricsFactory(metricsFactory metrics.EntryFactory) ECSClientOption { + return func(client *ecsClient) { + client.metricsFactory = metricsFactory + } +} diff --git a/ecs-agent/api/ecs/client/ecs_client_option_test.go b/ecs-agent/api/ecs/client/ecs_client_option_test.go index 390a2bcd2e8..23d476e8bd6 100644 --- a/ecs-agent/api/ecs/client/ecs_client_option_test.go +++ b/ecs-agent/api/ecs/client/ecs_client_option_test.go @@ -22,6 +22,7 @@ import ( mock_ecs "github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/mocks" "github.com/aws/amazon-ecs-agent/ecs-agent/async" + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" "github.com/stretchr/testify/assert" ) @@ -102,3 +103,10 @@ func TestWithSubmitStateChangeClient(t *testing.T) { option(client) assert.Equal(t, newSubmitStateChangeClient, client.submitStateChangeClient) } + +func TestWithMetricsFactory(t *testing.T) { + client := &ecsClient{} // client.metricsFactory is nil by default + option := WithMetricsFactory(metrics.NewNopEntryFactory()) + option(client) + assert.NotNil(t, client.metricsFactory) +} diff --git a/ecs-agent/api/ecs/interface.go b/ecs-agent/api/ecs/interface.go index 6c962dc2b90..fb3b3fab53f 100644 --- a/ecs-agent/api/ecs/interface.go +++ b/ecs-agent/api/ecs/interface.go @@ -14,8 +14,6 @@ package ecs import ( - "time" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/request" @@ -64,8 +62,6 @@ type ECSClient interface { UpdateContainerInstancesState(instanceARN, status string) error // GetHostResources retrieves a map that map the resource name to the corresponding resource GetHostResources() (map[string]*ecs.Resource, error) - // GetDiscoverPollEndpointDuration retrieves the time it takes for the DiscoverPollEndpoint call - GetDiscoverPollEndpointDuration() time.Duration } // ECSSDK is an interface that specifies the subset of the AWS Go SDK's ECS