Skip to content

Commit 9f3104e

Browse files
committed
Backends/ApMonBackend: update metric payload format
1 parent 5affd7a commit 9f3104e

2 files changed

Lines changed: 57 additions & 17 deletions

File tree

src/Backends/ApMonBackend.cxx

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "ApMonBackend.h"
1818
#include <iostream>
1919
#include <sstream>
20+
#include <unistd.h>
21+
#include <limits.h>
2022
#include "../MonLogger.h"
2123
#include "../Exceptions/MonitoringException.h"
2224

@@ -59,9 +61,28 @@ void ApMonBackend::addGlobalTag(std::string_view /*name*/, std::string_view valu
5961
mEntity += value;
6062
}
6163

64+
std::string ApMonBackend::getNodeName()
65+
{
66+
const char* env_p = std::getenv("ALIEN_PROC_ID");
67+
if (env_p) {
68+
return std::string(env_p);
69+
}
70+
71+
char hostname[HOST_NAME_MAX];
72+
if (gethostname(hostname, sizeof(hostname)) == 0) {
73+
hostname[sizeof(hostname) - 1] = '\0';
74+
return std::string(hostname);
75+
}
76+
77+
MonLogger::Get(Severity::Error) << "Failed to get hostname, using 'unknown'" << MonLogger::End();
78+
return "unknown";
79+
}
80+
6281
void ApMonBackend::send(const Metric& metric)
6382
{
64-
std::string name = metric.getName();
83+
std::string clusterName(mClusterName);
84+
std::string metricName = metric.getName();
85+
std::string nodeName = getNodeName();
6586
std::string entity = mEntity;
6687
for (const auto& [key, value] : metric.getTags()) {
6788
entity += ',';
@@ -72,46 +93,59 @@ void ApMonBackend::send(const Metric& metric)
7293
if (mRunNumber != 0) entity += (",run=" + std::to_string(mRunNumber));
7394

7495
int valueSize = metric.getValuesSize();
96+
int totalParams = valueSize * 2; // each metric value has a source parameter
7597
char **paramNames, **paramValues;
7698
int* valueTypes;
77-
paramNames = (char**)std::malloc(valueSize * sizeof(char*));
78-
paramValues = (char**)std::malloc(valueSize * sizeof(char*));
79-
valueTypes = (int*)std::malloc(valueSize * sizeof(int));
99+
paramNames = (char**)std::malloc(totalParams * sizeof(char*));
100+
paramValues = (char**)std::malloc(totalParams * sizeof(char*));
101+
valueTypes = (int*)std::malloc(totalParams * sizeof(int));
80102
// the scope of values must be the same as sendTimedParameters method
81103
int intValue;
82104
double doubleValue;
83105
std::string stringValue;
84106

85107
auto& values = metric.getValues();
108+
std::string sourceName = metricName + "_src";
86109

87-
for (int i = 0; i < valueSize; i++) {
88-
paramNames[i] = const_cast<char*>(values[i].first.c_str());
110+
for (int i = 0; i < valueSize; ++i) {
111+
int metricIdx = i * 2;
112+
int sourceIdx = metricIdx + 1;
113+
paramNames[metricIdx] = const_cast<char*>(metricName.c_str());
89114
std::visit(overloaded{
90115
[&](int value) {
91-
valueTypes[i] = XDR_INT32;
116+
valueTypes[metricIdx] = XDR_INT32;
92117
intValue = value;
93-
paramValues[i] = reinterpret_cast<char*>(&intValue);
118+
paramValues[metricIdx] = reinterpret_cast<char*>(&intValue);
94119
},
95120
[&](double value) {
96-
valueTypes[i] = XDR_REAL64;
121+
valueTypes[metricIdx] = XDR_REAL64;
97122
doubleValue = value;
98-
paramValues[i] = reinterpret_cast<char*>(&doubleValue);
123+
paramValues[metricIdx] = reinterpret_cast<char*>(&doubleValue);
99124
},
100125
[&](const std::string& value) {
101-
valueTypes[i] = XDR_STRING;
126+
valueTypes[metricIdx] = XDR_STRING;
102127
stringValue = value;
103-
paramValues[i] = const_cast<char*>(stringValue.c_str());
128+
paramValues[metricIdx] = const_cast<char*>(stringValue.c_str());
104129
},
105130
[&](uint64_t value) {
106-
valueTypes[i] = XDR_REAL64;
131+
valueTypes[metricIdx] = XDR_REAL64;
107132
doubleValue = static_cast<double>(value);
108-
paramValues[i] = reinterpret_cast<char*>(&doubleValue);
133+
paramValues[metricIdx] = reinterpret_cast<char*>(&doubleValue);
109134
},
110-
}, values[i].second);
135+
}, values[metricIdx].second);
136+
137+
paramNames[sourceIdx] = const_cast<char*>(sourceName.c_str());
138+
valueTypes[sourceIdx] = XDR_STRING;
139+
stringValue = entity;
140+
paramValues[sourceIdx] = const_cast<char*>(stringValue.c_str());
111141
}
112142

113-
mApMon->sendTimedParameters(const_cast<char*>(name.c_str()), const_cast<char*>(entity.c_str()),
114-
valueSize, paramNames, valueTypes, paramValues, convertTimestamp(metric.getTimestamp()));
143+
mApMon->sendTimedParameters(
144+
const_cast<char*>(clusterName.c_str()),
145+
const_cast<char*>(nodeName.c_str()),
146+
totalParams, paramNames, valueTypes, paramValues,
147+
convertTimestamp(metric.getTimestamp())
148+
);
115149

116150
std::free(paramNames);
117151
std::free(paramValues);

src/Backends/ApMonBackend.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@ class ApMonBackend final : public Backend
6868
/// \return timestamp as integer (milliseconds from epoch)
6969
int convertTimestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp);
7070

71+
/// Gets node name
72+
/// It looks for environment variable ALIEN_PROC_ID and if it is not set, it uses hostname as node name
73+
/// \return node name as string
74+
std::string getNodeName();
75+
7176
std::unique_ptr<ApMon> mApMon; ///< ApMon object
7277
std::string mEntity; ///< MonALISA entity, created out of global tags
78+
inline static constexpr std::string_view mClusterName = "O2Monitoring_Nodes"; ///< MonALISA cluster name
7379
};
7480

7581
} // namespace backends

0 commit comments

Comments
 (0)