Skip to content

Commit 5e099f6

Browse files
authored
Merge pull request #208 from amirkaws/amirkaws-cold-start-capture-warning-bug-fix
fix: ColdStart capture warning
2 parents f608f8b + 9188eef commit 5e099f6

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

examples/Metrics/src/HelloWorld/Function.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public Function(IDynamoDBContext dynamoDbContext, HttpClient httpClient)
7474
/// <returns>API Gateway Proxy response</returns>
7575
[Logging(LogEvent = true)]
7676
[Metrics(CaptureColdStart = true)]
77-
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
78-
ILambdaContext context)
77+
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
7978
{
8079
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
8180

libraries/src/AWS.Lambda.Powertools.Metrics/Internal/MetricsAspectHandler.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ public void OnExit(AspectEventArgs eventArgs)
7878
/// <param name="eventArgs">Aspect Arguments</param>
7979
public void OnEntry(AspectEventArgs eventArgs)
8080
{
81-
if (!_isColdStart || !_captureColdStartEnabled) return;
82-
81+
if (!_isColdStart)
82+
return;
83+
84+
_isColdStart = false;
85+
86+
if (!_captureColdStartEnabled)
87+
return;
88+
8389
var nameSpace = _metrics.GetNamespace();
8490
var service = _metrics.GetService();
8591
Dictionary<string, string> dimensions = null;
@@ -102,8 +108,6 @@ public void OnEntry(AspectEventArgs eventArgs)
102108
service,
103109
dimensions
104110
);
105-
106-
_isColdStart = false;
107111
}
108112

109113
/// <summary>

libraries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class Metrics : IMetrics
4545
/// If true, Powertools will throw an exception on empty metrics when trying to flush
4646
/// </summary>
4747
private readonly bool _raiseOnEmptyMetrics;
48+
49+
/// <summary>
50+
/// The capture cold start enabled
51+
/// </summary>
52+
private readonly bool _captureColdStartEnabled;
4853

4954
/// <summary>
5055
/// Creates a Metrics object that provides features to send metrics to Amazon Cloudwatch using the Embedded metric
@@ -55,14 +60,16 @@ public class Metrics : IMetrics
5560
/// <param name="nameSpace">Metrics Namespace Identifier</param>
5661
/// <param name="service">Metrics Service Name</param>
5762
/// <param name="raiseOnEmptyMetrics">Instructs metrics validation to throw exception if no metrics are provided</param>
63+
/// <param name="captureColdStartEnabled">Instructs metrics capturing the ColdStart is enabled</param>
5864
internal Metrics(IPowertoolsConfigurations powertoolsConfigurations, string nameSpace = null, string service = null,
59-
bool raiseOnEmptyMetrics = false)
65+
bool raiseOnEmptyMetrics = false, bool captureColdStartEnabled = false)
6066
{
6167
if (_instance != null) return;
6268

6369
_instance = this;
6470
_powertoolsConfigurations = powertoolsConfigurations;
6571
_raiseOnEmptyMetrics = raiseOnEmptyMetrics;
72+
_captureColdStartEnabled = captureColdStartEnabled;
6673
_context = InitializeContext(nameSpace, service, null);
6774
}
6875

@@ -80,7 +87,7 @@ void IMetrics.AddMetric(string key, double value, MetricUnit unit)
8087
{
8188
if (string.IsNullOrWhiteSpace(key))
8289
throw new ArgumentNullException(
83-
"'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.");
90+
$"'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.");
8491

8592
if (value < 0) {
8693
throw new ArgumentException(
@@ -132,7 +139,7 @@ void IMetrics.AddDimension(string key, string value)
132139
{
133140
if (string.IsNullOrWhiteSpace(key))
134141
throw new ArgumentNullException(
135-
"'AddDimension' method requires a valid dimension key. 'Null' or empty values are not allowed.");
142+
$"'AddDimension' method requires a valid dimension key. 'Null' or empty values are not allowed.");
136143

137144
_context.AddDimension(key, value);
138145
}
@@ -150,7 +157,7 @@ void IMetrics.AddMetadata(string key, object value)
150157
{
151158
if (string.IsNullOrWhiteSpace(key))
152159
throw new ArgumentNullException(
153-
"'AddMetadata' method requires a valid metadata key. 'Null' or empty values are not allowed.");
160+
$"'AddMetadata' method requires a valid metadata key. 'Null' or empty values are not allowed.");
154161

155162
_context.AddMetadata(key, value);
156163
}
@@ -168,7 +175,7 @@ void IMetrics.SetDefaultDimensions(Dictionary<string, string> defaultDimensions)
168175
foreach (var item in defaultDimensions)
169176
if (string.IsNullOrWhiteSpace(item.Key) || string.IsNullOrWhiteSpace(item.Value))
170177
throw new ArgumentNullException(
171-
"'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty values are not allowed.");
178+
$"'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty values are not allowed.");
172179

173180
_context.SetDefaultDimensions(DictionaryToList(defaultDimensions));
174181
}
@@ -197,8 +204,9 @@ void IMetrics.Flush(bool metricsOverflow)
197204
}
198205
else
199206
{
200-
Console.WriteLine(
201-
"##WARNING## Metrics and Metadata have not been specified. No data will be sent to Cloudwatch Metrics.");
207+
if (!_captureColdStartEnabled)
208+
Console.WriteLine(
209+
"##WARNING## Metrics and Metadata have not been specified. No data will be sent to Cloudwatch Metrics.");
202210
}
203211
}
204212

@@ -230,7 +238,7 @@ void IMetrics.PushSingleMetric(string metricName, double value, MetricUnit unit,
230238
{
231239
if (string.IsNullOrWhiteSpace(metricName))
232240
throw new ArgumentNullException(
233-
"'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.");
241+
$"'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.");
234242

235243
using var context = InitializeContext(nameSpace, service, defaultDimensions);
236244
context.AddMetric(metricName, value, unit);

libraries/src/AWS.Lambda.Powertools.Metrics/MetricsAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public class MetricsAttribute : MethodAspectAttribute
139139
PowertoolsConfigurations.Instance,
140140
Namespace,
141141
Service,
142-
RaiseOnEmptyMetrics
142+
RaiseOnEmptyMetrics,
143+
CaptureColdStart
143144
);
144145

145146
/// <summary>

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/EMFValidationTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ public void WhenCaptureColdStart_CreateSeparateBlob()
3434
// Arrange
3535
var methodName = Guid.NewGuid().ToString();
3636
var consoleOut = new StringWriter();
37+
const bool captureColdStartEnabled = true;
3738
Console.SetOut(consoleOut);
3839

3940
var configurations = new Mock<IPowertoolsConfigurations>();
4041

4142
var logger = new Metrics(
4243
configurations.Object,
4344
nameSpace: "dotnet-powertools-test",
44-
service: "testService"
45+
service: "testService",
46+
captureColdStartEnabled: captureColdStartEnabled
4547
);
4648

4749
var handler = new MetricsAspectHandler(
4850
logger,
49-
true
51+
captureColdStartEnabled
5052
);
5153

5254
var eventArgs = new AspectEventArgs { Name = methodName };
@@ -74,19 +76,21 @@ public void WhenCaptureColdStartEnabled_ValidateExists()
7476
// Arrange
7577
var methodName = Guid.NewGuid().ToString();
7678
var consoleOut = new StringWriter();
79+
const bool captureColdStartEnabled = true;
7780
Console.SetOut(consoleOut);
7881

7982
var configurations = new Mock<IPowertoolsConfigurations>();
8083

8184
var logger = new Metrics(
8285
configurations.Object,
8386
nameSpace: "dotnet-powertools-test",
84-
service: "testService"
87+
service: "testService",
88+
captureColdStartEnabled: captureColdStartEnabled
8589
);
8690

8791
var handler = new MetricsAspectHandler(
8892
logger,
89-
true
93+
captureColdStartEnabled
9094
);
9195

9296
var eventArgs = new AspectEventArgs { Name = methodName };

0 commit comments

Comments
 (0)