Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

OpenCensus Stackdriver Stats Exporter

The OpenCensus Stackdriver Stats Exporter is a stats exporter that exports data to Stackdriver Monitoring. Stackdriver Monitoring provides visibility into the performance, uptime, and overall health of cloud-powered applications. Stackdriver ingests that data and generates insights via dashboards, charts, and alerts.



To use this exporter, you must have an application that you'd like to monitor. The app can be on Google Cloud Platform, on-premise, or another cloud platform.

In order to be able to push your stats to Stackdriver Monitoring, you must:

  1. Create a Cloud project.
  2. Enable billing.
  3. Enable the Stackdriver Monitoring API.

These steps enable the API but don't require that your app is hosted on Google Cloud Platform.

Hello "Stackdriver Stats"

Add the dependencies to your project

For Maven add to your pom.xml:


For Gradle add to your dependencies:

compile 'io.opencensus:opencensus-api:0.19.2'
compile 'io.opencensus:opencensus-exporter-stats-stackdriver:0.19.2'
runtime 'io.opencensus:opencensus-impl:0.19.2'

Register the exporter

This uses the default configuration for authentication and a given project ID.

public class MyMainClass {
  public static void main(String[] args) {

Set Monitored Resource for exporter

By default, Stackdriver Stats Exporter will try to automatically detect the environment if your application is running on GCE, GKE or AWS EC2, and generate a corresponding Stackdriver GCE/GKE/EC2 monitored resource. For GKE particularly, you may want to set up some environment variables so that Exporter can correctly identify your pod, cluster and container. Follow the Kubernetes instruction here and here.

Otherwise, Exporter will use a global Stackdriver monitored resource with a project_id label, and it works fine when you have only one exporter running.

If you want to have multiple processes exporting stats for the same metric concurrently, and your application is running on some different environment than GCE, GKE or AWS EC2 (for example DataFlow), please associate a unique monitored resource with each exporter if possible. Please note that there is also an "opencensus_task" metric label that uniquely identifies the uploaded stats.

To set a custom MonitoredResource:

public class MyMainClass {
  public static void main(String[] args) {
    // A sample DataFlow monitored resource.
    MonitoredResource myResource = MonitoredResource.newBuilder()
                                               .putLabels("project_id", "my_project")
                                               .putLabels("job_name", "my_job")
                                               .putLabels("region", "us-east1")
    // Set a custom MonitoredResource. Please make sure each Stackdriver Stats Exporter has a 
    // unique MonitoredResource.      

For a complete list of valid Stackdriver monitored resources, please refer to Stackdriver Documentation. Please also note that although there are a lot of monitored resources available on Stackdriver, only a small subset of them are compatible with the Opencensus Stackdriver Stats Exporter.


This exporter uses google-cloud-java, for details about how to configure the authentication see here.

If you prefer to manually set the credentials use:

        .setCredentials(new GoogleCredentials(new AccessToken(accessToken, expirationTime)))
        .setExportInterval(Duration.create(10, 0))

Specifying a Project ID

This exporter uses google-cloud-java, for details about how to configure the project ID see here.

If you prefer to manually set the project ID use:


Java Versions

Java 7 or above is required for using this exporter.


Why did I get a PERMISSION_DENIED error from Stackdriver when using this exporter?

To use our Stackdriver Stats exporter, you need to set up billing for your cloud project, since creating and uploading custom metrics to Stackdriver Monitoring is not free.

To enable billing, follow the instructions here.

What is "opencensus_task" metric label ?

Stackdriver requires that each Timeseries to be updated only by one task at a time. A Timeseries is uniquely identified by the MonitoredResource and the Metric's labels. Stackdriver exporter adds a new Metric label for each custom Metric to ensure the uniqueness of the Timeseries. The format of the label is: {LANGUAGE}-{PID}@{HOSTNAME}, if {PID} is not available a random number will be used.

Why did I get an error "java.lang.NoSuchMethodError:", like ""?

This is probably because there is a version conflict on Guava in the dependency tree.

For example, is introduced to Guava 20.0. If your application has a dependency that bundles a Guava with version 19.0 or below (for example, gRPC 1.10.0), it might cause a NoSuchMethodError since doesn't exist before Guava 20.0.

In this case, please either add an explicit dependency on a newer version of Guava that has the new method (20.0 in the previous example), or if possible, upgrade the dependency that depends on Guava to a newer version that depends on the newer Guava (for example, upgrade to gRPC 1.12.0).

You can’t perform that action at this time.