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));
}
}