forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: 1712 - Validate Instrument meta data (name, unit, description) (o…
- Loading branch information
Showing
9 changed files
with
316 additions
and
9 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
38 changes: 38 additions & 0 deletions
38
sdk/include/opentelemetry/sdk/metrics/instrument_metadata_validator.h
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,38 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include <string> | ||
# include "opentelemetry/common/macros.h" | ||
# include "opentelemetry/nostd/string_view.h" | ||
# include "opentelemetry/version.h" | ||
|
||
# if HAVE_WORKING_REGEX | ||
# include <regex> | ||
# endif | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
class InstrumentMetaDataValidator | ||
{ | ||
public: | ||
InstrumentMetaDataValidator(); | ||
bool ValidateName(nostd::string_view name) const; | ||
bool ValidateUnit(nostd::string_view unit) const; | ||
bool ValidateDescription(nostd::string_view description) const; | ||
|
||
private: | ||
# if HAVE_WORKING_REGEX | ||
const std::regex name_reg_key_; | ||
const std::regex unit_reg_key_; | ||
# endif | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
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
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
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
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,80 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/sdk/metrics/instrument_metadata_validator.h" | ||
# include "opentelemetry/common/macros.h" | ||
# include "opentelemetry/nostd/string_view.h" | ||
|
||
# include <algorithm> | ||
# include <iostream> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
// instrument-name = ALPHA 0*62 ("_" / "." / "-" / ALPHA / DIGIT) | ||
const std::string kInstrumentNamePattern = "[a-zA-Z][-_.a-zA-Z0-9]{0,62}"; | ||
// | ||
const std::string kInstrumentUnitPattern = "[\x01-\x7F]{0,63}"; | ||
// instrument-unit = It can have a maximum length of 63 ASCII chars | ||
|
||
InstrumentMetaDataValidator::InstrumentMetaDataValidator() | ||
# if HAVE_WORKING_REGEX | ||
// clang-format off | ||
: name_reg_key_{kInstrumentNamePattern}, | ||
unit_reg_key_{kInstrumentUnitPattern} | ||
// clang-format on | ||
# endif | ||
{} | ||
|
||
bool InstrumentMetaDataValidator::ValidateName(nostd::string_view name) const | ||
{ | ||
|
||
# if HAVE_WORKING_REGEX | ||
return std::regex_match(name.data(), name_reg_key_); | ||
# else | ||
const size_t kMaxSize = 63; | ||
// size atmost 63 chars | ||
if (name.size() > kMaxSize) | ||
{ | ||
return false; | ||
} | ||
// first char should be alpha | ||
if (!isalpha(name[0])) | ||
{ | ||
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 != '.'; }); | ||
# endif | ||
} | ||
|
||
bool InstrumentMetaDataValidator::ValidateUnit(nostd::string_view unit) const | ||
{ | ||
# if HAVE_WORKING_REGEX | ||
return std::regex_match(unit.data(), unit_reg_key_); | ||
# else | ||
const size_t kMaxSize = 63; | ||
// length atmost 63 chars | ||
if (unit.size() > kMaxSize) | ||
{ | ||
return false; | ||
} | ||
// all should be ascii chars. | ||
return !std::any_of(unit.begin(), unit.end(), | ||
[](char c) { return static_cast<unsigned char>(c) > 127; }); | ||
# endif | ||
} | ||
|
||
bool InstrumentMetaDataValidator::ValidateDescription(nostd::string_view /*description*/) const | ||
{ | ||
return true; | ||
} | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
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
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
Oops, something went wrong.