Skip to content
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
15 changes: 15 additions & 0 deletions libraries/AWS.Lambda.Powertools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.Kafka
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.ModuleInitializer.Tests", "tests\AWS.Lambda.Powertools.ModuleInitializer.Tests\AWS.Lambda.Powertools.ModuleInitializer.Tests.csproj", "{E1F2A3B4-C5D6-7E8F-9A0B-1C2D3E4F5A6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.ConcurrencyTests", "tests\AWS.Lambda.Powertools.ConcurrencyTests\AWS.Lambda.Powertools.ConcurrencyTests.csproj", "{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}"
EndProject

Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -704,6 +706,18 @@ Global
{E1F2A3B4-C5D6-7E8F-9A0B-1C2D3E4F5A6B}.Release|x86.ActiveCfg = Release|Any CPU
{E1F2A3B4-C5D6-7E8F-9A0B-1C2D3E4F5A6B}.Release|x86.Build.0 = Release|Any CPU

{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Debug|x64.ActiveCfg = Debug|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Debug|x64.Build.0 = Debug|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Debug|x86.ActiveCfg = Debug|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Debug|x86.Build.0 = Debug|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Release|Any CPU.Build.0 = Release|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Release|x64.ActiveCfg = Release|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Release|x64.Build.0 = Release|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Release|x86.ActiveCfg = Release|Any CPU
{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection

GlobalSection(NestedProjects) = preSolution
Expand Down Expand Up @@ -764,5 +778,6 @@ Global
{B640DB80-C982-407B-A2EC-CD29AC77DDB8} = {73C9B1E5-3893-47E8-B373-17E5F5D7E6F5}
{E1F2A3B4-C5D6-7E8F-9A0B-1C2D3E4F5A6B} = {1CFF5568-8486-475F-81F6-06105C437528}

{D2951A1A-D0EF-4CA4-AB4D-5ABAEFD164F5} = {1CFF5568-8486-475F-81F6-06105C437528}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Common.Core;

namespace AWS.Lambda.Powertools.Common;
Expand Down Expand Up @@ -167,7 +168,7 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
/// </summary>
/// <value>The X-Ray trace identifier.</value>
public string XRayTraceId =>
GetEnvironmentVariable(Constants.XrayTraceIdEnv);
LambdaTraceProvider.CurrentTraceId;

/// <summary>
/// Gets a value indicating whether this instance is Lambda.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Logging.Tests")]
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Logging.Tests")]
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.ConcurrencyTests")]
31 changes: 26 additions & 5 deletions libraries/src/AWS.Lambda.Powertools.Logging/Logger.Scope.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using AWS.Lambda.Powertools.Logging.Internal;
using AWS.Lambda.Powertools.Logging.Internal.Helpers;

Expand All @@ -9,10 +11,25 @@ namespace AWS.Lambda.Powertools.Logging;
public static partial class Logger
{
/// <summary>
/// Gets the scope.
/// Thread-safe dictionary for per-thread scope storage.
/// Uses ManagedThreadId as key to ensure isolation when Lambda processes
/// multiple concurrent requests (AWS_LAMBDA_MAX_CONCURRENCY > 1).
/// </summary>
private static readonly ConcurrentDictionary<int, Dictionary<string, object>> _threadScopes = new();

/// <summary>
/// Gets the scope for the current thread.
/// Creates a new dictionary if one doesn't exist for this thread.
/// </summary>
/// <value>The scope.</value>
private static IDictionary<string, object> Scope { get; } = new Dictionary<string, object>(StringComparer.Ordinal);
private static IDictionary<string, object> Scope
{
get
{
var threadId = Environment.CurrentManagedThreadId;
return _threadScopes.GetOrAdd(threadId, _ => new Dictionary<string, object>(StringComparer.Ordinal));
}
}

/// <summary>
/// Gets the correlation identifier from the log context.
Expand Down Expand Up @@ -70,7 +87,7 @@ public static void AppendKeys(IEnumerable<KeyValuePair<string, string>> keys)
/// Remove additional keys from the log context.
/// </summary>
/// <param name="keys">The list of keys.</param>
public static void RemoveKeys(params string[] keys)
public static void RemoveKeys(params string[] keys)
{
if (keys == null) return;
foreach (var key in keys)
Expand All @@ -88,11 +105,15 @@ public static IEnumerable<KeyValuePair<string, object>> GetAllKeys()
}

/// <summary>
/// Removes all additional keys from the log context.
/// Removes all additional keys from the log context for the current thread.
/// </summary>
internal static void RemoveAllKeys()
{
Scope.Clear();
var threadId = Environment.CurrentManagedThreadId;
if (_threadScopes.TryGetValue(threadId, out var scope))
{
scope.Clear();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="AWSSDK.SecretsManager" />
<PackageReference Include="AWSSDK.SimpleSystemsManagement" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Amazon.Lambda.Core"/>
<ProjectReference Include="..\AWS.Lambda.Powertools.Common\AWS.Lambda.Powertools.Common.csproj" Condition="'$(Configuration)'=='Debug'"/>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="AWSXRayRecorder.Core" />
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" />
<PackageReference Include="Amazon.Lambda.Core"/>
<ProjectReference Include="..\AWS.Lambda.Powertools.Common\AWS.Lambda.Powertools.Common.csproj" Condition="'$(Configuration)'=='Debug'"/>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion libraries/src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageVersion Include="Apache.Avro" Version="1.12.0" />
<PackageVersion Include="AspectInjector" Version="2.8.1" />
<PackageVersion Include="Amazon.Lambda.Core" Version="2.7.1" />
<PackageVersion Include="Amazon.Lambda.Core" Version="2.8.0" />
<PackageVersion Include="AWSSDK.AppConfig" Version="4.0.2.2" />
<PackageVersion Include="AWSSDK.AppConfigData" Version="4.0.1.2" />
<PackageVersion Include="AWSSDK.BedrockAgentRuntime" Version="4.0.2.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>default</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>AWS.Lambda.Powertools.ConcurrencyTests</AssemblyName>
<RootNamespace>AWS.Lambda.Powertools.ConcurrencyTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<!-- Package versions are Centrally managed in Directory.Packages.props file -->
<!-- More info https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management -->
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AWS.Lambda.Powertools.Logging\AWS.Lambda.Powertools.Logging.csproj" />
<ProjectReference Include="..\..\src\AWS.Lambda.Powertools.Metrics\AWS.Lambda.Powertools.Metrics.csproj" />
</ItemGroup>

</Project>
Loading
Loading