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

Disable optional rules by default on the NuGet package scripts #2958

Merged
merged 8 commits into from
Sep 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set DOTNET_STARTUP_HOOKS=%BASE_PATH%OpenTelemetry.AutoInstrumentation.StartupHoo

:: Settings for OpenTelemetry
set OTEL_DOTNET_AUTO_HOME=%BASE_PATH%
set OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED=false

@echo on
%*
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export DOTNET_STARTUP_HOOKS=${BASE_PATH}/OpenTelemetry.AutoInstrumentation.Start

# Settings for OpenTelemetry
export OTEL_DOTNET_AUTO_HOME=${BASE_PATH}
export OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED=false

$@
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ internal class RuleEngine
new MinSupportedFrameworkRule()
};

private readonly List<Rule> _otherRules = new()
{
new OpenTelemetrySdkMinimumVersionRule(),
new AssemblyFileVersionRule(),
new NativeProfilerDiagnosticsRule()
};
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> rules)
internal RuleEngine(Lazy<List<Rule>> optionalRules)
{
_otherRules = rules;
_optionalRules = optionalRules;
}

internal bool ValidateRules()
Expand All @@ -65,7 +61,7 @@ internal bool ValidateRules()
}

// All the rules are validated here.
foreach (var rule in _otherRules)
foreach (var rule in _optionalRules.Value)
{
if (!EvaluateRule(rule))
{
Expand Down Expand Up @@ -93,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
Loading