Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Powertools User-Agent for Lambda execution environment #246

Merged
merged 7 commits into from
May 3, 2023

Conversation

hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Apr 21, 2023

Issue number: #213

Summary

Update the execution environment so that the appropriate metadata can be attached to the user agent string during service calls via the AWS SDK for .NET

Changes

  • Add method to set context in PowertoolsConfigurations.cs.
  • New PowertoolsEnvironment interface as a facade for System.Environment. Makes it testable
  • Context naming convention for Utilities PTFeature/Name/Version
  • Call SetExecutionEnvironment from Metrics, Tracing and Logging utilities
  • This method is called with the Type and uses reflection to get the Name and Version of the calling Assembly
  • Many more tests

New method to set the execution environment in the AWS.Lambda.Powertools.Common project

public void SetExecutionEnvironment<T>(T type)
{
    const string envName = "AWS_EXECUTION_ENV";

    var envValue = new StringBuilder();

    // If there is an existing execution environment variable add the annotations package as a suffix.
    if(!string.IsNullOrEmpty(GetEnvironmentVariable(envName)))
    {
        envValue.Append($"{GetEnvironmentVariable(envName)} ");
    }

    var assemblyVersion = _powertoolsEnvironment.GetAssemblyVersion(type);
    var assemblyName =_powertoolsEnvironment.GetAssemblyName(type);

    envValue.Append($"{ParseAssemblyName(assemblyName)}/{assemblyVersion}");

    SetEnvironmentVariable(envName, envValue.ToString());
}

Each Utility will have to Set the execution context

_powertoolsConfigurations.SetExecutionEnvironment(this);

User experience

User-Agent header is updated and includes the Utilities used in the request to an AWS service using AWS SDK

image

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@auto-assign auto-assign bot requested a review from sliedig April 21, 2023 18:16
@hjgraca hjgraca requested a review from amirkaws April 21, 2023 18:16
@hjgraca hjgraca linked an issue Apr 21, 2023 that may be closed by this pull request
2 tasks
@hjgraca hjgraca changed the title Feature: Powertools User-Agent for Lambda execution environment feat: Powertools User-Agent for Lambda execution environment Apr 21, 2023
@github-actions github-actions bot added the feature New features or minor changes label Apr 21, 2023
@codecov-commenter
Copy link

codecov-commenter commented Apr 21, 2023

Codecov Report

Patch coverage: 88.73% and project coverage change: +3.99 🎉

Comparison is base (8bb7863) 56.29% compared to head (418c005) 60.29%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #246      +/-   ##
===========================================
+ Coverage    56.29%   60.29%   +3.99%     
===========================================
  Files           41       42       +1     
  Lines         1796     1851      +55     
===========================================
+ Hits          1011     1116     +105     
+ Misses         785      735      -50     
Flag Coverage Δ
unittests 60.29% <88.73%> (+3.99%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...AWS.Lambda.Powertools.Common/Core/SystemWrapper.cs 75.55% <84.37%> (+75.55%) ⬆️
...Lambda.Powertools.Tracing/Internal/XRayRecorder.cs 95.83% <90.00%> (+95.83%) ⬆️
...da.Powertools.Common/Core/PowertoolsEnvironment.cs 92.85% <92.85%> (ø)
...Powertools.Common/Core/PowertoolsConfigurations.cs 91.42% <100.00%> (+19.55%) ⬆️
...da.Powertools.Logging/Internal/PowertoolsLogger.cs 91.07% <100.00%> (+0.05%) ⬆️
...aries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs 84.37% <100.00%> (+0.12%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@sliedig sliedig added the do-not-merge PRs that are blocked for varying reasons label Apr 26, 2023
@sliedig
Copy link
Contributor

sliedig commented Apr 26, 2023

Waiting on confirmation from SDK team

…ment from powertoolsconfigurations. Update tests
Copy link

@normj normj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks good to me. I just added a couple comments to confirm behavior.

}

/// <inheritdoc />
public void SetExecutionEnvironment<T>(T type)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the whole system so it might already being doing this but make sure this is only called once per power tools assembly. These assembly look up calls are can be costly and we don't want to it for every request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @normj . The implementation prevents multiple instances of SystemWrapper to be created. The instances that create systemwrapper are singletons. The constructors are called once for the lambda lifetime.

@sliedig sliedig removed the do-not-merge PRs that are blocked for varying reasons label May 2, 2023
Copy link
Contributor

@amirkaws amirkaws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

/// <inheritdoc />
public void SetExecutionEnvironment<T>(T type)
{
_systemWrapper.SetExecutionEnvironment(type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one calls SystemWrapper. SetExecutionEnvironment and the system Wrapper I believe calls PowertoolsEnvironment, I believe there are too many layers of abstractions.

@hjgraca hjgraca merged commit b9dfe8b into aws-powertools:develop May 3, 2023
@hjgraca hjgraca deleted the feat_set-execution-context branch May 3, 2023 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New features or minor changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Powertools User-Agent for Lambda execution environment
5 participants