Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions src/Tools/dotnet-counters/CounterMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ private void HandleBufferedEvents()

public async Task<ReturnCode> Monitor(
CancellationToken ct,
List<string> counter_list,
string counters,
int processId,
int refreshInterval,
Expand Down Expand Up @@ -206,7 +205,7 @@ public async Task<ReturnCode> Monitor(
{
// the launch command may misinterpret app arguments as the old space separated
// provider list so we need to ignore it in that case
_counterList = ConfigureCounters(counters, _processId != 0 ? counter_list : null);
_counterList = ConfigureCounters(counters);
_renderer = new ConsoleWriter(new DefaultConsole(useAnsi), showDeltaColumn:showDeltas);
_diagnosticsClient = holder.Client;
_settings = new MetricsPipelineSettings();
Expand Down Expand Up @@ -251,7 +250,6 @@ public async Task<ReturnCode> Monitor(
}
public async Task<ReturnCode> Collect(
CancellationToken ct,
List<string> counter_list,
string counters,
int processId,
int refreshInterval,
Expand Down Expand Up @@ -291,7 +289,7 @@ public async Task<ReturnCode> Collect(
{
// the launch command may misinterpret app arguments as the old space separated
// provider list so we need to ignore it in that case
_counterList = ConfigureCounters(counters, _processId != 0 ? counter_list : null);
_counterList = ConfigureCounters(counters);
_settings = new MetricsPipelineSettings();
_settings.Duration = duration == TimeSpan.Zero ? Timeout.InfiniteTimeSpan : duration;
_settings.MaxHistograms = maxHistograms;
Expand Down Expand Up @@ -362,7 +360,7 @@ private static void ValidateNonNegative(int value, string argName)
}
}

internal List<EventPipeCounterGroup> ConfigureCounters(string commaSeparatedProviderListText, List<string> providerList)
internal List<EventPipeCounterGroup> ConfigureCounters(string commaSeparatedProviderListText)
{
List<EventPipeCounterGroup> counters = new();
try
Expand All @@ -379,23 +377,6 @@ internal List<EventPipeCounterGroup> ConfigureCounters(string commaSeparatedProv
throw new CommandLineErrorException("Error parsing --counters argument: " + e.Message);
}

if (providerList != null)
{
try
{
foreach (string providerText in providerList)
{
ParseCounterProvider(providerText, counters);
}
}
catch (FormatException e)
{
// the FormatException message strings thrown by ParseCounterProvider are controlled
// by us and anticipate being integrated into the command-line error text.
throw new CommandLineErrorException("Error parsing counter_list: " + e.Message);
}
}

if (counters.Count == 0)
{
_stdOutput.WriteLine($"--counters is unspecified. Monitoring System.Runtime counters by default.");
Expand Down
14 changes: 1 addition & 13 deletions src/Tools/dotnet-counters/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ private static Command MonitorCommand()
name: "monitor",
description: "Start monitoring a .NET application")
{
CounterList,
CounterOption,
ProcessIdOption,
RefreshIntervalOption,
Expand All @@ -39,9 +38,8 @@ private static Command MonitorCommand()

monitorCommand.TreatUnmatchedTokensAsErrors = false; // see the logic in Main

monitorCommand.SetAction((parseResult, ct) => new CounterMonitor(parseResult.Configuration.Output, parseResult.Configuration.Error).Monitor(
monitorCommand.SetAction(static (parseResult, ct) => new CounterMonitor(parseResult.Configuration.Output, parseResult.Configuration.Error).Monitor(
ct,
counter_list: parseResult.GetValue(CounterList),
counters: parseResult.GetValue(CounterOption),
processId: parseResult.GetValue(ProcessIdOption),
refreshInterval: parseResult.GetValue(RefreshIntervalOption),
Expand All @@ -64,7 +62,6 @@ private static Command CollectCommand()
name: "collect",
description: "Monitor counters in a .NET application and export the result into a file")
{
CounterList,
CounterOption,
ProcessIdOption,
RefreshIntervalOption,
Expand All @@ -82,7 +79,6 @@ private static Command CollectCommand()

collectCommand.SetAction((parseResult, ct) => new CounterMonitor(parseResult.Configuration.Output, parseResult.Configuration.Error).Collect(
ct,
counter_list: parseResult.GetValue(CounterList),
counters: parseResult.GetValue(CounterOption),
processId: parseResult.GetValue(ProcessIdOption),
refreshInterval: parseResult.GetValue(RefreshIntervalOption),
Expand Down Expand Up @@ -143,14 +139,6 @@ private static Command CollectCommand()
" To discover well-known provider and counter names, please visit https://learn.microsoft.com/dotnet/core/diagnostics/built-in-metrics."
};

private static readonly Argument<List<string>> CounterList =
new(name: "counter_list")
{
Description = @"A space separated list of counter providers. Counters can be specified <provider_name> or <provider_name>[comma_separated_counter_names]. If the provider_name is used without a qualifying counter_names then all counters will be shown. To discover provider and counter names, use the list command.",
Hidden = true,
DefaultValueFactory = _ => new List<string>()
};

private static Command ListCommand()
{
Command listCommand = new(
Expand Down
3 changes: 1 addition & 2 deletions src/tests/dotnet-counters/CounterMonitorPayloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ await TestRunnerUtilities.ExecuteCollection((ct) => {
return Task.Run(async () =>
await monitor.Collect(
ct: ct,
counter_list: counterList,
counters: null,
counters: string.Join(',', counterList),
processId: testRunner.Pid,
refreshInterval: 1,
format: exportFormat,
Expand Down
37 changes: 6 additions & 31 deletions src/tests/dotnet-counters/CounterMonitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,27 @@ public void GenerateCounterListTestManyProvidersWithFilter()
public void GenerateCounterListWithOptionAndArgumentsTest()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
List<string> commandLineProviderArgs = new() { "System.Runtime", "MyEventSource" };
string countersOptionText = "MyEventSource1,MyEventSource2";
List<EventPipeCounterGroup> counters = monitor.ConfigureCounters(countersOptionText, commandLineProviderArgs);
Assert.Contains("MyEventSource", counters.Select(g => g.ProviderName));
List<EventPipeCounterGroup> counters = monitor.ConfigureCounters(countersOptionText);
Assert.Contains("MyEventSource1", counters.Select(g => g.ProviderName));
Assert.Contains("MyEventSource2", counters.Select(g => g.ProviderName));
Assert.Contains("System.Runtime", counters.Select(g => g.ProviderName));
}

[Fact]
public void GenerateCounterListWithOptionAndArgumentsTestWithDupEntries()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
List<string> commandLineProviderArgs = new() { "System.Runtime", "MyEventSource" };
string countersOptionText = "System.Runtime,MyEventSource";
List<EventPipeCounterGroup> counters = monitor.ConfigureCounters(countersOptionText, commandLineProviderArgs);
Assert.Equal(2, counters.Count());
Assert.Contains("MyEventSource", counters.Select(g => g.ProviderName));
Assert.Contains("System.Runtime", counters.Select(g => g.ProviderName));
}

[Fact]
public void ParseErrorUnbalancedBracketsInCountersArg()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
string countersOptionText = "System.Runtime[cpu-usage,MyEventSource";
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText, null));
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText));
Assert.Equal("Error parsing --counters argument: Expected to find closing ']' in counter_provider", e.Message);
}

[Fact]
public void ParseErrorUnbalancedBracketsInCounterList()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
string countersOptionText = "System.Runtime,MyEventSource";
List<string> commandLineProviderArgs = new() { "System.Runtime[cpu-usage", "MyEventSource" };
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText, commandLineProviderArgs));
Assert.Equal("Error parsing counter_list: Expected to find closing ']' in counter_provider", e.Message);
}

[Fact]
public void ParseErrorTrailingTextInCountersArg()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
string countersOptionText = "System.Runtime[cpu-usage]hello,MyEventSource";
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText, null));
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText));
Assert.Equal("Error parsing --counters argument: Unexpected characters after closing ']' in counter_provider", e.Message);
}

Expand All @@ -134,7 +109,7 @@ public void ParseErrorEmptyProvider()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
string countersOptionText = ",MyEventSource";
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText, null));
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText));
Assert.Equal("Error parsing --counters argument: Expected non-empty counter_provider", e.Message);
}

Expand All @@ -143,7 +118,7 @@ public void ParseErrorMultipleCounterLists()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
string countersOptionText = "System.Runtime[cpu-usage][working-set],MyEventSource";
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText, null));
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText));
Assert.Equal("Error parsing --counters argument: Expected at most one '[' in counter_provider", e.Message);
}

Expand All @@ -152,7 +127,7 @@ public void ParseErrorMultiplePrefixesOnSameProvider()
{
CounterMonitor monitor = new(TextWriter.Null, TextWriter.Null);
string countersOptionText = "System.Runtime,MyEventSource,EventCounters\\System.Runtime";
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText, null));
CommandLineErrorException e = Assert.Throws<CommandLineErrorException>(() => monitor.ConfigureCounters(countersOptionText));
Assert.Equal("Error parsing --counters argument: Using the same provider name with and without the EventCounters\\ prefix in the counter list is not supported.", e.Message);
}
}
Expand Down