Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit f1c961d

Browse files
g-easyIan Sturdy
authored andcommitted
Re-style stats StdoutExporter. (#71)
1 parent c19efee commit f1c961d

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

opencensus/exporters/stats/stdout/internal/stdout_exporter.cc

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "absl/memory/memory.h"
2222
#include "absl/strings/str_cat.h"
23+
#include "absl/strings/str_split.h"
2324
#include "absl/time/time.h"
2425
#include "opencensus/stats/stats.h"
2526

@@ -30,10 +31,18 @@ namespace stats {
3031
namespace {
3132

3233
// Functions to print data for different aggregation types.
33-
std::string DataToString(double data) { return absl::StrCat(": ", data); }
34-
std::string DataToString(int64_t data) { return absl::StrCat(": ", data); }
34+
std::string DataToString(double data) { return absl::StrCat(": ", data, "\n"); }
35+
std::string DataToString(int64_t data) {
36+
return absl::StrCat(": ", data, "\n");
37+
}
3538
std::string DataToString(const opencensus::stats::Distribution& data) {
36-
return absl::StrCat("\n", data.DebugString());
39+
std::string output = "\n";
40+
std::vector<std::string> lines = absl::StrSplit(data.DebugString(), '\n');
41+
// Add indent.
42+
for (const auto& line : lines) {
43+
absl::StrAppend(&output, " ", line, "\n");
44+
}
45+
return output;
3746
}
3847

3948
} // namespace
@@ -83,23 +92,23 @@ void StdoutExporter::Handler::ExportViewDataImpl(
8392
const opencensus::stats::ViewDescriptor& descriptor, absl::Time start_time,
8493
absl::Time end_time,
8594
const opencensus::stats::ViewData::DataMap<DataValueT>& data) {
86-
std::string output;
87-
absl::StrAppend(&output, "\nData for view \"", descriptor.name(), "\" from ",
88-
absl::FormatTime(start_time), " to ",
89-
absl::FormatTime(end_time), ":\n");
90-
for (const auto& tag_key : descriptor.columns()) {
91-
absl::StrAppend(
92-
&output, tag_key,
93-
tag_key.size() > 10 ? "" : std::string(10 - tag_key.size(), ' '));
95+
if (data.size() == 0) {
96+
std::cout << absl::StrCat("No data for view \"", descriptor.name(),
97+
"\" from ", absl::FormatTime(start_time),
98+
".\n\n");
99+
return;
94100
}
95-
absl::StrAppend(&output, "\n");
101+
// Build a string so we can write it to cout in one shot to minimize crosstalk
102+
// if multiple threads write to cout simultaneously.
103+
std::string output = absl::StrCat("Data for view \"", descriptor.name(),
104+
"\" from ", absl::FormatTime(start_time),
105+
" to ", absl::FormatTime(end_time), ":\n");
96106
for (const auto& row : data) {
97-
for (const auto& tag_value : row.first) {
98-
absl::StrAppend(
99-
&output, tag_value,
100-
tag_value.size() > 10 ? "" : std::string(10 - tag_value.size(), ' '));
107+
absl::StrAppend(&output, " ");
108+
for (int i = 0; i < descriptor.columns().size(); ++i) {
109+
absl::StrAppend(&output, descriptor.columns()[i], "=", row.first[i], " ");
101110
}
102-
absl::StrAppend(&output, DataToString(row.second), "\n");
111+
absl::StrAppend(&output, DataToString(row.second));
103112
}
104113
absl::StrAppend(&output, "\n");
105114
std::cout << output;

0 commit comments

Comments
 (0)