C++ Quickstart

Build and run the example:
git clone https://github.com/census-instrumentation/opencensus-cpp.git
cd opencensus-cpp
bazel build //examples/helloworld:helloworld

Quickstart Example

The example demonstrates how to record stats and traces for a video processing system. It records data with the “frontend” tag so that collected data can be broken by the frontend user who initiated the video processing.

In this case we are using stdout exporters which we register at the beginning.

// Register stdout exporters.

We define a measure for video size which records the sizes in megabytes "MBy".
// Call measure so that it is initialized.

We create a view and register it with the local Stdout exporter.
// Create view to see the processed video size distribution broken down
// by frontend. The view has bucket boundaries (0, 256, 65536)
//that will group measure values into histogram buckets.

constopencensus::stats::ViewDescriptor video_size_view =
   .set_description("processed video size over time")
(opencensus::stats::BucketBoundaries::Exponential(2, 256, 256)))

// Create the view.
opencensus::stats::View view(video_size_view);

// Register the view for export.

Example View
name: "my.org/views/video_size"
measure: name: "my.org/measure/video_size"; units: "MBy";
description: "size of processed videos"; type: int64
aggregation: Distribution with Buckets:
aggregation window: Cumulative
columns: my.org/keys/frontend
description: "processed video size over time"
video size : count: 1 mean: 25648 sum of squared deviation: 0 min: 25648 max: 25648
histogram counts: 0, 0, 0, 0, 0, 0, 0, 0, 1, 0

In this case the view stores a distribution. The example records 1 video size to the view, which is 25648. This shows up in the histogram, with 1 bucket having a single value in it.

opencensus::stats::Record({{VideoSizeMeasure(), 25648}},{{kFrontendKey, "video size"}});

Example Span
Name: my.org/ProcessVideo
Parent SpanId: 0000000000000000 (remote: false)
Start time: 2018-03-04T22:42:54.492839757-08:00
End time: 2018-03-04T22:42:54.500995971-08:00
Attributes: (0 dropped)
Annotations: (0 dropped)
  2018-03-04T22:42:54.492858312-08:00: Start processing video.
  2018-03-04T22:42:54.500992796-08:00: Finished processing video.
Message events: (0 dropped)
Links: (0 dropped)
Span ended: true
Status: OK

Span context information is displayed in hexadecimal on a single line which is the concatenation of TraceId, SpanId, and span options. Parent SpanId is displayed on the following line. In this case there is no parent (root span), so the parent id is 0. There were 2 attributes added. After work has been completed a span must be ended by the user. A span that is still active (i.e. not ended), will not be exported.

span.AddAnnotation("Start processing video.");
span.AddAnnotation("Finished processing video.");