AnyCollect  1.1.2
Matcher.h
Go to the documentation of this file.
1 //
2 // Matcher.h
3 //
4 // Created on October 25th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <map>
24 #include <optional>
25 #include <regex>
26 #include <string>
27 #include <string_view>
28 #include <vector>
29 
30 #include "Config.h"
31 #include "Metric.h"
32 
33 using namespace std::literals;
34 
35 
36 namespace AnyCollect {
40  class Matcher {
41  public:
42  static constexpr char matchEscapeChar = '\\';
43  static constexpr char matchSubstitutionPrefix = '$';
44  static constexpr std::string_view matchSubstitutionPathPrefix = "path_"sv;
45 
46  protected:
47  std::vector<std::string> name_;
48  std::string value_;
49  std::string unit_;
50  std::map<std::string, std::string> tags_;
51  bool computeRate_;
53 
54  public:
58  Matcher() noexcept;
59 
65  Matcher(const Config::expression::metric& config) noexcept;
66 
67 
71  const std::vector<std::string>& name() const noexcept;
72 
76  const std::string& value() const noexcept;
77 
81  const std::string& unit() const noexcept;
82 
86  const std::map<std::string, std::string>& tags() const noexcept;
87 
91  bool computeRate() const noexcept;
92 
96  bool convertToUnitsPerSecond() const noexcept;
97 
98 
102  void setName(const std::vector<std::string>& name) noexcept;
103 
107  void setValue(const std::string& value) noexcept;
108 
112  void setUnit(const std::string& unit) noexcept;
113 
117  void setTags(const std::map<std::string, std::string>& tags) noexcept;
118 
122  void setComputeRate(bool computeRate) noexcept;
123 
127  void setConvertToUnitsPerSecond(bool convertToUnitsPerSecond) noexcept;
128 
129 
137  std::optional<std::vector<std::string>> getName(const std::cmatch& match, const std::vector<std::string>& pathParts) const noexcept;
138 
146  std::optional<double> getValue(const std::cmatch& match, const std::vector<std::string>& pathParts) const noexcept;
147 
155  std::optional<std::string> getUnit(const std::cmatch& match, const std::vector<std::string>& pathParts) const noexcept;
156 
164  std::optional<std::map<std::string, std::string>> getTags(const std::cmatch& match, const std::vector<std::string>& pathParts) const noexcept;
165 
173  std::optional<Metric> getMetric(const std::cmatch& match, const std::vector<std::string>& pathParts) const noexcept;
174  };
175 }
std::map< std::string, std::string > tags_
Pattern for the tags of the metric.
Definition: Matcher.h:50
std::string value_
Pattern for the value of the metric.
Definition: Matcher.h:48
std::string unit_
Pattern for the unit of the metric.
Definition: Matcher.h:49
STL namespace.
bool computeRate_
Whether the metric is a rate.
Definition: Matcher.h:51
bool convertToUnitsPerSecond_
Whether the metric should be converted to units per second.
Definition: Matcher.h:52
Struct used to represent the JSON configuration file.
Definition: Config.h:35
Class used to represent a matcher, that is a way to convert expressions matches into a metric...
Definition: Matcher.h:40
std::vector< std::string > name_
Pattern for the name of the metric.
Definition: Matcher.h:47
Class used to represent a metric object (with a name, unit, tags, a value and a timestamp) ...
Definition: Metric.h:33
T getValue(const nlohmann::json &j, const K &key) noexcept
Parse a JSON value of specified type from a JSON dictionary.
Definition: Config.h:127