Skip to content

Commit 9bf5fc0

Browse files
committed
Add monitoring_get_resource tag/code sample to monitoring snippets.
1 parent 5c552d6 commit 9bf5fc0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

monitoring/cloud-client/src/main/java/com/example/monitoring/Snippets.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse;
2525
import com.google.cloud.monitoring.v3.MetricServiceClient.ListMonitoredResourceDescriptorsPagedResponse;
2626
import com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse;
27+
import com.google.gson.Gson;
2728
import com.google.monitoring.v3.Aggregation;
2829
import com.google.monitoring.v3.CreateMetricDescriptorRequest;
2930
import com.google.monitoring.v3.CreateTimeSeriesRequest;
@@ -39,6 +40,7 @@
3940
import com.google.monitoring.v3.TypedValue;
4041
import com.google.protobuf.Duration;
4142
import com.google.protobuf.util.Timestamps;
43+
4244
import java.io.IOException;
4345
import java.util.ArrayList;
4446
import java.util.HashMap;
@@ -49,8 +51,8 @@
4951

5052

5153
public class Snippets {
52-
5354
private static final String CUSTOM_METRIC_DOMAIN = "custom.googleapis.com";
55+
private static final Gson gson = new Gson();
5456

5557
/**
5658
* Exercises the methods defined in this class.
@@ -392,6 +394,17 @@ void listMonitoredResources() throws IOException {
392394
// [END monitoring_list_resources]
393395
}
394396

397+
// [START monitoring_get_resource]
398+
void getMonitoredResource(String resourceId) throws IOException {
399+
String projectId = System.getProperty("projectId");
400+
MetricServiceClient client = MetricServiceClient.create();
401+
MonitoredResourceDescriptorName name = MonitoredResourceDescriptorName.of(projectId,
402+
resourceId);
403+
MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);
404+
System.out.println("Retrieved Monitored Resource: " + gson.toJson(response));
405+
}
406+
// [END monitoring_get_resource]
407+
395408
/**
396409
* Gets full information for a monitored resource.
397410
*

monitoring/cloud-client/src/test/java/com/example/monitoring/SnippetsIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.ByteArrayOutputStream;
2222
import java.io.PrintStream;
23+
2324
import org.junit.After;
2425
import org.junit.Before;
2526
import org.junit.Test;
@@ -70,6 +71,19 @@ public void testListMetricsDescriptor() throws Exception {
7071
assertThat(got).contains("metricDescriptors/bigquery.googleapis.com/query/count");
7172
}
7273

74+
@Test
75+
public void testGetMetricsDescriptor() throws Exception {
76+
// Act
77+
System.setProperty("projectId", SnippetsIT.getProjectId());
78+
Snippets snippets = new Snippets();
79+
80+
snippets.getMonitoredResource("api");
81+
82+
// Assert
83+
String got = bout.toString();
84+
assertThat(got).contains("Produced API");
85+
}
86+
7387
@Test
7488
public void testListTimeSeries() throws Exception {
7589
// Act

0 commit comments

Comments
 (0)