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")); }
public static List<MetricDescriptorName> parseList(List<String> formattedStrings) { List<MetricDescriptorName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
public static List<String> toStringList(List<MetricDescriptorName> values) { List<String> list = new ArrayList<String>(values.size()); for (MetricDescriptorName value : values) { if (value == null) { list.add(""); } else { list.add(value.toString()); } } return list; }
public static String format(String project, String metricDescriptor) { return newBuilder() .setProject(project) .setMetricDescriptor(metricDescriptor) .build() .toString(); }
@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())); }
public MetricDescriptorName build() { return new MetricDescriptorName(this); } }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
public static MetricDescriptorName of(String project, String metricDescriptor) { return newBuilder().setProject(project).setMetricDescriptor(metricDescriptor).build(); }
@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())); }
@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 } }
/** * Gets a single metric descriptor. This method does not require a Stackdriver account. * * <p>Sample code: * * <pre><code> * try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) { * MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); * MetricDescriptor response = metricServiceClient.getMetricDescriptor(name); * } * </code></pre> * * @param name The metric descriptor on which to execute the request. The format is * `"projects/{project_id_or_number}/metricDescriptors/{metric_id}"`. An example value of * `{metric_id}` is `"compute.googleapis.com/instance/disk/read_bytes_count"`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final MetricDescriptor getMetricDescriptor(MetricDescriptorName name) { GetMetricDescriptorRequest request = GetMetricDescriptorRequest.newBuilder() .setName(name == null ? null : name.toString()) .build(); return getMetricDescriptor(request); }
@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 } }
/** * Deletes a metric descriptor. Only user-created [custom metrics](/monitoring/custom-metrics) can * be deleted. * * <p>Sample code: * * <pre><code> * try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) { * MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); * metricServiceClient.deleteMetricDescriptor(name); * } * </code></pre> * * @param name The metric descriptor on which to execute the request. The format is * `"projects/{project_id_or_number}/metricDescriptors/{metric_id}"`. An example of * `{metric_id}` is: `"custom.googleapis.com/my_test_metric"`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void deleteMetricDescriptor(MetricDescriptorName name) { DeleteMetricDescriptorRequest request = DeleteMetricDescriptorRequest.newBuilder() .setName(name == null ? null : name.toString()) .build(); deleteMetricDescriptor(request); }
/** * 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] }
/** * Gets a single metric descriptor. This method does not require a Stackdriver account. * * <p>Sample code: * * <pre><code> * try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) { * MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); * MetricDescriptor response = metricServiceClient.getMetricDescriptor(name); * } * </code></pre> * * @param name The metric descriptor on which to execute the request. The format is * `"projects/{project_id_or_number}/metricDescriptors/{metric_id}"`. An example value of * `{metric_id}` is `"compute.googleapis.com/instance/disk/read_bytes_count"`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final MetricDescriptor getMetricDescriptor(MetricDescriptorName name) { GetMetricDescriptorRequest request = GetMetricDescriptorRequest.newBuilder() .setName(name == null ? null : name.toString()) .build(); return getMetricDescriptor(request); }
/** * Deletes a metric descriptor. Only user-created [custom metrics](/monitoring/custom-metrics) can * be deleted. * * <p>Sample code: * * <pre><code> * try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) { * MetricDescriptorName name = MetricDescriptorName.of("[PROJECT]", "[METRIC_DESCRIPTOR]"); * metricServiceClient.deleteMetricDescriptor(name); * } * </code></pre> * * @param name The metric descriptor on which to execute the request. The format is * `"projects/{project_id_or_number}/metricDescriptors/{metric_id}"`. An example of * `{metric_id}` is: `"custom.googleapis.com/my_test_metric"`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void deleteMetricDescriptor(MetricDescriptorName name) { DeleteMetricDescriptorRequest request = DeleteMetricDescriptorRequest.newBuilder() .setName(name == null ? null : name.toString()) .build(); deleteMetricDescriptor(request); }