Skip to content

Add monitoring_get_resource tag/code sample to monitoring snippets. #1306

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

Merged
merged 2 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse;
import com.google.cloud.monitoring.v3.MetricServiceClient.ListMonitoredResourceDescriptorsPagedResponse;
import com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse;
import com.google.gson.Gson;
import com.google.monitoring.v3.Aggregation;
import com.google.monitoring.v3.CreateMetricDescriptorRequest;
import com.google.monitoring.v3.CreateTimeSeriesRequest;
Expand All @@ -39,6 +40,7 @@
import com.google.monitoring.v3.TypedValue;
import com.google.protobuf.Duration;
import com.google.protobuf.util.Timestamps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -49,8 +51,8 @@


public class Snippets {

private static final String CUSTOM_METRIC_DOMAIN = "custom.googleapis.com";
private static final Gson gson = new Gson();

/**
* Exercises the methods defined in this class.
Expand Down Expand Up @@ -392,6 +394,17 @@ void listMonitoredResources() throws IOException {
// [END monitoring_list_resources]
}

// [START monitoring_get_resource]
void getMonitoredResource(String resourceId) throws IOException {
String projectId = System.getProperty("projectId");
MetricServiceClient client = MetricServiceClient.create();
MonitoredResourceDescriptorName name =
MonitoredResourceDescriptorName.of(projectId, resourceId);
MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);
System.out.println("Retrieved Monitored Resource: " + gson.toJson(response));
}
// [END monitoring_get_resource]

/**
* Gets full information for a monitored resource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -70,6 +71,19 @@ public void testListMetricsDescriptor() throws Exception {
assertThat(got).contains("metricDescriptors/bigquery.googleapis.com/query/count");
}

@Test
public void testGetMetricsDescriptor() throws Exception {
// Act
System.setProperty("projectId", SnippetsIT.getProjectId());
Snippets snippets = new Snippets();

snippets.getMonitoredResource("api");

// Assert
String got = bout.toString();
assertThat(got).contains("Produced API");
}

@Test
public void testListTimeSeries() throws Exception {
// Act
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018 Google LLC
*
* 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.example;

import com.google.cloud.monitoring.v3.NotificationChannelServiceClient;
import com.google.monitoring.v3.NotificationChannelName;

import java.io.IOException;

public class DeleteNotificationChannel {
/**
* Demonstrates deleting a notification channel by name.
* @param channelName Name of the notification channel to delete.
*/
// [START monitoring_alert_delete_channel]
static void deleteNotificationChannel(String channelName) throws IOException {
String projectId = System.getProperty("projectId");
try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) {
NotificationChannelName name = NotificationChannelName.of(projectId, channelName);
client.deleteNotificationChannel(channelName, false);
System.out.println("Deleted notification channel " + channelName);
}
}
// [END monitoring_alert_delete_channel]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2018 Google LLC
*
* 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.example;

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

import com.google.cloud.monitoring.v3.NotificationChannelServiceClient;
import com.google.monitoring.v3.NotificationChannel;
import com.google.monitoring.v3.ProjectName;

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

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests for delete notification channel sample.
*/
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class DeleteNotificationChannelIT {
private ByteArrayOutputStream bout;
private PrintStream out;
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT";
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
private static String NOTIFICATION_CHANNEL_NAME = "channelname";
private static NotificationChannel NOTIFICATION_CHANNEL;

private static String getProjectId() {
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
if (projectId == null) {
projectId = System.getProperty(LEGACY_PROJECT_ENV_NAME,
System.getenv(LEGACY_PROJECT_ENV_NAME));
}
return projectId;
}

@BeforeClass
public static void setupClass() throws IOException {
try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) {
String projectId = getProjectId();
NOTIFICATION_CHANNEL = NotificationChannel.newBuilder()
.setType("email")
.putLabels("email_address", "[email protected]")
.build();
NotificationChannel channel = client.createNotificationChannel(ProjectName.of(projectId),
NOTIFICATION_CHANNEL);
NOTIFICATION_CHANNEL_NAME = channel.getName();
}
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
System.setProperty("projectId", DeleteNotificationChannelIT.getProjectId());
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testDeleteNotificationChannel() throws Exception {
// Act
DeleteNotificationChannel.deleteNotificationChannel(NOTIFICATION_CHANNEL_NAME);
// Assert
String got = bout.toString();
assertThat(got).contains(NOTIFICATION_CHANNEL_NAME);
}
}