Skip to content

Commit

Permalink
Merge pull request #20 from GoogleCloudPlatform/monitoring
Browse files Browse the repository at this point in the history
Move java cloud monitoring sample into github.
  • Loading branch information
jerjou committed Jul 17, 2015
2 parents dfb5d4b + 7c8dcbd commit 105bcfd
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
70 changes: 70 additions & 0 deletions monitoring/pom.xml
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 monitoring/src/main/java/CloudMonitoringAuthSample.java
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 monitoring/src/test/java/CloudMonitoringAuthSampleTest.java
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.*"));
}
}
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<appengine.version>1.9.19</appengine.version>
<appengine.app.version>1</appengine.app.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.http.version>1.19.0</project.http.version>
<project.oauth.version>1.19.0</project.oauth.version>
</properties>

<prerequisites>
Expand All @@ -24,6 +26,7 @@
<module>bigquery</module>
<module>cloud-storage/xml-api/cmdline-sample</module>
<module>cloud-storage/xml-api/serviceaccount-appengine-sample</module>
<module>monitoring</module>
</modules>

<build>
Expand Down

0 comments on commit 105bcfd

Please sign in to comment.