Skip to content

Commit

Permalink
Add a feature flag in UserAgent for AWS backend to monitor the adopti…
Browse files Browse the repository at this point in the history
…ons (#5178)
  • Loading branch information
mxiamxia authored Oct 1, 2021
1 parent 5118335 commit 9d6efde
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
19 changes: 15 additions & 4 deletions exporter/awsemfexporter/cwlog_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package awsemfexporter

import (
"fmt"
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
62 changes: 50 additions & 12 deletions exporter/awsemfexporter/cwlog_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
2 changes: 1 addition & 1 deletion exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9d6efde

Please sign in to comment.