Skip to content

Commit a6ed77d

Browse files
committed
Fix: Handle multiple tags in dotnet-counters display
Fixes #5683 The ConsoleWriter was not properly validating tag parsing, causing IndexOutOfRangeException when processing tags. This prevented subsequent tags from being displayed. Changes: - Split tags with max count of 2 to handle values containing '=' - Validate array length before accessing elements - Trim whitespace from tag keys - Skip malformed tags gracefully instead of crashing This ensures all tags in multi-dimensional metrics are displayed correctly in dotnet-counters.
1 parent 1215ad8 commit a6ed77d

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

src/Tools/dotnet-counters/Exporters/ConsoleWriter.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,33 @@ private bool RenderTagSetsInColumnMode(ref int row, ObservedCounter counter)
192192
for (int i = 0; i < tags.Length; i++)
193193
{
194194
string tag = tags[i];
195-
string[] keyValue = tag.Split("=");
196-
int posTag = observedTags.FindIndex (tag => tag.header == keyValue[0]);
195+
string[] keyValue = tag.Split(new char[] { '=' }, 2); // Split into at most 2 parts to handle values with '='
196+
197+
// Skip malformed tags that don't have both key and value
198+
if (keyValue.Length != 2)
199+
{
200+
continue;
201+
}
202+
203+
string key = keyValue[0].Trim();
204+
string value = keyValue[1];
205+
206+
// Skip empty keys
207+
if (string.IsNullOrEmpty(key))
208+
{
209+
continue;
210+
}
211+
212+
int posTag = observedTags.FindIndex (tag => tag.header == key);
197213
if (posTag == -1)
198214
{
199-
observedTags.Add((keyValue[0], new string[counter.TagSets.Count]));
200-
columnHeaderLen.Add(keyValue[0].Length);
215+
observedTags.Add((key, new string[counter.TagSets.Count]));
216+
columnHeaderLen.Add(key.Length);
201217
maxValueColumnLen.Add(default(int));
202218
posTag = observedTags.Count - 1;
203219
}
204-
observedTags[posTag].values[tagsCount] = keyValue[1];
205-
maxValueColumnLen[posTag] = Math.Max(keyValue[1].Length, maxValueColumnLen[posTag]);
220+
observedTags[posTag].values[tagsCount] = value;
221+
maxValueColumnLen[posTag] = Math.Max(value.Length, maxValueColumnLen[posTag]);
206222
}
207223
tagsCount++;
208224
}

0 commit comments

Comments
 (0)