-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): update metrics to version 2.0.0, enhance cold start tr…
…acking, and improve documentation
- Loading branch information
Showing
33 changed files
with
929 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
libraries/src/AWS.Lambda.Powertools.Common/Core/ConsoleWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace AWS.Lambda.Powertools.Common; | ||
|
||
/// <inheritdoc /> | ||
public class ConsoleWrapper : IConsoleWrapper | ||
{ | ||
/// <inheritdoc /> | ||
public void WriteLine(string message) => Console.WriteLine(message); | ||
/// <inheritdoc /> | ||
public void Debug(string message) => System.Diagnostics.Debug.WriteLine(message); | ||
/// <inheritdoc /> | ||
public void Error(string message) => Console.Error.WriteLine(message); | ||
/// <inheritdoc /> | ||
public string ReadLine() => Console.ReadLine(); | ||
} |
46 changes: 46 additions & 0 deletions
46
libraries/src/AWS.Lambda.Powertools.Common/Core/IConsoleWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
namespace AWS.Lambda.Powertools.Common; | ||
|
||
/// <summary> | ||
/// Wrapper for console operations to facilitate testing by abstracting system console interactions. | ||
/// </summary> | ||
public interface IConsoleWrapper | ||
{ | ||
/// <summary> | ||
/// Writes the specified message followed by a line terminator to the standard output stream. | ||
/// </summary> | ||
/// <param name="message">The message to write.</param> | ||
void WriteLine(string message); | ||
|
||
/// <summary> | ||
/// Writes a debug message to the trace listeners in the Debug.Listeners collection. | ||
/// </summary> | ||
/// <param name="message">The debug message to write.</param> | ||
void Debug(string message); | ||
|
||
/// <summary> | ||
/// Writes the specified error message followed by a line terminator to the standard error stream. | ||
/// </summary> | ||
/// <param name="message">The error message to write.</param> | ||
void Error(string message); | ||
|
||
/// <summary> | ||
/// Reads the next line of characters from the standard input stream. | ||
/// </summary> | ||
/// <returns>The next line of characters from the input stream, or null if no more lines are available.</returns> | ||
string ReadLine(); | ||
} |
76 changes: 76 additions & 0 deletions
76
libraries/src/AWS.Lambda.Powertools.Metrics.AspNetCore/Http/ColdStartTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.Lambda.Core; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace AWS.Lambda.Powertools.Metrics.AspNetCore.Http; | ||
|
||
|
||
/// <summary> | ||
/// Tracks and manages cold start metrics for Lambda functions in ASP.NET Core applications. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class is responsible for detecting and recording the first invocation (cold start) of a Lambda function. | ||
/// It ensures thread-safe tracking of cold starts and proper metric capture using the provided IMetrics implementation. | ||
/// </remarks> | ||
internal class ColdStartTracker : IDisposable | ||
{ | ||
private readonly IMetrics _metrics; | ||
private static bool _coldStart = true; | ||
private static readonly object _lock = new(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ColdStartTracker"/> class. | ||
/// </summary> | ||
/// <param name="metrics">The metrics implementation to use for capturing cold start metrics.</param> | ||
public ColdStartTracker(IMetrics metrics) | ||
{ | ||
_metrics = metrics; | ||
} | ||
|
||
/// <summary> | ||
/// Tracks the cold start of the Lambda function. | ||
/// </summary> | ||
/// <param name="context">The current HTTP context.</param> | ||
internal void TrackColdStart(HttpContext context) | ||
{ | ||
if (!_coldStart) return; | ||
|
||
lock (_lock) | ||
{ | ||
if (!_coldStart) return; | ||
_metrics.CaptureColdStartMetric(context.Items["LambdaContext"] as ILambdaContext); | ||
_coldStart = false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Resets the cold start tracking state. | ||
/// </summary> | ||
internal static void ResetColdStart() | ||
{ | ||
lock (_lock) | ||
{ | ||
_coldStart = true; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
ResetColdStart(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.