-
Notifications
You must be signed in to change notification settings - Fork 438
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) (#…
- 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.
dd7e257
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.BM_NaiveSpinLockThrashing/2/process_time/real_time
0.48668454163265923
ms/iter0.21761562383287006
ms/iter2.24
This comment was automatically generated by workflow using github-action-benchmark.
dd7e257
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.BM_BaselineBuffer/1
2891670.2270507812
ns/iter450627.0372365531
ns/iter6.42
BM_BaselineBuffer/2
6073367.595672607
ns/iter2755729.1984558105
ns/iter2.20
BM_LockFreeBuffer/1
3831773.9963531494
ns/iter343451.4616312253
ns/iter11.16
BM_LockFreeBuffer/2
4135840.41595459
ns/iter1313504.695892334
ns/iter3.15
This comment was automatically generated by workflow using github-action-benchmark.