From 9d6efde0d96e83e164b89d0777281925359a3e4c Mon Sep 17 00:00:00 2001 From: Min Xia Date: Fri, 1 Oct 2021 11:32:25 -0700 Subject: [PATCH] Add a feature flag in UserAgent for AWS backend to monitor the adoptions (#5178) --- exporter/awsemfexporter/cwlog_client.go | 19 ++++-- exporter/awsemfexporter/cwlog_client_test.go | 62 ++++++++++++++++---- exporter/awsemfexporter/emf_exporter.go | 2 +- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/exporter/awsemfexporter/cwlog_client.go b/exporter/awsemfexporter/cwlog_client.go index 353ba8cb4455..4dc0699ffc74 100644 --- a/exporter/awsemfexporter/cwlog_client.go +++ b/exporter/awsemfexporter/cwlog_client.go @@ -16,6 +16,7 @@ package awsemfexporter import ( "fmt" + "regexp" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -52,10 +53,10 @@ func newCloudWatchLogClient(svc cloudwatchlogsiface.CloudWatchLogsAPI, logger *z } // newCloudWatchLogsClient create cloudWatchLogClient -func newCloudWatchLogsClient(logger *zap.Logger, awsConfig *aws.Config, buildInfo component.BuildInfo, sess *session.Session) *cloudWatchLogClient { +func newCloudWatchLogsClient(logger *zap.Logger, awsConfig *aws.Config, buildInfo component.BuildInfo, logGroupName string, sess *session.Session) *cloudWatchLogClient { client := cloudwatchlogs.New(sess, awsConfig) client.Handlers.Build.PushBackNamed(handler.RequestStructuredLogHandler) - client.Handlers.Build.PushFrontNamed(newCollectorUserAgentHandler(buildInfo)) + client.Handlers.Build.PushFrontNamed(newCollectorUserAgentHandler(buildInfo, logGroupName)) return newCloudWatchLogClient(client, logger) } @@ -175,9 +176,19 @@ func (client *cloudWatchLogClient) CreateStream(logGroup, streamName *string) (t return "", nil } -func newCollectorUserAgentHandler(buildInfo component.BuildInfo) request.NamedHandler { +func newCollectorUserAgentHandler(buildInfo component.BuildInfo, logGroupName string) request.NamedHandler { + fn := request.MakeAddToUserAgentHandler(collectorDistribution, buildInfo.Version) + if matchContainerInsightsPattern(logGroupName) { + fn = request.MakeAddToUserAgentHandler(collectorDistribution, buildInfo.Version, "ContainerInsights") + } return request.NamedHandler{ Name: "otel.collector.UserAgentHandler", - Fn: request.MakeAddToUserAgentHandler(collectorDistribution, buildInfo.Version), + Fn: fn, } } + +func matchContainerInsightsPattern(logGroupName string) bool { + regexP := "^/aws/.*containerinsights/.*/(performance|prometheus)$" + r, _ := regexp.Compile(regexP) + return r.MatchString(logGroupName) +} diff --git a/exporter/awsemfexporter/cwlog_client_test.go b/exporter/awsemfexporter/cwlog_client_test.go index 7bbdf05d76ed..5b73fe8dcc87 100644 --- a/exporter/awsemfexporter/cwlog_client_test.go +++ b/exporter/awsemfexporter/cwlog_client_test.go @@ -442,19 +442,57 @@ func TestLogUnknownError(t *testing.T) { func TestUserAgent(t *testing.T) { logger := zap.NewNop() - buildInfo := component.BuildInfo{ - Version: "1.0", + tests := []struct { + name string + buildInfo component.BuildInfo + logGroupName string + expectedUserAgentStr string + }{ + { + "emptyLogGroup", + component.BuildInfo{Version: "1.0"}, + "", + "opentelemetry-collector-contrib/1.0", + }, + { + "non container insights", + component.BuildInfo{Version: "1.1"}, + "test-group", + "opentelemetry-collector-contrib/1.1", + }, + { + "container insights EKS", + component.BuildInfo{Version: "1.0"}, + "/aws/containerinsights/eks-cluster-name/performance", + "opentelemetry-collector-contrib/1.0 (ContainerInsights)", + }, + { + "container insights ECS", + component.BuildInfo{Version: "1.0"}, + "/aws/ecs/containerinsights/ecs-cluster-name/performance", + "opentelemetry-collector-contrib/1.0 (ContainerInsights)", + }, + { + "container insights prometheus", + component.BuildInfo{Version: "1.0"}, + "/aws/containerinsights/cluster-name/prometheus", + "opentelemetry-collector-contrib/1.0 (ContainerInsights)", + }, } session, _ := session.NewSession() - cwlog := newCloudWatchLogsClient(logger, &aws.Config{}, buildInfo, session) - logClient := cwlog.svc.(*cloudwatchlogs.CloudWatchLogs) - - req := request.New(aws.Config{}, metadata.ClientInfo{}, logClient.Handlers, nil, &request.Operation{ - HTTPMethod: "GET", - HTTPPath: "/", - }, nil, nil) - - logClient.Handlers.Build.Run(req) - assert.Contains(t, req.HTTPRequest.UserAgent(), "opentelemetry-collector-contrib/1.0") + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cwlog := newCloudWatchLogsClient(logger, &aws.Config{}, tc.buildInfo, tc.logGroupName, session) + logClient := cwlog.svc.(*cloudwatchlogs.CloudWatchLogs) + + req := request.New(aws.Config{}, metadata.ClientInfo{}, logClient.Handlers, nil, &request.Operation{ + HTTPMethod: "GET", + HTTPPath: "/", + }, nil, nil) + + logClient.Handlers.Build.Run(req) + assert.Contains(t, req.HTTPRequest.UserAgent(), tc.expectedUserAgentStr) + }) + } } diff --git a/exporter/awsemfexporter/emf_exporter.go b/exporter/awsemfexporter/emf_exporter.go index bc60c448426f..5cc20a06c5fc 100644 --- a/exporter/awsemfexporter/emf_exporter.go +++ b/exporter/awsemfexporter/emf_exporter.go @@ -76,7 +76,7 @@ func newEmfPusher( } // create CWLogs client with aws session config - svcStructuredLog := newCloudWatchLogsClient(logger, awsConfig, params.BuildInfo, session) + svcStructuredLog := newCloudWatchLogsClient(logger, awsConfig, params.BuildInfo, expConfig.LogGroupName, session) collectorIdentifier, _ := uuid.NewRandom() expConfig.Validate()