Skip to content

Commit 3eaadf9

Browse files
authored
ApMon: send multiple timed parameters (#17)
1 parent f974016 commit 3eaadf9

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

examples/8-Multiple.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ int main(int argc, char *argv[]) {
1313
// configure monitoring (once per process), pass configuration path as parameter
1414
Monitoring::Configure("file://" + GetConfigFromCmdLine(argc, argv));
1515

16-
Monitoring::Get().send("measurementName", {{10, "myMetricInt"}, {10.10, "myMetricFloat"}});
16+
Monitoring::Get().send("measurementName", {{20, "myMetricIntMultiple"}, {20.30, "myMetricFloatMultple"}});
1717
}

src/Backends/ApMonBackend.cxx

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ ApMonBackend::ApMonBackend(const std::string& configurationFile)
3131

3232
inline int ApMonBackend::convertTimestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp)
3333
{
34-
return std::chrono::duration_cast <std::chrono::milliseconds>(
35-
timestamp.time_since_epoch()
36-
).count();
34+
return static_cast<int>(std::chrono::system_clock::to_time_t(timestamp));
3735
}
3836

3937
void ApMonBackend::addGlobalTag(std::string name, std::string value)
@@ -44,12 +42,52 @@ void ApMonBackend::addGlobalTag(std::string name, std::string value)
4442

4543
void ApMonBackend::sendMultiple(std::string measurement, std::vector<Metric>&& metrics)
4644
{
47-
for (auto& m : metrics) {
48-
std::string tempName = m.getName();
49-
m.setName(measurement + "-" + m.getName());
50-
send(m);
51-
m.setName(tempName);
45+
int noMetrics = metrics.size();
46+
char **paramNames, **paramValues;
47+
int *valueTypes;
48+
paramNames = (char **)std::malloc(noMetrics * sizeof(char *));
49+
paramValues = (char **)std::malloc(noMetrics * sizeof(char *));
50+
valueTypes = (int *)std::malloc(noMetrics * sizeof(int));
51+
// the scope of values must be the same as sendTimedParameters method
52+
int intValue;
53+
double doubleValue;
54+
std::string stringValue;
55+
56+
for (int i = 0; i < noMetrics; i++) {
57+
paramNames[i] = const_cast<char*>(metrics[i].getName().c_str());
58+
switch(metrics[i].getType()) {
59+
case MetricType::INT :
60+
{
61+
valueTypes[i] = XDR_INT32;
62+
intValue = boost::get<int>(metrics[i].getValue());
63+
paramValues[i] = reinterpret_cast<char*>(&intValue);
64+
}
65+
break;
66+
67+
case MetricType::STRING :
68+
{
69+
valueTypes[i] = XDR_STRING;
70+
stringValue = boost::get<std::string>(metrics[i].getValue());
71+
paramValues[i] = const_cast<char*>(stringValue.c_str());
72+
}
73+
break;
74+
75+
case MetricType::DOUBLE :
76+
{
77+
valueTypes[i] = XDR_REAL64;
78+
doubleValue = boost::get<double>(metrics[i].getValue());
79+
paramValues[i] = reinterpret_cast<char*>(&doubleValue);
80+
}
81+
break;
82+
}
5283
}
84+
85+
mApMon->sendTimedParameters(const_cast<char*>(entity.c_str()), const_cast<char*>(entity.c_str()),
86+
noMetrics, paramNames, valueTypes, paramValues, convertTimestamp(metrics[0].getTimestamp()));
87+
88+
std::free(paramNames);
89+
std::free(paramValues);
90+
std::free(valueTypes);
5391
}
5492

5593
void ApMonBackend::send(const Metric& metric)

src/Backends/ApMonBackend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class ApMonBackend final : public Backend
4343
void send(const Metric& metric) override;
4444

4545
/// Sends multiple metric in single packet
46-
/// Not supported by the backend therefore it falls back to sending metric one by one
4746
/// \param name measurement name
4847
/// \param metrics list of metrics
4948
void sendMultiple(std::string measurement, std::vector<Metric>&& metrics) override;

0 commit comments

Comments
 (0)