SPEAKER: Observability is increasingly
required of our systems as we work to improve reliability.
Metrics represent the health of your system over time
and are a crucial component of observability.
And while services running in GCP benefit from out-of-the-box
platform instrumentation, it's often necessary to add more.
Today, we'll take a look at how to do that with OpenCensus
and Stackdriver.
This is the Stack Doctor.
[MUSIC PLAYING]
Last time, we looked at how our microservices can benefit
from a service mesh that exports monitoring, logging,
and tracing data to Stackdriver.
Now, let's add our own telemetry with custom metrics.
When considering adding metric instrumentation
to your applications, you generally
have three options to choose from.
Your first option is to use the Stackdriver Monitoring
API to send metrics and data points to Stackdriver directly.
This is what the Stackdriver monitoring agent does.
However, you should know that this API
is low level and proprietary.
And at this point, we rarely recommend using it directly.
Let's look at some more extensible
and easier-to-use options.
Your second option is to use Prometheus.
It's an open source monitoring solution.
Because using Prometheus involves
more than just instrument and your code,
we'll come back to this in our next episode.
Your third option and the one we recommend
for creating your own custom metric instrumentation
is OpenCensus.
It's a single distribution of libraries
that collect metrics and distributed traces
from your services.
Let's look at how to use it to create
custom metrics and Stackdriver.
We'll use Node.js for this example.
First, we need to install the two required packages--
OpenCensus itself and the Stackdriver Explorer.
We'll do that by installing the packages
with NPM install at the command line
or include them as part of the Docker file.
Next, we'll include these packages in our code.
This needs to be done at the very top of the file
before any other code.
We are now ready to start creating metrics.
But let's review the basic concepts of the OpenCensus data
model first.
To start, we'll need a view.
It'll describe how to collect and aggregate
individual measurements.
Views can be thought of as the time series we'll
be adding points to later.
Next, we'll use measures to describe how the data
points will be recorded.
We'll also specify an aggregation,
which is a function to summarize the data.
Finally, we'll create measurements.
These will be our actual data points.
We'll need to use the view we created to aggregate them.
Otherwise, it'll get dropped on ingest.
Now that we've covered the data model,
let's walk through the code we'll
need to write to actually create and send metrics.
We'll put a link to the repo and my actual code
and a detailed solution paper in the video description.
First, we'll define our export interval
and create the measure.
This is where we define the metric that we'll
be sending the Stackdriver.
The key things here are the fact that it's
going to be an Int64 value and the name of the description
of the metric.
Next, we'll use that definition to actually register
the metric with Stackdriver.
We're calling to create new method
to create the view and then the register view method
to actually register it.
Note that I'm using the last value aggregation type.
This means that only the last value
that was captured during the export interval will be sent.
You can also use distribution count and some aggregations.
After that, we'll register the Stackdriver exporter.
This is the mechanism that will send
the metrics to the Stackdriver monitoring backend.
Finally, we are ready to create actual data points.
We do that by referring to the measure we already created
and specifying a value.
We are finally ready to run our code
and send the metric data to the Stackdriver.
And there it is.
I can see the metric in Metrics Explorer.
It has the name I assigned it.
And it's showing the last value that was
generated when I ran the code.
This can be an easy way to expose additional information
about the performance and health of your application
to Stackdriver thanks to OpenCensus.
Thank you for joining me today and come back
next time when we look at another approach to custom
metrics with Prometheus.
That's it for this episode.
This is the Stack Doctor.
Stay healthy out there.
[MUSIC PLAYING]