Skip to content

Commit

Permalink
Merge pull request #637 from aws-powertools/develop
Browse files Browse the repository at this point in the history
chore: Sync Main with Develop release 1.13
  • Loading branch information
hjgraca authored Aug 27, 2024
2 parents 0d0e5c9 + 04cbc46 commit f7f7a22
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 58 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ Not using .NET? No problem, we have you covered. Here are the other members of t

Knowing which companies are using this library is important to help prioritize the project internally. If your company is using Powertools for AWS Lambda (.NET), you can request to have your name and logo added to the README file by raising a [Support Powertools for AWS Lambda (.NET) (become a reference)](https://github.com/aws-powertools/powertools-lambda-dotnet/issues/new?assignees=&labels=customer-reference&template=support_powertools.yml&title=%5BSupport+Lambda+Powertools%5D%3A+%3Cyour+organization+name%3E) issue.

The following companies, among others, use Powertools:

* [Caylent](https://caylent.com/)
* [Pushpay](https://pushpay.com/)

### Sharing your work

Share what you did with Powertools for AWS Lambda (.NET) 💞💞. Blog post, workshops, presentation, sample apps and others. Check out what the community has already shared about Powertools for AWS Lambda (.NET) [here](https://docs.powertools.aws.dev/lambda/dotnet/we_made_this/).
Expand Down
4 changes: 2 additions & 2 deletions docs/core/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ These improvements offer the potential to eliminate build-time trimming warnings

For the Tracing utility to work correctly and without trim warnings please add the following to your `.csproj` file

```xaml
```xml
<ItemGroup>
<TrimmerRootAssembly Include="AWSSDK.Core" />
<TrimmerRootAssembly Include="AWSXRayRecorder.Core" />
Expand All @@ -298,4 +298,4 @@ For the Tracing utility to work correctly and without trim warnings please add t

Note that when you receive a trim warning, adding the class that generates the warning to TrimmerRootAssembly might not resolve the issue. A trim warning indicates that the class is trying to access some other class that can't be determined until runtime. To avoid runtime errors, add this second class to TrimmerRootAssembly.

To learn more about managing trim warnings, see [Introduction to trim warnings](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings) in the Microsoft .NET documentation.
To learn more about managing trim warnings, see [Introduction to trim warnings](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings) in the Microsoft .NET documentation.
46 changes: 45 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,51 @@ We have provided a few examples that should you how to use the each of the core
## Connect

* **Powertools for AWS Lambda (.NET) on Discord**: `#dotnet` - **[Invite link](https://discord.gg/B8zZKbbyET){target="_blank"}**
* **Email**: [email protected]
* **Email**: [email protected]

## Support Powertools for AWS Lambda (.NET)

There are many ways you can help us gain future investments to improve everyone's experience:
<div class="grid cards" markdown>
- :heart:{ .lg .middle } __Become a public reference__
---
Add your company name and logo on our [landing page](https://powertools.aws.dev).
[:octicons-arrow-right-24: GitHub Issue template](https://github.com/aws-powertools/powertools-lambda-dotnet/issues/new?assignees=&labels=customer-reference&template=support_powertools.yml&title=%5BSupport+Lambda+Powertools%5D%3A+%3Cyour+organization+name%3E){target="_blank"}
- :mega:{ .lg .middle } __Share your work__
---
Blog posts, video, and sample projects about Powertools for AWS Lambda.
[:octicons-arrow-right-24: GitHub Issue template](https://github.com/aws-powertools/powertools-lambda-dotnet/issues/new?assignees=&labels=customer-reference&template=support_powertools.yml&title=%5BSupport+Lambda+Powertools%5D%3A+%3Cyour+organization+name%3E){target="_blank"}
- :partying_face:{ .lg .middle } __Join the community__
---
Connect, ask questions, and share what features you use.
[:octicons-arrow-right-24: Discord invite](https://discord.gg/B8zZKbbyET){target="blank"}
</div>
### Becoming a reference customer
Knowing which companies are using this library is important to help prioritize the project internally. The following companies, among others, use Powertools:
<div class="grid" style="text-align:center;" markdown>
[**Caylent**](https://caylent.com/){target="_blank" rel="nofollow"}
{ .card }
[**Pushpay**](https://pushpay.com/){target="_blank" rel="nofollow"}
{ .card }
## Tenets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RecordHandlerResult
/// <summary>
/// Returns an empty <see cref="RecordHandlerResult"/> value.
/// </summary>
public static readonly RecordHandlerResult None = new();
public static RecordHandlerResult None { get; } = null!;

/// <summary>
/// Convenience method for the creation of a <see cref="RecordHandlerResult"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Text;
using AspectInjector.Broker;
Expand All @@ -39,7 +38,7 @@ public class TracingAspect
/// X-Ray Recorder
/// </summary>
private readonly IXRayRecorder _xRayRecorder;

/// <summary>
/// If true, then is cold start
/// </summary>
Expand All @@ -49,17 +48,17 @@ public class TracingAspect
/// If true, capture annotations
/// </summary>
private static bool _captureAnnotations = true;

/// <summary>
/// If true, annotations have been captured
/// </summary>
private bool _isAnnotationsCaptured;

/// <summary>
/// Tracing namespace
/// </summary>
private string _namespace;

/// <summary>
/// The capture mode
/// </summary>
Expand All @@ -73,18 +72,14 @@ public TracingAspect()
_xRayRecorder = XRayRecorder.Instance;
_powertoolsConfigurations = PowertoolsConfigurations.Instance;
}

/// <summary>
/// the code is executed instead of the target method.
/// The call to original method is wrapped around the following code
/// the original code is called with var result = target(args);
/// </summary>
/// <param name="instance"></param>
/// <param name="name"></param>
/// <param name="args"></param>
/// <param name="hostType"></param>
/// <param name="method"></param>
/// <param name="returnType"></param>
/// <param name="target"></param>
/// <param name="triggers"></param>
/// <returns></returns>
Expand All @@ -97,38 +92,39 @@ public object Around(
{
// Before running Function

var trigger = triggers.OfType<TracingAttribute>().First();
try
{
var trigger = triggers.OfType<TracingAttribute>().First();

if(TracingDisabled())
if (TracingDisabled())
return target(args);

_namespace = trigger.Namespace;
_captureMode = trigger.CaptureMode;


var segmentName = !string.IsNullOrWhiteSpace(trigger.SegmentName) ? trigger.SegmentName : $"## {name}";
var nameSpace = GetNamespace();

_xRayRecorder.BeginSubsegment(segmentName);
_xRayRecorder.SetNamespace(nameSpace);

if (_captureAnnotations)
if (_captureAnnotations)
{
_xRayRecorder.AddAnnotation("ColdStart", _isColdStart);

_captureAnnotations = false;
_isAnnotationsCaptured = true;

if (_powertoolsConfigurations.IsServiceDefined)
_xRayRecorder.AddAnnotation("Service", _powertoolsConfigurations.Service);
}

_isColdStart = false;

// return of the handler
var result = target(args);


// must get capture after all subsegments run
_captureMode = trigger.CaptureMode;

if (CaptureResponse())
{
_xRayRecorder.AddMetadata
Expand All @@ -138,12 +134,13 @@ public object Around(
result
);
}

// after
return result;
}
catch (Exception e)
{
_captureMode = trigger.CaptureMode;
HandleException(e, name);
throw;
}
Expand All @@ -153,16 +150,17 @@ public object Around(
/// the code is injected after the method ends.
/// </summary>
[Advice(Kind.After)]
public void OnExit() {
if(TracingDisabled())
public void OnExit()
{
if (TracingDisabled())
return;

if (_isAnnotationsCaptured)
_captureAnnotations = true;

_xRayRecorder.EndSubsegment();
}

/// <summary>
/// Code that handles when exceptions occur in the client method
/// </summary>
Expand All @@ -173,7 +171,7 @@ private void HandleException(Exception exception, string name)
if (CaptureError())
{
var nameSpace = GetNamespace();

var sb = new StringBuilder();
sb.AppendLine($"Exception type: {exception.GetType()}");
sb.AppendLine($"Exception message: {exception.Message}");
Expand All @@ -187,7 +185,7 @@ private void HandleException(Exception exception, string name)
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
sb.AppendLine("---END Inner Exception");
}

_xRayRecorder.AddMetadata
(
nameSpace,
Expand All @@ -209,7 +207,7 @@ private string GetNamespace()
{
return !string.IsNullOrWhiteSpace(_namespace) ? _namespace : _powertoolsConfigurations.Service;
}

/// <summary>
/// Method that checks if tracing is disabled
/// </summary>
Expand All @@ -230,7 +228,7 @@ private bool TracingDisabled()

return false;
}

/// <summary>
/// Captures the response.
/// </summary>
Expand All @@ -250,16 +248,16 @@ private bool CaptureResponse()
return false;
}
}

/// <summary>
/// Captures the error.
/// </summary>
/// <returns><c>true</c> if tracing should capture errors, <c>false</c> otherwise.</returns>
private bool CaptureError()
{
if(TracingDisabled())
if (TracingDisabled())
return false;

switch (_captureMode)
{
case TracingCaptureMode.EnvironmentVariable:
Expand All @@ -273,7 +271,7 @@ private bool CaptureError()
return false;
}
}

/// <summary>
/// Resets static variables for test.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* 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
Expand Down Expand Up @@ -41,7 +41,8 @@ internal class XRayRecorder : IXRayRecorder
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static IXRayRecorder Instance => _instance ??= new XRayRecorder(AWSXRayRecorder.Instance, PowertoolsConfigurations.Instance);
public static IXRayRecorder Instance =>
_instance ??= new XRayRecorder(AWSXRayRecorder.Instance, PowertoolsConfigurations.Instance);

public XRayRecorder(IAWSXRayRecorder awsxRayRecorder, IPowertoolsConfigurations powertoolsConfigurations)
{
Expand All @@ -56,7 +57,7 @@ public XRayRecorder(IAWSXRayRecorder awsxRayRecorder, IPowertoolsConfigurations
/// Checks whether current execution is in AWS Lambda.
/// </summary>
/// <returns>Returns true if current execution is in AWS Lambda.</returns>
private static bool _isLambda;
private static bool _isLambda;

/// <summary>
/// Gets the emitter.
Expand Down Expand Up @@ -112,14 +113,30 @@ public void AddMetadata(string nameSpace, string key, object value)
if (_isLambda)
_awsxRayRecorder.AddMetadata(nameSpace, key, value);
}

/// <summary>
/// Ends the subsegment.
/// </summary>
public void EndSubsegment()
{
if (_isLambda)
if (!_isLambda) return;
try
{
_awsxRayRecorder.EndSubsegment();
}
catch (Exception e)
{
// if it fails at this stage the data is lost
// so lets create a new subsegment with the error

Console.WriteLine("Error in Tracing utility - see Exceptions tab in Cloudwatch Traces");

_awsxRayRecorder.TraceContext.ClearEntity();
_awsxRayRecorder.BeginSubsegment("Error in Tracing utility - see Exceptions tab");
_awsxRayRecorder.AddException(e);
_awsxRayRecorder.MarkError();
_awsxRayRecorder.EndSubsegment();
}
}

/// <summary>
Expand Down
11 changes: 0 additions & 11 deletions libraries/src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,4 @@
<Compile Remove="..\AWS.Lambda.Powertools.Common\obj\**" />
</ItemGroup>


<ItemGroup Condition="'$(MSBuildProjectName)' == 'AWS.Lambda.Powertools.Idempotency' AND '$(Configuration)'=='Release'">

<ProjectReference Remove="..\AWS.Lambda.Powertools.JMESPath\AWS.Lambda.Powertools.JMESPath.csproj" />

<Compile Include="..\AWS.Lambda.Powertools.JMESPath\**\*.cs">
<Link>JMESPath\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
<Compile Remove="..\AWS.Lambda.Powertools.JMESPath\obj\**" />
</ItemGroup>

</Project>
Loading

0 comments on commit f7f7a22

Please sign in to comment.