Search in sources :

Example 1 with MetricDescriptor

use of com.google.api.MetricDescriptor in project google-cloud-java by GoogleCloudPlatform.

the class MetricServiceClientTest method listMetricDescriptorsTest.

@Test
@SuppressWarnings("all")
public void listMetricDescriptorsTest() {
    String nextPageToken = "";
    MetricDescriptor metricDescriptorsElement = MetricDescriptor.newBuilder().build();
    List<MetricDescriptor> metricDescriptors = Arrays.asList(metricDescriptorsElement);
    ListMetricDescriptorsResponse expectedResponse = ListMetricDescriptorsResponse.newBuilder().setNextPageToken(nextPageToken).addAllMetricDescriptors(metricDescriptors).build();
    mockMetricService.addResponse(expectedResponse);
    ProjectName name = ProjectName.create("[PROJECT]");
    ListMetricDescriptorsPagedResponse pagedListResponse = client.listMetricDescriptors(name);
    List<MetricDescriptor> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getMetricDescriptorsList().get(0), resources.get(0));
    List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListMetricDescriptorsRequest actualRequest = (ListMetricDescriptorsRequest) actualRequests.get(0);
    Assert.assertEquals(name, actualRequest.getNameAsProjectName());
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) ProjectName(com.google.monitoring.v3.ProjectName) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) ListMetricDescriptorsResponse(com.google.monitoring.v3.ListMetricDescriptorsResponse) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.spi.v3.PagedResponseWrappers.ListMetricDescriptorsPagedResponse) Test(org.junit.Test)

Example 2 with MetricDescriptor

use of com.google.api.MetricDescriptor in project google-cloud-java by GoogleCloudPlatform.

the class MetricServiceClientTest method createMetricDescriptorExceptionTest.

@Test
@SuppressWarnings("all")
public void createMetricDescriptorExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockMetricService.addException(exception);
    try {
        ProjectName name = ProjectName.create("[PROJECT]");
        MetricDescriptor metricDescriptor = MetricDescriptor.newBuilder().build();
        client.createMetricDescriptor(name, metricDescriptor);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) ProjectName(com.google.monitoring.v3.ProjectName) StatusRuntimeException(io.grpc.StatusRuntimeException) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 3 with MetricDescriptor

use of com.google.api.MetricDescriptor in project google-cloud-java by GoogleCloudPlatform.

the class MetricServiceClientTest method createMetricDescriptorTest.

@Test
@SuppressWarnings("all")
public void createMetricDescriptorTest() {
    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);
    ProjectName name = ProjectName.create("[PROJECT]");
    MetricDescriptor metricDescriptor = MetricDescriptor.newBuilder().build();
    MetricDescriptor actualResponse = client.createMetricDescriptor(name, metricDescriptor);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateMetricDescriptorRequest actualRequest = (CreateMetricDescriptorRequest) actualRequests.get(0);
    Assert.assertEquals(name, actualRequest.getNameAsProjectName());
    Assert.assertEquals(metricDescriptor, actualRequest.getMetricDescriptor());
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) ProjectName(com.google.monitoring.v3.ProjectName) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 4 with MetricDescriptor

use of com.google.api.MetricDescriptor in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method createMetricDescriptor.

/**
 * Creates a metric descriptor.
 * <p>
 * See: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create
 *
 * @param type The metric type
 */
void createMetricDescriptor(String type) throws IOException {
    // [START monitoring_create_metric]
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    String metricType = CUSTOM_METRIC_DOMAIN + "/" + type;
    final MetricServiceClient client = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    MetricDescriptor descriptor = MetricDescriptor.newBuilder().setType(metricType).setDescription("This is a simple example of a custom metric.").setMetricKind(MetricDescriptor.MetricKind.GAUGE).setValueType(MetricDescriptor.ValueType.DOUBLE).build();
    CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(name.toString()).setMetricDescriptor(descriptor).build();
    client.createMetricDescriptor(request);
// [END monitoring_create_metric]
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest)

Example 5 with MetricDescriptor

use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtilsTest method createMetricDescriptor_cumulative.

@Test
public void createMetricDescriptor_cumulative() {
    MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR_2, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
    assertThat(metricDescriptor.getName()).isEqualTo("projects/" + PROJECT_ID + "/metricDescriptors/custom.googleapis.com/opencensus/" + METRIC_NAME);
    assertThat(metricDescriptor.getDescription()).isEqualTo(METRIC_DESCRIPTION);
    assertThat(metricDescriptor.getDisplayName()).isEqualTo("OpenCensus/" + METRIC_NAME);
    assertThat(metricDescriptor.getType()).isEqualTo("custom.googleapis.com/opencensus/" + METRIC_NAME);
    assertThat(metricDescriptor.getUnit()).isEqualTo("1");
    assertThat(metricDescriptor.getMetricKind()).isEqualTo(MetricKind.CUMULATIVE);
    assertThat(metricDescriptor.getValueType()).isEqualTo(MetricDescriptor.ValueType.INT64);
    assertThat(metricDescriptor.getLabelsList()).containsExactly(LabelDescriptor.newBuilder().setKey(StackdriverExportUtils.OPENCENSUS_TASK_KEY.getKey()).setDescription(StackdriverExportUtils.OPENCENSUS_TASK_KEY.getDescription()).setValueType(ValueType.STRING).build());
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) Test(org.junit.Test)

Aggregations

MetricDescriptor (com.google.api.MetricDescriptor)15 Test (org.junit.Test)9 ProjectName (com.google.monitoring.v3.ProjectName)5 CreateMetricDescriptorRequest (com.google.monitoring.v3.CreateMetricDescriptorRequest)4 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)3 ListMetricDescriptorsRequest (com.google.monitoring.v3.ListMetricDescriptorsRequest)3 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)3 ListMetricDescriptorsPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)2 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)2 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)2 TimeSeries (com.google.monitoring.v3.TimeSeries)2 LabelKey (io.opencensus.metrics.LabelKey)2 Metric (com.google.api.Metric)1 ApiException (com.google.api.gax.grpc.ApiException)1 ApiException (com.google.api.gax.rpc.ApiException)1 ServiceOptions (com.google.cloud.ServiceOptions)1 BigQuery (com.google.cloud.bigquery.BigQuery)1 BigQueryOptions (com.google.cloud.bigquery.BigQueryOptions)1 Job (com.google.cloud.bigquery.Job)1 JobId (com.google.cloud.bigquery.JobId)1