Skip to content

Commit 07d1b8b

Browse files
authored
Improve tags handling (#100)
* Improve tags handling * Fix Flume tag names * Use string_view in addGlobalTag * Fix ApMon backend * Flume tag fix * Adjust code comments * Fix tag escaping in Influx backend * Fix prefix Flume global tags
1 parent 4a675ca commit 07d1b8b

File tree

20 files changed

+170
-140
lines changed

20 files changed

+170
-140
lines changed

examples/2-TaggedMetrics.cxx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55

66
#include "Monitoring/MonitoringFactory.h"
77

8-
using Monitoring = o2::monitoring::MonitoringFactory;
9-
using o2::monitoring::Metric;
8+
using namespace o2::monitoring;
109

1110
int main() {
1211

1312
// Configure monitoring
1413
// Pass string with list of URLs as parameter
15-
auto monitoring = Monitoring::Get("stdout://");
14+
auto monitoring = MonitoringFactory::Get("stdout://");
15+
16+
/// Add global tags
17+
monitoring->addGlobalTag("example", "yes");
18+
monitoring->addGlobalTag(tags::Subsystem::DPL);
1619

1720
// now send an application specific metric with additional tags
1821
// 10 is the value
1922
// myMetric is the name of the metric
20-
// then vector of key-value tags
21-
monitoring->send(Metric{10, "myMetric"}.addTags({{"tag1", "value1"}, {"tag2", "value2"}}));
23+
// then add predefined tag
24+
monitoring->send(Metric{10, "myMetric"}.addTags({tags::Detector::TPC}));
2225
}

examples/7-InternalBenchamrk.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ void test(std::unique_ptr<Monitoring>& monitoring) {
1919
void testWithTags(std::unique_ptr<Monitoring>& monitoring) {
2020
monitoring->addGlobalTag("benchmark", "yes");
2121
for (int i = 0; i < 100000; i++) {
22-
monitoring->send(Metric{10, "myMetricInt"}.addTags({{"tag1", "val1"}}));
23-
monitoring->send(Metric{10.10, "myMetricFloat"}.addTags({{"tag2", "val2"}}));
22+
monitoring->send(Metric{10, "myMetricInt"}.addTags({tags::Detector::TPC}));
23+
monitoring->send(Metric{10.10, "myMetricFloat"}.addTags({tags::Subsystem::QC}));
2424
}
2525
}
2626

27-
int main(int argc, char** argv) {
27+
int main() {
2828
static constexpr std::array<std::string_view,4> backends = {
2929
"no-op://",
3030
"flume://localhost:1234",

include/Monitoring/Backend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Backend
5454
virtual void sendMultiple(std::string measurement, std::vector<Metric>&& metrics) = 0;
5555

5656
/// Sets a tag
57-
virtual void addGlobalTag(std::string name, std::string value) = 0;
57+
virtual void addGlobalTag(std::string_view name, std::string_view value) = 0;
5858
};
5959

6060
} // namespace monitoring

include/Monitoring/Metric.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <chrono>
1111
#include <vector>
1212
#include <boost/variant.hpp>
13-
#include "Tag.h"
13+
#include "Tags.h"
1414

1515
namespace o2
1616
{
@@ -27,32 +27,27 @@ class Metric
2727
/// Integer metric construtor
2828
/// \param value metric value (int)
2929
/// \param name metric name
30-
/// \param timestamp metric timestamp in milliseconds
31-
Metric(int value, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp = Metric::getCurrentTimestamp());
30+
Metric(int value, const std::string& name);
3231

3332
/// String metric construtor
3433
/// \param value metric value (string)
3534
/// \param name the metric name
36-
/// \param timestamp metric timestamp in milliseconds
37-
Metric(std::string value, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp = Metric::getCurrentTimestamp());
35+
Metric(std::string value, const std::string& name);
3836

3937
/// Double metric constructor
4038
/// \param value metric value (double)
4139
/// \param name metric name
42-
/// \param timestamp metric timestamp in milliseconds
43-
Metric(double value, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp = Metric::getCurrentTimestamp());
40+
Metric(double value, const std::string& name);
4441

4542
/// uint64_t metric constructor
4643
/// \param value metric value (uint64_t)
4744
/// \param name metric name
48-
/// \param timestamp metric timestamp in milliseconds
49-
Metric(uint64_t value, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp = Metric::getCurrentTimestamp());
45+
Metric(uint64_t value, const std::string& name);
5046

5147
/// boost variant metric constructor, required by derived metrics logic
5248
/// \param value metric value (boost variant)
5349
/// \param name metric name
54-
/// \param timestamp metric timestamp in milliseconds
55-
Metric(boost::variant< int, std::string, double, uint64_t >, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp = Metric::getCurrentTimestamp());
50+
Metric(boost::variant< int, std::string, double, uint64_t >, const std::string& name);
5651

5752
/// Default destructor
5853
~Metric() = default;
@@ -77,21 +72,18 @@ class Metric
7772
int getType() const;
7873

7974
/// Tag list getter
80-
/// \return vector of tags
81-
std::vector<Tag> getTags() const;
75+
/// \return tags
76+
const std::vector<unsigned int>& getTags() const;
8277

8378
/// Add user defined tags
8479
/// \param tags r-value to vector of tags
8580
/// \return r-value to "this" - to be able to chain methods
86-
Metric&& addTags(std::vector<Tag>&& tags);
81+
Metric&& addTags(std::initializer_list<unsigned int>&& tags);
8782

8883
/// Generetes current timestamp
8984
/// return timestamp as std::chrono::system_clock
9085
static auto getCurrentTimestamp() -> decltype(std::chrono::system_clock::now());
9186

92-
/// Tagset vector size getter
93-
std::size_t tagSize() const;
94-
9587
protected:
9688
/// Metric value
9789
boost::variant< int, std::string, double, uint64_t > mValue;
@@ -103,7 +95,7 @@ class Metric
10395
std::chrono::time_point<std::chrono::system_clock> mTimestamp;
10496

10597
/// Metric tags
106-
std::vector<Tag> tagSet;
98+
std::vector<unsigned int> mTags;
10799
};
108100

109101
} // namespace monitoring

include/Monitoring/Monitoring.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ class Monitoring
8787
/// Adds global tag
8888
/// \param name tag name
8989
/// \param value tag value
90-
void addGlobalTag(std::string name, std::string value);
90+
void addGlobalTag(std::string_view name, std::string_view value);
91+
92+
/// Adds predefined global tag
93+
/// \param tag tag index (use predefined enums form tag:: namespace)
94+
void addGlobalTag(const unsigned int tag);
9195

9296
/// Returns a metric which will be periodically sent to backends
9397
/// \param name metric name

include/Monitoring/Tag.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

include/Monitoring/Tags.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
///
2+
/// \file Tags.h
3+
/// \author Adam Wegrzynek <[email protected]>
4+
///
5+
6+
#ifndef ALICEO2_MONITORING_TAG_H
7+
#define ALICEO2_MONITORING_TAG_H
8+
9+
#include <string>
10+
11+
namespace o2
12+
{
13+
/// ALICE O2 Monitoring system
14+
namespace monitoring
15+
{
16+
17+
namespace tags
18+
{
19+
using namespace std::string_view_literals;
20+
21+
// Detector tag indexes
22+
static constexpr std::string_view detectorTag = "detector";
23+
enum Detector { ACO = 0 , AD, CPV, EMC, FMD, HMP, MCH, MTR, PHS, PMD, ITS, T0, TOF, TPC, TRD, V0};
24+
25+
// Subsystem tag indexes
26+
static constexpr std::string_view subsystemTag = "subsystem";
27+
enum Subsystem { QC = 16, Readout, DPL, CRU };
28+
29+
// Single tag array
30+
static constexpr std::array<std::pair<std::string_view, std::string_view>, 20> TAG_ARRAY = {{
31+
{detectorTag, "ACO"sv}, // 0
32+
{detectorTag, "AD"sv}, // 1
33+
{detectorTag, "CPV"sv}, // 2
34+
{detectorTag, "EMC"sv}, // 3
35+
{detectorTag, "FMD"sv}, // 4
36+
{detectorTag, "HMP"sv}, // 5
37+
{detectorTag, "MCH"sv}, // 6
38+
{detectorTag, "MTR"sv}, // 7
39+
{detectorTag, "PHS"sv}, // 8
40+
{detectorTag, "PHS"sv}, // 9
41+
{detectorTag, "PMD"sv}, // 10
42+
{detectorTag, "ITS"sv}, // 11
43+
{detectorTag, "TOF"sv}, // 12
44+
{detectorTag, "TPC"sv}, // 13
45+
{detectorTag, "TRD"sv}, // 14
46+
{detectorTag, "V0"sv}, // 15
47+
{subsystemTag, "QC"sv},
48+
{subsystemTag, "Readout"sv},
49+
{subsystemTag, "DLP"sv},
50+
{subsystemTag, "CRU"sv}
51+
}};
52+
}
53+
54+
} // namespace monitoring
55+
} // namespace o2
56+
57+
#endif // ALICEO2_MONITORING_TAG_H

src/Backends/ApMonBackend.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ inline int ApMonBackend::convertTimestamp(const std::chrono::time_point<std::chr
3434
return static_cast<int>(std::chrono::system_clock::to_time_t(timestamp));
3535
}
3636

37-
void ApMonBackend::addGlobalTag(std::string, std::string value)
37+
void ApMonBackend::addGlobalTag(std::string_view /*name*/, std::string_view value)
3838
{
3939
if (!entity.empty()) entity += ".";
4040
entity += value;
@@ -143,8 +143,11 @@ void ApMonBackend::sendMultiple(std::string, std::vector<Metric>&& metrics)
143143
void ApMonBackend::send(const Metric& metric)
144144
{
145145
std::string name = metric.getName();
146-
for (const auto& tag : metric.getTags()) {
147-
name += "," + tag.name + "=" + tag.value;
146+
for (const auto& tagIndex : metric.getTags()) {
147+
name += ",";
148+
name += tags::TAG_ARRAY[tagIndex].first;
149+
name += "=";
150+
name += tags::TAG_ARRAY[tagIndex].second;
148151
}
149152

150153
switch(metric.getType()) {

src/Backends/ApMonBackend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ApMonBackend final : public Backend
5454
/// Extends entity value
5555
/// \param name tag name (unused)
5656
/// \param value tag value that is concatenated to entity string
57-
void addGlobalTag(std::string name, std::string value) override;
57+
void addGlobalTag(std::string_view name, std::string_view value) override;
5858
private:
5959
/// Converts timestamp to format supported by ApMonBackend
6060
/// \param timestamp timestamp in std::chrono::time_point format

src/Backends/Flume.cxx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ std::string Flume::metricToJson(const Metric& metric)
3232
header.put<std::string>("timestamp", std::to_string(convertTimestamp(metric.getTimestamp())));
3333
header.put<std::string>("name", metric.getName());
3434
header.put<std::string>("value_value", boost::lexical_cast<std::string>(metric.getValue()));
35-
for (const auto& tag : metric.getTags()) {
36-
header.put<std::string>("tag_" + tag.name, tag.value);
37-
}
35+
36+
for (const auto& tagIndex : metric.getTags()) {
37+
header.put<std::string>("tag_" + std::string(tags::TAG_ARRAY[tagIndex].first.data()), tags::TAG_ARRAY[tagIndex].second.data());
38+
}
3839
event.push_back(std::make_pair("headers", header));
3940
event.put<std::string>("body", "");
4041
std::stringstream ss;
@@ -66,8 +67,8 @@ std::string Flume::metricsToJson(std::string measurement, std::vector<Metric>&&
6667
boost::property_tree::ptree header = globalHeader;
6768
header.put<std::string>("timestamp", std::to_string(convertTimestamp(metrics.front().getTimestamp())));
6869
header.put<std::string>("name", measurement);
69-
for (const auto& tag : metrics.front().getTags()) {
70-
header.put<std::string>("tag_" + tag.name, tag.value);
70+
for (const auto& tagIndex : metrics.front().getTags()) {
71+
header.put<std::string>("tag_" + std::string(tags::TAG_ARRAY[tagIndex].first.data()), tags::TAG_ARRAY[tagIndex].second.data());
7172
}
7273
for (auto& metric : metrics) {
7374
header.put<std::string>("value_" + metric.getName(), boost::lexical_cast<std::string>(metric.getValue()));
@@ -96,9 +97,9 @@ inline unsigned long Flume::convertTimestamp(const std::chrono::time_point<std::
9697
).count();
9798
}
9899

99-
void Flume::addGlobalTag(std::string name, std::string value)
100+
void Flume::addGlobalTag(std::string_view name, std::string_view value)
100101
{
101-
globalHeader.put<std::string>(name, value);
102+
globalHeader.put<std::string>("tag_" + std::string(name), value.data());
102103
}
103104

104105
} // namespace backends

0 commit comments

Comments
 (0)