Project
Loading...
Searching...
No Matches
DPLMonitoringBackend.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
17#include <fmt/format.h>
18#include <sstream>
19
20namespace o2::framework
21{
22
24 : mRegistry{registry}
25{
26}
27
28void DPLMonitoringBackend::addGlobalTag(std::string_view name, std::string_view value)
29{
30 // FIXME: tags are ignored by DPL in any case...
31 mTagString += fmt::format("{}{}={}", mTagString.empty() ? "" : ",", name.data(), value);
32}
33
34void DPLMonitoringBackend::send(std::vector<o2::monitoring::Metric>&& metrics)
35{
36 for (auto& m : metrics) {
37 send(m);
38 }
39}
40
41inline unsigned long convertTimestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp)
42{
43 return std::chrono::duration_cast<std::chrono::milliseconds>(
44 timestamp.time_since_epoch())
45 .count();
46}
47
48void DPLMonitoringBackend::send(o2::monitoring::Metric const& metric)
49{
50 std::array<char, 4096> buffer;
51 auto mStream = fmt::format_to(buffer.begin(), "[METRIC] {}", metric.getName());
52 for (auto& value : metric.getValues()) {
53 auto stringValue = std::visit(overloaded{
54 [](const std::string& value) -> std::string { return value; },
55 [](auto value) -> std::string { return std::to_string(value); }},
56 value.second);
57 if (metric.getValuesSize() == 1) {
58 mStream = fmt::format_to(mStream, ",{} {}", metric.getFirstValueType(), stringValue);
59 } else {
60 mStream = fmt::format_to(mStream, " {}={}", value.first, stringValue);
61 }
62 }
63 // FIXME: tags are ignored by the DPL backend in any case...
64 mStream = fmt::format_to(mStream, " {} {}", convertTimestamp(metric.getTimestamp()), mTagString);
65
66 bool first = mTagString.empty();
67 for (const auto& [key, value] : metric.getTags()) {
68 if (!first) {
69 mStream = fmt::format_to(mStream, ",");
70 }
71 first = true;
72 mStream = fmt::format_to(mStream, "{}={}", o2::monitoring::tags::TAG_KEY[key], o2::monitoring::tags::GetValue(value));
73 }
74 mStream = fmt::format_to(mStream, "\n");
75 auto size = std::distance(buffer.begin(), mStream);
76 if (size + 1 >= 4096) {
77 throw runtime_error_f("Metric too long");
78 }
79 buffer[size] = '\0';
80 mRegistry.get<framework::DriverClient>().tell(buffer.data(), size);
81}
82
83} // namespace o2::framework
StringRef key
DPLMonitoringBackend(ServiceRegistryRef registry)
Default constructor.
void addGlobalTag(std::string_view name, std::string_view value) override
void send(const o2::monitoring::Metric &metric) override
A service API to communicate with the driver.
const GLfloat * m
Definition glcorearb.h:4066
GLuint buffer
Definition glcorearb.h:655
GLsizeiptr size
Definition glcorearb.h:659
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
Definition glcorearb.h:5500
GLsizei const GLfloat * value
Definition glcorearb.h:819
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
unsigned long convertTimestamp(const std::chrono::time_point< std::chrono::system_clock > &timestamp)
RuntimeErrorRef runtime_error_f(const char *,...)
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
From https://en.cppreference.com/w/cpp/utility/variant/visit.