23 #include <tinyexpr/tinyexpr.h> 36 computeRate_(config.computeRate),
37 convertToUnitsPerSecond_(config.convertToUnitsPerSecond)
53 const std::map<std::string, std::string>&
Matcher::tags() const noexcept {
83 this->computeRate_ = computeRate;
87 this->convertToUnitsPerSecond_ = convertToUnitsPerSecond;
91 inline uint64_t
parseUint(
const char*& buffer) noexcept {
93 while (!std::isdigit(*buffer))
95 while (std::isdigit(*buffer)) {
96 result = (result << 1) + (result << 3) + *buffer -
'0';
102 inline void replaceMatches(std::string& str,
const std::cmatch& match,
const std::vector<std::string>& pathParts) {
103 const char* buffer = str.data();
104 bool escaped =
false;
105 while (*buffer !=
'\0') {
108 size_t pos = buffer - str.data() - 1;
110 buffer = str.data() + pos;
120 size_t pos = buffer - str.data();
122 bool replace =
false;
123 std::string_view replacementString;
125 if (isdigit(*buffer)) {
128 if (i < match.size() && match[i].matched)
129 replacementString = std::string_view{match[i].first,
static_cast<size_t>(std::distance(match[i].first, match[i].second))};
136 if (i < pathParts.size())
137 replacementString = std::string_view{pathParts[i].data()};
141 str.replace(pos, buffer - str.data() - pos, replacementString);
142 buffer = str.data() + pos + replacementString.size();
152 std::optional<std::vector<std::string>>
Matcher::getName(
const std::cmatch& match,
const std::vector<std::string>& pathParts)
const noexcept {
153 std::vector<std::string> name = this->name_;
154 for (
auto& part : name) {
157 return std::optional<std::vector<std::string>>{};
159 return std::make_optional(std::move(name));
162 std::optional<double>
Matcher::getValue(
const std::cmatch& match,
const std::vector<std::string>& pathParts)
const noexcept {
163 std::string expression = this->value_;
166 double value = te_interp(expression.c_str(), &error);
168 return std::make_optional(value);
169 return std::optional<double>{};
172 std::optional<std::string>
Matcher::getUnit(
const std::cmatch& match,
const std::vector<std::string>& pathParts)
const noexcept {
173 std::string unit = this->unit_;
175 return std::make_optional(std::move(unit));
178 std::optional<std::map<std::string, std::string>>
Matcher::getTags(
const std::cmatch& match,
const std::vector<std::string>& pathParts)
const noexcept {
179 std::map<std::string, std::string> tags;
180 for (
auto& [key, value] : this->tags_) {
182 auto valueCopy = value;
185 if (keyCopy.empty() || valueCopy.empty())
186 return std::optional<std::map<std::string, std::string>>{};
188 tags.insert_or_assign(std::move(keyCopy), std::move(valueCopy));
190 return std::make_optional(std::move(tags));
193 std::optional<Metric>
Matcher::getMetric(
const std::cmatch& match,
const std::vector<std::string>& pathParts)
const noexcept {
194 auto name = this->getName(match, pathParts);
195 if (!name.has_value())
196 return std::optional<Metric>{};
197 auto unit = this->getUnit(match, pathParts);
198 if (!unit.has_value())
199 return std::optional<Metric>{};
200 auto tags = this->getTags(match, pathParts);
201 if (!tags.has_value())
202 return std::optional<Metric>{};
203 return std::make_optional<Metric>(std::move(name.value()), std::move(tags.value()), std::move(unit.value()));
void setConvertToUnitsPerSecond(bool convertToUnitsPerSecond) noexcept
Sets whether the metric should be converted to units per second.
void setTags(const std::map< std::string, std::string > &tags) noexcept
Sets the pattern for the tags of the metric.
const std::string & unit() const noexcept
Returns the pattern for the unit of the metric.
std::map< std::string, std::string > tags_
Pattern for the tags of the metric.
std::string value_
Pattern for the value of the metric.
std::string unit_
Pattern for the unit of the metric.
std::optional< std::string > getUnit(const std::cmatch &match, const std::vector< std::string > &pathParts) const noexcept
Use an expression match to compute the metric's unit.
const std::vector< std::string > & name() const noexcept
Returns the pattern for the name of the metric.
bool computeRate_
Whether the metric is a rate.
Matcher() noexcept
Construct a new Matcher object.
std::optional< std::vector< std::string > > getName(const std::cmatch &match, const std::vector< std::string > &pathParts) const noexcept
Use an expression match to compute the metric's name.
bool convertToUnitsPerSecond_
Whether the metric should be converted to units per second.
uint64_t parseUint(const char *&buffer) noexcept
void setComputeRate(bool computeRate) noexcept
Sets whether the metric is a rate.
std::optional< std::map< std::string, std::string > > getTags(const std::cmatch &match, const std::vector< std::string > &pathParts) const noexcept
Use an expression match to compute the metric's tags.
const std::string & value() const noexcept
Returns the pattern for the value of the metric.
bool computeRate() const noexcept
Returns whether the metric is a rate.
std::optional< double > getValue(const std::cmatch &match, const std::vector< std::string > &pathParts) const noexcept
Use an expression match to compute the metric's value.
static constexpr char matchEscapeChar
Escape character.
std::vector< std::string > name_
Pattern for the name of the metric.
static constexpr char matchSubstitutionPrefix
Variables prefix character.
void setUnit(const std::string &unit) noexcept
Sets the pattern for the unit of the metric.
const std::map< std::string, std::string > & tags() const noexcept
Returns the pattern for the tags of the metric.
static constexpr std::string_view matchSubstitutionPathPrefix
Path part prefix variable string.
void setValue(const std::string &value) noexcept
Sets the pattern for the value of the metric.
std::optional< Metric > getMetric(const std::cmatch &match, const std::vector< std::string > &pathParts) const noexcept
Use an expression match to compute the metric.
bool convertToUnitsPerSecond() const noexcept
Returns whether the metric should be converted to units per second.
void setName(const std::vector< std::string > &name) noexcept
Sets the pattern for the name of the metric.
void replaceMatches(std::string &str, const std::cmatch &match, const std::vector< std::string > &pathParts)