From 44096c8072ab45e8409d73931a33c87d2fbedaa3 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 14 Sep 2023 18:44:01 +0200 Subject: [PATCH] [SDK] Allow metric instrument names to contain / characters (#2310) --- sdk/src/metrics/instrument_metadata_validator.cc | 12 +++++++----- .../metrics/instrument_metadata_validator_test.cc | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sdk/src/metrics/instrument_metadata_validator.cc b/sdk/src/metrics/instrument_metadata_validator.cc index aba6344efc..ff963c3dd1 100644 --- a/sdk/src/metrics/instrument_metadata_validator.cc +++ b/sdk/src/metrics/instrument_metadata_validator.cc @@ -17,8 +17,8 @@ namespace sdk { namespace metrics { -// instrument-name = ALPHA 0*254 ("_" / "." / "-" / ALPHA / DIGIT) -const std::string kInstrumentNamePattern = "[a-zA-Z][-_.a-zA-Z0-9]{0,254}"; +// instrument-name = ALPHA 0*254 ("_" / "." / "-" / "/" / ALPHA / DIGIT) +const std::string kInstrumentNamePattern = "[a-zA-Z][-_./a-zA-Z0-9]{0,254}"; // const std::string kInstrumentUnitPattern = "[\x01-\x7F]{0,63}"; // instrument-unit = It can have a maximum length of 63 ASCII chars @@ -49,9 +49,11 @@ bool InstrumentMetaDataValidator::ValidateName(nostd::string_view name) const { return false; } - // subsequent chars should be either of alphabets, digits, underscore, minus, dot - return !std::any_of(std::next(name.begin()), name.end(), - [](char c) { return !isalnum(c) && c != '-' && c != '_' && c != '.'; }); + // subsequent chars should be either of alphabets, digits, underscore, + // minus, dot, slash + return !std::any_of(std::next(name.begin()), name.end(), [](char c) { + return !isalnum(c) && (c != '-') && (c != '_') && (c != '.') && (c != '/'); + }); #endif } diff --git a/sdk/test/metrics/instrument_metadata_validator_test.cc b/sdk/test/metrics/instrument_metadata_validator_test.cc index 0f9e7134a7..17b76a9419 100644 --- a/sdk/test/metrics/instrument_metadata_validator_test.cc +++ b/sdk/test/metrics/instrument_metadata_validator_test.cc @@ -24,6 +24,7 @@ TEST(InstrumentMetadataValidator, TestName) "123€AAA€BBB", // unicode characters "/\\sdsd", // string starting with special character "***sSSs", // string starting with special character + "a\\broken\\path", // contains backward slash CreateVeryLargeString(25) + "X", // total 256 characters CreateVeryLargeString(26), // string much bigger than 255 characters }; @@ -37,6 +38,7 @@ TEST(InstrumentMetadataValidator, TestName) "s123", // starting with char, followed by numbers "dsdsdsd_-.", // string , and valid nonalphanumeric "d1234_-sDSDs.sdsd344", // combination of all valid characters + "a/path/to/some/metric", // contains forward slash CreateVeryLargeString(5) + "ABCERTYG", // total 63 characters CreateVeryLargeString(5) + "ABCERTYGJ", // total 64 characters CreateVeryLargeString(24) + "ABCDEFGHI", // total 254 characters