Skip to content

Commit

Permalink
Fixed normalization for metric tag values (#3281)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell authored Apr 12, 2024
1 parent a1d388f commit 025d60c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Fixed normalization for metric tag values for carriage return, line feed and tab characters ([#3281](https://github.com/getsentry/sentry-dotnet/pull/3281))

### Dependencies

- Bump Java SDK from v7.7.0 to v7.8.0 ([#3275](https://github.com/getsentry/sentry-dotnet/pull/3275))
Expand Down
9 changes: 5 additions & 4 deletions src/Sentry/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ internal static DateTimeOffset GetCutoff() => DateTimeOffset.UtcNow

private static readonly Lazy<KeyValuePair<string, string>[]> LazyTagValueReplacements = new(() =>
[
new KeyValuePair<string, string>("\n", "<LF>"),
new KeyValuePair<string, string>("\r", "<CR>"),
new KeyValuePair<string, string>("\t", "<HT>"),
new KeyValuePair<string, string>(@"\", @"\\"),
new KeyValuePair<string, string>("\n", @"\n"),
new KeyValuePair<string, string>("\r", @"\r"),
new KeyValuePair<string, string>("\t", @"\t"),
new KeyValuePair<string, string>("|", "\u007c"),
new KeyValuePair<string, string>(",", "\u002c")
]);
private static KeyValuePair<string, string>[] TagValueReplacements => LazyTagValueReplacements.Value;
internal static string SanitizeTagValue(string input)
{
// Replace back slashes before we add any of these ourselves when substituting "\n" for other control characters
input = input.Replace(@"\", @"\\");
foreach (var (reservedCharacter, replacementValue) in TagValueReplacements)
{
input = input.Replace(reservedCharacter, replacementValue);
Expand Down
8 changes: 4 additions & 4 deletions test/Sentry.Tests/MetricHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ public void SanitizeMetricKeyOrName_ReplacesInvalidCharactersWithUnderscore(stri

[Theory]
[InlineData("Test123_:/@.{}[]$-", "Test123_:/@.{}[]$-")] // Valid characters
[InlineData("test\nvalue", "test<LF>value")]
[InlineData("test\rvalue", "test<CR>value")]
[InlineData("test\tvalue", "test<HT>value")]
[InlineData("test\nvalue", @"test\nvalue")]
[InlineData("test\rvalue", @"test\rvalue")]
[InlineData("test\tvalue", @"test\tvalue")]
[InlineData(@"test\value", @"test\\value")]
[InlineData("test|value", "test\u007cvalue")]
[InlineData("test,value", "test\u002cvalue")]
public void SanitizeValue_ShouldReplaceReservedCharacters(string input, string expected)
public void SanitizeTagValue_ShouldReplaceReservedCharacters(string input, string expected)
{
// Act
var result = MetricHelper.SanitizeTagValue(input);
Expand Down

0 comments on commit 025d60c

Please sign in to comment.