Search in sources :

Example 1 with MonitoredResource

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

the class LoggingClientTest method writeLogEntriesExceptionTest.

@Test
@SuppressWarnings("all")
public void writeLogEntriesExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockLoggingServiceV2.addException(exception);
    try {
        LogNameOneof logName = LogNameOneof.from(LogName.create("[PROJECT]", "[LOG]"));
        MonitoredResource resource = MonitoredResource.newBuilder().build();
        Map<String, String> labels = new HashMap<>();
        List<LogEntry> entries = new ArrayList<>();
        client.writeLogEntries(logName, resource, labels, entries);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : LogNameOneof(com.google.logging.v2.LogNameOneof) HashMap(java.util.HashMap) StatusRuntimeException(io.grpc.StatusRuntimeException) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) LogEntry(com.google.logging.v2.LogEntry) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 2 with MonitoredResource

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

the class LoggingClientTest method writeLogEntriesTest.

@Test
@SuppressWarnings("all")
public void writeLogEntriesTest() {
    WriteLogEntriesResponse expectedResponse = WriteLogEntriesResponse.newBuilder().build();
    mockLoggingServiceV2.addResponse(expectedResponse);
    LogNameOneof logName = LogNameOneof.from(LogName.create("[PROJECT]", "[LOG]"));
    MonitoredResource resource = MonitoredResource.newBuilder().build();
    Map<String, String> labels = new HashMap<>();
    List<LogEntry> entries = new ArrayList<>();
    WriteLogEntriesResponse actualResponse = client.writeLogEntries(logName, resource, labels, entries);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLoggingServiceV2.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    WriteLogEntriesRequest actualRequest = (WriteLogEntriesRequest) actualRequests.get(0);
    Assert.assertEquals(logName, actualRequest.getLogNameAsLogNameOneof());
    Assert.assertEquals(resource, actualRequest.getResource());
    Assert.assertEquals(labels, actualRequest.getLabelsMap());
    Assert.assertEquals(entries, actualRequest.getEntriesList());
}
Also used : LogNameOneof(com.google.logging.v2.LogNameOneof) WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) HashMap(java.util.HashMap) WriteLogEntriesResponse(com.google.logging.v2.WriteLogEntriesResponse) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) LogEntry(com.google.logging.v2.LogEntry) Test(org.junit.Test)

Example 3 with MonitoredResource

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

the class GroupServiceClientTest method listGroupMembersTest.

@Test
@SuppressWarnings("all")
public void listGroupMembersTest() {
    String nextPageToken = "";
    int totalSize = -705419236;
    MonitoredResource membersElement = MonitoredResource.newBuilder().build();
    List<MonitoredResource> members = Arrays.asList(membersElement);
    ListGroupMembersResponse expectedResponse = ListGroupMembersResponse.newBuilder().setNextPageToken(nextPageToken).setTotalSize(totalSize).addAllMembers(members).build();
    mockGroupService.addResponse(expectedResponse);
    GroupName name = GroupName.create("[PROJECT]", "[GROUP]");
    ListGroupMembersPagedResponse pagedListResponse = client.listGroupMembers(name);
    List<MonitoredResource> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getMembersList().get(0), resources.get(0));
    List<GeneratedMessageV3> actualRequests = mockGroupService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListGroupMembersRequest actualRequest = (ListGroupMembersRequest) actualRequests.get(0);
    Assert.assertEquals(name, actualRequest.getNameAsGroupName());
}
Also used : GroupName(com.google.monitoring.v3.GroupName) ListGroupMembersResponse(com.google.monitoring.v3.ListGroupMembersResponse) ListGroupMembersRequest(com.google.monitoring.v3.ListGroupMembersRequest) MonitoredResource(com.google.api.MonitoredResource) ListGroupMembersPagedResponse(com.google.cloud.monitoring.spi.v3.PagedResponseWrappers.ListGroupMembersPagedResponse) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 4 with MonitoredResource

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

the class Snippets method writeTimeSeries.

/**
 * Demonstrates writing a time series value for the metric type
 * 'custom.google.apis.com/my_metric'.
 * <p>
 * This method assumes `my_metric` descriptor has already been created as a
 * DOUBLE value_type and GAUGE metric kind. If the metric descriptor
 * doesn't exist, it will be auto-created.
 */
// CHECKSTYLE OFF: VariableDeclarationUsageDistance
void writeTimeSeries() throws IOException {
    // [START monitoring_write_timeseries]
    String projectId = System.getProperty("projectId");
    // Instantiates a client
    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<>();
    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(name.toString()).addAllTimeSeries(timeSeriesList).build();
    // Writes time series data
    metricServiceClient.createTimeSeries(request);
    System.out.println("Done writing time series value.");
// [END monitoring_write_timeseries]
}
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 5 with MonitoredResource

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

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    if (projectId == null) {
        System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");
        return;
    }
    // Instantiates a client
    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");
    metricServiceClient.close();
}
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)

Aggregations

MonitoredResource (com.google.api.MonitoredResource)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 TimeSeries (com.google.monitoring.v3.TimeSeries)3 Metric (com.google.api.Metric)2 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)2 LogEntry (com.google.logging.v2.LogEntry)2 LogNameOneof (com.google.logging.v2.LogNameOneof)2 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)2 Point (com.google.monitoring.v3.Point)2 ProjectName (com.google.monitoring.v3.ProjectName)2 TimeInterval (com.google.monitoring.v3.TimeInterval)2 TypedValue (com.google.monitoring.v3.TypedValue)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)2 ApiException (com.google.api.gax.grpc.ApiException)1 ListGroupMembersPagedResponse (com.google.cloud.monitoring.spi.v3.PagedResponseWrappers.ListGroupMembersPagedResponse)1 WriteLogEntriesRequest (com.google.logging.v2.WriteLogEntriesRequest)1 WriteLogEntriesResponse (com.google.logging.v2.WriteLogEntriesResponse)1 GroupName (com.google.monitoring.v3.GroupName)1