public static MetricDescriptorName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "MetricDescriptorName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("metric_descriptor")); }
@Test @SuppressWarnings("all") public void deleteMetricDescriptorExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockMetricService.addException(exception); try { MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); client.deleteMetricDescriptor(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void getMetricDescriptorExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockMetricService.addException(exception); try { MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); client.getMetricDescriptor(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void deleteMetricDescriptorTest() { Empty expectedResponse = Empty.newBuilder().build(); mockMetricService.addResponse(expectedResponse); MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); client.deleteMetricDescriptor(name); List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests(); Assert.assertEquals(1, actualRequests.size()); DeleteMetricDescriptorRequest actualRequest = (DeleteMetricDescriptorRequest) actualRequests.get(0); Assert.assertEquals(name, MetricDescriptorName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Delete a metric descriptor. * * @param name Name of metric descriptor to delete */ void deleteMetricDescriptor(String name) throws IOException { // [START monitoring_delete_metric] String projectId = System.getProperty("projectId"); final MetricServiceClient client = MetricServiceClient.create(); MetricDescriptorName metricName = MetricDescriptorName.of(projectId, name); client.deleteMetricDescriptor(metricName); System.out.println("Deleted descriptor " + name); // [END monitoring_delete_metric] }
@Test @SuppressWarnings("all") public void getMetricDescriptorTest() { String name2 = "name2-1052831874"; String type = "type3575610"; String unit = "unit3594628"; String description = "description-1724546052"; String displayName = "displayName1615086568"; MetricDescriptor expectedResponse = MetricDescriptor.newBuilder() .setName(name2) .setType(type) .setUnit(unit) .setDescription(description) .setDisplayName(displayName) .build(); mockMetricService.addResponse(expectedResponse); MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); MetricDescriptor actualResponse = client.getMetricDescriptor(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetMetricDescriptorRequest actualRequest = (GetMetricDescriptorRequest) actualRequests.get(0); Assert.assertEquals(name, MetricDescriptorName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }