Skip to content

Commit 37d4454

Browse files
authored
Fix issue with returning string_view (#215)
1 parent 7c6c3fd commit 37d4454

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

examples/2-TaggedMetrics.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ int main()
2424
// then add predefined tag
2525
monitoring->send(Metric{10, "myMetric"}.addTag(tags::Key::Detector, tags::Value::ACO));
2626
monitoring->send(Metric{10, "myMetric"}.addTag(tags::Key::CRU, 0));
27+
monitoring->send(Metric{10, "myMetric"}.addTag(tags::Key::CRU, 12));
28+
monitoring->send(Metric{10, "myMetric"}.addTag(tags::Key::CRU, 1234));
2729
}

include/Monitoring/Tags.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static constexpr std::array<std::string_view, 39> TAG_VALUE = {{
154154

155155
static constexpr std::string_view GetValue(const int value)
156156
{
157-
return value > 0 ? TAG_VALUE[value] : std::to_string(0 - value);
157+
return TAG_VALUE[value];
158158
}
159159

160160
} // namespace tags

src/Backends/ApMonBackend.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void ApMonBackend::send(const Metric& metric)
6666
entity += ',';
6767
entity += tags::TAG_KEY[key];
6868
entity += '=';
69-
entity += tags::GetValue(value);
69+
(value > 0) ? entity += tags::GetValue(value) : entity += std::to_string(0 - value);
7070
}
7171

7272
int valueSize = metric.getValuesSize();

src/Backends/InfluxDB.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ std::string InfluxDB::toInfluxLineProtocol(const Metric& metric)
8989
convert << name << "," << tagSet;
9090

9191
for (const auto& [key, value] : metric.getTags()) {
92-
convert << "," << tags::TAG_KEY[key] << "=" << tags::GetValue(value);
92+
convert << "," << tags::TAG_KEY[key] << "=";
93+
(value > 0) ? convert << tags::GetValue(value) : convert << (0 - value);
9394
}
9495
convert << ' ';
9596
for (const auto& [name, value] : metric.getValues()) {

src/Backends/StdOut.cxx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,27 @@ void StdOut::send(std::vector<Metric>&& metrics)
6565

6666
void StdOut::send(const Metric& metric)
6767
{
68-
std::ostringstream mStream;
69-
mStream << "[" << mPrefix << "] " << metric.getName();
68+
std::ostringstream convert;
69+
convert << "[" << mPrefix << "] " << metric.getName();
7070
for (auto& value : metric.getValues()) {
7171
auto stringValue = std::visit(overloaded{
7272
[](const std::string& value) -> std::string { return value; },
7373
[](auto value) -> std::string { return std::to_string(value); }
7474
}, value.second);
7575
if (metric.getValuesSize() == 1) {
76-
mStream << ',' << metric.getFirstValueType() << ' ' << stringValue;
76+
convert << ',' << metric.getFirstValueType() << ' ' << stringValue;
7777
} else {
78-
mStream << ' ' << value.first << '=' << stringValue;
78+
convert << ' ' << value.first << '=' << stringValue;
7979
}
8080
}
81-
mStream << ' ' << convertTimestamp(metric.getTimestamp()) << ' ' << tagString;
81+
convert << ' ' << convertTimestamp(metric.getTimestamp()) << ' ' << tagString;
8282

8383
for (const auto& [key, value] : metric.getTags()) {
84-
mStream << ',' << tags::TAG_KEY[key] << "=" << tags::GetValue(value);
84+
convert << ',' << tags::TAG_KEY[key] << "=";
85+
(value > 0) ? convert << tags::GetValue(value) : convert << (0 - value);
8586
}
86-
mStream << '\n';
87-
std::cout << mStream.str();
87+
convert << '\n';
88+
std::cout << convert.str();
8889
}
8990

9091
} // namespace backends

0 commit comments

Comments
 (0)