@@ -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+ << " \n View 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 << " \n Waiting for exporters to run...\n " ;
107+ absl::SleepFor (absl::Milliseconds (5100 ));
106108}
0 commit comments