Skip to content

Commit c869f1b

Browse files
[aws/cwlogs] Add component type in user agent header for CWL PLE requests (#29595)
**Description:** Add the component name as a field in outgoing CloudWatch logs put log event requests. This change allows us to distinguish the source of requests between the AWS EMF exporter and AWS CloudWatch logs exporter. **Testing:** Update unit tests
1 parent 4302c5b commit c869f1b

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

.chloggen/cwlogs_useragent.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: awsemfexporter/awscloudwatchlogsexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add component name to user agent header for outgoing put log even requests
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [29595]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

exporter/awscloudwatchlogsexporter/exporter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"go.opentelemetry.io/collector/pdata/plog"
2323
"go.uber.org/zap"
2424

25+
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter/internal/metadata"
2526
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
2627
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs"
2728
)
@@ -60,7 +61,7 @@ func newCwLogsPusher(expConfig *Config, params exp.CreateSettings) (*cwlExporter
6061
}
6162

6263
// create CWLogs client with aws session config
63-
svcStructuredLog := cwlogs.NewClient(params.Logger, awsConfig, params.BuildInfo, expConfig.LogGroupName, expConfig.LogRetention, expConfig.Tags, session)
64+
svcStructuredLog := cwlogs.NewClient(params.Logger, awsConfig, params.BuildInfo, expConfig.LogGroupName, expConfig.LogRetention, expConfig.Tags, session, metadata.Type)
6465
collectorIdentifier, err := uuid.NewRandom()
6566

6667
if err != nil {

exporter/awsemfexporter/emf_exporter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.opentelemetry.io/collector/pdata/pmetric"
1919
"go.uber.org/zap"
2020

21+
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter/internal/metadata"
2122
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
2223
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs"
2324
)
@@ -55,7 +56,7 @@ func newEmfExporter(config *Config, set exporter.CreateSettings) (*emfExporter,
5556
}
5657

5758
// create CWLogs client with aws session config
58-
svcStructuredLog := cwlogs.NewClient(set.Logger, awsConfig, set.BuildInfo, config.LogGroupName, config.LogRetention, config.Tags, session)
59+
svcStructuredLog := cwlogs.NewClient(set.Logger, awsConfig, set.BuildInfo, config.LogGroupName, config.LogRetention, config.Tags, session, metadata.Type)
5960
collectorIdentifier, err := uuid.NewRandom()
6061

6162
if err != nil {

internal/aws/cwlogs/cwlog_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ func newCloudWatchLogClient(svc cloudwatchlogsiface.CloudWatchLogsAPI, logRetent
4545
}
4646

4747
// NewClient create Client
48-
func NewClient(logger *zap.Logger, awsConfig *aws.Config, buildInfo component.BuildInfo, logGroupName string, logRetention int64, tags map[string]*string, sess *session.Session) *Client {
48+
func NewClient(logger *zap.Logger, awsConfig *aws.Config, buildInfo component.BuildInfo, logGroupName string, logRetention int64, tags map[string]*string, sess *session.Session, componentName string) *Client {
4949
client := cloudwatchlogs.New(sess, awsConfig)
5050
client.Handlers.Build.PushBackNamed(handler.RequestStructuredLogHandler)
51-
client.Handlers.Build.PushFrontNamed(newCollectorUserAgentHandler(buildInfo, logGroupName))
51+
client.Handlers.Build.PushFrontNamed(newCollectorUserAgentHandler(buildInfo, logGroupName, componentName))
5252
return newCloudWatchLogClient(client, logRetention, tags, logger)
5353
}
5454

@@ -175,10 +175,10 @@ func (client *Client) CreateStream(logGroup, streamName *string) error {
175175
return nil
176176
}
177177

178-
func newCollectorUserAgentHandler(buildInfo component.BuildInfo, logGroupName string) request.NamedHandler {
179-
fn := request.MakeAddToUserAgentHandler(buildInfo.Command, buildInfo.Version)
178+
func newCollectorUserAgentHandler(buildInfo component.BuildInfo, logGroupName string, componentName string) request.NamedHandler {
179+
fn := request.MakeAddToUserAgentHandler(buildInfo.Command, buildInfo.Version, componentName)
180180
if matchContainerInsightsPattern(logGroupName) {
181-
fn = request.MakeAddToUserAgentHandler(buildInfo.Command, buildInfo.Version, "ContainerInsights")
181+
fn = request.MakeAddToUserAgentHandler(buildInfo.Command, buildInfo.Version, componentName, "ContainerInsights")
182182
}
183183
return request.NamedHandler{
184184
Name: "otel.collector.UserAgentHandler",

internal/aws/cwlogs/cwlog_client_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ func TestLogUnknownError(t *testing.T) {
533533

534534
func TestUserAgent(t *testing.T) {
535535
logger := zap.NewNop()
536-
536+
expectedComponentName := "mockComponentName"
537537
tests := []struct {
538538
name string
539539
buildInfo component.BuildInfo
@@ -544,44 +544,44 @@ func TestUserAgent(t *testing.T) {
544544
"emptyLogGroup",
545545
component.BuildInfo{Command: "opentelemetry-collector-contrib", Version: "1.0"},
546546
"",
547-
"opentelemetry-collector-contrib/1.0",
547+
fmt.Sprintf("opentelemetry-collector-contrib/1.0 (%s)", expectedComponentName),
548548
},
549549
{
550550
"buildInfoCommandUsed",
551551
component.BuildInfo{Command: "test-collector-contrib", Version: "1.0"},
552552
"",
553-
"test-collector-contrib/1.0",
553+
fmt.Sprintf("test-collector-contrib/1.0 (%s)", expectedComponentName),
554554
},
555555
{
556556
"non container insights",
557557
component.BuildInfo{Command: "opentelemetry-collector-contrib", Version: "1.1"},
558558
"test-group",
559-
"opentelemetry-collector-contrib/1.1",
559+
fmt.Sprintf("opentelemetry-collector-contrib/1.1 (%s)", expectedComponentName),
560560
},
561561
{
562562
"container insights EKS",
563563
component.BuildInfo{Command: "opentelemetry-collector-contrib", Version: "1.0"},
564564
"/aws/containerinsights/eks-cluster-name/performance",
565-
"opentelemetry-collector-contrib/1.0 (ContainerInsights)",
565+
fmt.Sprintf("opentelemetry-collector-contrib/1.0 (%s; ContainerInsights)", expectedComponentName),
566566
},
567567
{
568568
"container insights ECS",
569569
component.BuildInfo{Command: "opentelemetry-collector-contrib", Version: "1.0"},
570570
"/aws/ecs/containerinsights/ecs-cluster-name/performance",
571-
"opentelemetry-collector-contrib/1.0 (ContainerInsights)",
571+
fmt.Sprintf("opentelemetry-collector-contrib/1.0 (%s; ContainerInsights)", expectedComponentName),
572572
},
573573
{
574574
"container insights prometheus",
575575
component.BuildInfo{Command: "opentelemetry-collector-contrib", Version: "1.0"},
576576
"/aws/containerinsights/cluster-name/prometheus",
577-
"opentelemetry-collector-contrib/1.0 (ContainerInsights)",
577+
fmt.Sprintf("opentelemetry-collector-contrib/1.0 (%s; ContainerInsights)", expectedComponentName),
578578
},
579579
}
580580

581-
session, _ := session.NewSession()
581+
testSession, _ := session.NewSession()
582582
for _, tc := range tests {
583583
t.Run(tc.name, func(t *testing.T) {
584-
cwlog := NewClient(logger, &aws.Config{}, tc.buildInfo, tc.logGroupName, 0, map[string]*string{}, session)
584+
cwlog := NewClient(logger, &aws.Config{}, tc.buildInfo, tc.logGroupName, 0, map[string]*string{}, testSession, expectedComponentName)
585585
logClient := cwlog.svc.(*cloudwatchlogs.CloudWatchLogs)
586586

587587
req := request.New(aws.Config{}, metadata.ClientInfo{}, logClient.Handlers, nil, &request.Operation{

0 commit comments

Comments
 (0)