OpenCensus export to stackdriver not working

342 views
Skip to first unread message

David Tildesley

unread,
Mar 26, 2019, 5:01:15 PM3/26/19
to Google Stackdriver Discussion Forum
Hi,

I used the example (QuickStart) java app to send OpenCensus (latest version) managed metrics to stackdriver.

I am running this from local laptop.

I am using service account private key - I have successfully used it in the same scenario, same project but using google java client library to publish the metrics to stackdriver - those metrics were visible in stackdriver monitoring.

But for some reason the OpenCensus export is not working. The metrics don't appear. But there is no error message - the example app appears to be doing what it should. Has anyone else experienced this.

The reason for using OpenCensus is because that is what Google officially recommend - they actively discourage using the client library for creating metrics. Code is below (note I have replaced the actual projectid with "myproject")

import com.google.api.MonitoredResource;
import
com.google.common.collect.Lists;
import
io.opencensus.common.Duration;
import
io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
import
io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
import
io.opencensus.stats.*;
import
io.opencensus.stats.Measure.MeasureLong;
import
io.opencensus.stats.View.Name;
import
io.opencensus.tags.TagKey;

import
java.io.IOException;
import
java.util.Collections;
import
java.util.Random;
import
java.util.concurrent.TimeUnit;

public class
QuickStart {
   
private static final int EXPORT_INTERVAL = 60;
    private static final
MeasureLong LATENCY_MS = MeasureLong.create(
           
"task_latency1",
           
"The task latency in milliseconds",
           
"ms");
   
// Latency in buckets:
    // [>=0ms, >=100ms, >=200ms, >=400ms, >=1s, >=2s, >=4s]
   
private static final BucketBoundaries LATENCY_BOUNDARIES = BucketBoundaries.create(
            Lists.newArrayList(
0d, 100d, 200d, 400d, 1000d, 2000d, 4000d));
    private static final
StatsRecorder STATS_RECORDER = Stats.getStatsRecorder();

    public static void
main(String[] args) throws IOException, InterruptedException {
       
// Register the view. It is imperative that this step exists,
        // otherwise recorded metrics will be dropped and never exported.
       
View view = (View) View.create(
                Name.create(
"task_latency_distribution1"),
               
"The distribution of the task latencies.",
               
LATENCY_MS,
               
Aggregation.Distribution.create(LATENCY_BOUNDARIES),
               
Collections.<TagKey>emptyList());

       
ViewManager viewManager = Stats.getViewManager();
       
viewManager.registerView(view);

       
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
        // Exporters use Application Default Credentials to authenticate.
        // See https://developers.google.com/identity/protocols/application-default-credentials
        // for more details.
        //StackdriverStatsExporter.createAndRegister();
       
StackdriverStatsExporter.createAndRegister(
                StackdriverStatsConfiguration
                        .builder()
                        .setProjectId(
"myproject")
                        .setExportInterval(Duration.fromMillis(
100000))
                        .setMonitoredResource(MonitoredResource.newBuilder()
                                .setType(
"global")
                                .putLabels(
"project_id","myproject")
                                .build())
                        .build())
;

       
// Record 100 fake latency values between 0 and 5 seconds.
       
Random rand = new Random();
        for
(int i = 0; i < 100; i++) {
           
long ms = (long) (TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS) * rand.nextDouble());
           
System.out.println(String.format("Latency %d: %d", i, ms));
           
STATS_RECORDER.newMeasureMap().put(LATENCY_MS, ms).record();

       
}

       
// The default export interval is 60 seconds. The thread with the StackdriverStatsExporter must
        // live for at least the interval past any metrics that must be collected, or some risk being
        // lost if they are recorded after the last export.

       
System.out.println(String.format(
               
"Sleeping %d seconds before shutdown to ensure all records are flushed.", EXPORT_INTERVAL));
       
Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS));
   
}
}

Mary Koes

unread,
Mar 27, 2019, 8:33:56 AM3/27/19
to David Tildesley, Morgan McLean, Pritam Shah, Google Stackdriver Discussion Forum

--
© 2016 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
 
Email preferences: You received this email because you signed up for the Google Stackdriver Discussion Google Group (google-stackdr...@googlegroups.com) to participate in discussions with other members of the GoogleStackdriver community.
---
You received this message because you are subscribed to the Google Groups "Google Stackdriver Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-stackdriver-d...@googlegroups.com.
To post to this group, send email to google-stackdr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-stackdriver-discussion/14ddb864-bca5-4c9a-b8f9-314af7b088d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Morgan McLean

unread,
Mar 27, 2019, 6:38:50 PM3/27/19
to Mary Koes, Rahul Patel, Dino Oliva, David Tildesley, Pritam Shah, Google Stackdriver Discussion Forum, Bogdan Drutu
+ Rahul and Dino who can assist
Message has been deleted
Message has been deleted

David Tildesley

unread,
Mar 30, 2019, 5:42:00 AM3/30/19
to Google Stackdriver Discussion Forum
Hi Rahul,

I haven't heard from you. I tried it again on another machine - same result - silent failure to export the metrics. I think maybe you were running it on a local GAE enviroment?  
However the objective for my test is to run it as an external application - i.e. no GAE.  Let me know if you think otherwise..

Regards,
David.

henr...@macys.com

unread,
Mar 30, 2019, 6:32:57 AM3/30/19
to Google Stackdriver Discussion Forum
Poor David,

I'm also running into a similar issue

Good luck

HP

Javier Kohen

unread,
Apr 1, 2019, 10:43:41 AM4/1/19
to Google Stackdriver Discussion Forum
David,

I'm not familiar with the implementation details, but I wonder if the program is exiting before writing the data. Looking at your code, it looks like you set the export interval to 100 seconds, but the program only sleeps for 60 seconds before exiting.

@Rahul can we get debug logs or metrics to understand how many requests have been sent?
Message has been deleted

David Tildesley

unread,
Apr 2, 2019, 2:43:20 AM4/2/19
to Google Stackdriver Discussion Forum
Hi Rahul,

Thanks for your assistance today. It is now working after your suggestion to set environment variable GOOGLE_CLOUD_PROJECT with my project id. 

It's not clear why explicitly setting the project_id in the code did not appear to work.

But anyway - we have confirmed that we can use the OpenCensus with StackDriver Exporter on a non Google environment which was the objective.

Regards,
David.

David Tildesley

unread,
Apr 2, 2019, 7:02:10 PM4/2/19
to Google Stackdriver Discussion Forum
Unfortunately, that was not the solution. I tried to repeat it on another machine - it failed to record the metrics. So I reviewed the changes I had made just prior to talking to Rahul:

2. Used exactly the imports (with the same version - 0.16.0) as documented at https://opencensus.io/exporters/supported-exporters/java/stackdriver-stats/#1

So I repeated this on the other machine - voila! the metrics appeared. 

I was then thinking that it could be a problem with the latest version (0.20.0) of OpenCensus I had been using up to then.

So I changed it back up to 0.20.0, recompiled etc. and ran it again - expecting not to see any metrics appear based on my new theory. about a problem in the version. But ... no ... wrong again - the metrics appeared.

So now I am wondering what is going on? I look at the example code from the link above, and really I don't see what is fundamentally different from what I had been using.

More investigation needed ...

David Tildesley

unread,
Apr 2, 2019, 8:52:03 PM4/2/19
to Google Stackdriver Discussion Forum
Right - so here is the conclusion - in the trial and error process to get the QuickStart sample running I am pretty sure I ended up with a library import that had a conflicting namespace. I haven't got the time to figure out what library caused it.

Now that I have stripped it back to the bare minimum I can start building it up again - e.g. specify the projectid in the code, change to a different resource type etc.


On Tuesday, April 2, 2019 at 7:43:20 PM UTC+13, David Tildesley wrote:
Reply all
Reply to author
Forward
0 new messages