Skip to content

Commit

Permalink
Change MetricSpec.TryParse to be Parse (#78729)
Browse files Browse the repository at this point in the history
* Change MetricSpec.TryParse to be Parse

It was always creating a MetricSpec and returning true.

* Update src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/MetricsEventSource.cs

Co-authored-by: Theodore Tsirpanis <[email protected]>

Co-authored-by: Theodore Tsirpanis <[email protected]>
  • Loading branch information
stephentoub and teo-tsirpanis authored Nov 27, 2022
1 parent 77de784 commit 62e87b4
Showing 1 changed file with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,24 +386,19 @@ private void ParseSpecs(string? metricsSpecs)
{
return;
}

string[] specStrings = metricsSpecs.Split(s_instrumentSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string specString in specStrings)
{
if (!MetricSpec.TryParse(specString, out MetricSpec spec))
MetricSpec spec = MetricSpec.Parse(specString);
Parent.Message($"Parsed metric: {spec}");
if (spec.InstrumentName != null)
{
Parent.Message($"Failed to parse metric spec: {specString}");
_aggregationManager!.Include(spec.MeterName, spec.InstrumentName);
}
else
{
Parent.Message($"Parsed metric: {spec}");
if (spec.InstrumentName != null)
{
_aggregationManager!.Include(spec.MeterName, spec.InstrumentName);
}
else
{
_aggregationManager!.Include(spec.MeterName);
}
_aggregationManager!.Include(spec.MeterName);
}
}
}
Expand Down Expand Up @@ -467,20 +462,18 @@ public MetricSpec(string meterName, string? instrumentName)
InstrumentName = instrumentName;
}

public static bool TryParse(string text, out MetricSpec spec)
public static MetricSpec Parse(string text)
{
int slashIdx = text.IndexOf(MeterInstrumentSeparator);
if (slashIdx == -1)
if (slashIdx < 0)
{
spec = new MetricSpec(text.Trim(), null);
return true;
return new MetricSpec(text.Trim(), null);
}
else
{
string meterName = text.Substring(0, slashIdx).Trim();
string? instrumentName = text.Substring(slashIdx + 1).Trim();
spec = new MetricSpec(meterName, instrumentName);
return true;
string meterName = text.AsSpan(0, slashIdx).Trim().ToString();
string? instrumentName = text.AsSpan(slashIdx + 1).Trim().ToString();
return new MetricSpec(meterName, instrumentName);
}
}

Expand Down

0 comments on commit 62e87b4

Please sign in to comment.