Skip to content

Commit 17fdf97

Browse files
authored
Merge pull request #281 from jj22ee/change-default-strat
Change context missing strategy behaviour to Log Error
2 parents 7f6eeac + cdee4c1 commit 17fdf97

11 files changed

+121
-39
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ You can configure X-Ray in the `appsettings` of your `App.config` or `Web.config
7272
<add key="AWSXRayPlugins" value="EC2Plugin, ECSPlugin, ElasticBeanstalkPlugin"/>
7373
<add key="SamplingRuleManifest" value="JSONs\DefaultSamplingRules.json"/>
7474
<add key="AwsServiceHandlerManifest" value="JSONs\AWSRequestInfo.json"/>
75-
<add key="UseRuntimeErrors" value="true"/>
75+
<add key="UseRuntimeErrors" value="false"/>
7676
<add key="CollectSqlQueries" value="false"/>
7777
</appSettings>
7878
</configuration>
@@ -91,7 +91,7 @@ a) In `appsettings.json` file, configure items under `XRay` key
9191
"SamplingRuleManifest": "SamplingRules.json",
9292
"AWSXRayPlugins": "EC2Plugin, ECSPlugin, ElasticBeanstalkPlugin",
9393
"AwsServiceHandlerManifest": "JSONs\AWSRequestInfo.json",
94-
"UseRuntimeErrors":"true",
94+
"UseRuntimeErrors":"false",
9595
"CollectSqlQueries":"false"
9696
}
9797
}

sdk/src/Core/AWSXRayRecorderImpl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected static Lazy<AWSXRayRecorder> LazyDefaultRecorder
6161

6262
private ISegmentEmitter _emitter;
6363
private bool disposed;
64-
protected ContextMissingStrategy cntxtMissingStrategy = ContextMissingStrategy.RUNTIME_ERROR;
64+
protected ContextMissingStrategy cntxtMissingStrategy = ContextMissingStrategy.LOG_ERROR;
6565
private Dictionary<string, object> serviceContext = new Dictionary<string, object>();
6666

6767
protected AWSXRayRecorderImpl(ISegmentEmitter emitter)

sdk/src/Core/AwsXrayRecorderBuilder.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class AWSXRayRecorderBuilder
3838

3939
private readonly List<IPlugin> _plugins = new List<IPlugin>();
4040
private ISamplingStrategy _samplingStrategy;
41-
private ContextMissingStrategy _contextMissingStrategy = ContextMissingStrategy.RUNTIME_ERROR;
41+
private ContextMissingStrategy _contextMissingStrategy = ContextMissingStrategy.LOG_ERROR;
4242
private ISegmentEmitter _segmentEmitter;
4343
private string _daemonAddress;
4444
private ITraceContext _traceContext;
@@ -89,7 +89,7 @@ public AWSXRayRecorderBuilder WithPluginsFromAppSettings()
8989

9090
/// <summary>
9191
/// Reads useRuntimeErrors settings from app settings, and adds into the builder.
92-
/// If the useRuntimeErrors settings doesn't exist, it defaults to true and ContextMissingStrategy.RUNTIME_ERROR is used.
92+
/// If the useRuntimeErrors settings doesn't exist, it defaults to false and ContextMissingStrategy.LOG_ERROR is used.
9393
/// </summary>
9494
/// <returns>The builder with context missing strategy set.</returns>
9595
public AWSXRayRecorderBuilder WithContextMissingStrategyFromAppSettings()
@@ -123,7 +123,7 @@ public AWSXRayRecorderBuilder WithPluginsFromConfig(XRayOptions xRayOptions)
123123

124124
/// <summary>
125125
/// Reads useRuntimeErrors settings from config instance, and adds into the builder.
126-
/// If the useRuntimeErrors settings doesn't exist, it defaults to true and ContextMissingStrategy.RUNTIME_ERROR is used.
126+
/// If the useRuntimeErrors settings doesn't exist, it defaults to false and ContextMissingStrategy.LOG_ERROR is used.
127127
/// </summary>
128128
/// <returns>The builder with context missing strategy set.</returns>
129129
public AWSXRayRecorderBuilder WithContextMissingStrategyFromConfig(XRayOptions xRayOptions)

sdk/src/Core/Internal/Utils/AppSettings.netcore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public XRayOptions(string pluginSetting, string samplingRuleManifest, string aws
8888
/// <summary>
8989
/// For missing Segments/Subsegments, if set to true, runtime exception is thrown, if set to false, runtime exceptions are avoided and logged.
9090
/// </summary>
91-
public bool UseRuntimeErrors { get; set; } = true;
91+
public bool UseRuntimeErrors { get; set; } = false;
9292

9393
/// <summary>
9494
/// Include the TraceableSqlCommand.CommandText in the sanitized_query section of

sdk/src/Core/Internal/Utils/AppSettings.netframework.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ private static bool GetSettingBool(string key, bool defaultValue = false)
131131
return defaultValue;
132132
}
133133

134-
// If the key not present, default value set to true.
135-
private static bool GetSettingBoolForRuntimeError(string key, bool defaultValue = true)
134+
// If the key not present, default value set to false.
135+
private static bool GetSettingBoolForRuntimeError(string key, bool defaultValue = false)
136136
{
137137
string value = GetSetting(key);
138138
if (bool.TryParse(value, out bool result))

sdk/src/Core/Internal/Utils/ConfigurationExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static XRayOptions GetXRayOptions(IConfiguration config, string configSe
6464
options.SamplingRuleManifest = GetSetting(SamplingRuleManifestKey, section);
6565
options.AwsServiceHandlerManifest =GetSetting(AWSServiceHandlerManifestKey, section);
6666
options.IsXRayTracingDisabled = GetSettingBool(DisableXRayTracingKey,section);
67-
options.UseRuntimeErrors = GetSettingBool(UseRuntimeErrorsKey, section, defaultValue: true);
67+
options.UseRuntimeErrors = GetSettingBool(UseRuntimeErrorsKey, section, defaultValue: false);
6868
options.CollectSqlQueries = GetSettingBool(CollectSqlQueries, section, defaultValue: false);
6969
return options;
7070
}

sdk/src/Core/Strategies/ContextMissingStrategy.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public enum ContextMissingStrategy
2525
/// <summary>
2626
/// The EntityNotAvailableException will be thrown if occurs at runtime.
2727
/// </summary>
28-
RUNTIME_ERROR = 0, // Set to 0, so it is default value
28+
RUNTIME_ERROR = 1,
2929

3030
/// <summary>
3131
/// The EntityNotAvailableException will be logged if occurs.
3232
/// </summary>
33-
LOG_ERROR = 1,
33+
LOG_ERROR = 0, // Set to 0, so it is default value
3434
}
3535
}

sdk/test/UnitTests/AWSXRayRecorderBuilderTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void TestSetStreamingStrategy()
149149
public void TestDefaultValueOfContextMissingStrategy()
150150
{
151151
var recorder = new AWSXRayRecorderBuilder().Build();
152-
Assert.AreEqual(ContextMissingStrategy.RUNTIME_ERROR, recorder.ContextMissingStrategy);
152+
Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy);
153153
}
154154

155155
[TestMethod]
@@ -175,7 +175,7 @@ public void TestSetContextMissingUsingConfiguration1() // Contextmissing starteg
175175
}
176176

177177
[TestMethod]
178-
public void TestSetContextMissingUsingConfiguration2() // Contextmissing startegy not set
178+
public void TestSetContextMissingUsingConfiguration2() // Contextmissing strategy not set
179179
{
180180
#if NETFRAMEWORK
181181
AppSettings.Reset();
@@ -184,7 +184,7 @@ public void TestSetContextMissingUsingConfiguration2() // Contextmissing starteg
184184
AWSXRayRecorderBuilder builder = new AWSXRayRecorderBuilder().WithContextMissingStrategyFromConfig(_xRayOptions);
185185
#endif
186186
AWSXRayRecorder recorder = builder.Build();
187-
Assert.AreEqual(ContextMissingStrategy.RUNTIME_ERROR, recorder.ContextMissingStrategy); // Default context missing strategy is set
187+
Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy); // Default context missing strategy is set
188188
}
189189

190190
[TestMethod]

sdk/test/UnitTests/AWSXRayRecorderTests.cs

+97-15
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,13 @@ public async Task TestTraceMethodAsyncThrowException()
463463
}
464464

465465
[TestMethod]
466-
public void TestSegmentMissingInTraceContext()
466+
public void TestSegmentMissingInTraceContextUsingRuntimeError()
467467
{
468+
var recorder = new AWSXRayRecorder();
469+
recorder.ContextMissingStrategy = ContextMissingStrategy.RUNTIME_ERROR;
468470
try
469471
{
470-
_recorder.EndSegment();
472+
recorder.EndSegment();
471473
Assert.Fail();
472474
}
473475
catch (EntityNotAvailableException)
@@ -477,7 +479,7 @@ public void TestSegmentMissingInTraceContext()
477479

478480
try
479481
{
480-
_recorder.BeginSubsegment("test");
482+
recorder.BeginSubsegment("test");
481483
Assert.Fail();
482484
}
483485
catch (EntityNotAvailableException)
@@ -487,7 +489,7 @@ public void TestSegmentMissingInTraceContext()
487489

488490
try
489491
{
490-
_recorder.EndSubsegment();
492+
recorder.EndSubsegment();
491493
Assert.Fail();
492494
}
493495
catch (EntityNotAvailableException)
@@ -496,6 +498,38 @@ public void TestSegmentMissingInTraceContext()
496498
}
497499
}
498500

501+
[TestMethod]
502+
public void TestSegmentMissingInTraceContextUsingDefaultStrategy()
503+
{
504+
var recorder = new AWSXRayRecorder();
505+
try
506+
{
507+
recorder.EndSegment();
508+
}
509+
catch (EntityNotAvailableException)
510+
{
511+
Assert.Fail();
512+
}
513+
514+
try
515+
{
516+
recorder.BeginSubsegment("test");
517+
}
518+
catch (EntityNotAvailableException)
519+
{
520+
Assert.Fail();
521+
}
522+
523+
try
524+
{
525+
recorder.EndSubsegment();
526+
}
527+
catch (EntityNotAvailableException)
528+
{
529+
Assert.Fail();
530+
}
531+
}
532+
499533
[TestMethod]
500534
public void TestStartSegmentWithNotSampledDecision()
501535
{
@@ -848,28 +882,75 @@ public void TestSetDaemonAddress()
848882
}
849883

850884
[TestMethod]
851-
[ExpectedException(typeof(EntityNotAvailableException))]
852885
public void TestEndSubsegmentWithSegment()
853886
{
854-
_recorder.BeginSegment("segment", TraceId);
855-
_recorder.EndSubsegment();
887+
try
888+
{
889+
_recorder.BeginSegment("segment", TraceId);
890+
_recorder.EndSubsegment();
891+
}
892+
catch (EntityNotAvailableException e)
893+
{
894+
Assert.Fail();
895+
}
856896
}
857897

858898
[TestMethod]
859-
[ExpectedException(typeof(EntityNotAvailableException))]
860899
public void TestStartSubsegmentWithoutSegment()
861900
{
862-
_recorder.BeginSubsegment("subsegment");
901+
try
902+
{
903+
_recorder.BeginSubsegment("subsegment");
904+
}
905+
catch (EntityNotAvailableException e)
906+
{
907+
Assert.Fail();
908+
}
909+
}
910+
911+
[TestMethod]
912+
public void TestEndSegmentWithSubsegment()
913+
{
914+
try
915+
{
916+
_recorder.BeginSegment("segment", TraceId);
917+
_recorder.BeginSubsegment("subsegment");
918+
_recorder.EndSegment();
919+
}
920+
catch (EntityNotAvailableException e)
921+
{
922+
Assert.Fail();
923+
}
924+
}
925+
926+
[TestMethod]
927+
[ExpectedException(typeof(EntityNotAvailableException))]
928+
public void TestEndSubsegmentWithSegmentUsingRuntimeError()
929+
{
930+
var recorder = new AWSXRayRecorder();
931+
recorder.ContextMissingStrategy = ContextMissingStrategy.RUNTIME_ERROR;
932+
recorder.BeginSegment("segment", TraceId);
933+
recorder.EndSubsegment();
863934
}
864935

865936
[TestMethod]
866937
[ExpectedException(typeof(EntityNotAvailableException))]
938+
public void TestStartSubsegmentWithoutSegmentUsingRuntimeError()
939+
{
940+
var recorder = new AWSXRayRecorder();
941+
recorder.ContextMissingStrategy = ContextMissingStrategy.RUNTIME_ERROR;
942+
recorder.BeginSubsegment("subsegment");
943+
}
867944

868-
public void TestEndSegmentWithSubsegment()
945+
[TestMethod]
946+
[ExpectedException(typeof(EntityNotAvailableException))]
947+
public void TestEndSegmentWithSubsegmentUsingRuntimeError()
869948
{
870-
_recorder.BeginSegment("segment", TraceId);
871-
_recorder.BeginSubsegment("subsegment");
872-
_recorder.EndSegment();
949+
var recorder = new AWSXRayRecorder();
950+
recorder.ContextMissingStrategy = ContextMissingStrategy.RUNTIME_ERROR;
951+
recorder.BeginSegment("segment", TraceId);
952+
recorder.BeginSubsegment("subsegment");
953+
recorder.EndSegment();
873954
}
874955

875956
[TestMethod]
@@ -882,7 +963,7 @@ public void TestSuppressEntityNotAvailableException()
882963
[TestMethod]
883964
public void TestDefaultValueOfContextMissingStrategy()
884965
{
885-
Assert.AreEqual(ContextMissingStrategy.RUNTIME_ERROR, _recorder.ContextMissingStrategy);
966+
Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, _recorder.ContextMissingStrategy);
886967
}
887968

888969
[TestMethod]
@@ -911,8 +992,9 @@ public void TestLogErrorModeForContextMissingStrategy()
911992
[TestMethod]
912993
public void TestDefaultContextMissingStrategy()
913994
{
995+
// Environment.SetEnvironmentVariable(AWSXRayRecorder.EnvironmentVariableContextMissingStrategy, "log_error");
914996
var recorder = AWSXRayRecorder.Instance;
915-
Assert.AreEqual(ContextMissingStrategy.RUNTIME_ERROR, recorder.ContextMissingStrategy);
997+
Assert.AreEqual(ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy);
916998
}
917999

9181000
[TestMethod]

sdk/test/UnitTests/TestAppSettings.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ public void TestUseRuntimeErrorsInvalid()
9595
ConfigurationManager.AppSettings[UseRuntimeErrors] = "invalid";
9696
AppSettings.Reset();
9797
var recorder = GetRecorder();
98-
Assert.AreEqual(Core.Strategies.ContextMissingStrategy.RUNTIME_ERROR ,recorder.ContextMissingStrategy);
98+
Assert.AreEqual(Core.Strategies.ContextMissingStrategy.LOG_ERROR ,recorder.ContextMissingStrategy);
9999
}
100100

101101

102102
[TestMethod]
103-
public void TestUseRuntimeErrorsNoKeyPresent()
103+
public void TestUseLogErrorsNoKeyPresent()
104104
{
105105
AppSettings.Reset();
106106
var recorder = GetRecorder();
107-
Assert.AreEqual(Core.Strategies.ContextMissingStrategy.RUNTIME_ERROR, recorder.ContextMissingStrategy);
107+
Assert.AreEqual(Core.Strategies.ContextMissingStrategy.LOG_ERROR, recorder.ContextMissingStrategy);
108108
}
109109

110110
[TestMethod]

sdk/test/UnitTests/TestXRayOptions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void TestInitializeInstance()
127127
Assert.IsNull(_xRayOptions.AwsServiceHandlerManifest);
128128
Assert.IsNull(_xRayOptions.PluginSetting);
129129
Assert.IsNull(_xRayOptions.SamplingRuleManifest);
130-
Assert.IsTrue(_xRayOptions.UseRuntimeErrors);
130+
Assert.IsFalse(_xRayOptions.UseRuntimeErrors);
131131

132132
Assert.AreEqual(typeof(UdpSegmentEmitter), AWSXRayRecorder.Instance.Emitter.GetType()); // Default emitter set
133133

@@ -147,7 +147,7 @@ public void TestInitializeInstanceWithRecorder1()
147147
Assert.IsNull(_xRayOptions.AwsServiceHandlerManifest);
148148
Assert.IsNull(_xRayOptions.PluginSetting);
149149
Assert.IsNull(_xRayOptions.SamplingRuleManifest);
150-
Assert.IsTrue(_xRayOptions.UseRuntimeErrors);
150+
Assert.IsFalse(_xRayOptions.UseRuntimeErrors);
151151

152152
Assert.AreEqual(AWSXRayRecorder.Instance.SamplingStrategy, recorder.SamplingStrategy); // Custom recorder set in TraceContext
153153
Assert.AreEqual(typeof(UdpSegmentEmitter), recorder.Emitter.GetType()); // Default emitter set
@@ -167,7 +167,7 @@ public void TestInitializeInstanceWithRecorder2()
167167
Assert.IsNull(_xRayOptions.AwsServiceHandlerManifest);
168168
Assert.IsNull(_xRayOptions.PluginSetting);
169169
Assert.IsNull(_xRayOptions.SamplingRuleManifest);
170-
Assert.IsTrue(_xRayOptions.UseRuntimeErrors);
170+
Assert.IsFalse(_xRayOptions.UseRuntimeErrors);
171171

172172
Assert.AreEqual(AWSXRayRecorder.Instance.SamplingStrategy, recorder.SamplingStrategy); // Custom recorder set in TraceContext
173173
Assert.AreEqual(typeof(DummyEmitter), recorder.Emitter.GetType()); // custom emitter set
@@ -201,16 +201,16 @@ public void TestUseRuntimeErrorsTrue()
201201
}
202202

203203
[TestMethod]
204-
public void TestUseRuntimeErrorsDefaultsTrue_WhenNotSpecifiedInJson()
204+
public void TestUseRuntimeErrorsDefaultsFalse_WhenNotSpecifiedInJson()
205205
{
206206
IConfiguration configuration = TestXRayOptions.BuildConfiguration("DisabledXRayMissing.json");
207207

208208
_xRayOptions = XRayConfiguration.GetXRayOptions(configuration);
209209

210210
AWSXRayRecorder.InitializeInstance(configuration);
211211

212-
Assert.IsTrue(_xRayOptions.UseRuntimeErrors);
213-
Assert.AreEqual(AWSXRayRecorder.Instance.ContextMissingStrategy, Core.Strategies.ContextMissingStrategy.RUNTIME_ERROR);
212+
Assert.IsFalse(_xRayOptions.UseRuntimeErrors);
213+
Assert.AreEqual(AWSXRayRecorder.Instance.ContextMissingStrategy, Core.Strategies.ContextMissingStrategy.LOG_ERROR);
214214
}
215215

216216
[TestMethod]

0 commit comments

Comments
 (0)