Skip to content

Commit 0791d94

Browse files
committed
Revert more non-validation changes
1 parent ad2e430 commit 0791d94

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/ActionDependencyAnalyzerTests.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,21 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
142142
[Fact]
143143
public async Task ProcessInfoTest()
144144
{
145-
CollectionRuleActionOptions actionOptions = null;
145+
PassThroughOptions settings = null;
146146
await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
147147
{
148148
CollectionRuleOptions options = rootOptions.CreateCollectionRule(DefaultRuleName)
149149
.AddPassThroughAction("a1", ConfigurationTokenParser.ProcessNameReference, ConfigurationTokenParser.ProcessIdReference, ConfigurationTokenParser.CommandLineReference)
150150
.SetStartupTrigger();
151151

152-
actionOptions = options.Actions.Last();
152+
settings = (PassThroughOptions)options.Actions.Last().Settings;
153153
}, host =>
154154
{
155155
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeoutMs);
156156

157157
CollectionRuleOptions ruleOptions = host.Services.GetRequiredService<IOptionsMonitor<CollectionRuleOptions>>().Get(DefaultRuleName);
158158
ILogger<CollectionRuleService> logger = host.Services.GetRequiredService<ILogger<CollectionRuleService>>();
159159
TimeProvider timeProvider = host.Services.GetRequiredService<TimeProvider>();
160-
ICollectionRuleActionOperations actionOperations = host.Services.GetRequiredService<ICollectionRuleActionOperations>();
161160

162161
const string processName = "actionProcess";
163162
const int processId = 123;
@@ -166,8 +165,8 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
166165
Guid instanceId = Guid.NewGuid();
167166
CollectionRuleContext context = new(DefaultRuleName, ruleOptions, new TestProcessInfo(instanceId, processId: processId, commandLine: commandLine), HostInfo.GetCurrent(timeProvider), logger);
168167

169-
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context, actionOperations);
170-
PassThroughOptions newSettings = (PassThroughOptions)analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, actionOptions);
168+
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context);
169+
PassThroughOptions newSettings = (PassThroughOptions)analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, settings);
171170

172171
Assert.Equal(processName, newSettings.Input1);
173172
Assert.Equal(processId.ToString(CultureInfo.InvariantCulture), newSettings.Input2);
@@ -182,30 +181,29 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
182181
[Fact]
183182
public async Task HostInfoTest()
184183
{
185-
CollectionRuleActionOptions actionOptions = null;
184+
PassThroughOptions settings = null;
186185
await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
187186
{
188187
CollectionRuleOptions options = rootOptions.CreateCollectionRule(DefaultRuleName)
189188
.AddPassThroughAction("a1", ConfigurationTokenParser.HostNameReference, ConfigurationTokenParser.UnixTimeReference, "test")
190189
.SetStartupTrigger();
191190

192-
actionOptions = options.Actions.Last();
191+
settings = (PassThroughOptions)options.Actions.Last().Settings;
193192
}, host =>
194193
{
195194
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeoutMs);
196195

197196
CollectionRuleOptions ruleOptions = host.Services.GetRequiredService<IOptionsMonitor<CollectionRuleOptions>>().Get(DefaultRuleName);
198197
ILogger<CollectionRuleService> logger = host.Services.GetRequiredService<ILogger<CollectionRuleService>>();
199198
MockTimeProvider timeProvider = host.Services.GetRequiredService<TimeProvider>() as MockTimeProvider;
200-
ICollectionRuleActionOperations actionOperations = host.Services.GetRequiredService<ICollectionRuleActionOperations>();
201199

202200
const string hostName = "exampleHost";
203201
Guid instanceId = Guid.NewGuid();
204202
HostInfo hostInfo = new HostInfo(hostName, timeProvider);
205203
CollectionRuleContext context = new(DefaultRuleName, ruleOptions, new TestProcessInfo(instanceId), hostInfo, logger);
206204

207-
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context, actionOperations);
208-
PassThroughOptions newSettings = (PassThroughOptions)analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, actionOptions);
205+
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context);
206+
PassThroughOptions newSettings = (PassThroughOptions)analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, settings);
209207

210208
Assert.Equal(hostName, newSettings.Input1);
211209
Assert.Equal(hostInfo.TimeProvider.GetUtcNow().ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture), newSettings.Input2);
@@ -225,30 +223,29 @@ public async Task InvalidTokenReferenceTest()
225223
string a2input3 = "$(Actions.a1.MissingResult)";
226224

227225
LogRecord record = new LogRecord();
228-
CollectionRuleActionOptions actionOptions = null;
226+
PassThroughOptions settings = null;
229227
await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
230228
{
231229
CollectionRuleOptions options = rootOptions.CreateCollectionRule(DefaultRuleName)
232230
.AddPassThroughAction("a1", "a1input1", "a1input2", "a1input3")
233231
.AddPassThroughAction("a2", a2input1, a2input2, a2input3)
234232
.SetStartupTrigger();
235233

236-
actionOptions = options.Actions.Last();
234+
settings = (PassThroughOptions)options.Actions.Last().Settings;
237235
}, host =>
238236
{
239237
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeoutMs);
240238

241239
CollectionRuleOptions ruleOptions = host.Services.GetRequiredService<IOptionsMonitor<CollectionRuleOptions>>().Get(DefaultRuleName);
242240
ILogger<CollectionRuleService> logger = host.Services.GetRequiredService<ILogger<CollectionRuleService>>();
243241
TimeProvider timeProvider = host.Services.GetRequiredService<TimeProvider>();
244-
ICollectionRuleActionOperations actionOperations = host.Services.GetRequiredService<ICollectionRuleActionOperations>();
245242

246243
Guid instanceId = Guid.NewGuid();
247244
CollectionRuleContext context = new(DefaultRuleName, ruleOptions, new TestProcessInfo(instanceId), HostInfo.GetCurrent(timeProvider), logger);
248245

249-
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context, actionOperations);
246+
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context);
250247
analyzer.GetActionDependencies(1);
251-
analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, actionOptions);
248+
analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, settings);
252249

253250
Assert.Equal(3, record.Events.Count);
254251
Assert.Equal(LoggingEventIds.InvalidActionReferenceToken.Id(), record.Events[0].EventId.Id);
@@ -267,28 +264,27 @@ await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
267264
[Fact]
268265
public async Task RuntimeIdReferenceTest()
269266
{
270-
CollectionRuleActionOptions actionOptions = null;
267+
PassThroughOptions settings = null;
271268
await TestHostHelper.CreateCollectionRulesHost(_outputHelper, rootOptions =>
272269
{
273270
CollectionRuleOptions options = rootOptions.CreateCollectionRule(DefaultRuleName)
274271
.AddPassThroughAction("a1", ConfigurationTokenParser.RuntimeIdReference, "test", "test")
275272
.SetStartupTrigger();
276273

277-
actionOptions = options.Actions.Last();
278-
}, host =>
274+
settings = (PassThroughOptions)options.Actions.Last().Settings;
275+
}, host =>
279276
{
280277
using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeoutMs);
281278

282279
CollectionRuleOptions ruleOptions = host.Services.GetRequiredService<IOptionsMonitor<CollectionRuleOptions>>().Get(DefaultRuleName);
283280
ILogger<CollectionRuleService> logger = host.Services.GetRequiredService<ILogger<CollectionRuleService>>();
284281
TimeProvider timeProvider = host.Services.GetRequiredService<TimeProvider>();
285-
ICollectionRuleActionOperations actionOperations = host.Services.GetRequiredService<ICollectionRuleActionOperations>();
286282

287283
Guid instanceId = Guid.NewGuid();
288284
CollectionRuleContext context = new(DefaultRuleName, ruleOptions, new TestProcessInfo(instanceId), HostInfo.GetCurrent(timeProvider), logger);
289285

290-
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context, actionOperations);
291-
PassThroughOptions newSettings = (PassThroughOptions)analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, actionOptions);
286+
ActionOptionsDependencyAnalyzer analyzer = ActionOptionsDependencyAnalyzer.Create(context);
287+
PassThroughOptions newSettings = (PassThroughOptions)analyzer.SubstituteOptionValues(new Dictionary<string, CollectionRuleActionResult>(), 1, settings);
292288

293289
Assert.Equal(instanceId.ToString("D"), newSettings.Input1);
294290

src/Tools/dotnet-monitor/CollectionRules/ActionListExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<IDictionary<string, CollectionRuleActionResult>> ExecuteAction
4949
List<ActionCompletionEntry> deferredCompletions = new(context.Options.Actions.Count);
5050

5151
var actionResults = new Dictionary<string, CollectionRuleActionResult>(StringComparer.Ordinal);
52-
var dependencyAnalyzer = ActionOptionsDependencyAnalyzer.Create(context, _actionOperations);
52+
var dependencyAnalyzer = ActionOptionsDependencyAnalyzer.Create(context);
5353

5454
try
5555
{
@@ -91,7 +91,7 @@ public async Task<IDictionary<string, CollectionRuleActionResult>> ExecuteAction
9191
));
9292
}
9393

94-
object? newSettings = dependencyAnalyzer.SubstituteOptionValues(actionResults, actionIndex, actionOption);
94+
object? newSettings = dependencyAnalyzer.SubstituteOptionValues(actionResults, actionIndex, actionOption.Settings);
9595
ICollectionRuleAction? action = factory.Create(context.ProcessInfo, newSettings);
9696

9797
try

src/Tools/dotnet-monitor/CollectionRules/ActionOptionsDependencyAnalyzer.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ internal sealed class ActionOptionsDependencyAnalyzer
3434

3535
private readonly CollectionRuleContext _ruleContext;
3636
private readonly ConfigurationTokenParser _tokenParser;
37-
private readonly ICollectionRuleActionOperations _actionOperations;
3837

3938
//Use action index instead of name, since it's possible for an unnamed action to have named dependencies.
4039
#nullable disable
@@ -78,18 +77,17 @@ public string GetActionResultToken() => string.Concat(ActionReferencePrefix,
7877
Action.Name, ConfigurationTokenParser.Separator, ResultName, ConfigurationTokenParser.SubstitutionSuffix);
7978
}
8079

81-
public static ActionOptionsDependencyAnalyzer Create(CollectionRuleContext context, ICollectionRuleActionOperations actionOperations)
80+
public static ActionOptionsDependencyAnalyzer Create(CollectionRuleContext context)
8281
{
83-
var analyzer = new ActionOptionsDependencyAnalyzer(context, new ConfigurationTokenParser(context.Logger), actionOperations);
82+
var analyzer = new ActionOptionsDependencyAnalyzer(context, new ConfigurationTokenParser(context.Logger));
8483
analyzer.EnsureDependencies();
8584
return analyzer;
8685
}
8786

88-
private ActionOptionsDependencyAnalyzer(CollectionRuleContext context, ConfigurationTokenParser tokenParser, ICollectionRuleActionOperations actionOperations)
87+
private ActionOptionsDependencyAnalyzer(CollectionRuleContext context, ConfigurationTokenParser tokenParser)
8988
{
9089
_ruleContext = context ?? throw new ArgumentNullException(nameof(context));
9190
_tokenParser = tokenParser ?? throw new ArgumentNullException(nameof(tokenParser));
92-
_actionOperations = actionOperations ?? throw new ArgumentNullException(nameof(actionOperations));
9391
}
9492

9593
#nullable disable
@@ -114,9 +112,8 @@ public IList<CollectionRuleActionOptions> GetActionDependencies(int actionIndex)
114112
}
115113
#nullable restore
116114

117-
public object? SubstituteOptionValues(IDictionary<string, CollectionRuleActionResult> actionResults, int actionIndex, CollectionRuleActionOptions actionOptions)
115+
public object? SubstituteOptionValues(IDictionary<string, CollectionRuleActionResult> actionResults, int actionIndex, object? settings)
118116
{
119-
object? settings = actionOptions.Settings;
120117
//Attempt to substitute context properties.
121118
object? originalSettings = settings;
122119

src/Tools/dotnet-monitor/CollectionRules/Configuration/CollectionRulePostConfigureNamedOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Extensions.Configuration;
1111
using Microsoft.Extensions.Options;
1212
using System.Collections.Generic;
13+
using System.ComponentModel.DataAnnotations;
1314
using System.Globalization;
1415

1516
namespace Microsoft.Diagnostics.Tools.Monitor.CollectionRules.Configuration
@@ -132,7 +133,7 @@ private void ResolveLimits(CollectionRuleOptions ruleOptions, IConfigurationSect
132133
if (!templatesOptions.TryGetValue(templateKey, out templatesValue))
133134
{
134135
templatesValue = new();
135-
ruleOptions.ErrorList.Add((string.Format(CultureInfo.CurrentCulture, Strings.ErrorMessage_TemplateNotFound, templateKey), memberName));
136+
ruleOptions.ErrorList.Add(new ValidationResult(string.Format(CultureInfo.CurrentCulture, Strings.ErrorMessage_TemplateNotFound, templateKey), [memberName]));
136137
return false;
137138
}
138139

src/Tools/dotnet-monitor/CollectionRules/Options/CollectionRuleOptions.Validate.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext vali
1717
List<ValidationResult> results = new();
1818

1919
// ErrorList is populated by incorrectly using templates - this will be empty if all templates names can be resolved or if templates are not used.
20-
// results.AddRange(ErrorList);
21-
foreach (var error in ErrorList)
22-
{
23-
results.Add(new ValidationResult(error.Error, [error.MemberName]));
24-
}
20+
results.AddRange(ErrorList);
2521

2622
if (results.Count > 0)
2723
{

src/Tools/dotnet-monitor/CollectionRules/Options/CollectionRuleOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ internal sealed partial class CollectionRuleOptions
3737
Description = nameof(OptionsDisplayStrings.DisplayAttributeDescription_CollectionRuleOptions_Limits))]
3838
public CollectionRuleLimitsOptions? Limits { get; set; }
3939

40-
internal List<(string Error, string MemberName)> ErrorList { get; } = new List<(string Error, string MemberName)>();
40+
internal List<ValidationResult> ErrorList { get; } = new List<ValidationResult>();
4141
}
4242
}

0 commit comments

Comments
 (0)