From 3eaadf9a70ddbde1ad1d20e4bc8166f3b1078c2a Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Fri, 25 Aug 2017 14:54:38 +0100 Subject: [PATCH] ApMon: send multiple timed parameters (#17) --- examples/8-Multiple.cxx | 2 +- src/Backends/ApMonBackend.cxx | 54 +++++++++++++++++++++++++++++------ src/Backends/ApMonBackend.h | 1 - 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/examples/8-Multiple.cxx b/examples/8-Multiple.cxx index 763c1221c..8a76303c4 100644 --- a/examples/8-Multiple.cxx +++ b/examples/8-Multiple.cxx @@ -13,5 +13,5 @@ int main(int argc, char *argv[]) { // configure monitoring (once per process), pass configuration path as parameter Monitoring::Configure("file://" + GetConfigFromCmdLine(argc, argv)); - Monitoring::Get().send("measurementName", {{10, "myMetricInt"}, {10.10, "myMetricFloat"}}); + Monitoring::Get().send("measurementName", {{20, "myMetricIntMultiple"}, {20.30, "myMetricFloatMultple"}}); } diff --git a/src/Backends/ApMonBackend.cxx b/src/Backends/ApMonBackend.cxx index 74014b992..e3bb09141 100644 --- a/src/Backends/ApMonBackend.cxx +++ b/src/Backends/ApMonBackend.cxx @@ -31,9 +31,7 @@ ApMonBackend::ApMonBackend(const std::string& configurationFile) inline int ApMonBackend::convertTimestamp(const std::chrono::time_point& timestamp) { - return std::chrono::duration_cast ( - timestamp.time_since_epoch() - ).count(); + return static_cast(std::chrono::system_clock::to_time_t(timestamp)); } void ApMonBackend::addGlobalTag(std::string name, std::string value) @@ -44,12 +42,52 @@ void ApMonBackend::addGlobalTag(std::string name, std::string value) void ApMonBackend::sendMultiple(std::string measurement, std::vector&& metrics) { - for (auto& m : metrics) { - std::string tempName = m.getName(); - m.setName(measurement + "-" + m.getName()); - send(m); - m.setName(tempName); + int noMetrics = metrics.size(); + char **paramNames, **paramValues; + int *valueTypes; + paramNames = (char **)std::malloc(noMetrics * sizeof(char *)); + paramValues = (char **)std::malloc(noMetrics * sizeof(char *)); + valueTypes = (int *)std::malloc(noMetrics * sizeof(int)); + // the scope of values must be the same as sendTimedParameters method + int intValue; + double doubleValue; + std::string stringValue; + + for (int i = 0; i < noMetrics; i++) { + paramNames[i] = const_cast(metrics[i].getName().c_str()); + switch(metrics[i].getType()) { + case MetricType::INT : + { + valueTypes[i] = XDR_INT32; + intValue = boost::get(metrics[i].getValue()); + paramValues[i] = reinterpret_cast(&intValue); + } + break; + + case MetricType::STRING : + { + valueTypes[i] = XDR_STRING; + stringValue = boost::get(metrics[i].getValue()); + paramValues[i] = const_cast(stringValue.c_str()); + } + break; + + case MetricType::DOUBLE : + { + valueTypes[i] = XDR_REAL64; + doubleValue = boost::get(metrics[i].getValue()); + paramValues[i] = reinterpret_cast(&doubleValue); + } + break; + } } + + mApMon->sendTimedParameters(const_cast(entity.c_str()), const_cast(entity.c_str()), + noMetrics, paramNames, valueTypes, paramValues, convertTimestamp(metrics[0].getTimestamp())); + + std::free(paramNames); + std::free(paramValues); + std::free(valueTypes); } void ApMonBackend::send(const Metric& metric) diff --git a/src/Backends/ApMonBackend.h b/src/Backends/ApMonBackend.h index 243e4f0b8..8ceda0b2d 100644 --- a/src/Backends/ApMonBackend.h +++ b/src/Backends/ApMonBackend.h @@ -43,7 +43,6 @@ class ApMonBackend final : public Backend void send(const Metric& metric) override; /// Sends multiple metric in single packet - /// Not supported by the backend therefore it falls back to sending metric one by one /// \param name measurement name /// \param metrics list of metrics void sendMultiple(std::string measurement, std::vector&& metrics) override;