Skip to content

Commit

Permalink
Fix style, Use Same Test Project
Browse files Browse the repository at this point in the history
Also fix error in README and change test project to this repo's project.
  • Loading branch information
Bill Prin committed Mar 30, 2016
1 parent 796dc2b commit d6011a7
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 108 deletions.
13 changes: 3 additions & 10 deletions monitoring/v3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ write a TimeSeries value to it.
## Prerequisites to run locally:

* [Maven 3](https://maven.apache.org)
* [GCloud CLI](https://cloud.google.com/sdk/gcloud/)
* Create a Cloud project

# Set Up Your Local Dev Environment

Expand All @@ -17,6 +19,7 @@ Create local credentials by running the following command and following the oaut
gcloud beta auth application-default login

To run:

* `mvn clean install`
* `./list_resources_example.sh <YOUR-PROJECT-ID>
* `./run_custom_metrics.sh <YOUR-PROJECT-ID>
Expand Down Expand Up @@ -57,13 +60,3 @@ to the appropriate project ID that matches the Service Account pointed to by
`GOOGLE_APPLICATION_CREDENTIALS`, then run:

mvn test -DskipTests=false

## Contributing changes

See [CONTRIBUTING.md](../../CONTRIBUTING.md).

## Licensing

See [LICENSE](../../LICENSE).


75 changes: 39 additions & 36 deletions monitoring/v3/src/main/java/CreateCustomMetric.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Copyright (c) 2015 Google Inc.
* <p/>
* 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
* <p/>
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* <p/>
*
* 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.
* 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.
*/

import com.google.api.services.monitoring.v3.Monitoring;
Expand All @@ -28,7 +28,6 @@
import com.google.api.services.monitoring.v3.model.TypedValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import org.joda.time.DateTime;

import java.io.IOException;
Expand All @@ -40,12 +39,12 @@
import java.util.TimeZone;




/**
* Class to demonstrate creating a custom metric with Cloud Monitoring. This class provides a few
* functions that create a custom GAUGE metric, writes a timeseries value to it, then reads that
* metric's value back within the last 5 minutes to see the value written.
* Class to demonstrate creating a custom metric with Cloud Monitoring.
* <p/>
* <p>This class provides a few functions that create a custom GAUGE metric, writes a timeseries
* value to it, then reads that metric's value back within the last 5 minutes to see the value
* written.</p>
*/
public class CreateCustomMetric {

Expand Down Expand Up @@ -111,7 +110,8 @@ public CreateCustomMetric(Monitoring monitoringService, String projectResource)
/**
* Constructs an instance of the class using the default metric name, and takes in a random
* number generaotr (used for test purposes).
* Package-private to be accessible to tests.
* <p/>
* <p>Package-private to be accessible to tests.</p>
*/
CreateCustomMetric(Monitoring monitoringService, String projectResource,
String metricName, int bound) {
Expand Down Expand Up @@ -162,7 +162,8 @@ private long getRandomPoint() {
/**
* This method creates a custom metric with arbitrary names, description,
* and units.
* Package-private to be accessible to tests.
* <p/>
* <p>Package-private to be accessible to tests.</p>
*/
MetricDescriptor createCustomMetric() throws IOException {
MetricDescriptor metricDescriptor = new MetricDescriptor();
Expand Down Expand Up @@ -196,9 +197,11 @@ MetricDescriptor createCustomMetric() throws IOException {
}

/**
* Retrieve the custom metric created by createCustomMetric. It can sometimes take a few moments
* before a new custom metric is ready to have TimeSeries written to it, so this method is used
* to check when it is ready.
* Retrieve the custom metric created by createCustomMetric.
* <p/>
* <p>It can sometimes take a few moments before a new custom metric is ready to have
* TimeSeries written to it, so this method is used
* to check when it is ready.</p>
*/
public MetricDescriptor getCustomMetric() throws IOException {
Monitoring.Projects.MetricDescriptors.List metrics =
Expand All @@ -218,10 +221,10 @@ public MetricDescriptor getCustomMetric() throws IOException {
}

/**
* Writes a timeseries value for the custom metric created. The value written
* is a random integer value for demonstration purposes. It's a GAUGE metric,
* Writes a timeseries value for the custom metric created.
* <p>The value written is a random integer value for demonstration purposes. It's a GAUGE metric,
* which means its a measure of a value at a point in time, and thus the start
* window and end window times are the same.
* window and end window times are the same.</p>
*
* @throws IOException On network error.
*/
Expand Down Expand Up @@ -264,21 +267,21 @@ void writeCustomMetricTimeseriesValue() throws IOException {
}

/**
* Read the TimeSeries value for the custom metrics created within a window of the
* last 5 minutes.
* <p>Read the TimeSeries value for the custom metrics created within a window of the
* last 5 minutes.</p>
*
* @return The TimeSeries response object reflecting the Timeseries of the custom metrics
* for the last 5 minutes.
* for the last 5 minutes.
* @throws IOException On network error.
*/
ListTimeSeriesResponse readTimeseriesValue() throws IOException {
ListTimeSeriesResponse response =
monitoringService.projects().timeSeries().list(projectResource)
.setFilter("metric.type=\"" + metricType + "\"")
.setPageSize(3)
.setIntervalStartTime(getStartTime())
.setIntervalEndTime(getNow())
.execute();
.setFilter("metric.type=\"" + metricType + "\"")
.setPageSize(3)
.setIntervalStartTime(getStartTime())
.setIntervalEndTime(getNow())
.execute();
return response;
}

Expand All @@ -302,8 +305,8 @@ public static void main(final String[] args) throws Exception {
// Create an authorized API client
Monitoring monitoringService = ListResources.authenticate();

CreateCustomMetric metricWriter = new CreateCustomMetric(
monitoringService, projectResource);
CreateCustomMetric metricWriter =
new CreateCustomMetric(monitoringService, projectResource);

MetricDescriptor metricDescriptor = metricWriter.createCustomMetric();

Expand Down
42 changes: 20 additions & 22 deletions monitoring/v3/src/main/java/ListResources.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Copyright (c) 2015 Google Inc.
* <p/>
* 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
* <p/>
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* <p/>
*
* 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.
* 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.
*/
// [START all]

Expand All @@ -25,7 +25,6 @@
import com.google.api.services.monitoring.v3.model.ListMetricDescriptorsResponse;
import com.google.api.services.monitoring.v3.model.ListMonitoredResourceDescriptorsResponse;
import com.google.api.services.monitoring.v3.model.ListTimeSeriesResponse;

import org.joda.time.DateTime;

import java.io.IOException;
Expand All @@ -35,7 +34,6 @@
import java.util.TimeZone;



/**
* Simple command-line program to demonstrate connecting to and retrieving data
* from the Google Cloud Monitoring API v3 using application default credentials.
Expand Down Expand Up @@ -91,9 +89,10 @@ private ListResources(Monitoring monitoringService, String projectResource) {

/**
* Query the projects.monitoredResourceDescriptors.list API method.
* This lists all the resources available to be monitored in the API.
* <p/>
* Package-private to be accessible to tests.
*
* <p>This lists all the resources available to be monitored in the API.</p>
*
* <p>Package-private to be accessible to tests.</p>
*/
void listMonitoredResourceDescriptors() throws IOException {
ListMonitoredResourceDescriptorsResponse monitoredResources =
Expand All @@ -105,14 +104,12 @@ void listMonitoredResourceDescriptors() throws IOException {

/**
* Query to MetricDescriptors.list
* This lists all the current metrics.
* <p/>
* Package-private to be accessible to tests.
* <p>This lists all the current metrics. Package-private to be accessible to tests.</p>
*/
void listMetricDescriptors() throws IOException {
ListMetricDescriptorsResponse metricsResponse =
this.monitoringService.projects().metricDescriptors()
.list(this.projectResource).execute();
.list(this.projectResource).execute();
this.outputStream.println("listMetricDescriptors response");
this.outputStream.println(metricsResponse.toPrettyString());
}
Expand Down Expand Up @@ -143,7 +140,8 @@ private static String getEndTime() {

/**
* Query to MetricDescriptors.list
* This lists all the current metrics.
*
* <p>This lists all the current metrics.</p>
*/
void listTimeseries() throws IOException {
ListTimeSeriesResponse timeSeriesList = this.monitoringService.projects().timeSeries()
Expand Down
40 changes: 14 additions & 26 deletions monitoring/v3/src/test/java/CreateCustomMetricTest.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,39 @@
/*
* Copyright (c) 2015 Google Inc.
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* 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.
* 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.
*/

import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.monitoring.v3.Monitoring;
import com.google.api.services.monitoring.v3.model.Point;
import com.google.common.collect.ImmutableList;

import org.apache.commons.lang3.RandomStringUtils;

import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Random;

import static com.google.common.truth.Truth.assertThat;

/**
* Integration tests for the basic Cloud Monitoring v3 examples. Running
* these tests requires that GOOGLE_APPLICATION_CREDENTIALS points to a
* Integration tests for the basic Cloud Monitoring v3 examples.
* <p/>
* <p>Running these tests requires that GOOGLE_APPLICATION_CREDENTIALS points to a
* valid JSON Service Account downloaded from a project with the Cloud
* Monitoring API enabled.
* Monitoring API enabled.</p>
*/
public class CreateCustomMetricTest {

/**
* Overrides the Random number generator so our tests get a predictable result.
*/
private static class MockRandom extends Random {

public int nextInt(int bound) {
return 4;
}
}

/**
* Google Cloud Monitoring client to integration test.
*/
Expand Down
28 changes: 14 additions & 14 deletions monitoring/v3/src/test/java/ListResourcesTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/*
* Copyright (c) 2015 Google Inc.
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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
* 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.
* 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.
*/

import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.monitoring.v3.Monitoring;

import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static com.google.common.truth.Truth.assertThat;

/**
* Integration tests for the basic Cloud Monitoring v3 examples. Running
* these tests requires that GOOGLE_APPLICATION_CREDENTIALS points to a
* Integration tests for the basic Cloud Monitoring v3 examples.
* <p/>
* <p>Running these tests requires that GOOGLE_APPLICATION_CREDENTIALS points to a
* valid JSON Service Account downloaded from a project with the Cloud
* Monitoring API enabled.
* Monitoring API enabled.</p>
*/
public class ListResourcesTest {

Expand Down

0 comments on commit d6011a7

Please sign in to comment.