-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pushing java sample. Kurtis approved pre-minor changes.
- Loading branch information
1 parent
9e660a1
commit 1fe3270
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2018 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!-- [START opencensus_pom] --> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>jar</packaging> | ||
<groupId>com.example</groupId> | ||
<artifactId>opencensus-samples</artifactId> | ||
<version>1.0</version> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.0.10</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<opencensus.version>0.15.0</opencensus.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- [START monitoring_opencensus_metrics_dependencies] --> | ||
<dependency> | ||
<groupId>io.opencensus</groupId> | ||
<artifactId>opencensus-api</artifactId> | ||
<version>${opencensus.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opencensus</groupId> | ||
<artifactId>opencensus-impl</artifactId> | ||
<version>${opencensus.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.opencensus</groupId> | ||
<artifactId>opencensus-exporter-stats-stackdriver</artifactId> | ||
<version>${opencensus.version}</version> | ||
</dependency> | ||
<!-- [END monitoring_opencensus_metrics_dependencies] --> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.6.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>com.example.opencensus.Quickstart</mainClass> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
<!-- [END opencensus_pom] --> |
88 changes: 88 additions & 0 deletions
88
opencensus/src/main/java/com/example/opencensus/Quickstart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.opencensus; | ||
|
||
// [START monitoring_opencensus_metrics_quickstart] | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter; | ||
import io.opencensus.stats.Aggregation; | ||
import io.opencensus.stats.BucketBoundaries; | ||
import io.opencensus.stats.Measure.MeasureLong; | ||
import io.opencensus.stats.Stats; | ||
import io.opencensus.stats.StatsRecorder; | ||
import io.opencensus.stats.View; | ||
import io.opencensus.stats.View.Name; | ||
import io.opencensus.stats.ViewManager; | ||
|
||
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_latency", | ||
"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.create( | ||
Name.create("task_latency_distribution_4"), | ||
"The distribution of the task latencies.", | ||
LATENCY_MS, | ||
Aggregation.Distribution.create(LATENCY_BOUNDARIES), | ||
Collections.emptyList()); | ||
|
||
ViewManager viewManager = Stats.getViewManager(); | ||
viewManager.registerView(view); | ||
|
||
// [START setup_exporter] | ||
// 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(); | ||
// [END setup_exporter] | ||
|
||
// [START example_code] | ||
// 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. | ||
|
||
Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS)); | ||
// [END example_code] | ||
} | ||
} | ||
// [END monitoring_opencensus_metrics_quickstart] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters