Skip to content

Conversation

@steveisok
Copy link
Member

@steveisok steveisok commented Jan 21, 2026

Summary

Defensive improvements to tag parsing in dotnet-counters (Related to #5683)

While investigating issue #5683, I found that the RenderTagSetsInColumnMode method in ConsoleWriter.cs was parsing tags without proper validation, which could lead to crashes on malformed input.

Note: This PR does not fix the originally reported issue in #5683. Investigation revealed that multi-tag functionality was already working correctly (existing test LongMultidimensionalTagsAreTruncated demonstrates this). The originally reported issue may be environment-specific,
version-specific, or related to a different code path. This PR provides defensive improvements discovered during the investigation.

Changes

  • Split tags with max count of 2 to handle tag values containing = characters
  • Validate array length before accessing elements to prevent IndexOutOfRangeException
  • Trim whitespace from tag keys for robustness
  • Skip malformed tags gracefully instead of crashing

Benefits

  • Prevents potential crashes when encountering malformed tag input
  • Handles edge cases (values with =, leading/trailing whitespace, etc.)
  • Makes the code more robust and maintainable
  • No behavior changes for valid input

Testing

Added MalformedTagsDoNotCrash test that demonstrates the defensive fix prevents real crashes:

Scenarios tested:

  • Tags without = character (e.g., "BadTagNoEquals")
  • Whitespace-only tags
  • Mixed valid/invalid tags (e.g., "InvalidTag,color=blue")

Evidence:

  • Without the defensive fix: Test crashes with IndexOutOfRangeException at line 204 in RenderTagSetsInColumnMode when trying to access keyValue[1] on a malformed tag
  • With the defensive fix: Test passes - malformed tags are skipped gracefully, valid tags are processed correctly

Fixes dotnet#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.
Copilot AI review requested due to automatic review settings January 21, 2026 22:43
@steveisok steveisok requested a review from a team as a code owner January 21, 2026 22:43
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes an IndexOutOfRangeException that occurred when processing multi-dimensional metric tags containing '=' characters in their values. The fix adds robust validation and error handling to the tag parsing logic in dotnet-counters.

Changes:

  • Modified tag splitting to limit to 2 parts, preventing issues when tag values contain '=' characters
  • Added validation to skip malformed tags gracefully instead of crashing
  • Added key trimming and empty key filtering for robust tag processing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

string key = keyValue[0].Trim();
string value = keyValue[1];
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider trimming the value as well to handle potential whitespace around the '=' delimiter, similar to how the key is trimmed. For example, if a tag is formatted as "key = value" with spaces around the equals sign, the value would currently preserve the leading space.

Suggested change
string value = keyValue[1];
string value = keyValue[1].Trim();

Copilot uses AI. Check for mistakes.
@noahfalk
Copy link
Member

@steveisok - Issue #5683 didn't use the '=' sign in the tag names that were part of the provided repro and it sounds like that is what this PR fixes. If there is a bug displaying such tags I'm glad to see us fix it, but its not clear if we are addressing 5683 or this is an unrelated bug in the same scenario. Do we know whether the original repro code that doesn't use '=' demonstrates some other problem?

@steveisok steveisok force-pushed the fix-dotnet-counters-multiple-tags branch from 217c863 to a6ed77d Compare January 22, 2026 01:40
@steveisok steveisok changed the title Fix: Handle multiple tags in dotnet-counters display Add defensive validation to tag parsing in dotnet-counters Jan 22, 2026
@steveisok
Copy link
Member Author

@steveisok - Issue #5683 didn't use the '=' sign in the tag names that were part of the provided repro and it sounds like that is what this PR fixes. If there is a bug displaying such tags I'm glad to see us fix it, but its not clear if we are addressing 5683 or this is an unrelated bug in the same scenario. Do we know whether the original repro code that doesn't use '=' demonstrates some other problem?

I don't think this is a fix to #5683, but instead a defensive improvement discovered in the investigation.

Adds a regression test that verifies malformed tags (tags without '=',
whitespace-only tags, etc.) are handled gracefully without crashing.

Without the defensive code fix:
- Test FAILS with IndexOutOfRangeException at line 204
- Crash occurs when trying to access keyValue[1] on split result

With the defensive code fix:
- Test PASSES - malformed tags are skipped gracefully
- Valid tags in mixed scenarios are still processed correctly

This demonstrates the defensive improvements have real value in
preventing crashes on malformed input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants