Search in sources :

Example 6 with MetricDescriptor

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

the class MetricServiceClientTest method getMetricDescriptorTest.

@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.create("[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, actualRequest.getNameAsMetricDescriptorName());
}
Also used : GetMetricDescriptorRequest(com.google.monitoring.v3.GetMetricDescriptorRequest) MetricDescriptor(com.google.api.MetricDescriptor) MetricDescriptorName(com.google.monitoring.v3.MetricDescriptorName) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 7 with MetricDescriptor

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

the class Snippets method listMetricDescriptors.

/**
 * Returns the first page of all metric descriptors.
 */
void listMetricDescriptors() throws IOException {
    // [START monitoring_list_descriptors]
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    final MetricServiceClient client = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder().setName(name.toString()).build();
    ListMetricDescriptorsPagedResponse response = client.listMetricDescriptors(request);
    System.out.println("Listing descriptors: ");
    for (MetricDescriptor d : response.iterateAll()) {
        System.out.println(d.getName() + " " + d.getDisplayName());
    }
// [END monitoring_list_descriptors]
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)

Example 8 with MetricDescriptor

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

the class BigQueryRunner method createMetricsIfNeeded.

// [END bigquery_logging_list_time_series]
// [START bigquery_logging_list_and_create_metrics]
private void createMetricsIfNeeded() {
    // If all required metrics already exist, no need to make service calls.
    if (REQUIRED_METRICS.stream().map(MetricDescriptor::getDisplayName).allMatch(existingMetrics::contains)) {
        return;
    }
    ListMetricDescriptorsRequest listMetricsRequest = ListMetricDescriptorsRequest.newBuilder().setName(projectName).setFilter(CUSTOM_METRIC_FILTER).build();
    ListMetricDescriptorsPagedResponse listMetricsResponse = client.listMetricDescriptors(listMetricsRequest);
    for (MetricDescriptor existingMetric : listMetricsResponse.iterateAll()) {
        existingMetrics.add(existingMetric.getDisplayName());
    }
    REQUIRED_METRICS.stream().filter(metric -> !existingMetrics.contains(metric.getDisplayName())).forEach(this::createMetric);
}
Also used : ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) MetricDescriptor(com.google.api.MetricDescriptor) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) BigQuery(com.google.cloud.bigquery.BigQuery) ArrayList(java.util.ArrayList) BigQueryOptions(com.google.cloud.bigquery.BigQueryOptions) Lists(com.google.common.collect.Lists) ServiceOptions(com.google.cloud.ServiceOptions) TypedValue(com.google.monitoring.v3.TypedValue) Job(com.google.cloud.bigquery.Job) TableResult(com.google.cloud.bigquery.TableResult) Point(com.google.monitoring.v3.Point) PrintStream(java.io.PrintStream) ImmutableSet(com.google.common.collect.ImmutableSet) JobInfo(com.google.cloud.bigquery.JobInfo) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse) Metric(com.google.api.Metric) TimeSeries(com.google.monitoring.v3.TimeSeries) Set(java.util.Set) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Timestamps(com.google.protobuf.util.Timestamps) IOException(java.io.IOException) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) JobId(com.google.cloud.bigquery.JobId) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest) TimeInterval(com.google.monitoring.v3.TimeInterval) MetricDescriptor(com.google.api.MetricDescriptor) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)

Example 9 with MetricDescriptor

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

the class BigQueryRunner method getTimeSeriesValues.

// [START bigquery_logging_list_time_series]
public List<TimeSeriesSummary> getTimeSeriesValues() {
    List<TimeSeriesSummary> summaries = Lists.newArrayList();
    createMetricsIfNeeded();
    for (MetricDescriptor metric : REQUIRED_METRICS) {
        ListTimeSeriesRequest listTimeSeriesRequest = ListTimeSeriesRequest.newBuilder().setName(projectName).setFilter(String.format("metric.type = \"%s\"", metric.getType())).setInterval(TimeInterval.newBuilder().setStartTime(Timestamps.subtract(Timestamps.fromMillis(System.currentTimeMillis()), com.google.protobuf.Duration.newBuilder().setSeconds(// 30 days ago
        60L * 60L * 24L * 30L).build())).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build()).build();
        try {
            ListTimeSeriesPagedResponse listTimeSeriesResponse = client.listTimeSeries(listTimeSeriesRequest);
            ArrayList<TimeSeries> timeSeries = Lists.newArrayList(listTimeSeriesResponse.iterateAll());
            summaries.addAll(timeSeries.stream().map(TimeSeriesSummary::fromTimeSeries).collect(Collectors.toList()));
        } catch (RuntimeException ex) {
            os.println("MetricDescriptors not yet synced. Please try again in a moment.");
        }
    }
    return summaries;
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) TimeSeries(com.google.monitoring.v3.TimeSeries) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Example 10 with MetricDescriptor

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

the class StackdriverExportUtilsTest method createMetricDescriptor.

@Test
public void createMetricDescriptor() {
    MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, "custom.googleapis.com/myorg/", "myorg/", DEFAULT_CONSTANT_LABELS);
    assertThat(metricDescriptor.getName()).isEqualTo("projects/" + PROJECT_ID + "/metricDescriptors/custom.googleapis.com/myorg/" + METRIC_NAME);
    assertThat(metricDescriptor.getDescription()).isEqualTo(METRIC_DESCRIPTION);
    assertThat(metricDescriptor.getDisplayName()).isEqualTo("myorg/" + METRIC_NAME);
    assertThat(metricDescriptor.getType()).isEqualTo("custom.googleapis.com/myorg/" + METRIC_NAME);
    assertThat(metricDescriptor.getUnit()).isEqualTo(METRIC_UNIT);
    assertThat(metricDescriptor.getMetricKind()).isEqualTo(MetricKind.CUMULATIVE);
    assertThat(metricDescriptor.getValueType()).isEqualTo(MetricDescriptor.ValueType.DOUBLE);
    assertThat(metricDescriptor.getLabelsList()).containsExactly(LabelDescriptor.newBuilder().setKey(LABEL_KEY.get(0).getKey()).setDescription(LABEL_KEY.get(0).getDescription()).setValueType(ValueType.STRING).build(), 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)16 Test (org.junit.Test)10 ProjectName (com.google.monitoring.v3.ProjectName)5 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)4 CreateMetricDescriptorRequest (com.google.monitoring.v3.CreateMetricDescriptorRequest)4 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 IOException (java.io.IOException)2 UUID (java.util.UUID)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