Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 62c3bb4

Browse files
authored
Keep quickstart consistent everywhere (#1540)
1 parent 0aef639 commit 62c3bb4

File tree

6 files changed

+105
-96
lines changed

6 files changed

+105
-96
lines changed

examples/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ java_binary(
163163
)
164164

165165
java_binary(
166-
name = "StackdriverExample",
167-
main_class = "io.opencensus.examples.stats.StackdriverExample",
166+
name = "StackdriverQuickstart",
167+
main_class = "io.opencensus.examples.stats.StackdriverQuickstart",
168168
runtime_deps = [
169169
":opencensus_examples",
170170
],

examples/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,21 @@ $ prometheus --config.file=prometheus.yaml
141141

142142
Stats will be shown on Prometheus UI on http://localhost:9090.
143143

144-
## To run "StackdriverExample" use
144+
## To run "StackdriverQuickstart" use
145145

146146
See the full tutorial on [OpenCensus website](https://opencensus.io/guides/exporters/supported-exporters/java/stackdriver/).
147147

148148
### Gradle
149149
```
150-
$ ./build/install/opencensus-examples/bin/StackdriverExample
150+
$ ./build/install/opencensus-examples/bin/StackdriverQuickstart
151151
```
152152

153153
### Maven
154154
```
155-
$ ./target/appassembler/bin/StackdriverExample
155+
$ ./target/appassembler/bin/StackdriverQuickstart
156156
```
157157

158158
### Bazel
159159
```
160-
$ ./bazel-bin/StackdriverExample
160+
$ ./bazel-bin/StackdriverQuickstart
161161
```

examples/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ task repl(type: CreateStartScripts) {
148148
classpath = jar.outputs.files + project.configurations.runtime
149149
}
150150

151-
task stackdriverStatsExample(type: CreateStartScripts) {
152-
mainClassName = 'io.opencensus.examples.stats.StackdriverExample'
153-
applicationName = 'StackdriverExample'
151+
task stackdriverStatsQuickstart(type: CreateStartScripts) {
152+
mainClassName = 'io.opencensus.examples.stats.StackdriverQuickstart'
153+
applicationName = 'StackdriverQuickstart'
154154
outputDir = new File(project.buildDir, 'tmp')
155155
classpath = jar.outputs.files + project.configurations.runtime
156156
}
@@ -165,6 +165,6 @@ applicationDistribution.into('bin') {
165165
from(helloWorldServer)
166166
from(helloWorldClient)
167167
from(repl)
168-
from(stackdriverStatsExample)
168+
from(stackdriverStatsQuickstart)
169169
fileMode = 0755
170170
}

examples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@
147147
<mainClass>io.opencensus.examples.quickstart.Repl</mainClass>
148148
</program>
149149
<program>
150-
<id>StackdriverExample</id>
151-
<mainClass>io.opencensus.examples.stats.StackdriverExample</mainClass>
150+
<id>StackdriverQuickstart</id>
151+
<mainClass>io.opencensus.examples.stats.StackdriverQuickstart</mainClass>
152152
</program>
153153
</programs>
154154
</configuration>

examples/src/main/java/io/opencensus/examples/stats/StackdriverExample.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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. */
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+
}

0 commit comments

Comments
 (0)