Search in sources :

Example 6 with TimeInterval

use of com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval in project java-monitoring by googleapis.

the class MetricServiceClientTest method listTimeSeriesExceptionTest4.

@Test
public void listTimeSeriesExceptionTest4() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockMetricService.addException(exception);
    try {
        String name = "name3373707";
        String filter = "filter-1274492040";
        TimeInterval interval = TimeInterval.newBuilder().build();
        ListTimeSeriesRequest.TimeSeriesView view = ListTimeSeriesRequest.TimeSeriesView.forNumber(0);
        client.listTimeSeries(name, filter, interval, view);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) TimeInterval(com.google.monitoring.v3.TimeInterval) StatusRuntimeException(io.grpc.StatusRuntimeException) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) Test(org.junit.Test)

Example 7 with TimeInterval

use of com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval in project java-monitoring by googleapis.

the class QuickstartSample method quickstart.

public static void quickstart(String projectId) throws IOException {
    // once, and can be reused for multiple requests.
    try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
        // Prepares an individual data point
        TimeInterval interval = TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
        TypedValue value = TypedValue.newBuilder().setDoubleValue(123.45).build();
        Point point = Point.newBuilder().setInterval(interval).setValue(value).build();
        List<Point> pointList = new ArrayList<>();
        pointList.add(point);
        ProjectName name = ProjectName.of(projectId);
        // Prepares the metric descriptor
        Map<String, String> metricLabels = new HashMap<String, String>();
        metricLabels.put("store_id", "Pittsburg");
        Metric metric = Metric.newBuilder().setType("custom.googleapis.com/stores/daily_sales").putAllLabels(metricLabels).build();
        // Prepares the monitored resource descriptor
        Map<String, String> resourceLabels = new HashMap<String, String>();
        resourceLabels.put("project_id", projectId);
        MonitoredResource resource = MonitoredResource.newBuilder().setType("global").putAllLabels(resourceLabels).build();
        // Prepares the time series request
        TimeSeries timeSeries = TimeSeries.newBuilder().setMetric(metric).setResource(resource).addAllPoints(pointList).build();
        List<TimeSeries> timeSeriesList = new ArrayList<>();
        timeSeriesList.add(timeSeries);
        CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(name.toString()).addAllTimeSeries(timeSeriesList).build();
        // Writes time series data
        metricServiceClient.createTimeSeries(request);
        System.out.printf("Done writing time series data.%n");
    }
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) HashMap(java.util.HashMap) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) Point(com.google.monitoring.v3.Point) Metric(com.google.api.Metric) TypedValue(com.google.monitoring.v3.TypedValue)

Example 8 with TimeInterval

use of com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval in project java-monitoring by googleapis.

the class TimeSeriesHeadersList method listTimeSeriesHeaders.

public static void listTimeSeriesHeaders(String projectId, String filter) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
        ProjectName projectName = ProjectName.of(projectId);
        // Restrict time to last 20 minutes
        long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000);
        TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
        // Prepares the list time series request with headers
        ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder().setName(projectName.toString()).setFilter(filter).setInterval(interval).setView(ListTimeSeriesRequest.TimeSeriesView.HEADERS).build();
        // Send the request to list the time series
        MetricServiceClient.ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
        // Process the response
        System.out.println("Got timeseries headers: ");
        response.iterateAll().forEach(timeSeries -> System.out.println(timeSeries));
    }
}
Also used : MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Example 9 with TimeInterval

use of com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval in project google-cloud-java by GoogleCloudPlatform.

the class MetricServiceClientTest method listTimeSeriesExceptionTest.

@Test
@SuppressWarnings("all")
public void listTimeSeriesExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockMetricService.addException(exception);
    try {
        ProjectName name = ProjectName.create("[PROJECT]");
        String filter = "filter-1274492040";
        TimeInterval interval = TimeInterval.newBuilder().build();
        ListTimeSeriesRequest.TimeSeriesView view = ListTimeSeriesRequest.TimeSeriesView.FULL;
        client.listTimeSeries(name, filter, interval, view);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeSeriesView(com.google.monitoring.v3.ListTimeSeriesRequest.TimeSeriesView) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 10 with TimeInterval

use of com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval in project java-docs-samples by GoogleCloudPlatform.

the class WorkloadGeneratorTest method testPipeline.

@Test
public void testPipeline() throws IOException, InterruptedException {
    String workloadJobName = "bigtable-workload-generator-test-" + new Date().getTime();
    final int WORKLOAD_DURATION = 10;
    final int WAIT_DURATION = WORKLOAD_DURATION * 60 * 1000;
    int rate = 1000;
    BigtableWorkloadOptions options = PipelineOptionsFactory.create().as(BigtableWorkloadOptions.class);
    options.setBigtableInstanceId(instanceId);
    options.setBigtableTableId(TABLE_ID);
    options.setWorkloadRate(rate);
    options.setRegion(REGION_ID);
    options.setWorkloadDurationMinutes(WORKLOAD_DURATION);
    options.setRunner(DataflowRunner.class);
    options.setJobName(workloadJobName);
    final PipelineResult pipelineResult = WorkloadGenerator.generateWorkload(options);
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    // Wait X minutes and then get metrics for the X minute period.
    Thread.sleep(WAIT_DURATION);
    long startMillis = System.currentTimeMillis() - WAIT_DURATION;
    TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder().setName(name.toString()).setFilter("metric.type=\"bigtable.googleapis.com/server/request_count\"").setInterval(interval).build();
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
    long startRequestCount = 0;
    long endRequestCount = 0;
    for (TimeSeries ts : response.iterateAll()) {
        startRequestCount = ts.getPoints(0).getValue().getInt64Value();
        endRequestCount = ts.getPoints(ts.getPointsCount() - 1).getValue().getInt64Value();
    }
    assertThat(endRequestCount - startRequestCount > rate);
    // Stop the running job.
    String jobId = ((DataflowPipelineJob) pipelineResult).getJobId();
    DataflowClient client = DataflowClient.create(options);
    Job job = client.getJob(jobId);
    assertThat(job.getCurrentState().equals("JOB_STATE_CANCELLED"));
}
Also used : TimeSeries(com.google.bigtable.repackaged.com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval) ProjectName(com.google.bigtable.repackaged.com.google.monitoring.v3.ProjectName) PipelineResult(org.apache.beam.sdk.PipelineResult) ListTimeSeriesPagedResponse(com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) Date(java.util.Date) ListTimeSeriesRequest(com.google.bigtable.repackaged.com.google.monitoring.v3.ListTimeSeriesRequest) BigtableWorkloadOptions(bigtable.WorkloadGenerator.BigtableWorkloadOptions) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) DataflowClient(org.apache.beam.runners.dataflow.DataflowClient) Job(com.google.api.services.dataflow.model.Job) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) Test(org.junit.Test)

Aggregations

TimeInterval (com.google.monitoring.v3.TimeInterval)23 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)16 TimeSeries (com.google.monitoring.v3.TimeSeries)14 ProjectName (com.google.monitoring.v3.ProjectName)13 Test (org.junit.Test)11 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)10 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)8 ListTimeSeriesResponse (com.google.monitoring.v3.ListTimeSeriesResponse)6 Metric (com.google.api.Metric)5 Point (com.google.monitoring.v3.Point)5 TypedValue (com.google.monitoring.v3.TypedValue)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 MonitoredResource (com.google.api.MonitoredResource)4 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Aggregation (com.google.monitoring.v3.Aggregation)2 FolderName (com.google.monitoring.v3.FolderName)2