Skip to content

Commit

Permalink
Add Lazy and method to create optional rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanotti committed Sep 15, 2023
1 parent 452ca11 commit 5efcf1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ internal class RuleEngine
new MinSupportedFrameworkRule()
};

private readonly List<Rule>? _optionalRules;
private readonly Lazy<List<Rule>> _optionalRules;

internal RuleEngine()
: this(new Lazy<List<Rule>>(CreateDefaultOptionalRules))
{
}

// This constructor is used for test purpose.
internal RuleEngine(List<Rule> optionalRules)
internal RuleEngine(Lazy<List<Rule>> optionalRules)
{
_optionalRules = optionalRules;
}
Expand All @@ -59,15 +60,8 @@ internal bool ValidateRules()
return result;
}

var optionalRules = _optionalRules ?? new()
{
new OpenTelemetrySdkMinimumVersionRule(),
new AssemblyFileVersionRule(),
new NativeProfilerDiagnosticsRule()
};

// All the rules are validated here.
foreach (var rule in optionalRules)
foreach (var rule in _optionalRules.Value)
{
if (!EvaluateRule(rule))
{
Expand Down Expand Up @@ -95,4 +89,14 @@ private static bool EvaluateRule(Rule rule)

return true;
}

private static List<Rule> CreateDefaultOptionalRules()
{
return new()
{
new OpenTelemetrySdkMinimumVersionRule(),
new AssemblyFileVersionRule(),
new NativeProfilerDiagnosticsRule()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void RuleEngineValidation_WhenShouldTrackIsTrue()
// Arrange
SetShouldTrackEnvironmentVariable(true);
var testRule = new TestRule();
var ruleEngine = new RuleEngine(new List<Rule> { testRule });
var ruleEngine = new RuleEngine(new Lazy<List<Rule>>(() => new() { testRule }));

// Act
var result = ruleEngine.ValidateRules();
Expand All @@ -48,7 +48,7 @@ public void RuleEngineValidation_WhenShouldTrackIsFalse()
// Arrange
SetShouldTrackEnvironmentVariable(false);
var testRule = new TestRule();
var ruleEngine = new RuleEngine(new List<Rule> { testRule });
var ruleEngine = new RuleEngine(new Lazy<List<Rule>>(() => new() { testRule }));

// Act
var result = ruleEngine.ValidateRules();
Expand All @@ -64,7 +64,7 @@ public void RuleEngineValidation_WhenShouldTrackIsNull()
// Arrange
SetShouldTrackEnvironmentVariable(null);
var testRule = new TestRule();
var ruleEngine = new RuleEngine(new List<Rule> { testRule });
var ruleEngine = new RuleEngine(new Lazy<List<Rule>>(() => new() { testRule }));

// Act
var result = ruleEngine.ValidateRules();
Expand All @@ -79,7 +79,7 @@ public void RuleEngineValidation_WhenShouldTrackIsNotSet()
{
// Arrange
var testRule = new TestRule();
var ruleEngine = new RuleEngine(new List<Rule> { testRule });
var ruleEngine = new RuleEngine(new Lazy<List<Rule>>(() => new() { testRule }));

// Act
var result = ruleEngine.ValidateRules();
Expand All @@ -95,7 +95,7 @@ public void RuleEngineValidation_WhenShouldTrackHasInvalidValue()
// Arrange
Environment.SetEnvironmentVariable("OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED", "Invalid");
var testRule = new TestRule();
var ruleEngine = new RuleEngine(new List<Rule> { testRule });
var ruleEngine = new RuleEngine(new Lazy<List<Rule>>(() => new() { testRule }));

// Act
var result = ruleEngine.ValidateRules();
Expand Down

0 comments on commit 5efcf1e

Please sign in to comment.