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

Commit df22bff

Browse files
authored
Show view data before sleeping, move comments around. (#89)
1 parent d28a1bb commit df22bff

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

examples/helloworld/hello_world.cc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,33 @@ int main(int argc, char **argv) {
7676
opencensus::stats::View view(video_size_view);
7777
video_size_view.RegisterForExport();
7878

79-
opencensus::trace::AlwaysSampler sampler;
79+
// Samplers are potentially expensive to construct. Use one long-lived sampler
80+
// instead of constructing one per Span.
81+
static opencensus::trace::AlwaysSampler sampler;
82+
83+
// Done initializing. Video processing starts here:
8084
auto span = opencensus::trace::Span::StartSpan("my.org/ProcessVideo", nullptr,
8185
{&sampler});
82-
83-
// Process video.
84-
// Record the processed video size.
8586
span.AddAnnotation("Start processing video.");
8687
// Sleep for [1,10] milliseconds to fake work.
8788
absl::SleepFor(absl::Milliseconds(rand() % 10 + 1));
89+
// Record the processed video size.
8890
opencensus::stats::Record({{VideoSizeMeasure(), 25648}},
8991
{{kFrontendKey, "video size"}});
9092
span.AddAnnotation("Finished processing video.");
9193
span.End();
9294

93-
// Sleep for ~5 seconds to ensure that exporters will process the span.
94-
std::cout << "Wait longer than the reporting duration...\n";
95-
absl::SleepFor(absl::Milliseconds(5100));
96-
97-
// Sleep while exporters run in the background.
98-
std::cout << "Views:\n" << video_size_view.DebugString() << "\n";
95+
// Report view data.
96+
std::cout << "video_size_view definitions:" << video_size_view.DebugString()
97+
<< "\nView data:\n";
9998
const auto data = view.GetData();
100-
if (data.type() == opencensus::stats::ViewData::Type::kDistribution) {
101-
for (auto &it : data.distribution_data()) {
102-
for (auto &name : it.first) std::cout << name << " : ";
103-
std::cout << it.second.DebugString() << "\n";
104-
}
99+
assert(data.type() == opencensus::stats::ViewData::Type::kDistribution);
100+
for (auto &it : data.distribution_data()) {
101+
std::cout << " ";
102+
for (auto &name : it.first) std::cout << name << " : ";
103+
std::cout << it.second.DebugString() << "\n";
105104
}
105+
106+
std::cout << "\nWaiting for exporters to run...\n";
107+
absl::SleepFor(absl::Milliseconds(5100));
106108
}

0 commit comments

Comments
 (0)