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
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ internal static class LoggingConstants
/// Constant for key exception
/// </summary>
internal const string KeyException = "Exception";

/// <summary>
/// Constant for key tenant id
/// </summary>
public const string KeyFunctionTenantId = "TenantId";
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ internal class LoggingLambdaContext
/// </summary>
internal int MemoryLimitInMB { get; private set; }

/// <summary>
/// The tenant ID associated with the Lambda invocation.
/// </summary>
internal string TenantId { get; private set; }

/// <summary>
/// The instance
/// </summary>
Expand Down Expand Up @@ -86,7 +91,8 @@ public static bool Extract(AspectEventArgs args)
InvokedFunctionArn = x.InvokedFunctionArn,
LogGroupName = x.LogGroupName,
LogStreamName = x.LogStreamName,
MemoryLimitInMB = x.MemoryLimitInMB
MemoryLimitInMB = x.MemoryLimitInMB,
TenantId = x.TenantId,
};
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@
logEntry.TryAdd(LoggingConstants.KeyFunctionArn, context.InvokedFunctionArn);
logEntry.TryAdd(LoggingConstants.KeyFunctionRequestId, context.AwsRequestId);
logEntry.TryAdd(LoggingConstants.KeyFunctionVersion, context.FunctionVersion);

if(!string.IsNullOrEmpty(context.TenantId))
logEntry.TryAdd(LoggingConstants.KeyFunctionTenantId, context.TenantId);
}

/// <summary>
Expand All @@ -474,6 +477,7 @@
MemoryLimitInMB = context.MemoryLimitInMB,
InvokedFunctionArn = context.InvokedFunctionArn,
AwsRequestId = context.AwsRequestId,
TenantId = context.TenantId
};
}

Expand Down Expand Up @@ -521,7 +525,7 @@
}

// For complex objects, use reflection to get properties
foreach (var property in state.GetType().GetProperties())

Check warning on line 528 in libraries/src/AWS.Lambda.Powertools.Logging/Internal/PowertoolsLogger.cs

View workflow job for this annotation

GitHub Actions / build

'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public class LogEntryLambdaContext
/// CloudWatch actions.
/// </summary>
public string InvokedFunctionArn { get; internal set; }

/// <summary>
/// Tenancy ID associated with the Lambda invocation.
/// </summary>
public string TenantId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,72 @@ public void OnEntry_WhenEventArgExist_LogEvent()
);
}

[Fact]
public void OnEntry_When_TenantId_Exist_Log()
{
// Arrange
var consoleOut = GetConsoleOutput();
var correlationId = Guid.NewGuid().ToString();
Logger.Configure(options =>
{
options.LogOutput = consoleOut;
});

var context = new TestLambdaContext()
{
TenantId = "Tenant-12345",
};

var testObj = new TestObject
{
Headers = new Header
{
MyRequestIdHeader = correlationId
}
};

// Act
_testHandlers.LogEvent(testObj, context);

consoleOut.Received(1).WriteLine(
Arg.Is<string>(i => i.Contains("TenantId\":\"Tenant-12345"))
);
}

[Theory]
[InlineData("")]
[InlineData(null)]
public void OnEntry_When_TenantId_Does_Not_Exist_Dont_Log(string tenantId)
{
// Arrange
var consoleOut = GetConsoleOutput();
var correlationId = Guid.NewGuid().ToString();
Logger.Configure(options =>
{
options.LogOutput = consoleOut;
});

var context = new TestLambdaContext()
{
TenantId = tenantId,
};

var testObj = new TestObject
{
Headers = new Header
{
MyRequestIdHeader = correlationId
}
};

// Act
_testHandlers.LogEvent(testObj, context);

consoleOut.DidNotReceive().WriteLine(
Arg.Is<string>(i => i.Contains("\"TenantId\""))
);
}

[Fact]
public void OnEntry_WhenEventArgExist_LogEvent_False_Should_Not_Log()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public void Extract_WhenHasLambdaContextArgument_InitializesLambdaContextInfo()
InvokedFunctionArn = Guid.NewGuid().ToString(),
LogGroupName = Guid.NewGuid().ToString(),
LogStreamName = Guid.NewGuid().ToString(),
MemoryLimitInMB = new Random().Next()
MemoryLimitInMB = new Random().Next(),
TenantId = Guid.NewGuid().ToString()
};

var args = Substitute.For<AspectEventArgs>();
Expand Down Expand Up @@ -52,6 +53,7 @@ public void Extract_WhenHasLambdaContextArgument_InitializesLambdaContextInfo()
Assert.Equal(LoggingLambdaContext.Instance.LogGroupName, lambdaContext.LogGroupName);
Assert.Equal(LoggingLambdaContext.Instance.LogStreamName, lambdaContext.LogStreamName);
Assert.Equal(LoggingLambdaContext.Instance.MemoryLimitInMB, lambdaContext.MemoryLimitInMB);
Assert.Equal(LoggingLambdaContext.Instance.TenantId, lambdaContext.TenantId);
LoggingLambdaContext.Clear();
Assert.Null(LoggingLambdaContext.Instance);
}
Expand Down Expand Up @@ -120,7 +122,8 @@ public void Extract_WhenInstance_Already_Created_Returns_False()
InvokedFunctionArn = Guid.NewGuid().ToString(),
LogGroupName = Guid.NewGuid().ToString(),
LogStreamName = Guid.NewGuid().ToString(),
MemoryLimitInMB = new Random().Next()
MemoryLimitInMB = new Random().Next(),
TenantId = Guid.NewGuid().ToString()
};

var args = Substitute.For<AspectEventArgs>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public void TestMethod()
FunctionName = "test-function",
FunctionVersion = "1",
AwsRequestId = "123",
InvokedFunctionArn = "arn:aws:lambda:us-east-1:123456789012:function:test-function"
InvokedFunctionArn = "arn:aws:lambda:us-east-1:123456789012:function:test-function",
TenantId = "tenant-123"
});

handler.TestMethodCorrelation(new ExampleClass
Expand All @@ -131,6 +132,7 @@ public void TestMethod()
Assert.Contains("\"Custom-key\": \"custom-value\"", logOutput);
Assert.Contains("\"FunctionName\": \"test-function\"", logOutput);
Assert.Contains("\"SamplingRate\": 0.002", logOutput);
Assert.Contains("\"TenantId\": \"tenant-123\"", logOutput);
}

[Fact]
Expand Down
Loading