-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize label names to comply with Prometheus requirements (#1253)
* Sanitize label names to comply with Prometheus requirements Signed-off-by: Tom Kerkhove <[email protected]> * Add new metric Signed-off-by: Tom Kerkhove <[email protected]> * Description Signed-off-by: Tom Kerkhove <[email protected]>
- Loading branch information
1 parent
014c89a
commit d423406
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Promitor.Integrations.Sinks.Prometheus/Extensions/StringExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// ReSharper disable once CheckNamespace | ||
|
||
namespace System | ||
{ | ||
public static class StringExtensions | ||
{ | ||
/// <summary> | ||
/// Ensures label key name is valid according to Prometheus requirements | ||
/// </summary> | ||
/// <param name="labelKey">Current label key value</param> | ||
public static string SanitizeForPrometheusLabelKey(this string labelKey) | ||
{ | ||
if (string.IsNullOrWhiteSpace(labelKey)) | ||
{ | ||
return labelKey; | ||
} | ||
|
||
return labelKey.Replace("/", "_").ToLower(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Promitor.Tests.Unit/Prometheus/StringExtensionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using Xunit; | ||
|
||
namespace Promitor.Tests.Unit.Prometheus | ||
{ | ||
[Category("Unit")] | ||
public class StringExtensionTests | ||
{ | ||
[Fact] | ||
public void SanitizeForPrometheusLabelKey_ValidKey_IdenticalOutput() | ||
{ | ||
// Arrange | ||
var labelKeyInput = "sample_label"; | ||
|
||
// Act | ||
var sanitizedLabelKey = labelKeyInput.SanitizeForPrometheusLabelKey(); | ||
|
||
// Assert | ||
Assert.Equal(labelKeyInput, sanitizedLabelKey); | ||
} | ||
|
||
[Fact] | ||
public void SanitizeForPrometheusLabelKey_KeyIsUppercase_ConvertedToLowercase() | ||
{ | ||
// Arrange | ||
var labelKeyInput = "SAMPLE_LABEL"; | ||
var expectedLabelKey = "sample_label"; | ||
|
||
// Act | ||
var sanitizedLabelKey = labelKeyInput.SanitizeForPrometheusLabelKey(); | ||
|
||
// Assert | ||
Assert.Equal(expectedLabelKey, sanitizedLabelKey); | ||
} | ||
|
||
[Fact] | ||
public void SanitizeForPrometheusLabelKey_KeyWithSlash_SlashIsReplaced() | ||
{ | ||
// Arrange | ||
var labelKeyInput = "sample/label"; | ||
var expectedLabelKey = "sample_label"; | ||
|
||
// Act | ||
var sanitizedLabelKey = labelKeyInput.SanitizeForPrometheusLabelKey(); | ||
|
||
// Assert | ||
Assert.Equal(expectedLabelKey, sanitizedLabelKey); | ||
} | ||
} | ||
} |