How to Find Performance Bottlenecks on Your Application (Effortlessly)

Eduardo Emery
Google Cloud - Community
5 min readAug 28, 2018

Nowadays, with so many tools and frameworks to make web and mobile development easy, we, developers, can find more time to focus on tracking and enhancing the application performance.

Usually, we try to focus on page loading time, latency, animations frame rates on different devices and so on; and getting to this development stage may be uncomfortable at first:

How do I measure things?

Is there anything that I can use for this?

And the harshest of them all:

How do I make sure I’m not interfering in the application with all this code for tracking?

This article will help you find out those answers without spending too much effort in coding.

Here, we’ll understand:

  1. How OpenCensus can help you
  2. An example of instrumented server
  3. How to send the collected data to various other services

OpenCensus and Node.js

OpenCensus is a tool that collects stats and tracing information from your application and sends it to many different services, such as Stackdriver, Jaeger, Zipkin and others.

The great thing about OpenCensus is that not only it is a vendor-agnostic library and supports implementation in many different languages such as Java, Go, Python, and Node.js, it has automatic tracing!

OpenCensus for Node.js (with which I had the opportunity to contribute to) supports automatic tracing for HTTP, HTTPS, HTTP2, and MongoDB. This means that once set up, it starts collecting traces without you having to explicitly indicate what is an operation in your application and how it should be named.

Actually, it is more than that, it is a guarantee that you will have a scalable low overhead code for collecting data about your application. And, by being vendor-agnostic, you are sure to be able to change your backend service easily in the future, if you need.

Now, let’s get an application to see how easy it can be instrumented with the automatic tracing feature of OpenCensus for Node.js.

Here, we will be using the Node Easy Notes App to make this demonstration.

The Example Application

To not reinvent the wheel, let’s use an already existing application called the Node Easy Notes App. It’s a simple CRUD application (Create, Read, Update and Delete) for notes. It uses Express and MongoDB to build and store the notes information.

Our goal will be to track the latency of its endpoints and get more information about the inside operations it had to make to give us a response.

This is useful to know where to focus the development team attention when enhancing the application performance. One can discover that, for example, the longtime an endpoint is taking to respond, is due to a non-efficient query that is being made in the database in a specific scenario.

So, let’s dive into it.

First, make sure you have Node.js (version 6 or greater) and npm installed.

Once downloaded the Node Easy Notes App, navigate to its root directory and install its dependencies by running:

npm install

After completed, you can test it by running:

node server.js

Now, go to localhost://3000 and see it working. You can also try different routes like localhost://3000/notes.

From this point on we already have the server up and running for the instrumentation step.

Instrumenting the Application

Maybe you are unfamiliar with instrumenting applications, but as Jaana Dogan said:

Allowing visibility at the platform levels helps our users to debug and determine unexpected events quickly.

This has a direct impact on your infrastructure and how your team manages the resources. But just because it is an important thing, doesn’t mean it has to be difficult.

The magic mentioned at the beginning will happen from this moment on, get ready!

First, install OpenCensus for Node.js with:

npm install @opencensus/nodejs

Then, simply open the server.js file and add these following lines in the begging of it:

var tracing = require('@opencensus/nodejs');tracing.start();

Voilà!

By adding that, we import and start an instance of tracing, the guy responsible for adding the plugins that will automatically collect traces for HTTP, HTTPS, HTTP2, and MongoDB.

That’s it, we are done for the day.

Now, you can run the application again with node server.js and access one of its endpoints, like localhost:3000/.

If you look to the terminal, you will see some tracing information being printed out as it performs operations.

Okay, this doesn’t look very friendly.

Well, it gets more elegant than that. You can add exporters to your tracing instance to be able to send data to other services like Stackdriver.

Sending the Collected Data to External Services

To accomplish this, we just need to import the exporter and instantiate it with the right parameters. In the OpenCensus for Node GitHub page, it is possible to find more information on each exporter separately.

For Stackdriver, you will need to have a Google Cloud Project with Stackdriver Tracing enabled. Make sure to also enable the Application Default Credentials for authentication (visit https://developers.google.com/identity/protocols/application-default-credentials for more information) with:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials.json

Once setting up the project and the environment is done. We just have to install the Stackdriver exporter with:

npm install @opencensus/exporter-stackdriver

After that, import and pass it to the tracing, like this:

var tracing = require('@opencensus/nodejs');var stackdriver = require('@opencensus/exporter-stackdriver');exporter = new stackdriver.StackdriverTraceExporter({projectId: "your-project-id"});tracing.start({exporter});

Run your application again with node server.js and play once more with the endpoints.

Viewing the Traces

Now, just go to the Google Cloud Console and see your traces! There, you can find different ways to look at your data. You can also select on a specific trace to know more about it, see its children spans and metadata.

Overview of traces
Getting more information about individual traces

Now it is your turn to play around.

Add OpenCensus for Node.js in your application just like we did here and comment below what insights it gave you!

Conclusion

Instrumentation can be a decisive step when launching an application to market, whether big or small ones.

It has a direct impact on the team activities planning and resources usage.

That’s why you must try to integrate tools like OpenCensus as soon as possible.

Given how easy it is to integrate, there are no apologies for not doing so.

Now, get this example, try it, change it, improve it and tell me your impressions and how easy or difficult it was.

Please leave a comment down below. I’d love to hear from you.

--

--