Search in sources :

Example 21 with TimeInterval

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

the class MetricServiceClientTest method listTimeSeriesTest3.

@Test
public void listTimeSeriesTest3() throws Exception {
    TimeSeries responsesElement = TimeSeries.newBuilder().build();
    ListTimeSeriesResponse expectedResponse = ListTimeSeriesResponse.newBuilder().setNextPageToken("").addAllTimeSeries(Arrays.asList(responsesElement)).build();
    mockMetricService.addResponse(expectedResponse);
    ProjectName name = ProjectName.of("[PROJECT]");
    String filter = "filter-1274492040";
    TimeInterval interval = TimeInterval.newBuilder().build();
    ListTimeSeriesRequest.TimeSeriesView view = ListTimeSeriesRequest.TimeSeriesView.forNumber(0);
    ListTimeSeriesPagedResponse pagedListResponse = client.listTimeSeries(name, filter, interval, view);
    List<TimeSeries> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getTimeSeriesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListTimeSeriesRequest actualRequest = ((ListTimeSeriesRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertEquals(filter, actualRequest.getFilter());
    Assert.assertEquals(interval, actualRequest.getInterval());
    Assert.assertEquals(view, actualRequest.getView());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) AbstractMessage(com.google.protobuf.AbstractMessage) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) ListTimeSeriesResponse(com.google.monitoring.v3.ListTimeSeriesResponse) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) Test(org.junit.Test)

Example 22 with TimeInterval

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

the class MetricServiceClientTest method listTimeSeriesExceptionTest.

@Test
public void listTimeSeriesExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockMetricService.addException(exception);
    try {
        FolderName name = FolderName.of("[FOLDER]");
        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) FolderName(com.google.monitoring.v3.FolderName) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) Test(org.junit.Test)

Example 23 with TimeInterval

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

the class ListTimeSeries method listTimeSeries.

public static void listTimeSeries(String filter, String projectId) 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
        ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder().setName(projectName.toString()).setFilter(filter).setInterval(interval).build();
        // Send the request to list the time series
        MetricServiceClient.ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
        // Process the response
        System.out.println("Got timeseries: ");
        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 24 with TimeInterval

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

the class CreateTimeSeries method createTimeSeries.

public static void createTimeSeries(String projectId) throws ApiException, IOException {
    // Instantiates a client
    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);
        // Prepares the metric descriptor
        Map<String, String> metricLabels = new HashMap<>();
        Metric metric = Metric.newBuilder().setType("custom.googleapis.com/my_metric").putAllLabels(metricLabels).build();
        // Prepares the monitored resource descriptor
        Map<String, String> resourceLabels = new HashMap<>();
        resourceLabels.put("instance_id", "1234567890123456789");
        resourceLabels.put("zone", "us-central1-f");
        MonitoredResource resource = MonitoredResource.newBuilder().setType("gce_instance").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(ProjectName.of(projectId).toString()).addAllTimeSeries(timeSeriesList).build();
        // Writes time series data
        metricServiceClient.createTimeSeries(request);
        System.out.println("Done writing time series value");
    }
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) 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)

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