Skip to content

Commit

Permalink
.NET v3: Adding .NET Lambda Powertools for logging. (#5559)
Browse files Browse the repository at this point in the history
* Adding Lambda Powertools logging.

* Updates to READMEs for Powertools.

* Whitespace formatting.

* Fix for a broken link.
  • Loading branch information
rlhagerm authored and ford-at-aws committed Dec 15, 2023
1 parent c31e9c0 commit a7c8550
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .doc_gen/cross-content/cross_FSA_NetV3_block.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
</itemizedlist>
<para> The full app can be deployed with the &CDK;. For source code and deployment
instructions, see the project in <ulink
url="https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/example_code/cross-services/FeedbackSentimentAnalyzer">
url="https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/cross-service/FeedbackSentimentAnalyzer">
GitHub</ulink>. </para>
</block>
2 changes: 1 addition & 1 deletion dotnetv3/cross-service/FeedbackSentimentAnalyzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ This document discusses the language-specific nuances of deploying the Feedback
### .NET Implementation Details
- This example includes AWS Lambda functions for the various operations of the Feedback Sentiment Analyzer.
- Each function was created using the AWS Lambda Template from the [AWS Toolkit for Visual Studio](https://aws.amazon.com/visualstudio/).
- Each function also uses [Powertools for AWS Lamba (.NET)](https://github.com/aws-powertools/powertools-lambda-dotnet) for enhanced logging.
- Each function also uses [Powertools for AWS Lambda (.NET)](https://github.com/aws-powertools/powertools-lambda-dotnet) for enhanced logging.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Logging;
using Microsoft.AspNetCore.Mvc;
using PamServices;

Expand Down Expand Up @@ -51,7 +52,7 @@ public IActionResult Upload([FromBody] UploadRequest uploadRequest)
[HttpGet("labels")]
public async Task<IActionResult> Get()
{
LambdaLogger.Log($"Test logging: getting labels.");
Logger.LogInformation($"Getting labels.");
var allLabels = await _labelService.GetAllItems();
var response = new LabelsResponse(allLabels.ToList());
return Ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Amazon.Rekognition;
using Amazon.S3;
using Amazon.Util;
using AWS.Lambda.Powertools.Logging;
using PamServices;

// Assembly attribute to enable the AWS Lambda function's JSON input to be converted into a .NET class.
Expand Down Expand Up @@ -66,19 +67,16 @@ public async Task FunctionHandler(S3Event evnt, ILambdaContext context)
var s3Event = evnt.Records?[0].S3;
try
{
context.Logger.LogInformation(
$"Processing object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}");
Logger.LogInformation($"Processing object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}");

var detectedLabels = await _imageService.DetectLabels(s3Event.Object.Key, s3Event.Bucket.Name);
await _labelService.AddImageLabels(s3Event.Object.Key, detectedLabels);
context.Logger.LogInformation(
$"Added labels {string.Join(',', detectedLabels)}");
Logger.LogInformation($"Added labels {string.Join(',', detectedLabels)}");
}
catch (Exception e)
{
context.Logger.LogInformation($"Error getting object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
context.Logger.LogInformation(e.Message);
context.Logger.LogInformation(e.StackTrace);
Logger.LogError($"Error getting object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}. " +
$"Make sure they exist and your bucket is in the same region as this function.", e);
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.SimpleNotificationService;
using Amazon.Util;
using AWS.Lambda.Powertools.Logging;
using PamServices;

namespace PamApi;
Expand Down Expand Up @@ -69,21 +69,19 @@ public async Task FunctionHandler(DownloadRequest request, ILambdaContext contex

try
{
context.Logger.LogInformation($"Starting download and zip operation: {JsonSerializer.Serialize(request)}");
Logger.LogInformation($"Starting download and zip operation.", request);
var labelsList = request.labels.ToList();
var imageKeys = await _labelService.GetAllImagesForLabels(labelsList);
var zipArchiveUrl =
await _storageService.GenerateZipFromImages(imageKeys, storageBucketName!,
workingBucketName!);
await _notificationService.SendNotification(topicArn!, "Image download",
$"Your images are available here: {zipArchiveUrl}");
LambdaLogger.Log($"url: {zipArchiveUrl}");
Logger.LogInformation($"url: {zipArchiveUrl}");
}
catch (Exception e)
{
context.Logger.LogInformation($"Error starting download.");
context.Logger.LogInformation(e.Message);
context.Logger.LogInformation(e.StackTrace);
Logger.LogError(e, $"Error starting download.");
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="Amazon.Extensions.Configuration.SystemsManager" Version="5.0.3" />
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="7.2.0" />
<PackageReference Include="Amazon.Lambda.S3Events" Version="3.0.1" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.3.2" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.103.5" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.7" />
<PackageReference Include="AWSSDK.Rekognition" Version="3.7.107.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Amazon.Rekognition;
using Amazon.S3;
using Amazon.Util;
using AWS.Lambda.Powertools.Logging;
using PamServices;

// Assembly attribute to enable the AWS Lambda function's JSON input to be converted into a .NET class.
Expand Down Expand Up @@ -66,19 +67,16 @@ public async Task FunctionHandler(S3Event evnt, ILambdaContext context)
var s3Event = evnt.Records?[0].S3;
try
{
context.Logger.LogInformation(
$"Processing object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}");
Logger.LogInformation($"Processing object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}");

var detectedLabels = await _imageService.DetectLabels(s3Event.Object.Key, s3Event.Bucket.Name);
await _labelService.AddImageLabels(s3Event.Object.Key, detectedLabels);
context.Logger.LogInformation(
$"Added labels {string.Join(',', detectedLabels)}");
Logger.LogInformation($"Added labels {string.Join(',', detectedLabels)}");
}
catch (Exception e)
{
context.Logger.LogInformation($"Error getting object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
context.Logger.LogInformation(e.Message);
context.Logger.LogInformation(e.StackTrace);
Logger.LogError($"Error getting object {s3Event!.Object.Key} from bucket {s3Event.Bucket.Name}. " +
$"Make sure they exist and your bucket is in the same region as this function.", e);
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Amazon.S3;
using Amazon.SimpleNotificationService;
using Amazon.Util;
using AWS.Lambda.Powertools.Logging;
using PamApi;
using PamServices;

Expand Down Expand Up @@ -70,21 +71,19 @@ public async Task FunctionHandler(DownloadRequest request, ILambdaContext contex

try
{
context.Logger.LogInformation($"Starting download and zip operation: {JsonSerializer.Serialize(request)}");
Logger.LogInformation($"Starting download and zip operation: ", request);
var labelsList = request.labels.ToList();
var imageKeys = await _labelService.GetAllImagesForLabels(labelsList);
var zipArchiveUrl =
await _storageService.GenerateZipFromImages(imageKeys, storageBucketName!,
workingBucketName!);
await _notificationService.SendNotification(topicArn!, "Image download",
$"Your images are available here: {zipArchiveUrl}");
LambdaLogger.Log($"url: {zipArchiveUrl}");
Logger.LogInformation($"url: {zipArchiveUrl}");
}
catch (Exception e)
{
context.Logger.LogInformation($"Error starting download.");
context.Logger.LogInformation(e.Message);
context.Logger.LogInformation(e.StackTrace);
Logger.LogError($"Error starting download.", e);
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Amazon.Lambda.Annotations.APIGateway;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Logging;
using PamServices;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
Expand Down Expand Up @@ -36,7 +37,7 @@ public Functions(StorageService storageService, LabelService labelService)
[HttpApi(LambdaHttpMethod.Get, "/labels")]
public async Task<APIGatewayHttpApiV2ProxyResponse> GetLabels(ILambdaContext context)
{
context.Logger.Log($"Test logging: getting labels.");
Logger.LogInformation($"Getting labels.");
var allLabels = await _labelService.GetAllItems();
var labelsResponse = new LabelsResponse(allLabels.ToList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<PackageReference Include="Amazon.Lambda.S3Events" Version="3.0.1" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
<PackageReference Include="Amazon.Lambda.Annotations" Version="1.0.0" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.3.2" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.7" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion dotnetv3/cross-service/PhotoAssetManager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ to use the AWS Cloud Development Kit (AWS CDK) or AWS Command Line Interface

## Build the code

The Visual Studio solution **PhotoAssetManager.sln** includes several projects with different purposes for this example.
The Visual Studio solution **PhotoAssetManager.sln** includes several projects with different purposes for this example. The Lambda functions included in the solution also use [Powertools for AWS Lambda (.NET)](https://github.com/aws-powertools/powertools-lambda-dotnet) for enhanced logging.

- **PamApi** - A Lambda Serverless API project that includes an ApiController for the serverless endpoints, a Swagger UI for development environments, and separate Download and DetectLabels functions.
- This is the project that will be packaged and deployed when using the `dotnet` language option when setting up the CDK stack.
Expand Down

0 comments on commit a7c8550

Please sign in to comment.