Skip to content

Commit 49f05e4

Browse files
committed
add context to tracing. New constructor injection and tests
1 parent 65726aa commit 49f05e4

File tree

2 files changed

+332
-13
lines changed

2 files changed

+332
-13
lines changed

libraries/src/AWS.Lambda.Powertools.Tracing/Internal/XRayRecorder.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Amazon.XRay.Recorder.Core.Internal.Emitters;
1919
using Amazon.XRay.Recorder.Core.Internal.Entities;
2020
using Amazon.XRay.Recorder.Core.Strategies;
21+
using AWS.Lambda.Powertools.Common;
2122

2223
namespace AWS.Lambda.Powertools.Tracing.Internal;
2324

@@ -28,6 +29,9 @@ namespace AWS.Lambda.Powertools.Tracing.Internal;
2829
/// <seealso cref="IXRayRecorder" />
2930
internal class XRayRecorder : IXRayRecorder
3031
{
32+
private static IAWSXRayRecorder _awsxRayRecorder;
33+
private static IPowertoolsConfigurations _powertoolsConfigurations;
34+
3135
/// <summary>
3236
/// The instance
3337
/// </summary>
@@ -37,25 +41,34 @@ internal class XRayRecorder : IXRayRecorder
3741
/// Gets the instance.
3842
/// </summary>
3943
/// <value>The instance.</value>
40-
public static IXRayRecorder Instance => _instance ??= new XRayRecorder();
44+
public static IXRayRecorder Instance => _instance ??= new XRayRecorder(AWSXRayRecorder.Instance, PowertoolsConfigurations.Instance);
45+
46+
public XRayRecorder(IAWSXRayRecorder awsxRayRecorder, IPowertoolsConfigurations powertoolsConfigurations)
47+
{
48+
_instance = this;
49+
_powertoolsConfigurations = powertoolsConfigurations;
50+
_powertoolsConfigurations.SetExecutionEnvironment(this);
51+
_isLambda = _powertoolsConfigurations.IsLambdaEnvironment;
52+
_awsxRayRecorder = awsxRayRecorder;
53+
}
4154

4255
/// <summary>
4356
/// Checks whether current execution is in AWS Lambda.
4457
/// </summary>
4558
/// <returns>Returns true if current execution is in AWS Lambda.</returns>
46-
private static readonly bool _isLambda = AWSXRayRecorder.IsLambda();
59+
private static bool _isLambda;
4760

4861
/// <summary>
4962
/// Gets the emitter.
5063
/// </summary>
5164
/// <value>The emitter.</value>
52-
public ISegmentEmitter Emitter => _isLambda ? AWSXRayRecorder.Instance.Emitter : null;
65+
public ISegmentEmitter Emitter => _isLambda ? _awsxRayRecorder.Emitter : null;
5366

5467
/// <summary>
5568
/// Gets the streaming strategy.
5669
/// </summary>
5770
/// <value>The streaming strategy.</value>
58-
public IStreamingStrategy StreamingStrategy => _isLambda ? AWSXRayRecorder.Instance.StreamingStrategy : null;
71+
public IStreamingStrategy StreamingStrategy => _isLambda ? _awsxRayRecorder.StreamingStrategy : null;
5972

6073
/// <summary>
6174
/// Begins the subsegment.
@@ -64,7 +77,7 @@ internal class XRayRecorder : IXRayRecorder
6477
public void BeginSubsegment(string name)
6578
{
6679
if (_isLambda)
67-
AWSXRayRecorder.Instance.BeginSubsegment(name);
80+
_awsxRayRecorder.BeginSubsegment(name);
6881
}
6982

7083
/// <summary>
@@ -74,7 +87,7 @@ public void BeginSubsegment(string name)
7487
public void SetNamespace(string value)
7588
{
7689
if (_isLambda)
77-
AWSXRayRecorder.Instance.SetNamespace(value);
90+
_awsxRayRecorder.SetNamespace(value);
7891
}
7992

8093
/// <summary>
@@ -85,7 +98,7 @@ public void SetNamespace(string value)
8598
public void AddAnnotation(string key, object value)
8699
{
87100
if (_isLambda)
88-
AWSXRayRecorder.Instance.AddAnnotation(key, value);
101+
_awsxRayRecorder.AddAnnotation(key, value);
89102
}
90103

91104
/// <summary>
@@ -97,7 +110,7 @@ public void AddAnnotation(string key, object value)
97110
public void AddMetadata(string nameSpace, string key, object value)
98111
{
99112
if (_isLambda)
100-
AWSXRayRecorder.Instance.AddMetadata(nameSpace, key, value);
113+
_awsxRayRecorder.AddMetadata(nameSpace, key, value);
101114
}
102115

103116
/// <summary>
@@ -106,7 +119,7 @@ public void AddMetadata(string nameSpace, string key, object value)
106119
public void EndSubsegment()
107120
{
108121
if (_isLambda)
109-
AWSXRayRecorder.Instance.EndSubsegment();
122+
_awsxRayRecorder.EndSubsegment();
110123
}
111124

112125
/// <summary>
@@ -116,7 +129,7 @@ public void EndSubsegment()
116129
public Entity GetEntity()
117130
{
118131
return _isLambda
119-
? AWSXRayRecorder.Instance.GetEntity()
132+
? _awsxRayRecorder.TraceContext.GetEntity()
120133
: new Subsegment("Root");
121134
}
122135

@@ -127,7 +140,7 @@ public Entity GetEntity()
127140
public void SetEntity(Entity entity)
128141
{
129142
if (_isLambda)
130-
AWSXRayRecorder.Instance.SetEntity(entity);
143+
_awsxRayRecorder.TraceContext.SetEntity(entity);
131144
}
132145

133146
/// <summary>
@@ -137,7 +150,7 @@ public void SetEntity(Entity entity)
137150
public void AddException(Exception exception)
138151
{
139152
if (_isLambda)
140-
AWSXRayRecorder.Instance.AddException(exception);
153+
_awsxRayRecorder.AddException(exception);
141154
}
142155

143156
/// <summary>
@@ -148,6 +161,6 @@ public void AddException(Exception exception)
148161
public void AddHttpInformation(string key, object value)
149162
{
150163
if (_isLambda)
151-
AWSXRayRecorder.Instance.AddHttpInformation(key, value);
164+
_awsxRayRecorder.AddHttpInformation(key, value);
152165
}
153166
}

0 commit comments

Comments
 (0)