Skip to content

Commit 527635c

Browse files
committed
Add OpenCensus sample code.
1 parent 31b8aa1 commit 527635c

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

opencensus/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2018 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- [START opencensus_pom] -->
18+
<project>
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>jar</packaging>
21+
<groupId>com.example</groupId>
22+
<artifactId>opencensus-samples</artifactId>
23+
<version>1.0</version>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.0.10</version>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
<maven.compiler.target>1.8</maven.compiler.target>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
</properties>
40+
41+
<dependencies>
42+
<!-- [START dependencies] -->
43+
<dependency>
44+
<groupId>io.opencensus</groupId>
45+
<artifactId>opencensus-api</artifactId>
46+
<version>0.15.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.opencensus</groupId>
50+
<artifactId>opencensus-impl</artifactId>
51+
<version>0.15.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.opencensus</groupId>
55+
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
56+
<version>0.15.0</version>
57+
</dependency>
58+
<!-- [END dependencies] -->
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.codehaus.mojo</groupId>
65+
<artifactId>exec-maven-plugin</artifactId>
66+
<version>1.6.0</version>
67+
<executions>
68+
<execution>
69+
<goals>
70+
<goal>java</goal>
71+
</goals>
72+
</execution>
73+
</executions>
74+
<configuration>
75+
<mainClass>com.example.opencensus.Quickstart</mainClass>
76+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
77+
</configuration>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
</project>
82+
<!-- [END opencensus_pom] -->
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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]

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181

8282
<module>memorystore/redis</module>
8383

84+
<module>opencensus</module>
85+
8486
<module>pubsub/cloud-client</module>
8587

8688
<module>spanner/cloud-client</module>

0 commit comments

Comments
 (0)