-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathDPLMonitoringBackend.cxx
More file actions
83 lines (74 loc) · 2.95 KB
/
DPLMonitoringBackend.cxx
File metadata and controls
83 lines (74 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "DPLMonitoringBackend.h"
#include "Framework/DriverClient.h"
#include "Framework/ServiceRegistry.h"
#include "Framework/RuntimeError.h"
#include "Framework/VariantHelpers.h"
#include <fmt/format.h>
#include <sstream>
namespace o2::framework
{
DPLMonitoringBackend::DPLMonitoringBackend(ServiceRegistryRef registry)
: mRegistry{registry}
{
}
void DPLMonitoringBackend::addGlobalTag(std::string_view name, std::string_view value)
{
// FIXME: tags are ignored by DPL in any case...
mTagString += fmt::format("{}{}={}", mTagString.empty() ? "" : ",", name.data(), value);
}
void DPLMonitoringBackend::send(std::vector<o2::monitoring::Metric>&& metrics)
{
for (auto& m : metrics) {
send(m);
}
}
inline unsigned long convertTimestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
timestamp.time_since_epoch())
.count();
}
void DPLMonitoringBackend::send(o2::monitoring::Metric const& metric)
{
std::array<char, 4096> buffer;
auto mStream = fmt::format_to(buffer.begin(), "[METRIC] {}", metric.getName());
for (auto& value : metric.getValues()) {
auto stringValue = std::visit(overloaded{
[](const std::string& value) -> std::string { return value; },
[](auto value) -> std::string { return std::to_string(value); }},
value.second);
if (metric.getValuesSize() == 1) {
mStream = fmt::format_to(mStream, ",{} {}", metric.getFirstValueType(), stringValue);
} else {
mStream = fmt::format_to(mStream, " {}={}", value.first, stringValue);
}
}
// FIXME: tags are ignored by the DPL backend in any case...
mStream = fmt::format_to(mStream, " {} {}", convertTimestamp(metric.getTimestamp()), mTagString);
bool first = mTagString.empty();
for (const auto& [key, value] : metric.getTags()) {
if (!first) {
mStream = fmt::format_to(mStream, ",");
}
first = true;
mStream = fmt::format_to(mStream, "{}={}", o2::monitoring::tags::TAG_KEY[key], o2::monitoring::tags::GetValue(value));
}
mStream = fmt::format_to(mStream, "\n");
auto size = std::distance(buffer.begin(), mStream);
if (size + 1 >= 4096) {
throw runtime_error_f("Metric too long");
}
buffer[size] = '\0';
mRegistry.get<framework::DriverClient>().tell(buffer.data(), size);
}
} // namespace o2::framework