-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix Metric.update and add snippets for Metric class #1221
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
...oud-examples/src/main/java/com/google/cloud/examples/logging/snippets/MetricSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* EDITING INSTRUCTIONS | ||
* This file is referenced in Metric's javadoc. Any change to this file should be reflected in | ||
* Metric's javadoc. | ||
*/ | ||
|
||
package com.google.cloud.examples.logging.snippets; | ||
|
||
import com.google.cloud.logging.Metric; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* This class contains a number of snippets for the {@link Metric} class. | ||
*/ | ||
public class MetricSnippets { | ||
|
||
private final Metric metric; | ||
|
||
public MetricSnippets(Metric metric) { | ||
this.metric = metric; | ||
} | ||
|
||
/** | ||
* Example of getting the metric's latest information. | ||
*/ | ||
// [TARGET reload()] | ||
public Metric reload() { | ||
// [START reload] | ||
Metric latestMetric = metric.reload(); | ||
if (latestMetric == null) { | ||
// the metric was not found | ||
} | ||
// [END reload] | ||
return latestMetric; | ||
} | ||
|
||
/** | ||
* Example of asynchronously getting the metric's latest information. | ||
*/ | ||
// [TARGET reloadAsync()] | ||
public Metric reloadAsync() throws ExecutionException, InterruptedException { | ||
// [START reloadAsync] | ||
Future<Metric> future = metric.reloadAsync(); | ||
// ... | ||
Metric latestMetric = future.get(); | ||
if (latestMetric == null) { | ||
// the metric was not found | ||
} | ||
// [END reloadAsync] | ||
return latestMetric; | ||
} | ||
|
||
/** | ||
* Example of updating the metric's information. | ||
*/ | ||
// [TARGET update()] | ||
public Metric update() { | ||
// [START update] | ||
Metric updatedMetric = metric.toBuilder() | ||
.description("A more detailed description") | ||
.build() | ||
.update(); | ||
// [END update] | ||
return updatedMetric; | ||
} | ||
|
||
/** | ||
* Example of asynchronously updating the metric's information. | ||
*/ | ||
// [TARGET updateAsync()] | ||
public Metric updateAsync() throws ExecutionException, InterruptedException { | ||
// [START updateAsync] | ||
Future<Metric> future = metric.toBuilder() | ||
.description("A more detailed description") | ||
.build() | ||
.updateAsync(); | ||
// ... | ||
Metric updatedMetric = future.get(); | ||
// [END updateAsync] | ||
return updatedMetric; | ||
} | ||
|
||
/** | ||
* Example of deleting the metric. | ||
*/ | ||
// [TARGET delete()] | ||
public boolean delete() { | ||
// [START delete] | ||
boolean deleted = metric.delete(); | ||
if (deleted) { | ||
// the metric was deleted | ||
} else { | ||
// the metric was not found | ||
} | ||
// [END delete] | ||
return deleted; | ||
} | ||
|
||
/** | ||
* Example of asynchronously deleting the metric. | ||
*/ | ||
// [TARGET deleteAsync()] | ||
public boolean deleteAsync() throws ExecutionException, InterruptedException { | ||
// [START deleteAsync] | ||
Future<Boolean> future = metric.deleteAsync(); | ||
// ... | ||
boolean deleted = future.get(); | ||
if (deleted) { | ||
// the metric was deleted | ||
} else { | ||
// the metric was not found | ||
} | ||
// [END deleteAsync] | ||
return deleted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...d-examples/src/test/java/com/google/cloud/examples/logging/snippets/ITMetricSnippets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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.google.cloud.examples.logging.snippets; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.cloud.logging.Logging; | ||
import com.google.cloud.logging.Metric; | ||
import com.google.cloud.logging.MetricInfo; | ||
import com.google.cloud.logging.testing.RemoteLoggingHelper; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
public class ITMetricSnippets { | ||
|
||
private static final String METRIC_NAME = RemoteLoggingHelper.formatForTest("it_metric_snippets"); | ||
private static final String METRIC_FILTER = "severity>=ERROR"; | ||
private static final String DESCRIPTION = "description"; | ||
private static final String UPDATED_DESCRIPTION = "A more detailed description"; | ||
|
||
private static Logging logging; | ||
private static MetricSnippets metricSnippets; | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
RemoteLoggingHelper helper = RemoteLoggingHelper.create(); | ||
logging = helper.options().service(); | ||
MetricInfo metricInfo = MetricInfo.builder(METRIC_NAME, METRIC_FILTER) | ||
.description(DESCRIPTION) | ||
.build(); | ||
metricSnippets = new MetricSnippets(logging.create(metricInfo)); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() throws Exception { | ||
if (logging != null) { | ||
logging.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMetric() throws InterruptedException, ExecutionException { | ||
Metric metric = metricSnippets.reload(); | ||
assertNotNull(metric); | ||
Metric updatedMetric = metricSnippets.update(); | ||
assertEquals(UPDATED_DESCRIPTION, updatedMetric.description()); | ||
updatedMetric = metricSnippets.reloadAsync(); | ||
assertNotNull(updatedMetric); | ||
assertEquals(UPDATED_DESCRIPTION, updatedMetric.description()); | ||
metric.update(); | ||
updatedMetric = metricSnippets.updateAsync(); | ||
assertEquals(UPDATED_DESCRIPTION, updatedMetric.description()); | ||
assertTrue(metricSnippets.delete()); | ||
assertFalse(metricSnippets.deleteAsync()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.