-
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.
Adding BigQuery + StackDriver Monitoring sample.
- Loading branch information
1 parent
e0918ef
commit 5aab7af
Showing
10 changed files
with
835 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,60 @@ | ||
# Google Cloud API Showcase: Cloud BigQuery & StackDriver Monitoring in App Engine Standard Java 8 Environment | ||
|
||
This API Showcase demonstrates how to run an AppEngine Standard application with dependencies on both | ||
[Google BigQuery][bigquery] and [StackDriver Monitoring][stackdriver]. | ||
|
||
[bigquery]: https://cloud.google.com/bigquery/docs | ||
[stackdriver]: https://cloud.google.com/monitoring/docs | ||
|
||
The home page of this application provides a form to initiate a query of public data, in this case StackOverflow | ||
questions tagged with `google-bigquery`. | ||
|
||
The home page also provides a summary view of the metrics that have been logged in the past 30 days. | ||
|
||
## Clone the sample app | ||
|
||
Copy the sample apps to your local machine, and cd to the appengine-java8/bigquery directory: | ||
|
||
``` | ||
git clone https://github.com/GoogleCloudPlatform/java-docs-samples | ||
cd appengine-java8/bigquery | ||
``` | ||
|
||
## Setup | ||
|
||
- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized: | ||
``` | ||
gcloud init | ||
``` | ||
- If this is the first time you are creating an App Engine project | ||
``` | ||
gcloud app create | ||
``` | ||
- For local development, [set up][set-up] authentication | ||
- Enable [BigQuery][bigquery-api] and [Monitoring][monitoring-api] APIs | ||
- If you have not already enabled your project for StackDriver, do so by following [these instructions][stackdriver-setup]. | ||
|
||
[set-up]: https://cloud.google.com/docs/authentication/getting-started | ||
[bigquery-api]: https://console.cloud.google.com/launcher/details/google/bigquery-json.googleapis.com | ||
[monitoring-api]: https://console.cloud.google.com/launcher/details/google/monitoring.googleapis.com | ||
[stackdriver-setup]: https://cloud.google.com/monitoring/accounts/tiers#not-enabled | ||
|
||
## Run locally | ||
Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see the most recent query | ||
run (since the app started) and the metrics from the past 30 days. | ||
|
||
``` | ||
mvn appengine:run | ||
``` | ||
|
||
## Deploy | ||
|
||
- Deploy to AppEngine Standard using the following Maven command. | ||
``` | ||
mvn appengine:deploy | ||
``` | ||
- Direct your browser to `https://<your-project-id>.appspot.com`. | ||
- View more in-depth metrics data on the [StackDriver Monitoring Dashboard][dashboard] | ||
|
||
[dashboard]: https://pantheon.corp.google.com/monitoring | ||
|
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,137 @@ | ||
<!-- | ||
Copyright 2018 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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-bigquery-monitoring-j8</artifactId> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not effect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.0.8</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
<version>1.9.60</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- [START dependencies] --> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-bigquery</artifactId> | ||
<version>0.33.0-beta</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-monitoring</artifactId> | ||
<version>0.33.0-beta</version> | ||
</dependency> | ||
<!-- [END dependencies ] --> | ||
|
||
<dependency> | ||
<groupId>commons-cli</groupId> | ||
<artifactId>commons-cli</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>2.9.9</version> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<version>1.9.60</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>1.9.60</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>2.13.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<version>1.9.60</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.39</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>com.google.cloud.tools</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>1.3.1</version> | ||
<configuration> | ||
<deploy.promote>true</deploy.promote> | ||
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion> | ||
</configuration> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.1.0</version> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
62 changes: 62 additions & 0 deletions
62
...ine-java8/bigquery/src/main/java/com/example/appengine/bigquery_logging/BigQueryHome.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,62 @@ | ||
package com.example.appengine.bigquery_logging; | ||
|
||
import com.google.cloud.bigquery.FieldValueList; | ||
import com.google.cloud.bigquery.TableResult; | ||
import com.google.protobuf.util.Timestamps; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class BigQueryHome { | ||
private static BigQueryRunner queryRunner; | ||
|
||
private static BigQueryRunner getQueryRunner() throws IOException { | ||
if (queryRunner == null) { | ||
queryRunner = BigQueryRunner.getInstance(); | ||
} | ||
return queryRunner; | ||
} | ||
|
||
public static String getMostRecentRun() throws IOException { | ||
return convertRunToHtmlTable(getQueryRunner().getMostRecentRunResult()); | ||
} | ||
|
||
public static String getMetricAverages() throws IOException { | ||
return convertAveragesToHtmlTable(getQueryRunner().getTimeSeriesValues()); | ||
} | ||
|
||
private static String convertRunToHtmlTable(TableResult result) { | ||
if (result == null) { | ||
return ""; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (FieldValueList row : result.iterateAll()) { | ||
sb.append("<tr>"); | ||
String url = row.get("url").getStringValue(); | ||
addColumn(sb, String.format("<a href=\"%s\">%s</a>", url, url)); | ||
addColumn(sb, row.get("view_count").getLongValue()); | ||
sb.append("</tr>"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static String convertAveragesToHtmlTable(List<TimeSeriesSummary> values) { | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for(TimeSeriesSummary metric : values) { | ||
sb.append("<tr>"); | ||
addColumn(sb, metric.getName()); | ||
addColumn(sb, metric.getValues().size()); | ||
addColumn(sb, metric.getMostRecentRunTime()); | ||
addColumn(sb, metric.getMostRecentValue()); | ||
addColumn(sb, metric.getAverage()); | ||
sb.append("</tr>"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static <T> void addColumn(StringBuilder sb, T content) { | ||
sb.append("<td>").append(content.toString()).append("</td>"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...gine-java8/bigquery/src/main/java/com/example/appengine/bigquery_logging/BigQueryRun.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,45 @@ | ||
/* | ||
* Copyright 2018 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. | ||
*/ | ||
|
||
package com.example.appengine.bigquery_logging; | ||
|
||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@WebServlet(name = "Run BigQuery", value = "/bigquery/run") | ||
public class BigQueryRun extends HttpServlet { | ||
private BigQueryRunner queryRunner; | ||
|
||
public BigQueryRun() throws IOException { | ||
this.queryRunner = BigQueryRunner.getInstance(); | ||
} | ||
|
||
@Override | ||
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
BigQueryRunner queryRunner = this.queryRunner; | ||
try { | ||
queryRunner.Run(); | ||
} catch (InterruptedException e) { | ||
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, | ||
"Interrupted while running BigQuery job."); | ||
} | ||
// redirect to home page | ||
resp.sendRedirect("/"); | ||
} | ||
} |
Oops, something went wrong.