Skip to content

Commit 98566d6

Browse files
Shun Fanlesv
Shun Fan
authored andcommitted
* Add compute stackdriver sample* Update readme for stackdriver* Add license to stackdriver files
* Add compute stackdriver sample * Update readme for stackdriver * Add license to stackdriver files
1 parent af0d5f2 commit 98566d6

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

appengine/cloudsql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cloud SQL sample for Google App Enginge
1+
# Cloud SQL sample for Google App Engine
22
This sample demonstrates how to use [Cloud SQL](https://cloud.google.com/sql/) on Google App Engine
33
## Setup
44
Before you can run or deploy the sample, you will need to create a [Cloud SQL instance](https://cloud.google.com/sql/docs/create-instance)

compute/stackdriver/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Stackdriver sample for Google Compute Engine
2+
This sample demonstrates how to use [Stackdriver](https://cloud.google.com/error-reporting/) on Google Compute Engine
3+
## Running on Compute Engine
4+
1. Create a compute instance on the Google Cloud Platform Developer's Console
5+
1. SSH into the instance you created
6+
1. Update packages and install required packages
7+
sudo apt-get update && sudo apt-get install git-core openjdk-8-jdk maven
8+
1. Follow the instructions to [Install the Stackdriver Logging Agent](https://cloud.google.com/logging/docs/agent/installation)
9+
1. Create /etc/google-fluentd/config.d/forward.conf and add
10+
<source>
11+
type forward
12+
port 24224
13+
</source>
14+
1. Restart the logging agent
15+
sudo service google-fluentd restart
16+
1. Clone the repo
17+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
18+
1. Navigate to the Stackdriver sample folder
19+
java-docs-samples/compute/stackdriver
20+
1. Make sure that openjdk 8 is the selected java version
21+
sudo update-alternatives --config java
22+
1. Use maven to package the class as a jar
23+
mvn clean package
24+
1. Switch to the target folder and execute the jar file
25+
java -jar compute-stackdriver-1.0-SNAPSHOT-jar-with-dependencies.jar
26+
1. On the Developer's Console, navigate to Stackdriver Error Reporting and verify that the sample
27+
error was logged.

compute/stackdriver/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!--
2+
Copyright 2016 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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>jar</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.compute</groupId>
21+
<artifactId>compute-stackdriver</artifactId>
22+
23+
<dependencies>
24+
<!-- [START dependencies] -->
25+
<dependency>
26+
<groupId>org.fluentd</groupId>
27+
<artifactId>fluent-logger</artifactId>
28+
<version>0.2.7</version>
29+
</dependency>
30+
<!-- [END dependencies] -->
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>maven-assembly-plugin</artifactId>
36+
<executions>
37+
<execution>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>single</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
<configuration>
45+
<archive>
46+
<manifest>
47+
<mainClass>com.example.compute.stackdriver.ExceptionUtil</mainClass>
48+
</manifest>
49+
</archive>
50+
<descriptorRefs>
51+
<descriptorRef>jar-with-dependencies</descriptorRef>
52+
</descriptorRefs>
53+
</configuration>
54+
</plugin>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<version>3.3</version>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<source>1.8</source>
61+
<target>1.8</target>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2016 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+
package com.example.compute.stackdriver;
18+
19+
import org.fluentd.logger.FluentLogger;
20+
21+
import java.io.PrintWriter;
22+
import java.io.StringWriter;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class ExceptionUtil {
27+
private static FluentLogger ERRORS = FluentLogger.getLogger("myapp");
28+
29+
public static void main(String[] args) {
30+
try {
31+
throw new Exception("Generic exception for testing Stackdriver");
32+
} catch (Exception e) {
33+
report(e);
34+
}
35+
}
36+
37+
public static void report(Throwable ex) {
38+
StringWriter exceptionWriter = new StringWriter();
39+
ex.printStackTrace(new PrintWriter(exceptionWriter));
40+
Map<String, Object> data = new HashMap<>();
41+
data.put("message", exceptionWriter.toString());
42+
Map<String,String> serviceContextData = new HashMap<>();
43+
serviceContextData.put("service", "myapp");
44+
data.put("serviceContext", serviceContextData);
45+
// ... add more metadata
46+
ERRORS.log("errors", data);
47+
}
48+
}

0 commit comments

Comments
 (0)