AnyCollect  1.1.2
Metric.h
Go to the documentation of this file.
1 //
2 // Metric.h
3 //
4 // Created on October 26th 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 <chrono>
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 
29 namespace AnyCollect {
33  class Metric {
34  protected:
35  static std::hash<std::string> hasher;
36  size_t key_;
37  size_t roundKey_;
38  std::vector<std::string> name_;
39  double previousValue_;
40  double value_;
41  std::chrono::system_clock::time_point timestamp_;
42  std::string unit_;
43  std::map<std::string, std::string> tags_;
44 
45  public:
49  Metric() = delete;
50 
58  Metric(const std::vector<std::string>& name, const std::map<std::string, std::string>& tags, const std::string& unit = "") noexcept;
59 
67  Metric(std::vector<std::string>&& name, std::map<std::string, std::string>&& tags, std::string&& unit = "") noexcept;
68 
74  Metric(const Metric& other) noexcept;
75 
81  Metric(Metric&& other) noexcept;
82 
88  Metric& operator=(Metric other) noexcept;
89 
90 
98  static size_t generateKey(const std::vector<std::string>& name, const std::map<std::string, std::string>& tags);
99 
106  static size_t generateKey(const Metric& metric);
107 
108 
112  const std::vector<std::string>& name() const noexcept;
113 
117  size_t key() const noexcept;
118 
122  size_t roundKey() const noexcept;
123 
127  double value() const noexcept;
128 
132  std::chrono::system_clock::time_point timestamp() const noexcept;
133 
137  const std::string& unit() const noexcept;
138 
142  const std::map<std::string, std::string>& tags() const noexcept;
143 
144 
152  void setNewValue(double value, bool computeRate, double unitsPerSecondFactor = 1.0) noexcept;
153 
162  void updateValue(double value, double unitsPerSecondFactor = 1.0) noexcept;
163 
167  void setTimestamp(std::chrono::system_clock::time_point timestamp) noexcept;
168 
172  void setRoundKey(size_t roundKey) noexcept;
173  };
174 }
void setTimestamp(std::chrono::system_clock::time_point timestamp) noexcept
Sets the timestamp of the metric.
Definition: Metric.cc:140
void setNewValue(double value, bool computeRate, double unitsPerSecondFactor=1.0) noexcept
Sets a new value to the metric.
Definition: Metric.cc:124
std::vector< std::string > name_
Array of strings representing the name of the metric.
Definition: Metric.h:38
size_t key() const noexcept
Returns the key of the metric.
Definition: Metric.cc:99
Metric()=delete
Deleted default constructor.
STL namespace.
const std::string & unit() const noexcept
Returns the unit of the metric.
Definition: Metric.cc:115
const std::vector< std::string > & name() const noexcept
Returns the name of the metric.
Definition: Metric.cc:95
size_t key_
Key of the metric (hash of its name, tag keys and tags values)
Definition: Metric.h:36
static size_t generateKey(const std::vector< std::string > &name, const std::map< std::string, std::string > &tags)
Compute the key of potential metric.
Definition: Metric.cc:84
std::string unit_
Unit of the metric.
Definition: Metric.h:42
std::chrono::system_clock::time_point timestamp_
Timestamp of the metric.
Definition: Metric.h:41
size_t roundKey() const noexcept
Returns the key of the collection iteration which created the metric.
Definition: Metric.cc:103
std::chrono::system_clock::time_point timestamp() const noexcept
Returns the timestamp of the metric.
Definition: Metric.cc:111
size_t roundKey_
Key of the collection iteration which created this metric.
Definition: Metric.h:37
const std::map< std::string, std::string > & tags() const noexcept
Returns the tags of the metric.
Definition: Metric.cc:119
std::map< std::string, std::string > tags_
Tags of the metric.
Definition: Metric.h:43
double value_
Current value of the metric.
Definition: Metric.h:40
Class used to represent a metric object (with a name, unit, tags, a value and a timestamp) ...
Definition: Metric.h:33
void setRoundKey(size_t roundKey) noexcept
Sets the round key of the metric.
Definition: Metric.cc:144
static std::hash< std::string > hasher
Object used to compute keys (string hashes)
Definition: Metric.h:35
double previousValue_
Previous value of the metric.
Definition: Metric.h:39
double value() const noexcept
Returns the value of the metric.
Definition: Metric.cc:107
void updateValue(double value, double unitsPerSecondFactor=1.0) noexcept
Adds a new value to the current value.
Definition: Metric.cc:133