Skip to content

Commit 2aa6cfc

Browse files
committed
merge upstream
2 parents 8e2e167 + ed8df93 commit 2aa6cfc

File tree

16 files changed

+420
-62
lines changed

16 files changed

+420
-62
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2525

2626
- name: Setup .NET SDK
27-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
27+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
2828
with:
2929
dotnet-version: |
3030
6.0.x

.github/workflows/docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
steps:
6060
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
6161
- name: Setup .NET 8.0
62-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
62+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
6363
with:
6464
dotnet-version: '8.x'
6565

.github/workflows/e2e-tests.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ jobs:
4141
mask-aws-account-id: true
4242

4343
- name: Set up .NET
44-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
44+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
4545
with:
4646
dotnet-version: '8.x'
4747

4848
- name: Setup Node.js
49-
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
49+
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
5050
with:
5151
node-version: "22"
5252

@@ -85,12 +85,12 @@ jobs:
8585
mask-aws-account-id: true
8686

8787
- name: Set up .NET
88-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
88+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
8989
with:
9090
dotnet-version: '8.x'
9191

9292
- name: Setup Node.js
93-
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
93+
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
9494
with:
9595
node-version: "22"
9696

@@ -126,7 +126,7 @@ jobs:
126126
mask-aws-account-id: true
127127

128128
- name: Set up .NET
129-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
129+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
130130
with:
131131
dotnet-version: '8.x'
132132

@@ -158,7 +158,7 @@ jobs:
158158
mask-aws-account-id: true
159159

160160
- name: Setup Node.js
161-
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
161+
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
162162
with:
163163
node-version: "22"
164164

@@ -199,7 +199,7 @@ jobs:
199199
mask-aws-account-id: true
200200

201201
- name: Setup Node.js
202-
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
202+
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
203203
with:
204204
node-version: "22"
205205

.github/workflows/examples-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2525

2626
- name: Setup .NET SDK
27-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
27+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
2828
with:
2929
dotnet-version: |
3030
6.0.x

.github/workflows/publish-artifacts-examples-tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3131

3232
- name: Setup .NET
33-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
33+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
3434
with:
3535
dotnet-version: |
3636
6.0.x
@@ -61,7 +61,7 @@ jobs:
6161
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
6262

6363
- name: Set up .NET
64-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
64+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
6565
with:
6666
dotnet-version: |
6767
6.0.x
@@ -120,7 +120,7 @@ jobs:
120120
path: ./packages/
121121

122122
- name: Setup .NET
123-
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0
123+
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # 4.3.1
124124
with:
125125
dotnet-version: |
126126
6.0.x

libraries/src/AWS.Lambda.Powertools.Common/Core/Constants.cs

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ internal static class Constants
2222
{
2323
internal const string AWSInitializationTypeEnv = "AWS_LAMBDA_INITIALIZATION_TYPE";
2424

25+
/// <summary>
26+
/// Constant for AWS_LAMBDA_INITIALIZATION_TYPE environment variable
27+
/// This is used to determine if the Lambda function is running in provisioned concurrency mode
28+
/// or not. If the value is "provisioned-concurrency", it indicates that the function is running in provisioned
29+
/// concurrency mode. Otherwise, it is running in standard mode.
30+
/// </summary>
31+
internal const string AWSInitializationTypeEnv = "AWS_LAMBDA_INITIALIZATION_TYPE";
32+
2533
/// <summary>
2634
/// Constant for POWERTOOLS_SERVICE_NAME environment variable
2735
/// </summary>

libraries/src/AWS.Lambda.Powertools.Common/Core/ISystemWrapper.cs

-11
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,4 @@ public interface ISystemWrapper
5959
/// </summary>
6060
/// <param name="type"></param>
6161
void SetExecutionEnvironment<T>(T type);
62-
63-
/// <summary>
64-
/// Sets console output
65-
/// Useful for testing and checking the console output
66-
/// <code>
67-
/// var consoleOut = new StringWriter();
68-
/// SystemWrapper.Instance.SetOut(consoleOut);
69-
/// </code>
70-
/// </summary>
71-
/// <param name="writeTo">The TextWriter instance where to write to</param>
72-
void SetOut(TextWriter writeTo);
7362
}

libraries/src/AWS.Lambda.Powertools.Common/Core/LambdaLifecycleTracker.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static class LambdaLifecycleTracker
1515
private static readonly AsyncLocal<bool?> CurrentInvocationColdStart = new AsyncLocal<bool?>();
1616

1717
private static string _lambdaInitType;
18-
private static string LambdaInitType => _lambdaInitType ?? Environment.GetEnvironmentVariable("AWS_LAMBDA_INITIALIZATION_TYPE");
18+
private static string LambdaInitType => _lambdaInitType ?? Environment.GetEnvironmentVariable(Constants.AWSInitializationTypeEnv);
1919

2020
/// <summary>
2121
/// Returns true if the current Lambda invocation is a cold start
@@ -53,9 +53,13 @@ public static bool IsColdStart
5353
/// <summary>
5454
/// Resets the cold start state for testing
5555
/// </summary>
56-
internal static void Reset()
56+
/// <param name="resetContainer">Whether to reset the container state (defaults to true)</param>
57+
internal static void Reset(bool resetContainer = true)
5758
{
58-
_isFirstContainer = true;
59+
if (resetContainer)
60+
{
61+
_isFirstContainer = true;
62+
}
5963
CurrentInvocationColdStart.Value = null;
6064
_lambdaInitType = null;
6165
}

libraries/src/AWS.Lambda.Powertools.Common/Core/SystemWrapper.cs

+68-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace AWS.Lambda.Powertools.Common;
2727
public class SystemWrapper : ISystemWrapper
2828
{
2929
private static IPowertoolsEnvironment _powertoolsEnvironment;
30+
private static bool _inTestMode = false;
31+
private static TextWriter _testOutputStream;
32+
private static bool _outputResetPerformed = false;
3033

3134
/// <summary>
3235
/// The instance
@@ -41,13 +44,11 @@ public SystemWrapper(IPowertoolsEnvironment powertoolsEnvironment)
4144
_powertoolsEnvironment = powertoolsEnvironment;
4245
_instance ??= this;
4346

44-
// Clear AWS SDK Console injected parameters StdOut and StdErr
45-
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
46-
standardOutput.AutoFlush = true;
47-
Console.SetOut(standardOutput);
48-
var errordOutput = new StreamWriter(Console.OpenStandardError());
49-
errordOutput.AutoFlush = true;
50-
Console.SetError(errordOutput);
47+
if (!_inTestMode)
48+
{
49+
// Clear AWS SDK Console injected parameters in production only
50+
ResetConsoleOutput();
51+
}
5152
}
5253

5354
/// <summary>
@@ -72,7 +73,15 @@ public string GetEnvironmentVariable(string variable)
7273
/// <param name="value">The value.</param>
7374
public void Log(string value)
7475
{
75-
Console.Write(value);
76+
if (_inTestMode && _testOutputStream != null)
77+
{
78+
_testOutputStream.Write(value);
79+
}
80+
else
81+
{
82+
EnsureConsoleOutputOnce();
83+
Console.Write(value);
84+
}
7685
}
7786

7887
/// <summary>
@@ -81,7 +90,15 @@ public void Log(string value)
8190
/// <param name="value">The value.</param>
8291
public void LogLine(string value)
8392
{
84-
Console.WriteLine(value);
93+
if (_inTestMode && _testOutputStream != null)
94+
{
95+
_testOutputStream.WriteLine(value);
96+
}
97+
else
98+
{
99+
EnsureConsoleOutputOnce();
100+
Console.WriteLine(value);
101+
}
85102
}
86103

87104
/// <summary>
@@ -126,9 +143,20 @@ public void SetExecutionEnvironment<T>(T type)
126143
SetEnvironmentVariable(envName, envValue.ToString());
127144
}
128145

129-
/// <inheritdoc />
130-
public void SetOut(TextWriter writeTo)
146+
/// <summary>
147+
/// Sets console output
148+
/// Useful for testing and checking the console output
149+
/// <code>
150+
/// var consoleOut = new StringWriter();
151+
/// SystemWrapper.Instance.SetOut(consoleOut);
152+
/// </code>
153+
/// </summary>
154+
/// <param name="writeTo">The TextWriter instance where to write to</param>
155+
156+
public static void SetOut(TextWriter writeTo)
131157
{
158+
_testOutputStream = writeTo;
159+
_inTestMode = true;
132160
Console.SetOut(writeTo);
133161
}
134162

@@ -152,4 +180,33 @@ private string ParseAssemblyName(string assemblyName)
152180

153181
return $"{Constants.FeatureContextIdentifier}/{assemblyName}";
154182
}
183+
184+
private static void EnsureConsoleOutputOnce()
185+
{
186+
if (_outputResetPerformed) return;
187+
ResetConsoleOutput();
188+
_outputResetPerformed = true;
189+
}
190+
191+
private static void ResetConsoleOutput()
192+
{
193+
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
194+
standardOutput.AutoFlush = true;
195+
Console.SetOut(standardOutput);
196+
var errorOutput = new StreamWriter(Console.OpenStandardError());
197+
errorOutput.AutoFlush = true;
198+
Console.SetError(errorOutput);
199+
}
200+
201+
public static void ClearOutputResetFlag()
202+
{
203+
_outputResetPerformed = false;
204+
}
205+
206+
// For test cleanup
207+
internal static void ResetTestMode()
208+
{
209+
_inTestMode = false;
210+
_testOutputStream = null;
211+
}
155212
}

libraries/src/AWS.Lambda.Powertools.Common/InternalsVisibleTo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Logging")]
1919
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Metrics")]
20+
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Tracing")]
2021
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Idempotency")]
2122
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Common.Tests")]
2223
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Tracing.Tests")]

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

+3-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Amazon.Lambda.Core;
2121
using AspectInjector.Broker;
2222
using AWS.Lambda.Powertools.Common;
23+
using AWS.Lambda.Powertools.Common.Core;
2324

2425
namespace AWS.Lambda.Powertools.Metrics;
2526

@@ -30,22 +31,12 @@ namespace AWS.Lambda.Powertools.Metrics;
3031
[Aspect(Scope.Global)]
3132
public class MetricsAspect
3233
{
33-
/// <summary>
34-
/// The is cold start
35-
/// </summary>
36-
private static bool _isColdStart;
37-
3834
/// <summary>
3935
/// Gets the metrics instance.
4036
/// </summary>
4137
/// <value>The metrics instance.</value>
4238
private static IMetrics _metricsInstance;
4339

44-
static MetricsAspect()
45-
{
46-
_isColdStart = true;
47-
}
48-
4940
/// <summary>
5041
/// Runs before the execution of the method marked with the Metrics Attribute
5142
/// </summary>
@@ -89,10 +80,9 @@ public void Before(
8980
Triggers = triggers
9081
};
9182

92-
if (_isColdStart)
83+
if (LambdaLifecycleTracker.IsColdStart)
9384
{
9485
_metricsInstance.CaptureColdStartMetric(GetContext(eventArgs));
95-
_isColdStart = false;
9686
}
9787
}
9888

@@ -112,7 +102,7 @@ public void Exit()
112102
internal static void ResetForTest()
113103
{
114104
_metricsInstance = null;
115-
_isColdStart = true;
105+
LambdaLifecycleTracker.Reset();
116106
Metrics.ResetForTest();
117107
}
118108

libraries/src/AWS.Lambda.Powertools.Tracing/Internal/TracingAspect.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Threading.Tasks;
2121
using AspectInjector.Broker;
2222
using AWS.Lambda.Powertools.Common;
23+
using AWS.Lambda.Powertools.Common.Core;
2324
using AWS.Lambda.Powertools.Common.Utils;
2425

2526
namespace AWS.Lambda.Powertools.Tracing.Internal;
@@ -41,11 +42,6 @@ public class TracingAspect
4142
/// </summary>
4243
private readonly IXRayRecorder _xRayRecorder;
4344

44-
/// <summary>
45-
/// If true, then is cold start
46-
/// </summary>
47-
private static bool _isColdStart = true;
48-
4945
/// <summary>
5046
/// If true, capture annotations
5147
/// </summary>
@@ -148,16 +144,14 @@ private void BeginSegment(string segmentName, string @namespace)
148144

149145
if (_captureAnnotations)
150146
{
151-
_xRayRecorder.AddAnnotation("ColdStart", _isColdStart);
147+
_xRayRecorder.AddAnnotation("ColdStart", LambdaLifecycleTracker.IsColdStart);
152148

153149
_captureAnnotations = false;
154150
_isAnnotationsCaptured = true;
155151

156152
if (_powertoolsConfigurations.IsServiceDefined)
157153
_xRayRecorder.AddAnnotation("Service", _powertoolsConfigurations.Service);
158154
}
159-
160-
_isColdStart = false;
161155
}
162156

163157
private void HandleResponse(string name, object result, TracingCaptureMode captureMode, string @namespace)
@@ -253,7 +247,7 @@ private bool CaptureError(TracingCaptureMode captureMode)
253247

254248
internal static void ResetForTest()
255249
{
256-
_isColdStart = true;
250+
LambdaLifecycleTracker.Reset();
257251
_captureAnnotations = true;
258252
}
259253
}

0 commit comments

Comments
 (0)