Saturday 2 May 2020

Tracing with OpenCensus and Stackdriver

In this post we'll see an example of tracing using OpenCensus, and exporting and viewing traces with Stackdriver, on Google Cloud Platform.

What is OpenCensus?
Is a software that collects traces and metrics from your application, displays them locally, and sends them to any analysis tool.

Thanks to distributed tracing, you can optimize the speed of your applications, understand how a request goes between your services, gather metrics about your architecture, to identify any bottleneck.

Traces and Spans.
A trace is a tree of spans. It is a collection of operations, showing the path of work through a system.

A span represents a single operation in a trace. It can be representative of an HTTP/HTTPS request, a remote procedure call (RPC), a query on a database, or even the path that a code takes in user code, and so on.

Example code in python
The core components we're going to instantiate in our code are the tracer and the exporter.

We'll also identify two types of span:
- a main span, calling a function in a loop
- a child span, doing HTTPS requests



Run the script
Install the dependencies:

pip3 install opencensus
pip3 install opencensus-ext-stackdriver

Make sure you are using a service-account with the correct Cloud Trace role.

Run the script:

python3 opencensus.py --project_id <gcp_project_name>

View the traces on GCP
On Google Cloud Platform console, go to Trace -> Trace list.

Search your trace using as filter: SpanName: oc_main_span
You should see something like this:


Clicking on the trace, the Trace Waterfall View should expand, and you can see all the spans:



On the right side, you can also see other details of the tracing.

If you want to create an Analysis Report, you should have at least 100 traces.






No comments:

Post a Comment