Skip to content

Commit 71938a9

Browse files
author
Jerjou Cheng
committed
Move java cloud monitoring sample into github.
Also - add tests.
1 parent dfb5d4b commit 71938a9

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed

monitoring/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud.monitoring.samples</groupId>
5+
<artifactId>cloud-monitoring-samples</artifactId>
6+
<packaging>jar</packaging>
7+
8+
<parent>
9+
<artifactId>doc-samples</artifactId>
10+
<groupId>com.google.cloud</groupId>
11+
<version>1.0.0</version>
12+
<relativePath>..</relativePath>
13+
</parent>
14+
15+
16+
<repositories>
17+
<repository>
18+
<id>googleapis</id>
19+
<url>https://google-api-client-libraries.appspot.com/mavenrepo</url>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.google.apis</groupId>
26+
<artifactId>google-api-services-cloudmonitoring</artifactId>
27+
<version>v2beta2-rev20-1.20.0</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.oauth-client</groupId>
31+
<artifactId>google-oauth-client</artifactId>
32+
<version>${project.oauth.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.google.http-client</groupId>
36+
<artifactId>google-http-client-jackson2</artifactId>
37+
<version>${project.http.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.google.oauth-client</groupId>
41+
<artifactId>google-oauth-client-jetty</artifactId>
42+
<version>${project.oauth.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.jcabi</groupId>
50+
<artifactId>jcabi-matchers</artifactId>
51+
<version>1.3</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<properties>
56+
<project.http.version>1.19.0</project.http.version>
57+
<project.oauth.version>1.19.0</project.oauth.version>
58+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
59+
</properties>
60+
61+
<build>
62+
<sourceDirectory>src/main/java</sourceDirectory>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-compiler-plugin</artifactId>
67+
<version>3.2</version>
68+
<configuration>
69+
<source>5</source>
70+
<target>5</target>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
</project>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright (c) 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
// [START all]
17+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
18+
import com.google.api.client.http.HttpTransport;
19+
import com.google.api.client.http.javanet.NetHttpTransport;
20+
import com.google.api.client.json.JsonFactory;
21+
import com.google.api.client.json.jackson2.JacksonFactory;
22+
import com.google.api.services.cloudmonitoring.CloudMonitoring;
23+
import com.google.api.services.cloudmonitoring.CloudMonitoringScopes;
24+
25+
import java.io.IOException;
26+
import java.security.GeneralSecurityException;
27+
28+
/**
29+
* Simple command-line program to demonstrate connecting to and retrieving data
30+
* from the Google Cloud Monitoring API using application default credentials.
31+
*/
32+
public final class CloudMonitoringAuthSample {
33+
34+
/**
35+
* The metric that we want to fetch.
36+
*/
37+
private static final String METRIC =
38+
"compute.googleapis.com/instance/disk/read_ops_count";
39+
40+
/**
41+
* The end of the time interval to fetch.
42+
*/
43+
private static final String YOUNGEST = "2015-01-01T00:00:00Z";
44+
45+
/**
46+
* Utility class doesn't need to be instantiated.
47+
*/
48+
private CloudMonitoringAuthSample() { }
49+
50+
51+
/**
52+
* Builds and returns a CloudMonitoring service object authorized with the
53+
* application default credentials.
54+
*
55+
* @return CloudMonitoring service object that is ready to make requests.
56+
* @throws GeneralSecurityException if authentication fails.
57+
* @throws IOException if authentication fails.
58+
*/
59+
private static CloudMonitoring authenticate()
60+
throws GeneralSecurityException, IOException {
61+
// Grab the Application Default Credentials from the environment.
62+
GoogleCredential credential = GoogleCredential.getApplicationDefault()
63+
.createScoped(CloudMonitoringScopes.all());
64+
65+
// Create and return the CloudMonitoring service object
66+
HttpTransport httpTransport = new NetHttpTransport();
67+
JsonFactory jsonFactory = new JacksonFactory();
68+
CloudMonitoring service = new CloudMonitoring.Builder(httpTransport,
69+
jsonFactory, credential)
70+
.setApplicationName("Demo")
71+
.build();
72+
return service;
73+
}
74+
75+
/**
76+
* Query the Google Cloud Monitoring API using a service account and print the
77+
* result to the console.
78+
*
79+
* @param args The first arg should be the project name you'd like to inspect.
80+
* @throws Exception if something goes wrong.
81+
*/
82+
public static void main(final String[] args) throws Exception {
83+
if (args.length != 2) {
84+
System.err.println(String.format("Usage: %s <project-name>", args[0]));
85+
return;
86+
}
87+
88+
String project = args[1];
89+
90+
// Create an authorized API client
91+
CloudMonitoring cloudmonitoring = authenticate();
92+
93+
CloudMonitoring.Timeseries.List timeseriesListRequest =
94+
cloudmonitoring.timeseries().list(project, METRIC, YOUNGEST);
95+
96+
System.out.println("Timeseries.list raw response:");
97+
System.out.println(timeseriesListRequest.execute().toPrettyString());
98+
99+
// This example only demonstrates completing the OAuth flow and displaying
100+
// the raw response from a simple request. See the API client library docs
101+
// for applicable methods for working with the returned data, including
102+
// getting results and paging through results.
103+
104+
}
105+
}
106+
// [END all]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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+
import static com.jcabi.matchers.RegexMatchers.*;
18+
import static org.junit.Assert.*;
19+
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.regex.Pattern;
27+
28+
/**
29+
* Tests the Cloud Monitoring auth sample.
30+
*/
31+
public class CloudMonitoringAuthSampleTest {
32+
private final ByteArrayOutputStream stdout =
33+
new ByteArrayOutputStream();
34+
private final ByteArrayOutputStream stderr =
35+
new ByteArrayOutputStream();
36+
37+
@Before
38+
public void setUp() {
39+
System.setOut(new PrintStream(stdout));
40+
System.setErr(new PrintStream(stderr));
41+
}
42+
43+
@After
44+
public void tearDown() {
45+
System.setOut(null);
46+
System.setErr(null);
47+
}
48+
49+
@Test
50+
public void testUsage() throws Exception {
51+
CloudMonitoringAuthSample.main(new String[] { "command-name" });
52+
assertEquals("Usage: command-name <project-name>\n", stderr.toString());
53+
}
54+
55+
@Test
56+
public void testListTimeSeries() throws Exception {
57+
CloudMonitoringAuthSample.main(new String[] { "", "cloud-samples-tests" });
58+
String out = stdout.toString();
59+
assertThat(out, containsPattern("Timeseries.list raw response:"));
60+
assertThat(out, containsPattern("\\{\\s*\"kind\" *: *\"cloudmonitoring#listTimeseriesResponse\","));
61+
assertThat(out, containsPattern(".*oldest.*"));
62+
}
63+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<module>bigquery</module>
2525
<module>cloud-storage/xml-api/cmdline-sample</module>
2626
<module>cloud-storage/xml-api/serviceaccount-appengine-sample</module>
27+
<module>monitoring</module>
2728
</modules>
2829

2930
<build>

0 commit comments

Comments
 (0)