Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ColdStart capture warning #208

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/Metrics/src/HelloWorld/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public Function(IDynamoDBContext dynamoDbContext, HttpClient httpClient)
/// <returns>API Gateway Proxy response</returns>
[Logging(LogEvent = true)]
[Metrics(CaptureColdStart = true)]
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
ILambdaContext context)
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
{
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ public void OnExit(AspectEventArgs eventArgs)
/// <param name="eventArgs">Aspect Arguments</param>
public void OnEntry(AspectEventArgs eventArgs)
{
if (!_isColdStart || !_captureColdStartEnabled) return;

if (!_isColdStart)
return;

_isColdStart = false;

if (!_captureColdStartEnabled)
return;

var nameSpace = _metrics.GetNamespace();
var service = _metrics.GetService();
Dictionary<string, string> dimensions = null;
Expand All @@ -102,8 +108,6 @@ public void OnEntry(AspectEventArgs eventArgs)
service,
dimensions
);

_isColdStart = false;
}

/// <summary>
Expand Down
24 changes: 16 additions & 8 deletions libraries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class Metrics : IMetrics
/// If true, Powertools will throw an exception on empty metrics when trying to flush
/// </summary>
private readonly bool _raiseOnEmptyMetrics;

/// <summary>
/// The capture cold start enabled
/// </summary>
private readonly bool _captureColdStartEnabled;

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

_instance = this;
_powertoolsConfigurations = powertoolsConfigurations;
_raiseOnEmptyMetrics = raiseOnEmptyMetrics;
_captureColdStartEnabled = captureColdStartEnabled;
_context = InitializeContext(nameSpace, service, null);
}

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

if (value < 0) {
throw new ArgumentException(
Expand Down Expand Up @@ -132,7 +139,7 @@ void IMetrics.AddDimension(string key, string value)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(
"'AddDimension' method requires a valid dimension key. 'Null' or empty values are not allowed.");
$"'AddDimension' method requires a valid dimension key. 'Null' or empty values are not allowed.");

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

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

_context.SetDefaultDimensions(DictionaryToList(defaultDimensions));
}
Expand Down Expand Up @@ -197,8 +204,9 @@ void IMetrics.Flush(bool metricsOverflow)
}
else
{
Console.WriteLine(
"##WARNING## Metrics and Metadata have not been specified. No data will be sent to Cloudwatch Metrics.");
if (!_captureColdStartEnabled)
Console.WriteLine(
"##WARNING## Metrics and Metadata have not been specified. No data will be sent to Cloudwatch Metrics.");
}
}

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

using var context = InitializeContext(nameSpace, service, defaultDimensions);
context.AddMetric(metricName, value, unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public class MetricsAttribute : MethodAspectAttribute
PowertoolsConfigurations.Instance,
Namespace,
Service,
RaiseOnEmptyMetrics
RaiseOnEmptyMetrics,
CaptureColdStart
);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ public void WhenCaptureColdStart_CreateSeparateBlob()
// Arrange
var methodName = Guid.NewGuid().ToString();
var consoleOut = new StringWriter();
const bool captureColdStartEnabled = true;
Console.SetOut(consoleOut);

var configurations = new Mock<IPowertoolsConfigurations>();

var logger = new Metrics(
configurations.Object,
nameSpace: "dotnet-powertools-test",
service: "testService"
service: "testService",
captureColdStartEnabled: captureColdStartEnabled
);

var handler = new MetricsAspectHandler(
logger,
true
captureColdStartEnabled
);

var eventArgs = new AspectEventArgs { Name = methodName };
Expand Down Expand Up @@ -74,19 +76,21 @@ public void WhenCaptureColdStartEnabled_ValidateExists()
// Arrange
var methodName = Guid.NewGuid().ToString();
var consoleOut = new StringWriter();
const bool captureColdStartEnabled = true;
Console.SetOut(consoleOut);

var configurations = new Mock<IPowertoolsConfigurations>();

var logger = new Metrics(
configurations.Object,
nameSpace: "dotnet-powertools-test",
service: "testService"
service: "testService",
captureColdStartEnabled: captureColdStartEnabled
);

var handler = new MetricsAspectHandler(
logger,
true
captureColdStartEnabled
);

var eventArgs = new AspectEventArgs { Name = methodName };
Expand Down