-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move java cloud monitoring sample into github. #20
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,70 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.google.cloud.monitoring.samples</groupId> | ||
<artifactId>cloud-monitoring-samples</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<artifactId>doc-samples</artifactId> | ||
<groupId>com.google.cloud</groupId> | ||
<version>1.0.0</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
|
||
<repositories> | ||
<repository> | ||
<id>googleapis</id> | ||
<url>https://google-api-client-libraries.appspot.com/mavenrepo</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.apis</groupId> | ||
<artifactId>google-api-services-cloudmonitoring</artifactId> | ||
<version>v2beta2-rev20-1.20.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.oauth-client</groupId> | ||
<artifactId>google-oauth-client</artifactId> | ||
<version>${project.oauth.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.http-client</groupId> | ||
<artifactId>google-http-client-jackson2</artifactId> | ||
<version>${project.http.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.oauth-client</groupId> | ||
<artifactId>google-oauth-client-jetty</artifactId> | ||
<version>${project.oauth.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.jcabi</groupId> | ||
<artifactId>jcabi-matchers</artifactId> | ||
<version>1.3</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src/main/java</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.2</version> | ||
<configuration> | ||
<source>5</source> | ||
<target>5</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
106 changes: 106 additions & 0 deletions
106
monitoring/src/main/java/CloudMonitoringAuthSample.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,106 @@ | ||
/** | ||
* Copyright (c) 2015 Google Inc. | ||
* | ||
* 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 all] | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
import com.google.api.client.http.HttpTransport; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.services.cloudmonitoring.CloudMonitoring; | ||
import com.google.api.services.cloudmonitoring.CloudMonitoringScopes; | ||
|
||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
|
||
/** | ||
* Simple command-line program to demonstrate connecting to and retrieving data | ||
* from the Google Cloud Monitoring API using application default credentials. | ||
*/ | ||
public final class CloudMonitoringAuthSample { | ||
|
||
/** | ||
* The metric that we want to fetch. | ||
*/ | ||
private static final String METRIC = | ||
"compute.googleapis.com/instance/disk/read_ops_count"; | ||
|
||
/** | ||
* The end of the time interval to fetch. | ||
*/ | ||
private static final String YOUNGEST = "2015-01-01T00:00:00Z"; | ||
|
||
/** | ||
* Utility class doesn't need to be instantiated. | ||
*/ | ||
private CloudMonitoringAuthSample() { } | ||
|
||
|
||
/** | ||
* Builds and returns a CloudMonitoring service object authorized with the | ||
* application default credentials. | ||
* | ||
* @return CloudMonitoring service object that is ready to make requests. | ||
* @throws GeneralSecurityException if authentication fails. | ||
* @throws IOException if authentication fails. | ||
*/ | ||
private static CloudMonitoring authenticate() | ||
throws GeneralSecurityException, IOException { | ||
// Grab the Application Default Credentials from the environment. | ||
GoogleCredential credential = GoogleCredential.getApplicationDefault() | ||
.createScoped(CloudMonitoringScopes.all()); | ||
|
||
// Create and return the CloudMonitoring service object | ||
HttpTransport httpTransport = new NetHttpTransport(); | ||
JsonFactory jsonFactory = new JacksonFactory(); | ||
CloudMonitoring service = new CloudMonitoring.Builder(httpTransport, | ||
jsonFactory, credential) | ||
.setApplicationName("Demo") | ||
.build(); | ||
return service; | ||
} | ||
|
||
/** | ||
* Query the Google Cloud Monitoring API using a service account and print the | ||
* result to the console. | ||
* | ||
* @param args The first arg should be the project name you'd like to inspect. | ||
* @throws Exception if something goes wrong. | ||
*/ | ||
public static void main(final String[] args) throws Exception { | ||
if (args.length != 2) { | ||
System.err.println(String.format("Usage: %s <project-name>", args[0])); | ||
return; | ||
} | ||
|
||
String project = args[1]; | ||
|
||
// Create an authorized API client | ||
CloudMonitoring cloudmonitoring = authenticate(); | ||
|
||
CloudMonitoring.Timeseries.List timeseriesListRequest = | ||
cloudmonitoring.timeseries().list(project, METRIC, YOUNGEST); | ||
|
||
System.out.println("Timeseries.list raw response:"); | ||
System.out.println(timeseriesListRequest.execute().toPrettyString()); | ||
|
||
// This example only demonstrates completing the OAuth flow and displaying | ||
// the raw response from a simple request. See the API client library docs | ||
// for applicable methods for working with the returned data, including | ||
// getting results and paging through results. | ||
|
||
} | ||
} | ||
// [END all] |
63 changes: 63 additions & 0 deletions
63
monitoring/src/test/java/CloudMonitoringAuthSampleTest.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,63 @@ | ||
/** | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import static com.jcabi.matchers.RegexMatchers.*; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Tests the Cloud Monitoring auth sample. | ||
*/ | ||
public class CloudMonitoringAuthSampleTest { | ||
private final ByteArrayOutputStream stdout = | ||
new ByteArrayOutputStream(); | ||
private final ByteArrayOutputStream stderr = | ||
new ByteArrayOutputStream(); | ||
|
||
@Before | ||
public void setUp() { | ||
System.setOut(new PrintStream(stdout)); | ||
System.setErr(new PrintStream(stderr)); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
System.setErr(null); | ||
} | ||
|
||
@Test | ||
public void testUsage() throws Exception { | ||
CloudMonitoringAuthSample.main(new String[] { "command-name" }); | ||
assertEquals("Usage: command-name <project-name>\n", stderr.toString()); | ||
} | ||
|
||
@Test | ||
public void testListTimeSeries() throws Exception { | ||
CloudMonitoringAuthSample.main(new String[] { "", "cloud-samples-tests" }); | ||
String out = stdout.toString(); | ||
assertThat(out, containsPattern("Timeseries.list raw response:")); | ||
assertThat(out, containsPattern("\\{\\s*\"kind\" *: *\"cloudmonitoring#listTimeseriesResponse\",")); | ||
assertThat(out, containsPattern(".*oldest.*")); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better if we move this into the dependency management of the root POM to make sure we stay consistent on versions.