Skip to content

Commit 7634fbc

Browse files
authored
Move from boost variant to std variant (#75)
1 parent 175939f commit 7634fbc

18 files changed

+150
-183
lines changed

cmake/FindApMon.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mark_as_advanced(ApMon_INCLUDE_DIR)
2424

2525
# find library
2626
find_library(ApMon_LIBRARY NAMES apmoncpp
27-
HINTS /usr/local ${APMON_ROOT}
27+
HINTS ${APMON_ROOT} /usr/local
2828
PATH_SUFFIXES lib lib64
2929
)
3030

include/Monitoring/ComplexMetric.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ComplexMetric : public o2::monitoring::Metric
5252
/// boost variant metric constructor, required by derived metrics logic
5353
/// \param value metric value (boost variant)
5454
/// \param name metric name
55-
ComplexMetric(boost::variant< int, std::string, double, uint64_t >, const std::string& name);
55+
ComplexMetric(std::variant< int, std::string, double, uint64_t >, const std::string& name);
5656

5757
/// Default destructor
5858
~ComplexMetric() = default;
@@ -61,7 +61,7 @@ class ComplexMetric : public o2::monitoring::Metric
6161
void resetTimestamp();
6262

6363
/// Assign operator overload, assignes new values to the metric object
64-
ComplexMetric& operator=(const boost::variant< int, std::string, double, uint64_t >& value);
64+
ComplexMetric& operator=(const std::variant< int, std::string, double, uint64_t >& value);
6565
};
6666

6767
} // namespace monitoring

include/Monitoring/Metric.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
#include <string>
1010
#include <chrono>
11+
#include <map>
1112
#include <vector>
1213
#include <regex>
13-
#include <boost/variant.hpp>
14+
#include <variant>
1415
#include "Tags.h"
1516

1617
namespace o2
@@ -55,11 +56,14 @@ class Metric
5556
/// boost variant metric constructor, required by derived metrics logic
5657
/// \param value metric value (boost variant)
5758
/// \param name metric name
58-
Metric(boost::variant< int, std::string, double, uint64_t >, const std::string& name, Verbosity verbosity = Metric::DefaultVerbosity);
59+
Metric(std::variant< int, std::string, double, uint64_t >, const std::string& name, Verbosity verbosity = Metric::DefaultVerbosity);
5960

6061
/// Default destructor
6162
~Metric() = default;
6263

64+
/// Assign operator overload, assignes new values to the metric object
65+
Metric& operator=(const std::variant< int, std::string, double, uint64_t >& value);
66+
6367
/// Name getter
6468
/// \return metric name
6569
const std::string& getName() const;
@@ -70,7 +74,7 @@ class Metric
7074

7175
/// Value getter
7276
/// \return metric value
73-
boost::variant< int, std::string, double, uint64_t > getValue() const;
77+
std::variant< int, std::string, double, uint64_t > getValue() const;
7478

7579
/// Value type getter
7680
/// \return type of value stores inside metric : 0 - int, 1 - std::string, 2 - double
@@ -115,7 +119,7 @@ class Metric
115119
Metric&& setTags(std::vector<std::pair<int, int>>&& tags);
116120

117121
/// Metric value
118-
boost::variant< int, std::string, double, uint64_t > mValue;
122+
std::variant< int, std::string, double, uint64_t > mValue;
119123

120124
/// Metric name
121125
std::string mName;

src/Backends/ApMonBackend.cxx

Lines changed: 34 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ namespace monitoring
2828
namespace backends
2929
{
3030

31+
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
32+
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
33+
3134
ApMonBackend::ApMonBackend(const std::string& path)
3235
{
3336
try {
@@ -65,31 +68,28 @@ void ApMonBackend::send(std::vector<Metric>&& metrics)
6568

6669
for (int i = 0; i < noMetrics; i++) {
6770
paramNames[i] = const_cast<char*>(metrics[i].getName().c_str());
68-
switch(metrics[i].getType()) {
69-
case MetricType::INT :
70-
{
71+
std::visit(overloaded {
72+
[&](int value) {
7173
valueTypes[i] = XDR_INT32;
72-
intValue = boost::get<int>(metrics[i].getValue());
74+
intValue = value;
7375
paramValues[i] = reinterpret_cast<char*>(&intValue);
74-
}
75-
break;
76-
77-
case MetricType::STRING :
78-
{
76+
},
77+
[&](double value) {
78+
valueTypes[i] = XDR_REAL64;
79+
doubleValue = value;
80+
paramValues[i] = reinterpret_cast<char*>(&doubleValue);
81+
},
82+
[&](const std::string& value) {
7983
valueTypes[i] = XDR_STRING;
80-
stringValue = boost::get<std::string>(metrics[i].getValue());
84+
stringValue = value;
8185
paramValues[i] = const_cast<char*>(stringValue.c_str());
82-
}
83-
break;
84-
85-
case MetricType::DOUBLE :
86-
{
86+
},
87+
[&](uint64_t value) {
8788
valueTypes[i] = XDR_REAL64;
88-
doubleValue = boost::get<double>(metrics[i].getValue());
89+
doubleValue = static_cast<double>(value);
8990
paramValues[i] = reinterpret_cast<char*>(&doubleValue);
90-
}
91-
break;
92-
}
91+
},
92+
}, metrics[i].getValue());
9393
}
9494

9595
mApMon->sendTimedParameters(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
@@ -102,89 +102,39 @@ void ApMonBackend::send(std::vector<Metric>&& metrics)
102102

103103
void ApMonBackend::sendMultiple(std::string, std::vector<Metric>&& metrics)
104104
{
105-
int noMetrics = metrics.size();
106-
char **paramNames, **paramValues;
107-
int *valueTypes;
108-
paramNames = (char **)std::malloc(noMetrics * sizeof(char *));
109-
paramValues = (char **)std::malloc(noMetrics * sizeof(char *));
110-
valueTypes = (int *)std::malloc(noMetrics * sizeof(int));
111-
// the scope of values must be the same as sendTimedParameters method
112-
int intValue;
113-
double doubleValue;
114-
std::string stringValue;
115-
116-
for (int i = 0; i < noMetrics; i++) {
117-
paramNames[i] = const_cast<char*>(metrics[i].getName().c_str());
118-
switch(metrics[i].getType()) {
119-
case MetricType::INT :
120-
{
121-
valueTypes[i] = XDR_INT32;
122-
intValue = boost::get<int>(metrics[i].getValue());
123-
paramValues[i] = reinterpret_cast<char*>(&intValue);
124-
}
125-
break;
126-
127-
case MetricType::STRING :
128-
{
129-
valueTypes[i] = XDR_STRING;
130-
stringValue = boost::get<std::string>(metrics[i].getValue());
131-
paramValues[i] = const_cast<char*>(stringValue.c_str());
132-
}
133-
break;
134-
135-
case MetricType::DOUBLE :
136-
{
137-
valueTypes[i] = XDR_REAL64;
138-
doubleValue = boost::get<double>(metrics[i].getValue());
139-
paramValues[i] = reinterpret_cast<char*>(&doubleValue);
140-
}
141-
break;
142-
}
143-
}
144-
145-
mApMon->sendTimedParameters(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
146-
noMetrics, paramNames, valueTypes, paramValues, convertTimestamp(metrics[0].getTimestamp()));
147-
148-
std::free(paramNames);
149-
std::free(paramValues);
150-
std::free(valueTypes);
105+
send(std::move(metrics));
151106
}
152107

153108
void ApMonBackend::send(const Metric& metric)
154109
{
155110
std::string name = metric.getName();
111+
112+
156113
for (const auto& [key, value] : metric.getTags()) {
157114
name += ',';
158115
name += tags::TAG_KEY[key];
159116
name += "=";
160117
name += tags::GetValue(value);
161118
}
162119

163-
switch(metric.getType()) {
164-
case MetricType::INT :
165-
{
166-
int value = boost::get<int>(metric.getValue());
120+
std::visit(overloaded {
121+
[this, &metric](int value) {
167122
mApMon->sendTimedParameter(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
168123
const_cast<char*>(metric.getName().c_str()), XDR_INT32, reinterpret_cast<char*>(&value), convertTimestamp(metric.getTimestamp()));
169-
}
170-
break;
171-
172-
case MetricType::STRING :
173-
{
174-
std::string value = boost::get<std::string>(metric.getValue());
124+
},
125+
[this, &metric](double value) {
126+
mApMon->sendTimedParameter(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
127+
const_cast<char*>(metric.getName().c_str()), XDR_REAL64, reinterpret_cast<char*>(&value), convertTimestamp(metric.getTimestamp()));
128+
},
129+
[this, &metric](const std::string& value) {
175130
mApMon->sendTimedParameter(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
176131
const_cast<char*>(metric.getName().c_str()), XDR_STRING, const_cast<char*>(value.c_str()), convertTimestamp(metric.getTimestamp()));
177-
}
178-
break;
179-
180-
case MetricType::DOUBLE :
181-
{
182-
double value = boost::get<double>(metric.getValue());
132+
},
133+
[this, &metric](uint64_t value) {
183134
mApMon->sendTimedParameter(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
184135
const_cast<char*>(metric.getName().c_str()), XDR_REAL64, reinterpret_cast<char*>(&value), convertTimestamp(metric.getTimestamp()));
185-
}
186-
break;
187-
}
136+
},
137+
}, metric.getValue());
188138
}
189139

190140
} // namespace backends

src/Backends/ApMonBackend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#ifndef ALICEO2_MONITORING_BACKENDS_APMONBACKEND_H
1717
#define ALICEO2_MONITORING_BACKENDS_APMONBACKEND_H
1818

19+
#include "Monitoring/Backend.h"
20+
#include <ApMon.h>
1921
#include <string>
2022
#include <chrono>
2123
#include <memory>
22-
#include <ApMon.h>
23-
#include "Monitoring/Backend.h"
2424

2525
namespace o2
2626
{

src/Backends/Flume.cxx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ namespace monitoring
2828
namespace backends
2929
{
3030

31+
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
32+
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
33+
3134
Flume::Flume(const std::string& host, unsigned int port) :
3235
mTransport(std::make_unique<transports::UDP>(host, port))
3336
{
@@ -41,11 +44,17 @@ std::string Flume::metricToJson(const Metric& metric)
4144
boost::property_tree::ptree header = globalHeader;
4245
header.put<std::string>("timestamp", std::to_string(convertTimestamp(metric.getTimestamp())));
4346
header.put<std::string>("name", metric.getName());
44-
header.put<std::string>("value_value", boost::lexical_cast<std::string>(metric.getValue()));
47+
48+
auto value = std::visit(overloaded {
49+
[](const std::string& value) -> std::string { return value; },
50+
[](auto value) -> std::string { return std::to_string(value); }
51+
}, metric.getValue());
52+
header.put<std::string>("value_value", value);
4553

4654
for (const auto& [key, value] : metric.getTags()) {
4755
header.put<std::string>("tag_" + std::string(tags::TAG_KEY[key].data()), tags::GetValue(value).data());
4856
}
57+
4958
event.push_back(std::make_pair("headers", header));
5059
event.put<std::string>("body", "");
5160
std::stringstream ss;
@@ -81,7 +90,11 @@ std::string Flume::metricsToJson(std::string measurement, std::vector<Metric>&&
8190
header.put<std::string>("tag_" + std::string(tags::TAG_KEY[key].data()), tags::GetValue(value).data());
8291
}
8392
for (auto& metric : metrics) {
84-
header.put<std::string>("value_" + metric.getName(), boost::lexical_cast<std::string>(metric.getValue()));
93+
auto value = std::visit(overloaded {
94+
[](const std::string& value) -> std::string { return value; },
95+
[](auto value) -> std::string { return std::to_string(value); }
96+
}, metric.getValue());
97+
header.put<std::string>("value_" + metric.getName(), value);
8598
}
8699
event.push_back(std::make_pair("headers", header));
87100
event.put<std::string>("body", "");

src/Backends/InfluxDB.cxx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
///
1515

1616
#include "InfluxDB.h"
17-
#include <boost/lexical_cast.hpp>
1817
#include <string>
18+
#include <variant>
1919
#include "../Transports/UDP.h"
2020
#include "../Transports/Unix.h"
2121
#include "../Exceptions/MonitoringException.h"
@@ -29,6 +29,9 @@ namespace monitoring
2929
namespace backends
3030
{
3131

32+
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
33+
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
34+
3235
InfluxDB::InfluxDB(const std::string& host, unsigned int port) :
3336
mTransport(std::make_unique<transports::UDP>(host, port))
3437
{
@@ -63,9 +66,14 @@ void InfluxDB::sendMultiple(std::string measurement, std::vector<Metric>&& metri
6366
convert << measurement << "," << tagSet << " ";
6467

6568
for (const auto& metric : metrics) {
66-
std::string value = boost::lexical_cast<std::string>(metric.getValue());
67-
prepareValue(value, metric.getType());
68-
convert << metric.getName() << "=" << value << ",";
69+
convert << metric.getName() << "=";
70+
std::visit(overloaded {
71+
[&convert](uint64_t value) { convert << value << 'i'; },
72+
[&convert](int value) { convert << value << 'i'; },
73+
[&convert](double value) { convert << value; },
74+
[&convert](const std::string& value) { convert << '"' << value << '"'; },
75+
}, metric.getValue());
76+
convert << ",";
6977
}
7078
convert.seekp(-1, std::ios_base::end);
7179
convert << " " << convertTimestamp(metrics.back().getTimestamp());
@@ -108,24 +116,17 @@ std::string InfluxDB::toInfluxLineProtocol(const Metric& metric) {
108116
convert << "," << tags::TAG_KEY[key] << "=" << tags::GetValue(value);
109117
}
110118

111-
std::string value = boost::lexical_cast<std::string>(metric.getValue());
112-
prepareValue(value, metric.getType());
113-
114-
convert << " value=" << value << " " << convertTimestamp(metric.getTimestamp());
115-
return convert.str();
116-
}
119+
convert << " value=";
117120

118-
void InfluxDB::prepareValue(std::string& value, int type)
119-
{
120-
if (type == MetricType::STRING) {
121-
escape(value);
122-
value.insert(value.begin(), '"');
123-
value.insert(value.end(), '"');
124-
}
121+
std::visit(overloaded {
122+
[&convert](uint64_t value) { convert << value << 'i'; },
123+
[&convert](int value) { convert << value << 'i'; },
124+
[&convert](double value) { convert << value; },
125+
[&convert](const std::string& value) { convert << '"' << value << '"'; },
126+
}, metric.getValue());
125127

126-
if (type == MetricType::INT) {
127-
value.insert(value.end(), 'i');
128-
}
128+
convert << " " << convertTimestamp(metric.getTimestamp());
129+
return convert.str();
129130
}
130131

131132
void InfluxDB::addGlobalTag(std::string_view name, std::string_view value)

src/Backends/InfluxDB.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ class InfluxDB final : public Backend
7979
/// \param escaped string rerference to escape characters from
8080
void escape(std::string& escaped);
8181

82-
/// Modifies values to Influx Line Protocol format
83-
/// \param value reference to value
84-
/// \param type type of the metric
85-
void prepareValue(std::string& value, int type);
82+
/// Converts metric to Influx Line Protocol format
83+
/// \param metric
8684
std::string toInfluxLineProtocol(const Metric& metric);
8785
};
8886

0 commit comments

Comments
 (0)