Skip to content
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

Adding BigQuery + StackDriver Monitoring API Showcase sample. #1001

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions appengine-java8/bigquery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java8/bigquery/README.md">
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>

# 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
```

Note: The first time the app is run (or after any metrics definitions have been deleted) it may take up to 5 minutes for
the MetricDescriptors to sync with StackDriver before any results are shown. If you do not see results, please wait a
few moments and try again.

## 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

137 changes: 137 additions & 0 deletions appengine-java8/bigquery/pom.xml
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.bigquerylogging;

import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.TableResult;

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(BigQueryRunner.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>");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.bigquerylogging;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "runQuery 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 {
try {
queryRunner.runQuery();
} catch (InterruptedException e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Interrupted while running BigQuery job.");
}
// redirect to home page
resp.sendRedirect("/");
}
}
Loading