-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDerivedMetrics.cxx
More file actions
141 lines (129 loc) · 4.95 KB
/
DerivedMetrics.cxx
File metadata and controls
141 lines (129 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///
/// \file DerivedMetrics.cxx
/// \author Adam Wegrzynek <adam.wegrzynek@cern.ch>
///
#include "Monitoring/DerivedMetrics.h"
#include "Exceptions/MonitoringException.h"
#include <chrono>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "VariantVisitorAdd.h"
#include "VariantVisitorRate.h"
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...)->overloaded<Ts...>;
namespace o2
{
/// ALICE O2 Monitoring system
namespace monitoring
{
bool DerivedMetrics::process(Metric& metric, DerivedMetricMode mode)
{
const std::map<DerivedMetricMode, std::function<bool(Metric&)>> map = {
{DerivedMetricMode::INCREMENT, [this](Metric& metric) {
auto tags = metric.getTags();
std::string key = metric.getName();
std::for_each(tags.begin(), tags.end(), [&key](auto const& pair) {
key += pair.second;
});
auto search = mStorage.find(key);
if (search != mStorage.end()) {
auto currentValue = metric.getFirstValue().second;
auto storedValue = search->second.getValues().back().second;
auto value = std::visit(VariantVisitorAdd{}, currentValue, storedValue);
mStorage.erase(search);
metric.addValue(value, metric.getFirstValue().first + "_increment");
} else {
metric.addValue(metric.getFirstValue().second, metric.getFirstValue().first + "_increment");
}
mStorage.insert(std::make_pair(key, metric));
return true;
}},
{DerivedMetricMode::RATE, [this](Metric& metric) {
/// create pseudo unique key
auto tags = metric.getTags();
std::string key = metric.getName();
std::for_each(tags.begin(), tags.end(), [&key](auto const& pair) {
key += pair.second;
});
// search for previous value
auto search = mStorage.find(key);
if (search == mStorage.end()) {
mStorage.insert(std::make_pair(key, metric));
metric.addValue((double)0.0, metric.getFirstValue().first + "_rate");
return true;
}
auto timestampDifference = std::chrono::duration_cast<std::chrono::milliseconds>(
metric.getTimestamp() - search->second.getTimestamp());
int timestampCount = timestampDifference.count();
// disallow dividing by 0
if (timestampCount == 0) {
//throw MonitoringException("DerivedMetrics", "Division by 0 when calculating rate for: " + metric.getName() + "/" + metric.getFirstValue().first);
}
auto current = metric.getFirstValue().second;
auto previous = search->second.getFirstValue().second;
auto rate = std::visit(VariantVisitorRate(timestampCount), current, previous);
// handle situation when a new run starts
auto isZero = std::visit(overloaded{
[](auto arg) { return arg == 0; },
[](const std::string& arg) { return arg == ""; }
}, current);
if (rate < 0 && isZero) {
rate = 0;
}
// swap metrics
mStorage.erase(key);
mStorage.insert(std::make_pair(key, metric));
// add rate field
metric.addValue(rate, metric.getFirstValue().first + "_rate");
return true;
}},
{DerivedMetricMode::SUPPRESS, [this](Metric& metric) {
auto tags = metric.getTags();
std::string key = metric.getName();
std::for_each(tags.begin(), tags.end(), [&key](auto const& pair) {
key += pair.second;
});
// if no previous values -> store and send
auto search = mStorage.find(key);
if (search == mStorage.end()) {
mStorage.insert(std::make_pair(key, metric));
return true;
}
// if same values and timeout not reached -> skip
if (metric.getFirstValue().second == search->second.getFirstValue().second
&& std::chrono::duration_cast<std::chrono::seconds>(
metric.getTimestamp() - search->second.getTimestamp()
) < std::chrono::seconds(DerivedMetrics::mSuppressTimeout)) {
return false;
}
mStorage.erase(key);
mStorage.insert(std::make_pair(key, metric));
return true;
}}
};
auto iterator = map.find(mode);
if (iterator == map.end()) {
throw MonitoringException("DerivedMetrics", "Unknown mode");
}
return iterator->second(metric);
}
std::chrono::seconds DerivedMetrics::mSuppressTimeout{300};
} // namespace monitoring
} // namespace o2