|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 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 com.example.opencensus; |
| 18 | + |
| 19 | +// [START monitoring_opencensus_metrics_quickstart] |
| 20 | + |
| 21 | +import com.google.common.collect.Lists; |
| 22 | + |
| 23 | +import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter; |
| 24 | +import io.opencensus.stats.Aggregation; |
| 25 | +import io.opencensus.stats.BucketBoundaries; |
| 26 | +import io.opencensus.stats.Measure.MeasureLong; |
| 27 | +import io.opencensus.stats.Stats; |
| 28 | +import io.opencensus.stats.StatsRecorder; |
| 29 | +import io.opencensus.stats.View; |
| 30 | +import io.opencensus.stats.View.Name; |
| 31 | +import io.opencensus.stats.ViewManager; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.Random; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +public class Quickstart { |
| 39 | + private static final int EXPORT_INTERVAL = 60; |
| 40 | + private static final MeasureLong LATENCY_MS = MeasureLong.create( |
| 41 | + "task_latency", |
| 42 | + "The task latency in milliseconds", |
| 43 | + "ms"); |
| 44 | + // Latency in buckets: |
| 45 | + // [>=0ms, >=100ms, >=200ms, >=400ms, >=1s, >=2s, >=4s] |
| 46 | + private static final BucketBoundaries LATENCY_BOUNDARIES = BucketBoundaries.create( |
| 47 | + Lists.newArrayList(0d, 100d, 200d, 400d, 1000d, 2000d, 4000d)); |
| 48 | + private static final StatsRecorder STATS_RECORDER = Stats.getStatsRecorder(); |
| 49 | + |
| 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 = View.create( |
| 54 | + Name.create("task_latency_distribution_4"), |
| 55 | + "The distribution of the task latencies.", |
| 56 | + LATENCY_MS, |
| 57 | + Aggregation.Distribution.create(LATENCY_BOUNDARIES), |
| 58 | + Collections.emptyList()); |
| 59 | + |
| 60 | + ViewManager viewManager = Stats.getViewManager(); |
| 61 | + viewManager.registerView(view); |
| 62 | + |
| 63 | + // [START setup_exporter] |
| 64 | + // Enable OpenCensus exporters to export metrics to Stackdriver Monitoring. |
| 65 | + // Exporters use Application Default Credentials to authenticate. |
| 66 | + // See https://developers.google.com/identity/protocols/application-default-credentials |
| 67 | + // for more details. |
| 68 | + StackdriverStatsExporter.createAndRegister(); |
| 69 | + // [END setup_exporter] |
| 70 | + |
| 71 | + // [START example_code] |
| 72 | + // Record 100 fake latency values between 0 and 5 seconds. |
| 73 | + Random rand = new Random(); |
| 74 | + for (int i = 0; i < 100; i++) { |
| 75 | + long ms = (long) (TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS) * rand.nextDouble()); |
| 76 | + System.out.println(String.format("Latency %d: %d", i, ms)); |
| 77 | + STATS_RECORDER.newMeasureMap().put(LATENCY_MS, ms).record(); |
| 78 | + } |
| 79 | + |
| 80 | + // The default export interval is 60 seconds. The thread with the StackdriverStatsExporter must |
| 81 | + // live for at least the interval past any metrics that must be collected, or some risk being |
| 82 | + // lost if they are recorded after the last export. |
| 83 | + |
| 84 | + Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS)); |
| 85 | + // [END example_code] |
| 86 | + } |
| 87 | +} |
| 88 | +// [END monitoring_opencensus_metrics_quickstart] |
0 commit comments