Skip to content

Commit c27959d

Browse files
authored
Merge pull request #493 from GoogleCloudPlatform/monitoring-quickstart
Add Monitoring quickstart sample.
2 parents adc9d2a + f2a276f commit c27959d

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

monitoring/cloud-client/README.md

Lines changed: 32 additions & 0 deletions
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+
### Write a time series to a metric (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

Lines changed: 57 additions & 0 deletions
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>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
import com.google.api.Metric;
21+
import com.google.api.MonitoredResource;
22+
23+
// Imports the Google Cloud client library
24+
import com.google.cloud.monitoring.spi.v3.MetricServiceClient;
25+
26+
import com.google.monitoring.v3.CreateTimeSeriesRequest;
27+
import com.google.monitoring.v3.Point;
28+
import com.google.monitoring.v3.ProjectName;
29+
import com.google.monitoring.v3.TimeInterval;
30+
import com.google.monitoring.v3.TimeSeries;
31+
import com.google.monitoring.v3.TypedValue;
32+
import com.google.protobuf.util.Timestamps;
33+
34+
import java.util.ArrayList;
35+
import java.util.HashMap;
36+
import java.util.List;
37+
import java.util.Map;
38+
39+
public class QuickstartSample {
40+
public static void main(String... args) throws Exception {
41+
// Your Google Cloud Platform project ID
42+
String projectId = System.getProperty("projectId");
43+
44+
if (projectId == null) {
45+
System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");
46+
return;
47+
}
48+
49+
// Instantiates a client
50+
MetricServiceClient metricServiceClient = MetricServiceClient.create();
51+
52+
// Prepares an individual data point
53+
TimeInterval interval = TimeInterval.newBuilder()
54+
.setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
55+
.build();
56+
TypedValue value = TypedValue.newBuilder()
57+
.setDoubleValue(123.45)
58+
.build();
59+
Point point = Point.newBuilder()
60+
.setInterval(interval)
61+
.setValue(value)
62+
.build();
63+
64+
List<Point> pointList = new ArrayList<>();
65+
pointList.add(point);
66+
67+
ProjectName name = ProjectName.create(projectId);
68+
69+
// Prepares the metric descriptor
70+
Map<String, String> metricLabels = new HashMap<String, String>();
71+
metricLabels.put("store_id", "Pittsburg");
72+
Metric metric = Metric.newBuilder()
73+
.setType("custom.googleapis.com/stores/daily_sales")
74+
.putAllLabels(metricLabels)
75+
.build();
76+
77+
// Prepares the monitored resource descriptor
78+
Map<String, String> resourceLabels = new HashMap<String, String>();
79+
resourceLabels.put("project_id", projectId);
80+
MonitoredResource resource = MonitoredResource.newBuilder()
81+
.setType("global")
82+
.putAllLabels(resourceLabels)
83+
.build();
84+
85+
// Prepares the time series request
86+
TimeSeries timeSeries = TimeSeries.newBuilder()
87+
.setMetric(metric)
88+
.setResource(resource)
89+
.addAllPoints(pointList)
90+
.build();
91+
List<TimeSeries> timeSeriesList = new ArrayList<>();
92+
timeSeriesList.add(timeSeries);
93+
94+
CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder()
95+
.setNameWithProjectName(name)
96+
.addAllTimeSeries(timeSeriesList)
97+
.build();
98+
99+
// Writes time series data
100+
metricServiceClient.createTimeSeries(request);
101+
102+
System.out.printf("Done writing time series data.%n");
103+
104+
metricServiceClient.close();
105+
}
106+
}
107+
// [END monitoring_quickstart]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT";
39+
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
40+
41+
private static String getProjectId() {
42+
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
43+
if (projectId == null) {
44+
projectId = System.getProperty(LEGACY_PROJECT_ENV_NAME,
45+
System.getenv(LEGACY_PROJECT_ENV_NAME));
46+
}
47+
return projectId;
48+
}
49+
50+
@Before
51+
public void setUp() {
52+
bout = new ByteArrayOutputStream();
53+
out = new PrintStream(bout);
54+
System.setOut(out);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
}
61+
62+
@Test
63+
public void testQuickstart() throws Exception {
64+
// Act
65+
System.setProperty("projectId", QuickstartSampleIT.getProjectId());
66+
QuickstartSample.main();
67+
68+
// Assert
69+
String got = bout.toString();
70+
assertThat(got).contains("Done writing time series data.");
71+
}
72+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<module>language/cloud-client</module>
8181
<module>logging</module>
8282
<module>logging/cloud-client</module>
83+
<module>monitoring/cloud-client</module>
8384
<module>monitoring/v2</module>
8485
<module>monitoring/v3</module>
8586
<module>pubsub/cloud-client</module>

0 commit comments

Comments
 (0)