@@ -28,6 +28,9 @@ namespace monitoring
2828namespace backends
2929{
3030
31+ template <class ... Ts> struct overloaded : Ts... { using Ts::operator ()...; };
32+ template <class ... Ts> overloaded (Ts...) -> overloaded<Ts...>;
33+
3134ApMonBackend::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
103103void 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
153108void 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
0 commit comments