AnyCollect  1.1.2
Metric.cc
Go to the documentation of this file.
1 //
2 // Metric.cc
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 #include <iostream>
22 
23 #include "Metric.h"
24 
25 
26 namespace AnyCollect {
27  std::hash<std::string> Metric::hasher{};
28 
29  Metric::Metric(const std::vector<std::string>& name, const std::map<std::string, std::string>& tags, const std::string& unit) noexcept :
30  roundKey_(-1),
31  name_(name),
32  unit_(unit),
33  tags_(tags)
34  {
35  this->key_ = Metric::generateKey(this->name_, this->tags_);
36  }
37 
38 
39  Metric::Metric(std::vector<std::string>&& name, std::map<std::string, std::string>&& tags, std::string&& unit) noexcept :
40  roundKey_(-1),
41  name_(name),
42  unit_(unit),
43  tags_(tags)
44  {
45  this->key_ = Metric::generateKey(this->name_, this->tags_);
46  }
47 
48 
49  Metric::Metric(const Metric& other) noexcept :
50  key_(other.key_),
51  roundKey_(other.roundKey_),
52  name_(other.name_),
53  previousValue_(other.previousValue_),
54  value_(other.value_),
55  timestamp_(other.timestamp_),
56  unit_(other.unit_),
57  tags_(other.tags_)
58  { }
59 
60  Metric::Metric(Metric&& other) noexcept :
61  key_(other.key_),
62  roundKey_(other.roundKey_),
63  name_(std::move(other.name_)),
64  previousValue_(other.previousValue_),
65  value_(other.value_),
66  timestamp_(other.timestamp_),
67  unit_(std::move(other.unit_)),
68  tags_(std::move(other.tags_))
69  { }
70 
71  Metric& Metric::operator=(Metric other) noexcept {
72  this->key_ = other.key_;
73  this->roundKey_ = other.roundKey_;
74  std::swap(this->name_, other.name_);
75  this->previousValue_ = other.previousValue_;
76  this->value_ = other.value_;
77  this->timestamp_ = other.timestamp_;
78  std::swap(this->unit_, other.unit_);
79  std::swap(this->tags_, other.tags_);
80  return *this;
81  }
82 
83 
84  size_t Metric::generateKey(const std::vector<std::string>& name, const std::map<std::string, std::string>& tags) {
85  std::string hash;
86  for (const auto& n : name)
87  hash.append(n);
88  for (const auto& [k, v] : tags) {
89  hash.append(k);
90  hash.append(v);
91  }
92  return Metric::hasher(hash);
93  }
94 
95  const std::vector<std::string>& Metric::name() const noexcept {
96  return this->name_;
97  }
98 
99  size_t Metric::key() const noexcept {
100  return this->key_;
101  }
102 
103  size_t Metric::roundKey() const noexcept {
104  return this->roundKey_;
105  }
106 
107  double Metric::value() const noexcept {
108  return this->value_;
109  }
110 
111  std::chrono::system_clock::time_point Metric::timestamp() const noexcept {
112  return this->timestamp_;
113  }
114 
115  const std::string& Metric::unit() const noexcept {
116  return this->unit_;
117  }
118 
119  const std::map<std::string, std::string>& Metric::tags() const noexcept {
120  return this->tags_;
121  }
122 
123 
124  void Metric::setNewValue(double value, bool computeRate, double unitsPerSecondFactor) noexcept {
125  if (computeRate)
126  this->value_ = value - this->previousValue_;
127  else
128  this->value_ = value;
129  this->previousValue_ = value;
130  this->value_ *= unitsPerSecondFactor;
131  }
132 
133  void Metric::updateValue(double value, double unitsPerSecondFactor) noexcept {
134  this->value_ /= unitsPerSecondFactor;
135  this->value_ += value;
136  this->previousValue_ += value;
137  this->value_ *= unitsPerSecondFactor;
138  }
139 
140  void Metric::setTimestamp(std::chrono::system_clock::time_point timestamp) noexcept {
141  this->timestamp_ = timestamp;
142  }
143 
144  void Metric::setRoundKey(size_t roundKey) noexcept{
145  this->roundKey_ = roundKey;
146  }
147 }
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.
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
Metric & operator=(Metric other) noexcept
Assignment operator.
Definition: Metric.cc:71
static std::hash< std::string > hasher
Object used to compute keys (string hashes)
Definition: Metric.h:35
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