Skip to content

Commit 69ceb8c

Browse files
authored
Update dependency version of AWSSDK.CloudWatchLogs to 4.0.13.3 from 4.0.8.4. (#346)
Also added new integ tests to verify computed user agent header.
1 parent 313d4e9 commit 69ceb8c

File tree

7 files changed

+134
-28
lines changed

7 files changed

+134
-28
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "AWS.Logger.Core",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Add the property PreconfiguredServiceClient to AWSLoggerConfig to make it easier to test special scenarios",
8+
"Update dependency version of AWSSDK.CloudWatchLogs to 4.0.13.3 from 4.0.8.4"
9+
]
10+
}
11+
]
12+
}

samples/AspNetCore/ConsoleSample/ConsoleSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="AWSSDK.Core" Version="4.0.0.32" />
17+
<PackageReference Include="AWSSDK.Core" Version="4.0.3.6" />
1818
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
1919
</ItemGroup>
2020

samples/Log4net/BasicAWSCredentialsConfigurationExample/BasicAWSCredentialsConfigurationExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="AWSSDK.Core" Version="4.0.0.32" />
11+
<PackageReference Include="AWSSDK.Core" Version="4.0.3.6" />
1212
<PackageReference Include="log4net" Version="2.0.15" />
1313
</ItemGroup>
1414

src/AWS.Logger.Core/AWS.Logger.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</ItemGroup>
4242

4343
<ItemGroup>
44-
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="4.0.8.4" />
44+
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="4.0.13.3" />
4545
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
4646
</ItemGroup>
4747

src/AWS.Logger.Core/AWSLoggerConfig.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
2+
using Amazon.CloudWatchLogs;
33
using Amazon.Runtime;
44
namespace AWS.Logger
55
{
@@ -215,5 +215,11 @@ public AWSLoggerConfig(string logGroup)
215215
/// change it only if the region cannot be determined from the service endpoint.
216216
/// </summary>
217217
public string AuthenticationRegion { get; set; }
218+
219+
/// <summary>
220+
/// An IAmazonCloudWatchLogs that has already been configured to connect to Amazon CloudWatch Logs. This property
221+
/// is general used for testing purposes testing special scenarios.
222+
/// </summary>
223+
public IAmazonCloudWatchLogs PreconfiguredServiceClient { get; set; }
218224
}
219225
}

src/AWS.Logger.Core/Core/AWSLoggerCore.cs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,39 +95,57 @@ public AWSLoggerCore(AWSLoggerConfig config, string logType)
9595
_config = config;
9696
_logType = logType;
9797

98-
var awsConfig = new AmazonCloudWatchLogsConfig();
99-
if (!string.IsNullOrWhiteSpace(_config.ServiceUrl))
98+
if (config.PreconfiguredServiceClient == null)
10099
{
101-
var serviceUrl = _config.ServiceUrl.Trim();
102-
awsConfig.ServiceURL = serviceUrl;
103-
if (serviceUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
100+
var awsConfig = new AmazonCloudWatchLogsConfig();
101+
if (!string.IsNullOrWhiteSpace(_config.ServiceUrl))
104102
{
105-
awsConfig.UseHttp = true;
103+
var serviceUrl = _config.ServiceUrl.Trim();
104+
awsConfig.ServiceURL = serviceUrl;
105+
if (serviceUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
106+
{
107+
awsConfig.UseHttp = true;
108+
}
106109
}
107-
}
108-
else
109-
{
110-
if (!string.IsNullOrEmpty(_config.Region))
110+
else
111111
{
112-
awsConfig.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(_config.Region);
112+
if (!string.IsNullOrEmpty(_config.Region))
113+
{
114+
awsConfig.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(_config.Region);
115+
}
113116
}
114-
}
115117

116-
if (!string.IsNullOrEmpty(_config.AuthenticationRegion))
117-
{
118-
awsConfig.AuthenticationRegion = _config.AuthenticationRegion;
119-
}
118+
if (!string.IsNullOrEmpty(_config.AuthenticationRegion))
119+
{
120+
awsConfig.AuthenticationRegion = _config.AuthenticationRegion;
121+
}
122+
123+
_client = new Lazy<IAmazonCloudWatchLogs>(() =>
124+
{
125+
var credentials = DetermineCredentials(config);
126+
var client = new AmazonCloudWatchLogsClient(credentials, awsConfig);
127+
128+
client.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
129+
client.ExceptionEvent += ServiceClientExceptionEvent;
120130

121-
_client = new Lazy<IAmazonCloudWatchLogs>(() =>
131+
return client;
132+
});
133+
}
134+
else
122135
{
123-
var credentials = DetermineCredentials(config);
124-
var client = new AmazonCloudWatchLogsClient(credentials, awsConfig);
136+
var preconfiguredClient = config.PreconfiguredServiceClient;
137+
if (preconfiguredClient is AmazonCloudWatchLogsClient preconfiguredClientImpl)
138+
{
139+
preconfiguredClientImpl.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
140+
preconfiguredClientImpl.ExceptionEvent += ServiceClientExceptionEvent;
141+
}
125142

126-
client.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
127-
client.ExceptionEvent += ServiceClientExceptionEvent;
128143

129-
return client;
130-
});
144+
_client = new Lazy<IAmazonCloudWatchLogs>(() =>
145+
{
146+
return preconfiguredClient;
147+
});
148+
}
131149

132150
StartMonitor();
133151
RegisterShutdownHook();
@@ -681,7 +699,7 @@ void ServiceClientBeforeRequestEvent(object sender, RequestEventArgs e)
681699
if (args != null && args.Request is Amazon.Runtime.Internal.IAmazonWebServiceRequest internalRequest && !internalRequest.UserAgentDetails.GetCustomUserAgentComponents().Contains(userAgentString))
682700
{
683701
internalRequest.UserAgentDetails.AddUserAgentComponent(userAgentString);
684-
}
702+
}
685703
}
686704

687705
void ServiceClientExceptionEvent(object sender, ExceptionEventArgs e)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Amazon.CloudWatchLogs;
2+
using Amazon.CloudWatchLogs.Model;
3+
using Amazon.Runtime;
4+
using AWS.Logger.Core;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
8+
using Xunit;
9+
10+
namespace AWS.Logger.AspNetCore.Tests
11+
{
12+
public class TestUserAgent
13+
{
14+
[Fact]
15+
public async Task VerifyUserAgent()
16+
{
17+
var client = new AmazonCloudWatchLogsClient();
18+
19+
var userAgentCaptured = new List<string>();
20+
client.AfterResponseEvent += (sender, args) =>
21+
{
22+
var we = args as WebServiceResponseEventArgs;
23+
if (we == null || we.Response is not PutLogEventsResponse)
24+
return;
25+
26+
var userAgent = we.Response.ResponseMetadata.Metadata["User-Agent"];
27+
userAgentCaptured.Add(userAgent);
28+
};
29+
30+
var core = new AWSLoggerCore(new AWSLoggerConfig
31+
{
32+
LogGroup = "/aws/logging-tests/verify-useragent",
33+
PreconfiguredServiceClient = client
34+
}, "aws-logger-aspnetcore#0.0.0.0");
35+
36+
core.AddMessage("Test message for User-Agent verification");
37+
core.Flush();
38+
39+
var start = DateTime.UtcNow;
40+
while (DateTime.UtcNow < start.AddSeconds(10))
41+
{
42+
if (userAgentCaptured.Count == 1)
43+
break;
44+
45+
await Task.Delay(100);
46+
}
47+
48+
Assert.Single(userAgentCaptured);
49+
Assert.Contains("ft/aws-logger-aspnetcore#0.0.0.0", userAgentCaptured[0]);
50+
51+
core.AddMessage("Test message for User-Agent verification");
52+
core.Flush();
53+
54+
start = DateTime.UtcNow;
55+
while (DateTime.UtcNow < start.AddSeconds(10))
56+
{
57+
if (userAgentCaptured.Count == 2)
58+
break;
59+
60+
await Task.Delay(100);
61+
}
62+
63+
Assert.Equal(2, userAgentCaptured.Count);
64+
// The user agent should not change between calls. This is verify the bug fix https://github.com/aws/aws-logging-dotnet/issues/340
65+
// where the user agent string kept growing with each call. Logging library was more susceptible this because it reuses the same underlying
66+
// PutLogEventsRequest instance for each push to send logs.
67+
Assert.Equal(userAgentCaptured[0], userAgentCaptured[1]);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)