Skip to content

Commit b9dfe8b

Browse files
authored
Merge pull request #246 from hjgraca/feat_set-execution-context
2 parents a646726 + 418c005 commit b9dfe8b

File tree

15 files changed

+737
-16
lines changed

15 files changed

+737
-16
lines changed

libraries/src/AWS.Lambda.Powertools.Common/Core/Constants.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,14 @@ internal static class Constants
8080
/// Constant for LAMBDA_TASK_ROOT environment variable
8181
/// </summary>
8282
internal const string LambdaTaskRoot = "LAMBDA_TASK_ROOT";
83+
84+
/// <summary>
85+
/// Constant for AWS_EXECUTION_ENV environment variable
86+
/// </summary>
87+
internal const string AwsExecutionEnvironmentVariableName = "AWS_EXECUTION_ENV";
88+
89+
/// <summary>
90+
/// Constant for Powertools feature identifier fo AWS_EXECUTION_ENV environment variable
91+
/// </summary>
92+
internal const string FeatureContextIdentifier = "PT";
8393
}

libraries/src/AWS.Lambda.Powertools.Common/Core/IPowertoolsConfigurations.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,10 @@ public interface IPowertoolsConfigurations
120120
/// <param name="defaultValue">if set to <c>true</c> [default value].</param>
121121
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
122122
bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue);
123+
124+
/// <summary>
125+
/// Sets the execution Environment Variable (AWS_EXECUTION_ENV)
126+
/// </summary>
127+
/// <param name="type"></param>
128+
void SetExecutionEnvironment<T>(T type);
123129
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace AWS.Lambda.Powertools.Common;
2+
3+
/// <summary>
4+
/// Interface for PowertoolsEnvironment
5+
/// </summary>
6+
public interface IPowertoolsEnvironment
7+
{
8+
/// <summary>
9+
/// Get environment variable by variable name
10+
/// </summary>
11+
/// <param name="variableName"></param>
12+
/// <returns>Environment variable</returns>
13+
string GetEnvironmentVariable(string variableName);
14+
15+
/// <summary>
16+
/// Set environment variable
17+
/// </summary>
18+
/// <param name="variableName"></param>
19+
/// <param name="value">Setting this to null will remove environment variable with that name</param>
20+
void SetEnvironmentVariable(string variableName, string value);
21+
22+
/// <summary>
23+
/// Get the calling Type Assembly Name
24+
/// </summary>
25+
/// <param name="type"></param>
26+
/// <typeparam name="T"></typeparam>
27+
/// <returns>Assembly Name</returns>
28+
string GetAssemblyName<T>(T type);
29+
30+
/// <summary>
31+
/// Get the calling Type Assembly Version
32+
/// </summary>
33+
/// <param name="type"></param>
34+
/// <typeparam name="T"></typeparam>
35+
/// <returns>Assembly Version in the Major.Minor.Build format</returns>
36+
string GetAssemblyVersion<T>(T type);
37+
}

libraries/src/AWS.Lambda.Powertools.Common/Core/ISystemWrapper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ public interface ISystemWrapper
4444
/// </summary>
4545
/// <returns>System.Double.</returns>
4646
double GetRandom();
47+
48+
/// <summary>
49+
/// Sets the environment variable.
50+
/// </summary>
51+
/// <param name="variable">The variable.</param>
52+
/// <param name="value"></param>
53+
void SetEnvironmentVariable(string variable, string value);
54+
55+
/// <summary>
56+
/// Sets the execution Environment Variable (AWS_EXECUTION_ENV)
57+
/// </summary>
58+
/// <param name="type"></param>
59+
void SetExecutionEnvironment<T>(T type);
4760
}

libraries/src/AWS.Lambda.Powertools.Common/Core/PowertoolsConfigurations.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,10 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
184184
/// <value><c>true</c> if [tracing is disabled]; otherwise, <c>false</c>.</value>
185185
public bool TracingDisabled =>
186186
GetEnvironmentVariableOrDefault(Constants.TracingDisabledEnv, false);
187+
188+
/// <inheritdoc />
189+
public void SetExecutionEnvironment<T>(T type)
190+
{
191+
_systemWrapper.SetExecutionEnvironment(type);
192+
}
187193
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
3+
namespace AWS.Lambda.Powertools.Common;
4+
5+
/// <inheritdoc />
6+
public class PowertoolsEnvironment : IPowertoolsEnvironment
7+
{
8+
/// <summary>
9+
/// The instance
10+
/// </summary>
11+
private static IPowertoolsEnvironment _instance;
12+
13+
/// <summary>
14+
/// Gets the instance.
15+
/// </summary>
16+
/// <value>The instance.</value>
17+
public static IPowertoolsEnvironment Instance => _instance ??= new PowertoolsEnvironment();
18+
19+
/// <inheritdoc />
20+
public string GetEnvironmentVariable(string variableName)
21+
{
22+
return Environment.GetEnvironmentVariable(variableName);
23+
}
24+
25+
/// <inheritdoc />
26+
public void SetEnvironmentVariable(string variableName, string value)
27+
{
28+
Environment.SetEnvironmentVariable(variableName, value);
29+
}
30+
31+
/// <inheritdoc />
32+
public string GetAssemblyName<T>(T type)
33+
{
34+
return type.GetType().Assembly.GetName().Name;
35+
}
36+
37+
/// <inheritdoc />
38+
public string GetAssemblyVersion<T>(T type)
39+
{
40+
var version = type.GetType().Assembly.GetName().Version;
41+
return version != null ? $"{version.Major}.{version.Minor}.{version.Build}" : string.Empty;
42+
}
43+
}

libraries/src/AWS.Lambda.Powertools.Common/Core/SystemWrapper.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Text;
1718

1819
namespace AWS.Lambda.Powertools.Common;
1920

@@ -24,6 +25,8 @@ namespace AWS.Lambda.Powertools.Common;
2425
/// <seealso cref="ISystemWrapper" />
2526
public class SystemWrapper : ISystemWrapper
2627
{
28+
private static IPowertoolsEnvironment _powertoolsEnvironment;
29+
2730
/// <summary>
2831
/// The instance
2932
/// </summary>
@@ -32,15 +35,17 @@ public class SystemWrapper : ISystemWrapper
3235
/// <summary>
3336
/// Prevents a default instance of the <see cref="SystemWrapper" /> class from being created.
3437
/// </summary>
35-
private SystemWrapper()
38+
public SystemWrapper(IPowertoolsEnvironment powertoolsEnvironment)
3639
{
40+
_powertoolsEnvironment = powertoolsEnvironment;
41+
_instance ??= this;
3742
}
3843

3944
/// <summary>
4045
/// Gets the instance.
4146
/// </summary>
4247
/// <value>The instance.</value>
43-
public static ISystemWrapper Instance => _instance ??= new SystemWrapper();
48+
public static ISystemWrapper Instance => _instance ??= new SystemWrapper(PowertoolsEnvironment.Instance);
4449

4550
/// <summary>
4651
/// Gets the environment variable.
@@ -49,7 +54,7 @@ private SystemWrapper()
4954
/// <returns>System.String.</returns>
5055
public string GetEnvironmentVariable(string variable)
5156
{
52-
return Environment.GetEnvironmentVariable(variable);
57+
return _powertoolsEnvironment.GetEnvironmentVariable(variable);
5358
}
5459

5560
/// <summary>
@@ -78,4 +83,57 @@ public double GetRandom()
7883
{
7984
return new Random().NextDouble();
8085
}
86+
87+
/// <inheritdoc />
88+
public void SetEnvironmentVariable(string variable, string value)
89+
{
90+
_powertoolsEnvironment.SetEnvironmentVariable(variable, value);
91+
}
92+
93+
/// <inheritdoc />
94+
public void SetExecutionEnvironment<T>(T type)
95+
{
96+
const string envName = Constants.AwsExecutionEnvironmentVariableName;
97+
var envValue = new StringBuilder();
98+
var currentEnvValue = GetEnvironmentVariable(envName);
99+
var assemblyName = ParseAssemblyName(_powertoolsEnvironment.GetAssemblyName(type));
100+
101+
// If there is an existing execution environment variable add the annotations package as a suffix.
102+
if(!string.IsNullOrEmpty(currentEnvValue))
103+
{
104+
// Avoid duplication - should not happen since the calling Instances are Singletons - defensive purposes
105+
if (currentEnvValue.Contains(assemblyName))
106+
{
107+
return;
108+
}
109+
110+
envValue.Append($"{currentEnvValue} ");
111+
}
112+
113+
var assemblyVersion = _powertoolsEnvironment.GetAssemblyVersion(type);
114+
115+
envValue.Append($"{assemblyName}/{assemblyVersion}");
116+
117+
SetEnvironmentVariable(envName, envValue.ToString());
118+
}
119+
120+
/// <summary>
121+
/// Parsing the name to conform with the required naming convention for the UserAgent header (PTFeature/Name/Version)
122+
/// Fallback to Assembly Name on exception
123+
/// </summary>
124+
/// <param name="assemblyName"></param>
125+
/// <returns></returns>
126+
private string ParseAssemblyName(string assemblyName)
127+
{
128+
try
129+
{
130+
var parsedName = assemblyName.Substring(assemblyName.LastIndexOf(".", StringComparison.Ordinal)+1);
131+
return $"{Constants.FeatureContextIdentifier}/{parsedName}";
132+
}
133+
catch
134+
{
135+
//NOOP
136+
}
137+
return $"{Constants.FeatureContextIdentifier}/{assemblyName}";
138+
}
81139
}

libraries/src/AWS.Lambda.Powertools.Logging/Internal/PowertoolsLogger.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public PowertoolsLogger(
7676
{
7777
(_name, _powertoolsConfigurations, _systemWrapper, _getCurrentConfig) = (name,
7878
powertoolsConfigurations, systemWrapper, getCurrentConfig);
79+
80+
_powertoolsConfigurations.SetExecutionEnvironment(this);
7981
}
8082

8183
/// <summary>

libraries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ internal Metrics(IPowertoolsConfigurations powertoolsConfigurations, string name
7171
_raiseOnEmptyMetrics = raiseOnEmptyMetrics;
7272
_captureColdStartEnabled = captureColdStartEnabled;
7373
_context = InitializeContext(nameSpace, service, null);
74+
75+
_powertoolsConfigurations.SetExecutionEnvironment(this);
7476
}
7577

7678
/// <summary>

0 commit comments

Comments
 (0)