• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.examples.stats;
18 
19 import com.google.common.collect.Lists;
20 import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
21 import io.opencensus.stats.Aggregation;
22 import io.opencensus.stats.BucketBoundaries;
23 import io.opencensus.stats.Measure.MeasureLong;
24 import io.opencensus.stats.Stats;
25 import io.opencensus.stats.StatsRecorder;
26 import io.opencensus.stats.View;
27 import io.opencensus.stats.View.Name;
28 import io.opencensus.stats.ViewManager;
29 import io.opencensus.tags.TagKey;
30 import java.io.IOException;
31 import java.util.Collections;
32 import java.util.Random;
33 import java.util.concurrent.TimeUnit;
34 
35 /**
36  * StackdriverQuickstart is an example of exporting a custom metric from OpenCensus to Stackdriver.
37  */
38 public final class StackdriverQuickstart {
39 
40   private static final int EXPORT_INTERVAL = 60;
41   private static final MeasureLong LATENCY_MS =
42       MeasureLong.create("task_latency", "The task latency in milliseconds", "ms");
43   // Latency in buckets:
44   // [>=0ms, >=100ms, >=200ms, >=400ms, >=1s, >=2s, >=4s]
45   private static final BucketBoundaries LATENCY_BOUNDARIES =
46       BucketBoundaries.create(Lists.newArrayList(0d, 100d, 200d, 400d, 1000d, 2000d, 4000d));
47   private static final StatsRecorder STATS_RECORDER = Stats.getStatsRecorder();
48 
49   /** Main launcher for the Stackdriver example. */
main(String[] args)50   public static void main(String[] args) throws IOException, InterruptedException {
51     // Register the view. It is imperative that this step exists,
52     // otherwise recorded metrics will be dropped and never exported.
53     View view =
54         View.create(
55             Name.create("task_latency_distribution"),
56             "The distribution of the task latencies.",
57             LATENCY_MS,
58             Aggregation.Distribution.create(LATENCY_BOUNDARIES),
59             Collections.<TagKey>emptyList());
60 
61     // Create the view manager
62     ViewManager viewManager = Stats.getViewManager();
63 
64     // Then finally register the views
65     viewManager.registerView(view);
66 
67     // [START setup_exporter]
68     // Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
69     // Exporters use Application Default Credentials to authenticate.
70     // See https://developers.google.com/identity/protocols/application-default-credentials
71     // for more details.
72     StackdriverStatsExporter.createAndRegister();
73     // [END setup_exporter]
74 
75     // Record 100 fake latency values between 0 and 5 seconds.
76     Random rand = new Random();
77     for (int i = 0; i < 100; i++) {
78       long ms = (long) (TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS) * rand.nextDouble());
79       System.out.println(String.format("Latency %d: %d", i, ms));
80       STATS_RECORDER.newMeasureMap().put(LATENCY_MS, ms).record();
81     }
82 
83     // The default export interval is 60 seconds. The thread with the StackdriverStatsExporter must
84     // live for at least the interval past any metrics that must be collected, or some risk being
85     // lost if they are recorded after the last export.
86 
87     System.out.println(
88         String.format(
89             "Sleeping %d seconds before shutdown to ensure all records are flushed.",
90             EXPORT_INTERVAL));
91     Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS));
92   }
93 }
94