-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RuleEngineTracker to control rule engine execution (#2407)
- Loading branch information
1 parent
765a89c
commit 44a24c4
Showing
7 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/OpenTelemetry.AutoInstrumentation.StartupHook/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// <copyright file="AssemblyInfo.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("OpenTelemetry.AutoInstrumentation.StartupHook.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...trumentation.StartupHook.Tests/OpenTelemetry.AutoInstrumentation.StartupHook.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\OpenTelemetry.AutoInstrumentation.StartupHook\OpenTelemetry.AutoInstrumentation.StartupHook.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
123 changes: 123 additions & 0 deletions
123
test/OpenTelemetry.AutoInstrumentation.StartupHook.Tests/RuleEngineTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// <copyright file="RuleEngineTests.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using OpenTelemetry.AutoInstrumentation.RulesEngine; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.AutoInstrumentation.StartupHook.Tests; | ||
|
||
public class RuleEngineTests : IDisposable | ||
{ | ||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable("OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED", null); | ||
} | ||
|
||
[Fact] | ||
public void RuleEngineValidation_WhenShouldTrackIsTrue() | ||
{ | ||
// Arrange | ||
SetShouldTrackEnvironmentVariable(true); | ||
var testRule = new TestRule(); | ||
var ruleEngine = new RuleEngine(new List<Rule> { testRule }); | ||
|
||
// Act | ||
var result = ruleEngine.Validate(); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(testRule.IsEvaluated); | ||
} | ||
|
||
[Fact] | ||
public void RuleEngineValidation_WhenShouldTrackIsFalse() | ||
{ | ||
// Arrange | ||
SetShouldTrackEnvironmentVariable(false); | ||
var testRule = new TestRule(); | ||
var ruleEngine = new RuleEngine(new List<Rule> { testRule }); | ||
|
||
// Act | ||
var result = ruleEngine.Validate(); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.False(testRule.IsEvaluated); | ||
} | ||
|
||
[Fact] | ||
public void RuleEngineValidation_WhenShouldTrackIsNull() | ||
{ | ||
// Arrange | ||
SetShouldTrackEnvironmentVariable(null); | ||
var testRule = new TestRule(); | ||
var ruleEngine = new RuleEngine(new List<Rule> { testRule }); | ||
|
||
// Act | ||
var result = ruleEngine.Validate(); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(testRule.IsEvaluated); | ||
} | ||
|
||
[Fact] | ||
public void RuleEngineValidation_WhenShouldTrackIsNotSet() | ||
{ | ||
// Arrange | ||
var testRule = new TestRule(); | ||
var ruleEngine = new RuleEngine(new List<Rule> { testRule }); | ||
|
||
// Act | ||
var result = ruleEngine.Validate(); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(testRule.IsEvaluated); | ||
} | ||
|
||
[Fact] | ||
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 }); | ||
|
||
// Act | ||
var result = ruleEngine.Validate(); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(testRule.IsEvaluated); | ||
} | ||
|
||
private void SetShouldTrackEnvironmentVariable(bool? value) | ||
{ | ||
Environment.SetEnvironmentVariable("OTEL_DOTNET_AUTO_RULE_ENGINE_ENABLED", value?.ToString()); | ||
} | ||
|
||
private class TestRule : Rule | ||
{ | ||
internal bool IsEvaluated { get; private set; } | ||
|
||
internal override bool Evaluate() | ||
{ | ||
IsEvaluated = true; | ||
return true; | ||
} | ||
} | ||
} |