Skip to content

Commit 6ef7517

Browse files
committed
Add Monitoring quickstart sample.
1 parent 47d84ab commit 6ef7517

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

monitoring/cloud-client/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Getting Started with Google Stackdriver Monitoring API and the Google Cloud Client libraries
2+
3+
[Google Stackdriver Monitoring API][monitoring] collects metrics, events, and
4+
metadata from Google Cloud Platform, Amazon Web Services (AWS), hosted uptime
5+
probes, application instrumentation, and a variety of common application
6+
components including Cassandra, Nginx, Apache Web Server, Elasticsearch and many
7+
others.
8+
9+
These sample Java applications demonstrate how to access the Stackdriver
10+
Monitoring API using the [Google Cloud Client Library for Java][google-cloud-java].
11+
12+
[monitoring]: https://cloud.google.com/monitoring/docs/
13+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
14+
15+
## Quickstart
16+
17+
Install [Maven](http://maven.apache.org/).
18+
19+
Build your project with:
20+
21+
mvn clean package -DskipTests
22+
23+
You can then run a given `ClassName` via:
24+
25+
mvn exec:java -Dexec.mainClass=com.example.monitoring.ClassName \
26+
-DpropertyName=propertyValue \
27+
-Dexec.args="arg1 'arg 2' arg3"
28+
29+
### Analyze a string for sentiment (using the quickstart sample)
30+
31+
mvn exec:java -Dexec.mainClass=com.example.monitoring.QuickstartSample \
32+
-DprojectId=YOUR_PROJECT_ID

monitoring/cloud-client/pom.xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2017, Google Inc.
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+
<groupId>com.example.monitoring</groupId>
19+
<artifactId>monitoring-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-monitoring</artifactId>
40+
<version>0.8.1-alpha</version>
41+
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.31</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright 2017, Google, Inc.
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.monitoring;
18+
19+
// [START monitoring_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.monitoring.spi.v3.MetricServiceClient;
22+
23+
import com.google.api.Metric;
24+
import com.google.api.MonitoredResource;
25+
import com.google.monitoring.v3.CreateTimeSeriesRequest;
26+
import com.google.monitoring.v3.Point;
27+
import com.google.monitoring.v3.ProjectName;
28+
import com.google.monitoring.v3.TimeInterval;
29+
import com.google.monitoring.v3.TimeSeries;
30+
import com.google.monitoring.v3.TypedValue;
31+
import com.google.protobuf.util.Timestamps;
32+
33+
import java.util.ArrayList;
34+
import java.util.HashMap;
35+
import java.util.List;
36+
import java.util.Map;
37+
38+
public class QuickstartSample {
39+
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT";
40+
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
41+
42+
private static String getProjectId() {
43+
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
44+
if (projectId == null) {
45+
projectId = System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME));
46+
}
47+
return projectId;
48+
}
49+
50+
public static void main(String... args) throws Exception {
51+
// Your Google Cloud Platform project ID
52+
String projectId = System.getProperty("projectId");
53+
54+
if (projectId == null) {
55+
System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");
56+
return;
57+
}
58+
59+
// Instantiates a client
60+
MetricServiceClient metricServiceClient = MetricServiceClient.create();
61+
62+
// Prepares an individual data point
63+
TimeInterval interval = TimeInterval.newBuilder()
64+
.setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
65+
.build();
66+
TypedValue value = TypedValue.newBuilder()
67+
.setDoubleValue(123.45)
68+
.build();
69+
Point point = Point.newBuilder()
70+
.setInterval(interval)
71+
.setValue(value)
72+
.build();
73+
74+
List<Point> pointList = new ArrayList<>();
75+
pointList.add(point);
76+
77+
ProjectName name = ProjectName.create(projectId);
78+
79+
// Prepares the metric descriptor
80+
Map<String, String> metricLabels = new HashMap<String, String>();
81+
metricLabels.put("store_id", "Pittsburg");
82+
Metric metric = Metric.newBuilder()
83+
.setType("custom.googleapis.com/stores/daily_sales")
84+
.putAllLabels(metricLabels)
85+
.build();
86+
87+
// Prepares the monitored resource descriptor
88+
Map<String, String> resourceLabels = new HashMap<String, String>();
89+
resourceLabels.put("project_id", projectId);
90+
MonitoredResource resource = MonitoredResource.newBuilder()
91+
.setType("global")
92+
.putAllLabels(resourceLabels)
93+
.build();
94+
95+
// Prepares the time series request
96+
TimeSeries timeSeries = TimeSeries.newBuilder()
97+
.setMetric(metric)
98+
.setResource(resource)
99+
.addAllPoints(pointList)
100+
.build();
101+
List<TimeSeries> timeSeriesList = new ArrayList<>();
102+
timeSeriesList.add(timeSeries);
103+
104+
CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder()
105+
.setNameWithProjectName(name)
106+
.addAllTimeSeries(timeSeriesList)
107+
.build();
108+
109+
// Writes time series data
110+
metricServiceClient.createTimeSeries(request);
111+
112+
System.out.printf("Done writing time series data.%n");
113+
114+
metricServiceClient.close();
115+
}
116+
}
117+
// [END monitoring_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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.monitoring;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for quickstart sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class QuickstartSampleIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testQuickstart() throws Exception {
53+
// Act
54+
QuickstartSample.main();
55+
56+
// Assert
57+
String got = bout.toString();
58+
assertThat(got).contains("Done writing time series data.");
59+
}
60+
}

0 commit comments

Comments
 (0)